/
index.js
489 lines (459 loc) · 13.4 KB
/
index.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
'use strict'
const Minipass = require('minipass')
const ssri = require('ssri')
const t = require('tap')
const zlib = require('zlib')
const defaultOpts = require('../lib/default-opts.js')
const tnock = require('./util/tnock.js')
t.equal(defaultOpts.registry, 'https://registry.npmjs.org/',
'default registry is the npm public registry')
// ok, now change it for the tests
defaultOpts.registry = 'https://mock.reg/'
const fetch = require('..')
const OPTS = {
timeout: 0,
retry: {
retries: 1,
factor: 1,
minTimeout: 1,
maxTimeout: 10,
},
}
t.test('hello world', t => {
tnock(t, defaultOpts.registry)
.get('/hello')
.reply(200, { hello: 'world' })
return fetch('/hello', {
method: false, // will fall back to GET if falsey,
...OPTS,
})
.then(res => {
t.equal(res.status, 200, 'got successful response')
return res.json()
})
.then(json => t.same(json, { hello: 'world' }, 'got correct body'))
})
t.test('JSON body param', t => {
tnock(t, defaultOpts.registry)
.matchHeader('content-type', ctype => {
t.equal(ctype[0], 'application/json', 'content-type automatically set')
return ctype[0] === 'application/json'
})
.post('/hello')
.reply(200, (uri, reqBody) => {
t.same(reqBody, {
hello: 'world',
}, 'got the JSON version of the body')
return reqBody
})
const opts = {
...OPTS,
method: 'POST',
body: { hello: 'world' },
}
return fetch('/hello', opts)
.then(res => {
t.equal(res.status, 200)
return res.json()
})
.then(json => t.same(json, { hello: 'world' }))
})
t.test('buffer body param', t => {
tnock(t, defaultOpts.registry)
.matchHeader('content-type', ctype => {
t.equal(ctype[0], 'application/octet-stream', 'content-type automatically set')
return ctype[0] === 'application/octet-stream'
})
.post('/hello')
.reply(200, (uri, reqBody) => {
t.same(
Buffer.from(reqBody, 'utf8'),
Buffer.from('hello', 'utf8'),
'got the JSON version of the body'
)
return reqBody
})
const opts = {
...OPTS,
method: 'POST',
body: Buffer.from('hello', 'utf8'),
}
return fetch('/hello', opts)
.then(res => {
t.equal(res.status, 200)
return res.buffer()
})
.then(buf =>
t.same(buf, Buffer.from('hello', 'utf8'), 'got response')
)
})
t.test('stream body param', t => {
tnock(t, defaultOpts.registry)
.matchHeader('content-type', ctype => {
t.equal(ctype[0], 'application/octet-stream', 'content-type automatically set')
return ctype[0] === 'application/octet-stream'
})
.post('/hello')
.reply(200, (uri, reqBody) => {
t.same(JSON.parse(reqBody), {
hello: 'world',
}, 'got the stringified version of the body')
return reqBody
})
const stream = new Minipass()
setImmediate(() => stream.end(JSON.stringify({ hello: 'world' })))
const opts = {
...OPTS,
method: 'POST',
body: stream,
}
return fetch('/hello', opts)
.then(res => {
t.equal(res.status, 200)
return res.json()
})
.then(json => t.same(json, { hello: 'world' }))
})
t.test('JSON body param', t => {
tnock(t, defaultOpts.registry)
.matchHeader('content-type', ctype => {
t.equal(ctype[0], 'application/json', 'content-type automatically set')
return ctype[0] === 'application/json'
})
.matchHeader('content-encoding', enc => {
t.equal(enc[0], 'gzip', 'content-encoding automatically set')
return enc[0] === 'gzip'
})
.post('/hello')
// NOTE: can't really test the body itself here because nock freaks out.
.reply(200)
const opts = {
...OPTS,
method: 'POST',
body: { hello: 'world' },
gzip: true,
}
return fetch('/hello', opts)
.then(res => {
t.equal(res.status, 200, 'request succeeded')
})
})
t.test('gzip + buffer body param', t => {
tnock(t, defaultOpts.registry)
.matchHeader('content-type', ctype => {
t.equal(ctype[0], 'application/octet-stream', 'content-type automatically set')
return ctype[0] === 'application/octet-stream'
})
.matchHeader('content-encoding', enc => {
t.equal(enc[0], 'gzip', 'content-encoding automatically set')
return enc[0] === 'gzip'
})
.post('/hello')
.reply(200, (uri, reqBody) => {
reqBody = zlib.gunzipSync(Buffer.from(reqBody, 'hex'))
t.same(
Buffer.from(reqBody, 'utf8').toString('utf8'),
'hello',
'got the JSON version of the body'
)
return reqBody
})
const opts = {
...OPTS,
method: 'POST',
body: Buffer.from('hello', 'utf8'),
gzip: true,
}
return fetch('/hello', opts)
.then(res => {
t.equal(res.status, 200)
return res.buffer()
})
.then(buf =>
t.same(buf, Buffer.from('hello', 'utf8'), 'got response')
)
})
t.test('gzip + stream body param', t => {
tnock(t, defaultOpts.registry)
.matchHeader('content-type', ctype => {
t.equal(ctype[0], 'application/octet-stream', 'content-type automatically set')
return ctype[0] === 'application/octet-stream'
})
.matchHeader('content-encoding', enc => {
t.equal(enc[0], 'gzip', 'content-encoding automatically set')
return enc[0] === 'gzip'
})
.post('/hello')
.reply(200, (uri, reqBody) => {
reqBody = zlib.gunzipSync(Buffer.from(reqBody, 'hex'))
t.same(JSON.parse(reqBody.toString('utf8')), {
hello: 'world',
}, 'got the stringified version of the body')
return reqBody
})
const stream = new Minipass()
setImmediate(() => stream.end(JSON.stringify({ hello: 'world' })))
const opts = {
...OPTS,
method: 'POST',
body: stream,
gzip: true,
query: {
everything: undefined,
is: undefined,
},
}
return fetch('/hello', opts)
.then(res => {
t.equal(res.status, 200)
return res.json()
})
.then(json => t.same(json, { hello: 'world' }))
})
t.test('query strings', t => {
tnock(t, defaultOpts.registry)
.get('/hello?hi=there&who=wor%20ld')
.reply(200, { hello: 'world' })
return fetch.json('/hello?hi=there', {
...OPTS,
query: 'who=wor ld',
}).then(json => t.equal(json.hello, 'world', 'query-string merged'))
})
t.test('query strings - undefined values', t => {
tnock(t, defaultOpts.registry)
.get('/hello?who=wor%20ld')
.reply(200, { ok: true })
return fetch.json('/hello', {
...OPTS,
query: { hi: undefined, who: 'wor ld' },
}).then(json => t.ok(json.ok, 'undefined keys not included in query string'))
})
t.test('json()', t => {
tnock(t, defaultOpts.registry)
.get('/hello')
.reply(200, { hello: 'world' })
return fetch.json('/hello', OPTS)
.then(json => t.same(json, { hello: 'world' }, 'got json body'))
})
t.test('query string with ?write=true', t => {
const cache = t.testdir()
const opts = { ...OPTS, preferOffline: true, cache }
const qsString = { ...opts, query: { write: 'true' } }
const qsBool = { ...opts, query: { write: true } }
tnock(t, defaultOpts.registry)
.get('/writeTrueTest?write=true')
.times(6)
.reply(200, { write: 'go for it' })
return fetch.json('/writeTrueTest?write=true', opts)
.then(res => t.strictSame(res, { write: 'go for it' }))
.then(() => fetch.json('/writeTrueTest?write=true', opts))
.then(res => t.strictSame(res, { write: 'go for it' }))
.then(() => fetch.json('/writeTrueTest', qsString))
.then(res => t.strictSame(res, { write: 'go for it' }))
.then(() => fetch.json('/writeTrueTest', qsString))
.then(res => t.strictSame(res, { write: 'go for it' }))
.then(() => fetch.json('/writeTrueTest', qsBool))
.then(res => t.strictSame(res, { write: 'go for it' }))
.then(() => fetch.json('/writeTrueTest', qsBool))
.then(res => t.strictSame(res, { write: 'go for it' }))
})
t.test('fetch.json.stream()', t => {
tnock(t, defaultOpts.registry).get('/hello').reply(200, {
a: 1,
b: 2,
c: 3,
})
return fetch.json.stream('/hello', '$*', OPTS).collect().then(data => {
t.same(data, [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
{ key: 'c', value: 3 },
], 'got a streamed JSON body')
})
})
t.test('fetch.json.stream opts.mapJSON', t => {
tnock(t, defaultOpts.registry).get('/hello').reply(200, {
a: 1,
b: 2,
c: 3,
})
return fetch.json.stream('/hello', '*', {
...OPTS,
mapJSON (value, [key]) {
return [key, value]
},
}).collect().then(data => {
t.same(data, [
['a', 1],
['b', 2],
['c', 3],
], 'data mapped')
})
})
t.test('fetch.json.stream gets fetch error on stream', t => {
return t.rejects(fetch.json.stream('/hello', '*', {
...OPTS,
body: Promise.reject(new Error('no body for you')),
method: 'POST',
gzip: true, // make sure we don't gzip the promise, lol!
}).collect(), {
message: 'no body for you',
})
})
t.test('opts.ignoreBody', t => {
tnock(t, defaultOpts.registry)
.get('/hello')
.reply(200, { hello: 'world' })
return fetch('/hello', { ...OPTS, ignoreBody: true })
.then(res => {
t.equal(res.body, null, 'body omitted')
})
})
t.test('method configurable', t => {
tnock(t, defaultOpts.registry)
.delete('/hello')
.reply(200)
const opts = {
...OPTS,
method: 'DELETE',
}
return fetch('/hello', opts)
.then(res => {
t.equal(res.status, 200, 'successfully used DELETE method')
})
})
t.test('npm-notice header logging', async t => {
tnock(t, defaultOpts.registry)
.get('/hello')
.reply(200, { hello: 'world' }, {
'npm-notice': 'npm <3 u',
})
let header, msg
process.on('log', (level, ...args) => {
if (level === 'notice') {
;[header, msg] = args
}
})
t.plan(3)
const res = await fetch('/hello', { ...OPTS })
t.equal(res.status, 200, 'got successful response')
t.equal(header, '', 'empty log header thing')
t.equal(msg, 'npm <3 u', 'logged out npm-notice at NOTICE level')
})
t.test('optionally verifies request body integrity', t => {
t.plan(3)
tnock(t, defaultOpts.registry)
.get('/hello')
.times(2)
.reply(200, 'hello')
const integrity = ssri.fromData('hello')
return fetch('/hello', { ...OPTS, integrity })
.then(res => res.buffer())
.then(buf => t.equal(
buf.toString('utf8'), 'hello', 'successfully got the right data')
)
.then(() => {
return fetch('/hello', { ...OPTS, integrity: 'sha1-nope' })
.then(res => {
t.ok(res.body, 'no error until body starts getting read')
return res
})
.then(res => res.buffer())
.then(
() => {
throw new Error('should not have succeeded')
},
err => t.equal(err.code, 'EINTEGRITY', 'got EINTEGRITY error')
)
})
})
t.test('pickRegistry() utility', t => {
const pick = fetch.pickRegistry
t.equal(pick('foo@1.2.3'), defaultOpts.registry, 'has good default')
t.equal(
pick('foo@1.2.3', {
registry: 'https://my.registry/here/',
scope: '@otherscope',
'@myscope:registry': 'https://my.scoped.registry/here/',
}),
'https://my.registry/here/',
'unscoped package uses `registry` setting'
)
t.equal(
pick('@user/foo@1.2.3', {
registry: 'https://my.registry/here/',
scope: '@myscope',
'@myscope:registry': 'https://my.scoped.registry/here/',
}),
'https://my.scoped.registry/here/',
'scoped package uses `@<scope>:registry` setting'
)
t.equal(
pick('@user/foo@1.2.3', {
registry: 'https://my.registry/here/',
scope: 'myscope',
'@myscope:registry': 'https://my.scoped.registry/here/',
}),
'https://my.scoped.registry/here/',
'scope @ is option@l'
)
t.end()
})
t.test('pickRegistry through opts.spec', t => {
tnock(t, defaultOpts.registry)
.get('/pkg')
.reply(200, { source: defaultOpts.registry })
const scopedReg = 'https://scoped.mock.reg/'
tnock(t, scopedReg)
.get('/pkg')
.times(2)
.reply(200, { source: scopedReg })
return fetch.json('/pkg', {
...OPTS,
spec: 'pkg@1.2.3',
'@myscope:registry': scopedReg,
}).then(json => t.equal(
json.source,
defaultOpts.registry,
'request made to main registry'
)).then(() => fetch.json('/pkg', {
...OPTS,
spec: 'pkg@1.2.3',
'@myscope:registry': scopedReg,
scope: '@myscope',
})).then(json => t.equal(
json.source,
scopedReg,
'request made to scope registry using opts.scope'
)).then(() => fetch.json('/pkg', Object.assign({
spec: '@myscope/pkg@1.2.3',
'@myscope:registry': scopedReg,
}))).then(json => t.equal(
json.source,
scopedReg,
'request made to scope registry using spec scope'
))
})
t.test('miscellaneous headers', t => {
tnock(t, defaultOpts.registry)
.matchHeader('npm-session', session =>
t.strictSame(session, ['foobarbaz'], 'session set from options'))
.matchHeader('npm-scope', scope =>
t.strictSame(scope, ['@foo'], 'scope set from options'))
.matchHeader('user-agent', ua =>
t.strictSame(ua, ['agent of use'], 'UA set from options'))
.matchHeader('npm-command', cmd =>
t.strictSame(cmd, ['hello-world'], 'command set from options'))
.get('/hello')
.reply(200, { hello: 'world' })
return fetch('/hello', {
...OPTS,
registry: null, // always falls back on falsey registry value
npmSession: 'foobarbaz',
scope: '@foo',
userAgent: 'agent of use',
npmCommand: 'hello-world',
}).then(res => {
t.equal(res.status, 200, 'got successful response')
})
})