Repository navigation
Expand file tree
/
Copy pathpnprServer.test.mjs
More file actions
131 lines (117 loc) · 4.26 KB
/
Copy pathpnprServer.test.mjs
File metadata and controls
131 lines (117 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import assert from 'node:assert/strict'
import crypto from 'node:crypto'
import fs from 'node:fs'
import http from 'node:http'
import path from 'node:path'
import test from 'node:test'
import tempy from 'tempy'
import writeYamlFile from 'write-yaml-file'
import {
collectLockedPackages,
packageSegments,
variantPath,
warmZstdEncodings,
} from './pnprServer.js'
const REGISTRY = 'http://127.0.0.1:41647/npm/'
test('collectLockedPackages keeps pnpr-rewritten registry tarballs and skips non-registry entries', async () => {
const dir = tempy.directory()
const lockfilePath = path.join(dir, 'pnpm-lock.yaml')
await writeYamlFile(lockfilePath, {
lockfileVersion: '9.0',
packages: {
'left-pad@1.3.0': {
resolution: {
integrity: 'sha512-plain',
},
},
'@scope/pkg@2.0.0': {
resolution: {
integrity: 'sha512-rewritten',
tarball: `${REGISTRY}@scope/pkg@2.0.0`,
},
},
'ext-tarball@https://example.com/ext.tgz': {
resolution: {
integrity: 'sha512-external',
tarball: 'https://example.com/ext.tgz',
},
},
'git-pkg@git+https://example.com/repo.git#deadbeef': {
resolution: {
type: 'git',
repo: 'https://example.com/repo.git',
commit: 'deadbeef',
},
},
},
})
const packages = new Map()
collectLockedPackages(lockfilePath, REGISTRY, packages)
assert.deepEqual([...packages.entries()], [
['left-pad@1.3.0', { name: 'left-pad', version: '1.3.0', integrity: 'sha512-plain' }],
['@scope/pkg@2.0.0', { name: '@scope/pkg', version: '2.0.0', integrity: 'sha512-rewritten' }],
])
})
test('collectLockedPackages refuses a lockfile with no registry packages', async () => {
const dir = tempy.directory()
const lockfilePath = path.join(dir, 'pnpm-lock.yaml')
await writeYamlFile(lockfilePath, {
lockfileVersion: '9.0',
packages: {
'ext@https://example.com/ext.tgz': {
resolution: {
integrity: 'sha512-external',
tarball: 'https://example.com/ext.tgz',
},
},
},
})
assert.throws(
() => collectLockedPackages(lockfilePath, REGISTRY, new Map()),
/No registry packages found/
)
})
test('packageSegments and variantPath match pnpr storage layout', () => {
assert.deepEqual(packageSegments('left-pad'), ['@', 'left-pad'])
assert.deepEqual(packageSegments('@scope/pkg'), ['@scope', 'pkg'])
assert.deepEqual(packageSegments('@Scope/Pkg'), ['@!53cope', '!50kg'])
const integrity = 'sha512-abc'
const hash = crypto.createHash('sha256').update(integrity).digest('hex')
assert.equal(
variantPath('/storage/.pnpr-cache/~tar-zstd19/packages', { name: '@scope/pkg', integrity }),
path.join('/storage/.pnpr-cache/~tar-zstd19/packages', '@scope', 'pkg', 'blobs', `${hash}.tar.zst`)
)
assert.equal(
variantPath('/storage/.pnpr-cache/~tar-zstd19/packages', { name: 'left-pad', integrity }),
path.join('/storage/.pnpr-cache/~tar-zstd19/packages', '@', 'left-pad', 'blobs', `${hash}.tar.zst`)
)
})
test('warmZstdEncodings writes variants at the expected layout and fails on layout mismatch', async () => {
const storageDir = tempy.directory()
const variantsDir = path.join(storageDir, '.pnpr-cache', '~tar-zstd19', 'packages')
const pkg = { name: '@scope/pkg', version: '1.0.0', integrity: 'sha512-test' }
const packages = new Map([['@scope/pkg@1.0.0', pkg]])
let writeAtExpectedPath = false
const server = http.createServer((req, res) => {
if (writeAtExpectedPath && req.url === '/npm/~zstd/@scope/pkg@1.0.0') {
const target = variantPath(variantsDir, pkg)
fs.mkdirSync(path.dirname(target), { recursive: true })
fs.writeFileSync(target, 'zstd-bytes')
}
res.writeHead(200)
res.end('tar-bytes')
})
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
const tarRegistry = `http://127.0.0.1:${server.address().port}/npm/~zstd/`
try {
await assert.rejects(
() => warmZstdEncodings({ storageDir, tarRegistry, packages }),
/No level-19 zstd encoding appeared/
)
writeAtExpectedPath = true
await warmZstdEncodings({ storageDir, tarRegistry, packages })
assert.ok(fs.existsSync(variantPath(variantsDir, pkg)))
} finally {
server.close()
}
})