Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add fallback for time-based resolution (#5302)
- Loading branch information
Showing
6 changed files
with
125 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@pnpm/npm-resolver": patch | ||
--- | ||
|
||
Pick a version even if it was published after the given date (if there is no better match). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"versions": { | ||
"1.0.0": { | ||
"name": "bad-dates", | ||
"version": "1.0.0", | ||
"_hasShrinkwrap": false, | ||
"directories": {}, | ||
"dist": { | ||
"integrity": "sha512-9cI+DmhNhA8ioT/3EJFnt0s1yehnAECyIOXdT+2uQGzcEEBaj8oNmVWj33+ZjPndMIFRQh8JeJlEu1uv5/J7pQ==", | ||
"shasum": "88009856b64a2f1eb7d8bb0179418424ae0452cb", | ||
"tarball": "https://registry.npmjs.org/bad-dates/-/bad-dates-1.0.0.tgz" | ||
} | ||
} | ||
}, | ||
"time": { | ||
"1.0.0": "2017-08-17T19:26:00.508Z" | ||
}, | ||
"name": "bad-dates", | ||
"dist-tags": { | ||
"latest": "1.0.0" | ||
}, | ||
"modified": "2017-08-17T19:26:00.508Z" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { createFetchFromRegistry } from '@pnpm/fetch' | ||
import _createResolveFromNpm from '@pnpm/npm-resolver' | ||
import fixtures from '@pnpm/test-fixtures' | ||
import loadJsonFile from 'load-json-file' | ||
import nock from 'nock' | ||
import tempy from 'tempy' | ||
|
||
const f = fixtures(__dirname) | ||
const registry = 'https://registry.npmjs.org/' | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
const badDatesMeta = loadJsonFile.sync<any>(f.find('bad-dates.json')) | ||
/* eslint-enable @typescript-eslint/no-explicit-any */ | ||
|
||
const fetch = createFetchFromRegistry({}) | ||
const getCredentials = () => ({ authHeaderValue: undefined, alwaysAuth: undefined }) | ||
const createResolveFromNpm = _createResolveFromNpm.bind(null, fetch, getCredentials) | ||
|
||
test('fall back to a newer version if there is no version published by the given date', async () => { | ||
nock(registry) | ||
.get('/bad-dates') | ||
.reply(200, badDatesMeta) | ||
|
||
const cacheDir = tempy.directory() | ||
const resolve = createResolveFromNpm({ | ||
cacheDir, | ||
filterMetadata: true, | ||
fullMetadata: true, | ||
}) | ||
const resolveResult = await resolve({ alias: 'bad-dates', pref: '^1.0.0' }, { | ||
registry, | ||
publishedBy: new Date('2015-08-17T19:26:00.508Z'), | ||
}) | ||
|
||
expect(resolveResult!.resolvedVia).toBe('npm-registry') | ||
expect(resolveResult!.id).toBe('registry.npmjs.org/bad-dates/1.0.0') | ||
}) | ||
|
||
test('request metadata when the one in cache does not have a version satisfiyng the range', async () => { | ||
const cacheDir = tempy.directory() | ||
const cachedMeta = { | ||
'dist-tags': {}, | ||
versions: {}, | ||
time: {}, | ||
cachedAt: '2016-08-17T19:26:00.508Z', | ||
} | ||
fs.mkdirSync(path.join(cacheDir, 'metadata-full-filtered/registry.npmjs.org'), { recursive: true }) | ||
fs.writeFileSync(path.join(cacheDir, 'metadata-full-filtered/registry.npmjs.org/bad-dates.json'), JSON.stringify(cachedMeta), 'utf8') | ||
|
||
nock(registry) | ||
.get('/bad-dates') | ||
.reply(200, badDatesMeta) | ||
|
||
const resolve = createResolveFromNpm({ | ||
cacheDir, | ||
filterMetadata: true, | ||
fullMetadata: true, | ||
}) | ||
const resolveResult = await resolve({ alias: 'bad-dates', pref: '^1.0.0' }, { | ||
registry, | ||
publishedBy: new Date('2015-08-17T19:26:00.508Z'), | ||
}) | ||
|
||
expect(resolveResult!.resolvedVia).toBe('npm-registry') | ||
expect(resolveResult!.id).toBe('registry.npmjs.org/bad-dates/1.0.0') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters