-
Notifications
You must be signed in to change notification settings - Fork 542
/
npm.ts
349 lines (310 loc) · 10.5 KB
/
npm.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import {
assertIsSemVerVersion,
createSnapManifest,
DEFAULT_REQUESTED_SNAP_VERSION,
getTargetVersion,
isValidUrl,
NpmSnapIdStruct,
SemVerRange,
SemVerVersion,
SnapManifest,
VirtualFile,
} from '@metamask/snaps-utils';
import { assert, assertStruct, isObject } from '@metamask/utils';
import concat from 'concat-stream';
import createGunzipStream from 'gunzip-maybe';
import pump from 'pump';
import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';
import { Readable, Writable } from 'stream';
import { extract as tarExtract } from 'tar-stream';
import { normalizeRelative } from '../../utils';
import { DetectSnapLocationOptions, SnapLocation } from './location';
const DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org';
interface NpmMeta {
registry: URL;
packageName: string;
requestedRange: SemVerRange;
version?: string;
fetch: typeof fetch;
}
export interface NpmOptions {
/**
* @default DEFAULT_REQUESTED_SNAP_VERSION
*/
versionRange?: SemVerRange;
/**
* Whether to allow custom NPM registries outside of {@link DEFAULT_NPM_REGISTRY}.
*
* @default false
*/
allowCustomRegistries?: boolean;
}
export class NpmLocation implements SnapLocation {
private readonly meta: NpmMeta;
private validatedManifest?: VirtualFile<SnapManifest>;
private files?: Map<string, VirtualFile>;
constructor(url: URL, opts: DetectSnapLocationOptions = {}) {
const allowCustomRegistries = opts.allowCustomRegistries ?? false;
const fetchFunction = opts.fetch ?? globalThis.fetch.bind(globalThis);
const requestedRange = opts.versionRange ?? DEFAULT_REQUESTED_SNAP_VERSION;
assertStruct(url.toString(), NpmSnapIdStruct, 'Invalid Snap Id: ');
let registry: string | URL;
if (
url.host === '' &&
url.port === '' &&
url.username === '' &&
url.password === ''
) {
registry = new URL(DEFAULT_NPM_REGISTRY);
} else {
registry = 'https://';
if (url.username) {
registry += url.username;
if (url.password) {
registry += `:${url.password}`;
}
registry += '@';
}
registry += url.host;
registry = new URL(registry);
assert(
allowCustomRegistries,
new TypeError(
`Custom NPM registries are disabled, tried to use "${registry.toString()}".`,
),
);
}
assert(
registry.pathname === '/' &&
registry.search === '' &&
registry.hash === '',
);
assert(
url.pathname !== '' && url.pathname !== '/',
new TypeError('The package name in NPM location is empty.'),
);
let packageName = url.pathname;
if (packageName.startsWith('/')) {
packageName = packageName.slice(1);
}
this.meta = {
requestedRange,
registry,
packageName,
fetch: fetchFunction,
};
}
async manifest(): Promise<VirtualFile<SnapManifest>> {
if (this.validatedManifest) {
return this.validatedManifest.clone();
}
const vfile = await this.fetch('snap.manifest.json');
const result = JSON.parse(vfile.toString());
vfile.result = createSnapManifest(result);
this.validatedManifest = vfile as VirtualFile<SnapManifest>;
return this.manifest();
}
async fetch(path: string): Promise<VirtualFile> {
const relativePath = normalizeRelative(path);
if (!this.files) {
await this.#lazyInit();
assert(this.files !== undefined);
}
const vfile = this.files.get(relativePath);
assert(
vfile !== undefined,
new TypeError(`File "${path}" not found in package.`),
);
return vfile.clone();
}
get packageName(): string {
return this.meta.packageName;
}
get version(): string {
assert(
this.meta.version !== undefined,
'Tried to access version without first fetching NPM package.',
);
return this.meta.version;
}
get registry(): URL {
return this.meta.registry;
}
get versionRange(): SemVerRange {
return this.meta.requestedRange;
}
async #lazyInit() {
assert(this.files === undefined);
const [tarballResponse, actualVersion] = await fetchNpmTarball(
this.meta.packageName,
this.meta.requestedRange,
this.meta.registry,
this.meta.fetch,
);
this.meta.version = actualVersion;
let canonicalBase = 'npm://';
if (this.meta.registry.username !== '') {
canonicalBase += this.meta.registry.username;
if (this.meta.registry.password !== '') {
canonicalBase += `:${this.meta.registry.password}`;
}
canonicalBase += '@';
}
canonicalBase += this.meta.registry.host;
// TODO(ritave): Lazily extract files instead of up-front extracting all of them
// We would need to replace tar-stream package because it requires immediate consumption of streams.
await new Promise<void>((resolve, reject) => {
this.files = new Map();
pump(
getNodeStream(tarballResponse),
// The "gz" in "tgz" stands for "gzip". The tarball needs to be decompressed
// before we can actually grab any files from it.
createGunzipStream(),
createTarballStream(
`${canonicalBase}/${this.meta.packageName}/`,
this.files,
),
(error) => {
error ? reject(error) : resolve();
},
);
});
}
}
/**
* Fetches the tarball (`.tgz` file) of the specified package and version from
* the public npm registry. Throws an error if fetching fails.
*
* @param packageName - The name of the package whose tarball to fetch.
* @param versionRange - The SemVer range of the package to fetch. The highest
* version satisfying the range will be fetched.
* @param registryUrl - The URL of the npm registry to fetch the tarball from.
* @param fetchFunction - The fetch function to use. Defaults to the global
* {@link fetch}. Useful for Node.js compatibility.
* @returns A tuple of the {@link Response} for the package tarball and the
* actual version of the package.
*/
async function fetchNpmTarball(
packageName: string,
versionRange: SemVerRange,
registryUrl: URL | string,
fetchFunction: typeof fetch,
): Promise<[ReadableStream, SemVerVersion]> {
const packageMetadata = await (
await fetchFunction(new URL(packageName, registryUrl).toString())
).json();
if (!isObject(packageMetadata)) {
throw new Error(
`Failed to fetch package "${packageName}" metadata from npm.`,
);
}
const versions = Object.keys((packageMetadata as any)?.versions ?? {}).map(
(version) => {
assertIsSemVerVersion(version);
return version;
},
);
const targetVersion = getTargetVersion(versions, versionRange);
if (targetVersion === null) {
throw new Error(
`Failed to find a matching version in npm metadata for package "${packageName}" and requested semver range "${versionRange}".`,
);
}
const tarballUrlString = (packageMetadata as any)?.versions?.[targetVersion]
?.dist?.tarball;
if (
!isValidUrl(tarballUrlString) ||
!tarballUrlString.toString().endsWith('.tgz')
) {
throw new Error(
`Failed to find valid tarball URL in NPM metadata for package "${packageName}".`,
);
}
// Override the tarball hostname/protocol with registryUrl hostname/protocol
const newRegistryUrl = new URL(registryUrl);
const newTarballUrl = new URL(tarballUrlString);
newTarballUrl.hostname = newRegistryUrl.hostname;
newTarballUrl.protocol = newRegistryUrl.protocol;
// Perform a raw fetch because we want the Response object itself.
const tarballResponse = await fetchFunction(newTarballUrl.toString());
if (!tarballResponse.ok || !tarballResponse.body) {
throw new Error(`Failed to fetch tarball for package "${packageName}".`);
}
return [tarballResponse.body, targetVersion];
}
/**
* The paths of files within npm tarballs appear to always be prefixed with
* "package/".
*/
const NPM_TARBALL_PATH_PREFIX = /^package\//u;
/**
* Converts a {@link ReadableStream} to a Node.js {@link Readable}
* stream. Returns the stream directly if it is already a Node.js stream.
* We can't use the native Web {@link ReadableStream} directly because the
* other stream libraries we use expect Node.js streams.
*
* @param stream - The stream to convert.
* @returns The given stream as a Node.js Readable stream.
*/
function getNodeStream(stream: ReadableStream): Readable {
if (typeof stream.getReader !== 'function') {
return stream as unknown as Readable;
}
return new ReadableWebToNodeStream(stream);
}
/**
* Creates a `tar-stream` that will get the necessary files from an npm Snap
* package tarball (`.tgz` file).
*
* @param canonicalBase - A base URI as specified in {@link https://github.com/MetaMask/SIPs/blob/main/SIPS/sip-8.md SIP-8}. Starting with 'npm:'. Will be used for canonicalPath vfile argument.
* @param files - An object to write target file contents to.
* @returns The {@link Writable} tarball extraction stream.
*/
function createTarballStream(
canonicalBase: string,
files: Map<string, VirtualFile>,
): Writable {
assert(
canonicalBase.endsWith('/'),
"Base needs to end with '/' for relative paths to be added as children instead of siblings.",
);
assert(
canonicalBase.startsWith('npm:'),
'Protocol mismatch, expected "npm:".',
);
// `tar-stream` is pretty old-school, so we create it first and then
// instrument it by adding event listeners.
const extractStream = tarExtract();
// "entry" is fired for every discreet entity in the tarball. This includes
// files and folders.
extractStream.on('entry', (header, entryStream, next) => {
const { name: headerName, type: headerType } = header;
if (headerType === 'file') {
// The name is a path if the header type is "file".
const path = headerName.replace(NPM_TARBALL_PATH_PREFIX, '');
return entryStream.pipe(
concat((data) => {
const vfile = new VirtualFile({
value: data,
path,
data: {
canonicalPath: new URL(path, canonicalBase).toString(),
},
});
assert(
!files.has(path),
'Malformed tarball, multiple files with the same path.',
);
files.set(path, vfile);
return next();
}),
);
}
// If we get here, the entry is not a file, and we want to ignore. The entry
// stream must be drained, or the extractStream will stop reading. This is
// effectively a no-op for the current entry.
entryStream.on('end', () => next());
return entryStream.resume();
});
return extractStream;
}