From 94156434685bc02e0119d1233220246d153180d9 Mon Sep 17 00:00:00 2001 From: Eli Skeggs Date: Wed, 5 Aug 2020 11:55:35 -0700 Subject: [PATCH] fix(queue-view): improve handling of falsy job.id values Fixes #181. --- .../dashboard/templates/queueJobsByState.hbs | 6 +++--- src/server/views/helpers/handlebars.js | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/server/views/dashboard/templates/queueJobsByState.hbs b/src/server/views/dashboard/templates/queueJobsByState.hbs index d647abf9..a69d2398 100644 --- a/src/server/views/dashboard/templates/queueJobsByState.hbs +++ b/src/server/views/dashboard/templates/queueJobsByState.hbs @@ -93,9 +93,9 @@ - +

- {{ this.id }} + {{#if this.id}}{{ this.id }}{{else}}missing id{{/if}} {{#if this.data.name}} {{ this.data.name }} {{else if this.name}} @@ -103,7 +103,7 @@ {{/if}}

-
+
{{~> dashboard/jobDetails this basePath=../basePath displayJobInline=true queueName=../queueName queueHost=../queueHost jobState=../state }}
diff --git a/src/server/views/helpers/handlebars.js b/src/server/views/helpers/handlebars.js index 5d664b20..81f920bd 100644 --- a/src/server/views/helpers/handlebars.js +++ b/src/server/views/helpers/handlebars.js @@ -14,6 +14,9 @@ const replacer = (key, value) => { } }; +// For jobs that don't have a valid ID, produce a random ID we can use in its place. +const idMapping = new WeakMap(); + const helpers = { json(obj, pretty = false) { const args = [obj, replacer]; @@ -44,8 +47,17 @@ const helpers = { block.push(options.fn(this)); }, - hashIdAttr(id) { - return crypto.createHash('sha256').update(id).digest('hex'); + hashIdAttr(obj) { + const { id } = obj; + if (typeof id === 'string') { + return crypto.createHash('sha256').update(id).digest('hex'); + } + let mapping = idMapping.get(obj); + if (!mapping) { + mapping = crypto.randomBytes(32).toString('hex'); + idMapping.set(obj, mapping); + } + return mapping; }, };