From 79de1b1ef361df7fad14e7bf03fa7344be4de49f Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@keylocation.sg> Date: Sun, 4 Jun 2017 07:10:12 +0200 Subject: [PATCH] Use npm Registry Filter Header (#249) Closes #245 --- lib/api/npm.js | 3 +++ test/api/__snapshots__/npm.spec.js.snap | 26 +++++++++++++++++++++++++ test/api/npm.spec.js | 15 ++------------ 3 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 test/api/__snapshots__/npm.spec.js.snap diff --git a/lib/api/npm.js b/lib/api/npm.js index fd54e203d2..13b6179de6 100644 --- a/lib/api/npm.js +++ b/lib/api/npm.js @@ -19,6 +19,9 @@ async function getDependency(name) { const authInfo = registryAuthToken(regUrl); const headers = {}; + // Reduce metadata https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md + headers.accept = 'application/vnd.npm.install-v1+json'; + if (authInfo) { headers.authorization = `${authInfo.type} ${authInfo.token}`; } diff --git a/test/api/__snapshots__/npm.spec.js.snap b/test/api/__snapshots__/npm.spec.js.snap new file mode 100644 index 0000000000..79fa151c59 --- /dev/null +++ b/test/api/__snapshots__/npm.spec.js.snap @@ -0,0 +1,26 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`api/npm should fetch package info from npm 1`] = ` +Array [ + "https://npm.mycustomregistry.com/foobar", + Object { + "headers": Object { + "accept": "application/vnd.npm.install-v1+json", + }, + "json": true, + }, +] +`; + +exports[`api/npm should send an authorization header if provided 1`] = ` +Array [ + "https://npm.mycustomregistry.com/foobar", + Object { + "headers": Object { + "accept": "application/vnd.npm.install-v1+json", + "authorization": "Basic 1234", + }, + "json": true, + }, +] +`; diff --git a/test/api/npm.spec.js b/test/api/npm.spec.js index 919121320e..dc08c8ad7a 100644 --- a/test/api/npm.spec.js +++ b/test/api/npm.spec.js @@ -17,10 +17,7 @@ describe('api/npm', () => { const res = await npm.getDependency('foobar'); expect(res).toMatchObject({ some: 'data' }); const call = got.mock.calls[0]; - expect(call).toMatchObject([ - 'https://npm.mycustomregistry.com/foobar', - { json: true, headers: {} }, - ]); + expect(call).toMatchSnapshot(); }); it('should send an authorization header if provided', async () => { registryUrl.mockImplementation(() => 'https://npm.mycustomregistry.com/'); @@ -32,14 +29,6 @@ describe('api/npm', () => { const res = await npm.getDependency('foobar'); expect(res).toMatchObject({ some: 'data' }); const call = got.mock.calls[0]; - expect(call).toMatchObject([ - 'https://npm.mycustomregistry.com/foobar', - { - json: true, - headers: { - authorization: 'Basic 1234', - }, - }, - ]); + expect(call).toMatchSnapshot(); }); }); -- GitLab