Debian Codegraph @

workspaces/arborist/test/fixtures/isolated-nock.js

raw
1 const nock = require('nock')
2 const tar = require('tar-stream')
3 const Stream = require('node:stream')
4 const fs = require('node:fs')
5 const path = require('node:path')
6 const os = require('node:os')
7
8 /* Utility to extract a buffer out of a stream */
9 class StreamToBuffer extends Stream.Writable {
10 constructor (opts) {
11 super(opts)
12 this._bufferArray = []
13 }
14
15 _write (chunk, encoding, callback) {
16 this._bufferArray.push(chunk)
17 callback()
18 }
19
20 _final (callback) {
21 this.buffer = Buffer.concat(this._bufferArray)
22 callback()
23 }
24 }
25
26 /**
27 * packPackageToStream
28 * Uses 'tar-stream' to create the tar stream without touching the file system.
29 */
30 function packPackageToStream (manifest, reg) {
31 const {
32 name,
33 version,
34 } = manifest
35 const { shrinkwrap, bundledDeps, ...rest } = manifest
36
37 const pack = tar.pack()
38 const manifestString = JSON.stringify({
39 ...rest,
40 })
41 const index = `console.log('Hello from ${name}@${version}!')`
42 const unscopedName = name.replace(/^@[^/]*\//, '')
43 pack.entry({ name: unscopedName, type: 'directory' })
44 pack.entry({ name: `${unscopedName}/package.json` }, manifestString)
45 pack.entry({ name: `${unscopedName}/index.js` }, index)
46 pack.entry({ name: `${unscopedName}/bin.js` }, '#!/usr/bin/env node\nconsole.log("bin")')
47 if (shrinkwrap) {
48 pack.entry({ name: `${unscopedName}/npm-shrinkwrap.json` }, shrinkwrap.replace(/##REG##/g, reg))
49 }
50 if (bundledDeps) {
51 pack.entry({ name: `${unscopedName}/node_modules`, type: 'directory' })
52 bundledDeps.forEach(d => {
53 pack.entry({ name: `${unscopedName}/node_modules/${d.name}`, type: 'directory' })
54 pack.entry({ name: `${unscopedName}/node_modules/${d.name}/package.json` }, JSON.stringify(d))
55 pack.entry({ name: `${unscopedName}/node_modules/${d.name}/index.js` }, `console.log('Hello from ${d.name}@${d.version}!')`
56 )
57 })
58 }
59
60 pack.finalize()
61 return pack
62 }
63
64 /**
65 * Pack a package for publish.
66 * Returns a buffer containing a tarball of the package.
67 */
68 async function packPackage (manifest, reg) {
69 const packStream = packPackageToStream(manifest, reg)
70
71 const tarBuffer = packStream.pipe(new StreamToBuffer())
72
73 return new Promise((resolve, reject) => {
74 tarBuffer.on('close', () => {
75 resolve(tarBuffer.buffer)
76 })
77 tarBuffer.on('error', reject)
78 })
79 }
80
81 /**
82 * Publish the given package to the given registry.
83 */
84 async function publishPackage (registry, manifest, packuments) {
85 const {
86 name,
87 version,
88 } = manifest
89 const { shrinkwrap, bundledDeps, ...rest } = manifest
90
91 if (packuments.has(name)) {
92 packuments.get(name).versions[version] = {
93 bin: './bin.js',
94 ...rest,
95 dist: {
96 tarball: `${registry}/${name.replace(/\//g, '-')}/${version}.tar`,
97 },
98 }
99 } else {
100 packuments.set(name, {
101 name,
102 'dist-tags': {
103 latest: version,
104 },
105 versions: {
106 [version]: {
107 ...rest,
108 bin: './bin.js',
109 dist: {
110 tarball: `${registry}/${name.replace(/\//g, '-')}/${version}.tar`,
111 },
112 },
113 },
114 })
115 }
116
117 const tarball = await packPackage(manifest, registry)
118
119 nock(registry)
120 .persist()
121 .get(`/${name.replace(/\//g, '-')}/${version}.tar`)
122 .reply(200, tarball)
123 }
124
125 /**
126 * Given an object decribing a dependency graph, this funcion will materialize
127 * this dependency graph on disk and in a registry.
128 *
129 * The input has the following shape:
130 * {
131 * registry: [ package1, package2,... ],
132 * root: { name: <name>, version: <version>, dependencies: <dependencies> }
133 * }
134 * package# represent a registry package and is of the following shape:
135 * { name: <name>, version: <version>, dependencies?: <dependencies> }
136 *
137 * The return value is of the form { dir, registry } where:
138 * - dir is the location on disk of the root package
139 * - registry is the url of the registry which has the necessary packages
140 *
141 * Only simple graphs are currenly supported, the following features will come in further iterations:
142 * - lockfile
143 * - shinkwrap
144 * - bundled dependencies
145 *
146 * The API of this function is not stable and is likely to evolve as we add more features.
147 */
148 async function getRepo (graph) {
149 // Generate a new random registry every time to prevent interference between tests
150 const registry = `https://${Math.random().toString(36).substring(2)}.test`
151
152 const packuments = new Map()
153 // Publish all the registery packages
154 await Promise.all(graph.registry.map(o =>
155 publishPackage(registry, o, packuments)))
156
157 packuments.forEach((packument, name) => {
158 nock(registry)
159 .persist()
160 .get(`/${name.replace(/\//g, '%2f')}`)
161 .reply(200, packument)
162 })
163
164 // Generate the root of the graph on disk
165 const root = graph.root
166 const workspaces = graph.workspaces || []
167 const hasScoped = workspaces.some(w => w.name.startsWith('@'))
168 const workspaceGlobs = hasScoped
169 ? ['packages/*', 'packages/@*/*']
170 : ['packages/*']
171 const repo = {
172 'package.json': JSON.stringify({
173 workspaces: workspaces.length !== 0 ? workspaceGlobs : undefined,
174 ...root,
175 }),
176 packages: {},
177 }
178 workspaces.forEach(wp => {
179 repo.packages[wp.name] = {
180 'package.json': JSON.stringify(wp),
181 'bin.js': '#!/usr/bin/env node\nconsole.log("bin")',
182 'index.js': `console.log('Hello from workspace ${wp.name}')`,
183 }
184 })
185 const dir = testdir(repo)
186 return { dir, registry }
187 }
188
189 function testdir (structure) {
190 const dir = fs.mkdtempSync(`${fs.realpathSync(os.tmpdir())}/test-`)
191 createDir(dir, structure)
192 return dir
193 }
194
195 function createDir (dir, structure) {
196 Object.entries(structure).forEach(([key, value]) => {
197 if (typeof value === 'object') {
198 const newDir = path.join(dir, key)
199 fs.mkdirSync(newDir, { recursive: true })
200 createDir(newDir, value)
201 } else {
202 fs.writeFileSync(path.join(dir, key), value)
203 }
204 })
205 }
206
207 exports.getRepo = getRepo