Skip to content

Commit

Permalink
fix: Handle edge cases around before:Date and filtering staged publishes
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 17, 2020
1 parent 0d8b55f commit ed2f92e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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'] || {}
Expand Down
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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')
Expand Down Expand Up @@ -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' })

Expand Down

0 comments on commit ed2f92e

Please sign in to comment.