-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathtarball-fetcher.js
365 lines (313 loc) · 11.2 KB
/
tarball-fetcher.js
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* @flow */
import {SecurityError, MessageError} from '../errors.js';
import type {FetchedOverride} from '../types.js';
import * as constants from '../constants.js';
import BaseFetcher from './base-fetcher.js';
import * as fsUtil from '../util/fs.js';
import {removePrefix} from '../util/misc.js';
import normalizeUrl from 'normalize-url';
const crypto = require('crypto');
const path = require('path');
const tarFs = require('tar-fs');
const url = require('url');
const fs = require('fs');
const stream = require('stream');
const gunzip = require('gunzip-maybe');
const invariant = require('invariant');
const ssri = require('ssri');
const RE_URL_NAME_MATCH = /\/(?:(@[^/]+)\/)?[^/]+\/-\/(?:@[^/]+\/)?([^/]+)$/;
const isHashAlgorithmSupported = name => {
const cachedResult = isHashAlgorithmSupported.__cache[name];
if (cachedResult != null) {
return cachedResult;
}
let supported = true;
try {
crypto.createHash(name);
} catch (error) {
if (error.message !== 'Digest method not supported') {
throw error;
}
supported = false;
}
isHashAlgorithmSupported.__cache[name] = supported;
return supported;
};
isHashAlgorithmSupported.__cache = {};
export default class TarballFetcher extends BaseFetcher {
validateError: ?Object = null;
validateIntegrity: ?Object = null;
async setupMirrorFromCache(): Promise<?string> {
const tarballMirrorPath = this.getTarballMirrorPath();
const tarballCachePath = this.getTarballCachePath();
if (tarballMirrorPath == null) {
return;
}
if (!await fsUtil.exists(tarballMirrorPath) && (await fsUtil.exists(tarballCachePath))) {
// The tarball doesn't exists in the offline cache but does in the cache; we import it to the mirror
await fsUtil.mkdirp(path.dirname(tarballMirrorPath));
await fsUtil.copy(tarballCachePath, tarballMirrorPath, this.reporter);
}
}
getTarballCachePath(): string {
return path.join(this.dest, constants.TARBALL_FILENAME);
}
getTarballMirrorPath(): ?string {
const {pathname} = url.parse(this.reference);
if (pathname == null) {
return null;
}
const match = pathname.match(RE_URL_NAME_MATCH);
let packageFilename;
if (match) {
const [, scope, tarballBasename] = match;
packageFilename = scope ? `${scope}-${tarballBasename}` : tarballBasename;
} else {
// fallback to base name
packageFilename = path.basename(pathname);
}
return this.config.getOfflineMirrorPath(packageFilename);
}
createExtractor(
resolve: (fetched: FetchedOverride) => void,
reject: (error: Error) => void,
tarballPath?: string,
): {
validateStream: ssri.integrityStream,
extractorStream: stream.Transform,
} {
const integrityInfo = this._supportedIntegrity();
const now = new Date();
const fs = require('fs');
const patchedFs = Object.assign({}, fs, {
utimes: (path, atime, mtime, cb) => {
fs.stat(path, (err, stat) => {
if (err) {
cb(err);
return;
}
if (stat.isDirectory()) {
fs.utimes(path, atime, mtime, cb);
return;
}
fs.open(path, 'a', (err, fd) => {
if (err) {
cb(err);
return;
}
fs.futimes(fd, atime, mtime, err => {
if (err) {
fs.close(fd, () => cb(err));
} else {
fs.close(fd, err => cb(err));
}
});
});
});
},
});
const validateStream = new ssri.integrityStream(integrityInfo);
const untarStream = tarFs.extract(this.dest, {
strip: 1,
dmode: 0o755, // all dirs should be readable
fmode: 0o644, // all files should be readable
chown: false, // don't chown. just leave as it is
map: header => {
header.mtime = now;
return header;
},
fs: patchedFs,
});
const extractorStream = gunzip();
validateStream.once('error', err => {
this.validateError = err;
});
validateStream.once('integrity', sri => {
this.validateIntegrity = sri;
});
untarStream.on('error', err => {
reject(new MessageError(this.config.reporter.lang('errorExtractingTarball', err.message, tarballPath)));
});
extractorStream.pipe(untarStream).on('finish', () => {
const error = this.validateError;
const hexDigest = this.validateIntegrity ? this.validateIntegrity.hexDigest() : '';
if (
this.config.updateChecksums &&
this.remote.integrity &&
this.validateIntegrity &&
this.remote.integrity !== this.validateIntegrity.toString()
) {
this.remote.integrity = this.validateIntegrity.toString();
}
if (integrityInfo.algorithms.length === 0) {
return reject(
new SecurityError(
this.config.reporter.lang('fetchBadIntegrityAlgorithm', this.packageName, this.remote.reference),
),
);
}
if (error) {
if (this.config.updateChecksums) {
this.remote.integrity = error.found.toString();
} else {
return reject(
new SecurityError(
this.config.reporter.lang(
'fetchBadHashWithPath',
this.packageName,
this.remote.reference,
error.found.toString(),
error.expected.toString(),
),
),
);
}
}
return resolve({
hash: this.hash || hexDigest,
});
});
return {validateStream, extractorStream};
}
getLocalPaths(override: ?string): Array<string> {
const paths: Array<?string> = [
override ? path.resolve(this.config.cwd, override) : null,
this.getTarballMirrorPath(),
this.getTarballCachePath(),
];
// $FlowFixMe: https://github.com/facebook/flow/issues/1414
return paths.filter(path => path != null);
}
async fetchFromLocal(override: ?string): Promise<FetchedOverride> {
const tarPaths = this.getLocalPaths(override);
const stream = await fsUtil.readFirstAvailableStream(tarPaths);
return new Promise((resolve, reject) => {
if (!stream) {
reject(new MessageError(this.reporter.lang('tarballNotInNetworkOrCache', this.reference, tarPaths)));
return;
}
invariant(stream, 'stream should be available at this point');
// $FlowFixMe - This is available https://nodejs.org/api/fs.html#fs_readstream_path
const tarballPath = stream.path;
const {validateStream, extractorStream} = this.createExtractor(resolve, reject, tarballPath);
stream.pipe(validateStream).pipe(extractorStream).on('error', err => {
reject(new MessageError(this.config.reporter.lang('fetchErrorCorrupt', err.message, tarballPath)));
});
});
}
async fetchFromExternal(): Promise<FetchedOverride> {
const registry = this.config.registries[this.registry];
try {
const headers = this.requestHeaders();
return await registry.request(
this.reference,
{
headers: {
'Accept-Encoding': 'gzip',
...headers,
},
buffer: true,
process: (req, resolve, reject) => {
// should we save this to the offline cache?
const tarballMirrorPath = this.getTarballMirrorPath();
const tarballCachePath = this.getTarballCachePath();
const {validateStream, extractorStream} = this.createExtractor(resolve, reject);
req.pipe(validateStream);
if (tarballMirrorPath) {
validateStream.pipe(fs.createWriteStream(tarballMirrorPath)).on('error', reject);
}
if (tarballCachePath) {
validateStream.pipe(fs.createWriteStream(tarballCachePath)).on('error', reject);
}
validateStream.pipe(extractorStream).on('error', reject);
},
},
this.packageName,
);
} catch (err) {
const tarballMirrorPath = this.getTarballMirrorPath();
const tarballCachePath = this.getTarballCachePath();
if (tarballMirrorPath && (await fsUtil.exists(tarballMirrorPath))) {
await fsUtil.unlink(tarballMirrorPath);
}
if (tarballCachePath && (await fsUtil.exists(tarballCachePath))) {
await fsUtil.unlink(tarballCachePath);
}
throw err;
}
}
requestHeaders(): {[string]: string} {
const registry = this.config.registries[this.registry];
const config = registry.config;
const requestParts = urlParts(this.reference);
return Object.keys(config).reduce((headers, option) => {
const parts = option.split(':');
if (parts.length === 3 && parts[1] === '_header') {
const registryParts = urlParts(parts[0]);
if (requestParts.host === registryParts.host && requestParts.path.startsWith(registryParts.path)) {
const headerName = parts[2];
const headerValue = config[option];
headers[headerName] = headerValue;
}
}
return headers;
}, {});
}
_fetch(): Promise<FetchedOverride> {
const isFilePath = this.reference.startsWith('file:');
this.reference = removePrefix(this.reference, 'file:');
const urlParse = url.parse(this.reference);
// legacy support for local paths in yarn.lock entries
const isRelativePath = urlParse.protocol
? urlParse.protocol.match(/^[a-z]:$/i)
: urlParse.pathname ? urlParse.pathname.match(/^(?:\.{1,2})?[\\\/]/) : false;
if (isFilePath || isRelativePath) {
return this.fetchFromLocal(this.reference);
}
return this.fetchFromLocal().catch(err => this.fetchFromExternal());
}
_findIntegrity(): ?Object {
if (this.remote.integrity) {
return ssri.parse(this.remote.integrity);
}
if (this.hash) {
return ssri.fromHex(this.hash, 'sha1');
}
return null;
}
_supportedIntegrity(): {integrity: ?Object, algorithms: Array<string>} {
const expectedIntegrity = this._findIntegrity() || {};
const expectedIntegrityAlgorithms = Object.keys(expectedIntegrity);
const shouldValidateIntegrity = (this.hash || this.remote.integrity) && !this.config.updateChecksums;
if (expectedIntegrityAlgorithms.length === 0 && !shouldValidateIntegrity) {
const algorithms = this.config.updateChecksums ? ['sha512'] : ['sha1'];
// for consistency, return sha1 for packages without a remote integrity (eg. github)
return {integrity: null, algorithms};
}
const algorithms = new Set();
const integrity = {};
for (const algorithm of expectedIntegrityAlgorithms) {
if (isHashAlgorithmSupported(algorithm)) {
algorithms.add(algorithm);
integrity[algorithm] = expectedIntegrity[algorithm];
}
}
return {integrity, algorithms: Array.from(algorithms)};
}
}
export class LocalTarballFetcher extends TarballFetcher {
_fetch(): Promise<FetchedOverride> {
return this.fetchFromLocal(this.reference);
}
}
type UrlParts = {
host: string,
path: string,
};
function urlParts(requestUrl: string): UrlParts {
const normalizedUrl = normalizeUrl(requestUrl);
const parsed = url.parse(normalizedUrl);
const host = parsed.host || '';
const path = parsed.path || '';
return {host, path};
}