File: fetchers.js

package info (click to toggle)
node-yarnpkg 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 92,980 kB
  • sloc: sh: 323; makefile: 19
file content (361 lines) | stat: -rw-r--r-- 11,843 bytes parent folder | download | duplicates (2)
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
/* @flow */
/* eslint max-len: 0 */

import {Reporter} from '../src/reporters/index.js';
import TarballFetcher, {LocalTarballFetcher} from '../src/fetchers/tarball-fetcher.js';
import BaseFetcher from '../src/fetchers/base-fetcher.js';
import CopyFetcher from '../src/fetchers/copy-fetcher.js';
import GitFetcher from '../src/fetchers/git-fetcher.js';
import Config from '../src/config.js';
import mkdir from './_temp.js';
import * as fs from '../src/util/fs.js';
import {readdirSync} from 'fs';

const path = require('path');
const ssri = require('ssri');

jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;

test('BaseFetcher.fetch', async () => {
  const dir = await mkdir('base-fetcher');
  const fetcher = new BaseFetcher(
    dir,
    {
      type: 'base',
      registry: 'npm',
      reference: '',
      hash: null,
    },
    await Config.create(),
  );
  let error;

  try {
    await fetcher.fetch();
  } catch (e) {
    error = e;
  }
  expect(error && error.message).toBe('Not implemented');
});

test('CopyFetcher.fetch', async () => {
  const a = await mkdir('copy-fetcher-a');
  await fs.writeFile(path.join(a, 'package.json'), '{}');
  await fs.writeFile(path.join(a, 'foo'), 'bar');

  const b = await mkdir('copy-fetcher-b');
  const fetcher = new CopyFetcher(
    b,
    {
      type: 'copy',
      reference: a,
      registry: 'npm',
      hash: null,
    },
    await Config.create(),
  );
  await fetcher.fetch();
  const content = await fs.readFile(path.join(b, 'package.json'));
  expect(content).toBe('{}');
  const contentFoo = await fs.readFile(path.join(b, 'foo'));
  expect(contentFoo).toBe('bar');
});

test('GitFetcher.fetch', async () => {
  const dir = await mkdir('git-fetcher');
  const fetcher = new GitFetcher(
    dir,
    {
      type: 'git',
      reference: 'https://github.com/sindresorhus/beeper',
      hash: '8beb0413a8028ca2d52dbb86c75f42069535591b',
      registry: 'npm',
    },
    await Config.create(),
  );
  await fetcher.fetch();
  const name = (await fs.readJson(path.join(dir, 'package.json'))).name;
  expect(name).toBe('beeper');
});

test('GitFetcher.getTarballMirrorPath without slashes in the repo path', async () => {
  const dir = await mkdir('git-fetcher');
  const config = await Config.create();
  config.registries.yarn.config['yarn-offline-mirror'] = 'test';

  const fetcher = new GitFetcher(
    dir,
    {
      type: 'git',
      reference: 'ssh://git@github.com:example-without-slash-repo.git',
      hash: '8beb0413a8028ca2d52dbb86c75f42069535591b',
      registry: 'npm',
    },
    config,
  );
  const cachePath = fetcher.getTarballMirrorPath();
  expect(cachePath).toBe(path.join('test', 'example-without-slash-repo.git-8beb0413a8028ca2d52dbb86c75f42069535591b'));
});

test('GitFetcher.fetch with prepare script', async () => {
  const dir = await mkdir('git-fetcher-with-prepare');
  const fetcher = new GitFetcher(
    dir,
    {
      type: 'git',
      reference: 'https://github.com/Volune/test-js-git-repo',
      hash: '0e56593e326069ed4bcec8126bb48a1891215c57',
      registry: 'npm',
    },
    await Config.create(),
  );
  await fetcher.fetch();
  const name = (await fs.readJson(path.join(dir, 'package.json'))).name;
  expect(name).toBe('test-js-git-repo');
  const dependencyName = (await fs.readJson(path.join(dir, 'dependency-package.json'))).name;
  expect(dependencyName).toBe('beeper');
  // The file "prepare.js" is not in "files" list
  expect(await fs.exists(path.join(dir, 'prepare.js'))).toBe(false);
  // Check the dependency with a bin script was correctly executed
  expect(await fs.exists(path.join(dir, 'testscript.output.txt'))).toBe(true);
  // Check executed lifecycle scripts
  expect(await fs.exists(path.join(dir, 'generated', 'preinstall'))).toBe(true);
  expect(await fs.exists(path.join(dir, 'generated', 'install'))).toBe(true);
  expect(await fs.exists(path.join(dir, 'generated', 'postinstall'))).toBe(true);
  expect(await fs.exists(path.join(dir, 'generated', 'prepublish'))).toBe(false);
});

test('TarballFetcher.fetch', async () => {
  const dir = await mkdir('tarball-fetcher');
  const fetcher = new TarballFetcher(
    dir,
    {
      type: 'tarball',
      hash: '51f12d36860fc3d2ab747377991746e8ea3faabb',
      reference: 'https://github.com/sindresorhus/beeper/archive/master.tar.gz',
      registry: 'npm',
    },
    await Config.create(),
  );

  await fetcher.fetch();
  const name = (await fs.readJson(path.join(dir, 'package.json'))).name;
  expect(name).toBe('beeper');
});

test('TarballFetcher.fetch throws on invalid hash', async () => {
  const dir = await mkdir('tarball-fetcher');
  const offlineMirrorDir = await mkdir('offline-mirror');

  const config = await Config.create({}, new Reporter());
  config.registries.npm.config['yarn-offline-mirror'] = offlineMirrorDir;

  const url = 'https://github.com/sindresorhus/beeper/archive/master.tar.gz';
  const fetcher = new TarballFetcher(
    dir,
    {
      type: 'tarball',
      hash: 'abcd',
      reference: url,
      registry: 'npm',
    },
    config,
  );

  expect(fetcher.fetch()).rejects.toMatchObject({
    message: expect.stringContaining("computed integrity doesn't match our records"),
  });
  expect(readdirSync(path.join(offlineMirrorDir))).toEqual([]);
});

test('TarballFetcher.fetch fixes hash if updateChecksums flag is true', async () => {
  const wrongHash = 'abcd';
  const dir = await mkdir(`tarball-fetcher-${wrongHash}`);
  const config = await Config.create({}, new Reporter());
  config.updateChecksums = true;
  const url = 'https://github.com/sindresorhus/beeper/archive/master.tar.gz';
  const fetcher = new TarballFetcher(
    dir,
    {
      type: 'tarball',
      hash: wrongHash,
      reference: url,
      registry: 'npm',
    },
    config,
  );
  await fetcher.fetch();
  const dirWithProperHash = dir.replace(wrongHash, fetcher.hash);
  const name = (await fs.readJson(path.join(dirWithProperHash, 'package.json'))).name;
  expect(name).toBe('beeper');
});

test('TarballFetcher.fetch throws on invalid integrity', async () => {
  const dir = await mkdir('tarball-fetcher');
  const offlineMirrorDir = await mkdir('offline-mirror');

  const config = await Config.create({}, new Reporter());
  config.registries.npm.config['yarn-offline-mirror'] = offlineMirrorDir;

  const fetcher = new TarballFetcher(
    dir,
    {
      type: 'tarball',
      hash: '6f86cbedd8be4ec987be9aaf33c9684db1b31e7e',
      reference: 'https://registry.npmjs.org/lodash.isempty/-/lodash.isempty-4.4.0.tgz',
      registry: 'npm',
      integrity: ssri.parse('sha512-foo'),
    },
    config,
  );

  expect(fetcher.fetch()).rejects.toMatchObject({
    message: expect.stringContaining("computed integrity doesn't match our records"),
  });
  expect(readdirSync(path.join(offlineMirrorDir))).toEqual([]);
});

test('TarballFetcher.fetch supports local ungzipped tarball', async () => {
  const dir = await mkdir('tarball-fetcher');
  const fetcher = new LocalTarballFetcher(
    dir,
    {
      type: 'tarball',
      hash: '25c5098052a7bd322c7db80c26852e9209f98d4f',
      reference: path.join(__dirname, 'fixtures', 'fetchers', 'tarball', 'ungzipped.tar'),
      registry: 'npm',
    },
    await Config.create(),
  );
  await fetcher.fetch();
  const name = (await fs.readJson(path.join(dir, 'package.json'))).name;
  expect(name).toBe('beeper');
});

test('TarballFetcher.fetch properly stores tarball of package in offline mirror', async () => {
  const dir = await mkdir('tarball-fetcher');
  const offlineMirrorDir = await mkdir('offline-mirror');

  const config = await Config.create();
  config.registries.npm.config['yarn-offline-mirror'] = offlineMirrorDir;

  const fetcher = new TarballFetcher(
    dir,
    {
      type: 'tarball',
      hash: '6f86cbedd8be4ec987be9aaf33c9684db1b31e7e',
      reference: 'https://registry.npmjs.org/lodash.isempty/-/lodash.isempty-4.4.0.tgz',
      registry: 'npm',
    },
    config,
  );

  await fetcher.fetch();
  const exists = await fs.exists(path.join(offlineMirrorDir, 'lodash.isempty-4.4.0.tgz'));
  expect(exists).toBe(true);
});

test('TarballFetcher.fetch properly stores tarball of scoped package in offline mirror', async () => {
  const dir = await mkdir('tarball-fetcher');
  const offlineMirrorDir = await mkdir('offline-mirror');

  const config = await Config.create();
  config.registries.npm.config['yarn-offline-mirror'] = offlineMirrorDir;

  const fetcher = new TarballFetcher(
    dir,
    {
      type: 'tarball',
      hash: '6f0ab73cdd7b82d8e81e80838b49e9e4c7fbcc44',
      reference: 'https://registry.npmjs.org/@exponent/configurator/-/configurator-1.0.2.tgz',
      registry: 'npm',
    },
    config,
  );

  await fetcher.fetch();
  const exists = await fs.exists(path.join(offlineMirrorDir, '@exponent-configurator-1.0.2.tgz'));
  expect(exists).toBe(true);
});

test('TarballFetcher.fetch properly stores tarball for scoped package resolved from artifactory registry', async () => {
  const dir = await mkdir('tarball-fetcher');
  const offlineMirrorDir = await mkdir('offline-mirror');

  const config = await Config.create();
  config.registries.npm.config['yarn-offline-mirror'] = offlineMirrorDir;

  const fetcher = new TarballFetcher(
    dir,
    {
      type: 'tarball',
      hash: '6f0ab73cdd7b82d8e81e80838b49e9e4c7fbcc44',
      reference:
        'https://artifactory.internal.site:443/artifactory/api/npm/external-mirror/@exponent/configurator/-/configurator-1.0.2.tgz',
      registry: 'npm',
    },
    config,
  );

  expect(fetcher.getTarballMirrorPath()).toBe(path.join(offlineMirrorDir, '@exponent-configurator-1.0.2.tgz'));
});

test('TarballFetcher.fetch properly stores tarball for scoped package resolved from new  style URLs', async () => {
  const dir = await mkdir('tarball-fetcher');
  const offlineMirrorDir = await mkdir('offline-mirror');

  const config = await Config.create();
  config.registries.npm.config['yarn-offline-mirror'] = offlineMirrorDir;

  const fetcher = new TarballFetcher(
    dir,
    {
      type: 'tarball',
      hash: '6f0ab73cdd7b82d8e81e80838b49e9e4c7fbcc44',
      reference:
        'https://artifactory.internal.site:443/artifactory/api/npm/external-mirror/@exponent/configurator/-/@exponent/configurator-1.0.2.tgz',
      registry: 'npm',
    },
    config,
  );

  expect(fetcher.getTarballMirrorPath()).toBe(path.join(offlineMirrorDir, '@exponent-configurator-1.0.2.tgz'));
});

test('TarballFetcher.fetch throws on truncated tar data', async () => {
  const dir = await mkdir('tarball-fetcher');
  const reporter = new Reporter();
  const fetcher = new TarballFetcher(
    dir,
    {
      type: 'tarball',
      hash: '5b0482e1cc75d37dfc7f6e6d663d08c96442dcd5',
      reference: 'file:' + path.join(__dirname, 'fixtures', 'fetchers', 'tarball', 'broken-tar-data.tgz'),
      registry: 'npm',
    },
    await Config.create({}, reporter),
  );
  await expect(fetcher.fetch()).rejects.toThrow(
    // The "." in ".tgz" should be escaped, but that doesn't work with reporter.lang
    new RegExp(reporter.lang('errorExtractingTarball', '.*', '.*broken-tar-data.tgz')),
  );
});

test('TarballFetcher.fetch throws on truncated tar header', async () => {
  const dir = await mkdir('tarball-fetcher');
  const reporter = new Reporter();
  const fetcher = new TarballFetcher(
    dir,
    {
      type: 'tarball',
      hash: '1d403a8c7ef4ce25b1e7b188ede272a42ce49a52',
      reference: 'file:' + path.join(__dirname, 'fixtures', 'fetchers', 'tarball', 'broken-tar-header.tgz'),
      registry: 'npm',
    },
    await Config.create({}, reporter),
  );
  await expect(fetcher.fetch()).rejects.toThrow(
    // The "." in ".tgz" should be escaped, but that doesn't work with reporter.lang
    new RegExp(reporter.lang('errorExtractingTarball', '.*', '.*broken-tar-header.tgz')),
  );
});