From 12b14c0ffa28f3931550a827f4f66e05557498ca Mon Sep 17 00:00:00 2001 From: Nicolas Thouvenin Date: Tue, 19 Dec 2023 15:35:21 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20with=20undefined=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/statements/identify.js | 25 ++++++++++++++---------- packages/core/test/identify.js | 23 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/packages/core/src/statements/identify.js b/packages/core/src/statements/identify.js index 1200ce90..faada7ce 100644 --- a/packages/core/src/statements/identify.js +++ b/packages/core/src/statements/identify.js @@ -50,17 +50,22 @@ export default async function identify(data, feed) { if (this.isLast()) { return feed.close(); } - if (!validKey(uri)) { - let identifier; - if (scheme === 'uid') { - identifier = await generate(nolookalikes, 8); - } else if (scheme === 'sha') { - identifier = await sha(JSON.stringify(data)); - } - if (identifier) { - const checksum = ncda(identifier, nolookalikes); - _.set(data, path, `${scheme}:/${identifier}${checksum}`); + try { + if (!validKey(uri)) { + let identifier; + if (scheme === 'uid') { + identifier = await generate(nolookalikes, 8); + } else if (scheme === 'sha') { + identifier = await sha(data); + } + if (identifier) { + const checksum = ncda(identifier, nolookalikes); + _.set(data, path, `${scheme}:/${identifier}${checksum}`); + } } } + catch (e) { + return feed.stop(e); + } return feed.send(data); } diff --git a/packages/core/test/identify.js b/packages/core/test/identify.js index cda73587..a0dca998 100644 --- a/packages/core/test/identify.js +++ b/packages/core/test/identify.js @@ -90,6 +90,29 @@ describe('[identify]', () => { done(); }); }); + + it('identify #2', (done) => { + from([ + { id: 1, b: 'a' }, + undefined, + { id: 1, b: 'a' }, + ]) + .pipe(ezs('identify', { path: 'uuid', scheme: 'sha' })) + .pipe(ezs.catch()) + .on('error', (e) => { + try { + expect(e.message).toEqual(expect.stringContaining('Received undefined')); + done(); + } catch(ee) { + done(ee); + } + }) + .on('data', () => true) + .on('end', () => { + done(new Error('Error is the right behavior')); + }); + }); + }); describe('validKey', () => {