From ed2f92e7fdc9cc7836b13ebc73e17d8fd296a07e Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 17 Feb 2020 10:37:22 -0800 Subject: [PATCH] fix: Handle edge cases around before:Date and filtering staged publishes --- index.js | 4 ++-- test/index.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b011eb6..5fb1279 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,7 @@ const engineOk = (manifest, npmVersion, nodeVersion) => { } const isBefore = (verTimes, ver, time) => - !verTimes || Date.parse(verTimes[ver]) <= time + !verTimes || !verTimes[ver] || Date.parse(verTimes[ver]) <= time const pickManifest = (packument, wanted, opts) => { const { @@ -32,7 +32,7 @@ const pickManifest = (packument, wanted, opts) => { const restricted = (packument.policyRestrictions && packument.policyRestrictions.versions) || {} - const time = before && verTimes ? Date.parse(before) : Infinity + const time = before && verTimes ? +(new Date(before)) : Infinity const spec = npa.resolve(name, wanted || defaultTag) const type = spec.type const distTags = packument['dist-tags'] || {} diff --git a/test/index.js b/test/index.js index 9f65d6f..72eeb87 100644 --- a/test/index.js +++ b/test/index.js @@ -365,6 +365,7 @@ test('accepts opts.before option to do date-based cutoffs', t => { '1.0.0': '2018-01-01T00:00:00.000Z', '2.0.0': '2018-01-02T00:00:00.000Z', '2.0.1': '2018-01-03T00:00:00.000Z', + '2.0.2': '2018-01-03T00:00:00.123Z', '3.0.0': '2018-01-04T00:00:00.000Z' }, versions: { @@ -385,6 +386,16 @@ test('accepts opts.before option to do date-based cutoffs', t => { }) t.equal(manifest.version, '2.0.0', 'tag specs pick highest before dist-tag but within the range in question') + manifest = pickManifest(metadata, '*', { + before: Date.parse('2018-01-03T00:00:00.000Z') + }) + t.equal(manifest.version, '2.0.1', 'numeric timestamp supported with ms accuracy') + + manifest = pickManifest(metadata, '*', { + before: new Date('2018-01-03T00:00:00.000Z') + }) + t.equal(manifest.version, '2.0.1', 'date obj supported with ms accuracy') + t.throws(() => pickManifest(metadata, '3.0.0', { before: '2018-01-02' }), { code: 'ETARGET' }, 'version filtered out by date') @@ -445,12 +456,19 @@ test('support selecting staged versions if allowed by options', t => { versions: { '2.0.0': { version: '2.0.0' } } + }, + time: { + '1.0.0': '2018-01-03T00:00:00.000Z' } } t.equal(pickManifest(pack, '1||2').version, '1.0.0') t.equal(pickManifest(pack, '1||2', { includeStaged: true }).version, '1.0.0') t.equal(pickManifest(pack, '2', { includeStaged: true }).version, '2.0.0') + t.equal(pickManifest(pack, '2', { + includeStaged: true, + before: '2018-01-01' + }).version, '2.0.0', 'version without time entry not subject to before filtering') t.throws(() => pickManifest(pack, '2'), { code: 'ETARGET' }) t.throws(() => pickManifest(pack, 'borked'), { code: 'ETARGET' })