From 6991da4906efdfc9824110c2e757ea80c67b3a22 Mon Sep 17 00:00:00 2001 From: Siarhei Date: Fri, 31 Jan 2020 13:44:43 +0300 Subject: [PATCH] feat: add "preventParsingData" option of job to prevent data parsing --- lib/job.js | 4 +++- lib/queue.js | 3 ++- test/test_job.js | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/job.js b/lib/job.js index 1c017107e..6bbb29cc9 100644 --- a/lib/job.js +++ b/lib/job.js @@ -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); diff --git a/lib/queue.js b/lib/queue.js index 2c6ab79c6..d9a8d3a5d 100755 --- a/lib/queue.js +++ b/lib/queue.js @@ -670,7 +670,8 @@ interface JobOptions repeat: { tz?: string, endDate?: Date | string | number - } + }, + preventParsingData: boolean; } */ diff --git a/test/test_job.js b/test/test_job.js index 91108a283..782a47e37 100644 --- a/test/test_job.js +++ b/test/test_job.js @@ -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); + }); + }); });