Skip to content

Commit

Permalink
fix: support pathnames in registry
Browse files Browse the repository at this point in the history
  • Loading branch information
velut committed Apr 25, 2022
1 parent 6c3ccb6 commit 211d1a7
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"log": {
"_recordingName": "fetchFromRegistry/supports registries with an included pathname",
"creator": {
"comment": "persister:fs",
"name": "Polly.JS",
"version": "6.0.5"
},
"entries": [
{
"_id": "29caba2f3343b13343c430c5e79b1ab8",
"_order": 0,
"cache": {},
"request": {
"bodySize": 0,
"cookies": [],
"headers": [
{
"_fromType": "array",
"name": "accept",
"value": "*/*"
},
{
"_fromType": "array",
"name": "user-agent",
"value": "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)"
},
{
"_fromType": "array",
"name": "accept-encoding",
"value": "gzip,deflate"
},
{
"_fromType": "array",
"name": "connection",
"value": "close"
},
{
"name": "host",
"value": "example.com"
}
],
"headersSize": 216,
"httpVersion": "HTTP/1.1",
"method": "GET",
"queryString": [],
"url": "https://example.com/some/path/name/short-time-ago"
},
"response": {
"bodySize": 648,
"content": {
"encoding": "base64",
"mimeType": "text/html; charset=UTF-8",
"size": 648,
"text": "[\"H4sIAAAAAAAA/31UTXPbIBC9+1ds1UsyIyQnaRqPLWn6mWkPaQ9pDz0SsbKYCFAByfZ08t+7Qo4jN5makYFdeLvvsZC9Eqb0uxah9qopZtljh1wUM6Bf5qVvsPi85aptED4ZxaXO0tE6G5co9BzKmluHPo86X7FFBGkxcdbetwx/d7LPo49Ge9SeDWEjKMdZHnnc+nQIvzpAvYSkucI86iVuWmP9ZP9GCl/nAntZIguTGKSWXvKGuZI3mJ89QTm/IzJDBvvApXPR6LszYgd/wjBMeXm/tqbTgpWmMXYJr6s5tfPVYYnidi31EuZPppYLIfX6yFZRpqziSja7JTDekpzM7ZxHFcPYs07G8KGR+v6Gl7fBdE2bYohucW0Qfn6NaPy9RQ23XLth8gWbHr0sOXzDDslyMMTw3hJ3wqalzKGV1VMuYfAQ/oXsJ3SDcEt4O5+32+cML1EB77x5geg5qtV/RRPUJhncGSvQMsuF7BzplFweAZgtczUXZkPI7RYu6Luibxjb9R0/mcehJfPz09WEDF8O6sXU99JJj2JC7TGTi8WbxWKSyXD+TGBpLPfSEEttNE5B3ykUksOJ4lu21+dq0Od0An6s4lFV/KPYROVjx8MkZJaGCi3CWWXpeB1n2VCbdDsp2L6O67NnN5NMo68tftTSgQh2oFFlLHQOYZg1Tef8QLhHwBHBDQ56DjpF98kl8Mt0RGIXtnhCGqtlj6ahIXkJoLNIdHxtOg+tlRSiNHS0Ugcxgebc3VOFhOgtWiWdI0eSpe0hz4weCItVHg3PhFum6WazSSTXPDF2nY4hXbpPMypujB1IEKAKQZKE0HgRELM0iJOle6nS8UH7CyjrfG/oBAAA\"]"
},
"cookies": [],
"headers": [
{
"name": "content-encoding",
"value": "gzip"
},
{
"name": "cache-control",
"value": "max-age=604800"
},
{
"name": "content-type",
"value": "text/html; charset=UTF-8"
},
{
"name": "date",
"value": "Mon, 25 Apr 2022 17:42:20 GMT"
},
{
"name": "expires",
"value": "Mon, 02 May 2022 17:42:20 GMT"
},
{
"name": "server",
"value": "EOS (vny/0452)"
},
{
"name": "vary",
"value": "Accept-Encoding"
},
{
"name": "content-length",
"value": "648"
},
{
"name": "connection",
"value": "close"
}
],
"headersSize": 261,
"httpVersion": "HTTP/1.1",
"redirectURL": "",
"status": 404,
"statusText": "Not Found"
},
"startedDateTime": "2022-04-25T17:42:20.016Z",
"time": 508,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 508
}
}
],
"pages": [],
"version": "1.2"
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"isomorphic-unfetch": "^3.1.0",
"make-error": "^1.3.6",
"tiny-lru": "^8.0.1",
"url-join": "4.0.1",
"validate-npm-package-name": "^4.0.0"
},
"devDependencies": {
Expand All @@ -77,6 +78,7 @@
"@types/pollyjs__core": "4.3.3",
"@types/pollyjs__persister-fs": "2.0.1",
"@types/setup-polly-jest": "0.5.1",
"@types/url-join": "4.0.1",
"@types/validate-npm-package-name": "3.0.3",
"debug": "4.3.4",
"dts-cli": "1.5.1",
Expand Down
9 changes: 4 additions & 5 deletions src/utils/fetch-from-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { npmRegistry, npmRegistryMirrors } from '../data/registries';
import { FetchError } from './errors';
import { fetch } from './fetch';
import { log } from './log';
import urlJoin from 'url-join';

export async function fetchFromRegistry<T>({
endpoint,
Expand All @@ -18,11 +19,9 @@ export async function fetchFromRegistry<T>({
mirrors?: string[];
cached?: boolean;
}): Promise<T> {
const urls = [registry, ...mirrors].map((host) => {
const url = new URL(endpoint, host);
url.search = query ?? '';
return url.href;
});
const urls = [registry, ...mirrors].map((base) =>
urlJoin(base, endpoint, query ? `?${query}` : '')
);

let lastError: FetchError | undefined;
for (const url of urls) {
Expand Down
15 changes: 15 additions & 0 deletions test/utils/fetch-from-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ describe('fetchFromRegistry', () => {
}
});

it('supports registries with an included pathname', async () => {
expect.assertions(2);

try {
await fetchFromRegistry({
endpoint: '/short-time-ago',
registry: 'https://example.com/some/path/name',
mirrors: [],
});
} catch (err) {
expect(err).toBeDefined();
expect(err instanceof FetchError).toBeTruthy();
}
});

it('throws if registry URLs are invalid', async () => {
expect.assertions(1);

Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
integrity sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==

"@types/url-join@4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@types/url-join/-/url-join-4.0.1.tgz#4989c97f969464647a8586c7252d97b449cdc045"
integrity sha512-wDXw9LEEUHyV+7UWy7U315nrJGJ7p1BzaCxDpEoLr789Dk1WDVMMlf3iBfbG2F8NdWnYyFbtTxUn2ZNbm1Q4LQ==

"@types/validate-npm-package-name@3.0.3":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/validate-npm-package-name/-/validate-npm-package-name-3.0.3.tgz#970c1949b0b677e970aa6e60db4e0f6aaf5fb293"
Expand Down Expand Up @@ -8101,6 +8106,11 @@ uri-js@^4.2.2:
dependencies:
punycode "^2.1.0"

url-join@4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/url-join/-/url-join-4.0.1.tgz#b642e21a2646808ffa178c4c5fda39844e12cde7"
integrity sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA==

url-parse-lax@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/url-parse-lax/-/url-parse-lax-3.0.0.tgz#16b5cafc07dbe3676c1b1999177823d6503acb0c"
Expand Down

0 comments on commit 211d1a7

Please sign in to comment.