Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add "preventParsingData" option of job to prevent data parsing #1631

Merged
merged 1 commit into from Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/job.js
Expand Up @@ -556,8 +556,10 @@ Job.prototype._saveAttempt = function(multi, err) {
};

Job.fromJSON = function(queue, json, jobId) {
const data = JSON.parse(json.data || '{}');
const opts = JSON.parse(json.opts || '{}');
const data = opts.preventParsingData
? json.data
: JSON.parse(json.data || '{}');

const job = new Job(queue, json.name || Job.DEFAULT_JOB_NAME, data, opts);

Expand Down
3 changes: 2 additions & 1 deletion lib/queue.js
Expand Up @@ -670,7 +670,8 @@ interface JobOptions
repeat: {
tz?: string,
endDate?: Date | string | number
}
},
preventParsingData: boolean;
}
*/

Expand Down
23 changes: 23 additions & 0 deletions test/test_job.js
Expand Up @@ -976,4 +976,27 @@ describe('Job', () => {
);
});
});

describe('.fromJSON', () => {
let data;

beforeEach(() => {
data = { foo: 'bar' };
});

it('should parse JSON data by default', async () => {
const job = await Job.create(queue, data, {});
const jobParsed = Job.fromJSON(queue, job.toData());

expect(jobParsed.data).to.eql(data);
});

it('should not parse JSON data if "preventParsingData" option is specified', async () => {
const job = await Job.create(queue, data, { preventParsingData: true });
const jobParsed = Job.fromJSON(queue, job.toData());
const expectedData = JSON.stringify(data);

expect(jobParsed.data).to.be(expectedData);
});
});
});