Skip to content

Commit f382d1d

Browse files
committed
Decorate manifest with publish time
Decorate the manifest with the publish time from the packument if this exists for the picked version. This is used in pacote to pick a valid signing key when verifying registry signatures and attestations: npm/pacote#284 Signed-off-by: Philip Harrison <philip@mailharrison.com>
1 parent 68edb8d commit f382d1d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

lib/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ const pickManifest = (packument, wanted, opts) => {
185185

186186
module.exports = (packument, wanted, opts = {}) => {
187187
const mani = pickManifest(packument, wanted, opts)
188+
// Decorate with publish time if available
189+
const _time = mani && packument.time && packument.time[mani.version] || null
190+
if (_time) {
191+
mani._time = _time
192+
}
188193
const picked = mani && normalizeBin(mani)
189194
const policyRestrictions = packument.policyRestrictions
190195
const restricted = (policyRestrictions && policyRestrictions.versions) || {}

test/index.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,66 @@ test('normalize package bins', t => {
606606

607607
t.end()
608608
})
609+
610+
test('decorates created time', t => {
611+
const name = 'foobar'
612+
const metadata = {
613+
name,
614+
versions: {
615+
'1.0.0': { name, version: '1.0.0' },
616+
'1.0.1': { name, version: '1.0.1' },
617+
'1.0.2': { name, version: '1.0.2' },
618+
'2.0.0': { name, version: '2.0.0' },
619+
},
620+
time: {
621+
'1.0.0': '2001-01-01T00:00:00.000Z',
622+
'1.0.1': '2017-01-01T00:00:00.000Z',
623+
'1.0.2': '2018-01-01T00:00:00.000Z',
624+
'2.0.0': '2019-01-01T00:00:00.000Z',
625+
},
626+
}
627+
628+
const manifest = pickManifest(metadata, '^1.0.0')
629+
t.strictSame(manifest, {
630+
name,
631+
version: '1.0.2',
632+
_time: '2018-01-01T00:00:00.000Z',
633+
}, 'decorated the package with time')
634+
635+
const metadataWithoutTime = {
636+
name,
637+
versions: {
638+
'1.0.0': { name, version: '1.0.0' },
639+
'1.0.1': { name, version: '1.0.1' },
640+
'1.0.2': { name, version: '1.0.2' },
641+
'2.0.0': { name, version: '2.0.0' },
642+
},
643+
}
644+
645+
const metadataNoTime = pickManifest(metadataWithoutTime, '^1.0.0')
646+
t.strictSame(metadataNoTime, {
647+
name,
648+
version: '1.0.2',
649+
}, 'ignores no time')
650+
651+
const metadataWithMissingTime = {
652+
name,
653+
versions: {
654+
'1.0.0': { name, version: '1.0.0' },
655+
'1.0.1': { name, version: '1.0.1' },
656+
'1.0.2': { name, version: '1.0.2' },
657+
'2.0.0': { name, version: '2.0.0' },
658+
},
659+
time: {
660+
'2.0.0': '2018-01-01T00:00:00.000Z',
661+
},
662+
}
663+
664+
const metadataMissingTime = pickManifest(metadataWithMissingTime, '^1.0.0')
665+
t.strictSame(metadataMissingTime, {
666+
name,
667+
version: '1.0.2',
668+
}, 'ignores missing time for version')
669+
670+
t.end()
671+
})

0 commit comments

Comments
 (0)