What is the use case of firebase-queue sanitize?

433 Views Asked by At

I am experimenting with firebase-queue. I saw the option for sanitizing. It's described in the doc as

sanitize - specifies whether the data object passed to the processing function is sanitized of internal keys reserved for use by the queue. Defaults to true.

What does it mean?

I am getting an error for not specifying { sanitize : false }

sanitize error

1

There are 1 best solutions below

1
On

When the sanitize option is set, the queue sanitizes (or cleans) the input provided to the processing function so that it resembles that which the original client placed onto the queue, and doesn't contain any of the keys added by the implementation of the queue itself.

If, however, you rely on a key (usually the keys starting with an underscore, e.g. _id) that is added by the queue, and not the original client, you need to set sanitize: false so those keys are returned to your function and they're not undefined.

You can clearly see the difference with a simple processing function that just performs a console.log(data).

A quick note about why these keys are removed by default: Reading or writing directly to the location (as it looks like you're perhaps doing, by passing undefined into the client SDK child() method instead of data._id) is generally a bad idea from within the worker itself as writes performed directly are not guarded by the extensive transaction logic in the queue to prevent race conditions. If you can isolate the work to taking input from the provided data field, and returning outputs to the resolve() function, you'll likely have a better time scaling up your queue.