Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ export class Queue<
protected stats: QueueStats;

/** Wrap the payload in a JSON encoding */
static encodePayload(p: unknown) {
static encodePayload(this: void, p: unknown) {
return JSON.stringify({ _: p });
}

/** Decode the payload, stripping away the outer JSON encoding */
static decodePayload<T>(s: string | null) {
return JSON.parse(s ?? "{}")._ as T;
static decodePayload<T>(this: void, s: string | null | unknown) {
return JSON.parse((s as string) ?? "{}")._ as T;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will probably need a try catch around it now, since non-strings really mess up the JSON.parse (it probably always needed one, but the addition of an unknown makes it way more likely to trigger this footgun)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you catch & rethrow a useful error here? This way the default encoders set a good example

}

constructor(driver: Driver, name: string, options?: QueueOptions) {
Expand All @@ -117,6 +117,12 @@ export class Queue<
},
statInterval:
options?.statInterval === 0 ? 0 : options?.statInterval ?? 5,
decodePayload:
options?.decodePayload ??
(this.constructor as typeof Queue).decodePayload,
encodePayload:
options?.encodePayload ??
(this.constructor as typeof Queue).encodePayload,
};

// initialize stats
Expand Down Expand Up @@ -242,7 +248,7 @@ export class Queue<
ref: v.ref ?? v4(),
ack: null,
visible: begin,
payload: Queue.encodePayload(v.payload),
payload: this.opts.encodePayload(v.payload),
attempts: {
tries: 0,
max: v.retries === 0 ? 0 : v.retries ?? 5,
Expand Down Expand Up @@ -422,7 +428,7 @@ export class Queue<
driver: this.driver,
name: this.name,
doc,
payload: Queue.decodePayload<TData>(doc.payload),
payload: this.opts.decodePayload<TData>(doc.payload),
handler,
emitter: this.events,
visibility,
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface QueueOptions {
* seconds. Defaults to `5`
*/
statInterval?: number;
decodePayload: <T>(payload: unknown) => T;
encodePayload: (payload: unknown) => unknown;
}

export interface ProcessorConfig<C> {
Expand Down Expand Up @@ -131,7 +133,7 @@ export interface QueueDoc {
/** If a job is marked dead, this will contain the error information */
error?: string;
/** The job's payload. If an object or object-like value is passed, it will be passed through JSON.stringify */
payload: string | null;
payload: string | null | unknown;
/** Information on the number of attempts and max allowed */
attempts: {
/** The current attempt number */
Expand Down
Loading