Skip to content

Commit 017aa58

Browse files
committed
Remove hardcoded mentions of CJS 5.0.0
1 parent 0d5c097 commit 017aa58

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

lib/rdf/PrefetchedDocumentLoader.ts

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
import type { IJsonLdContext } from 'jsonld-context-parser';
22
import { FetchDocumentLoader } from 'jsonld-context-parser';
3+
import semverMajor = require('semver/functions/major');
34
import type { Logger } from 'winston';
45

56
/**
67
* A document loader that first loads from a precomputed set of contexts,
78
* and only then does an HTTP(S) lookup for the context.
89
*/
910
export class PrefetchedDocumentLoader extends FetchDocumentLoader {
11+
public static readonly CJS_MAJOR_VERSION: number = semverMajor(require('../../package.json').version);
1012
public static readonly CONTEXT_URL: string =
11-
'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^5.0.0/components/context.jsonld';
13+
`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^${PrefetchedDocumentLoader.CJS_MAJOR_VERSION}.0.0/components/context.jsonld`;
14+
15+
public static readonly CONTEXT_PATTERN: RegExp =
16+
/https:\/\/linkedsoftwaredependencies.org\/bundles\/npm\/componentsjs\/\^([0-9]+).0.0\/components\/context.jsonld/u;
1217

1318
private static readonly DEFAULT_CONTEXT: any = require('../../components/context.json');
1419

1520
private static readonly DEFAULT_CONTEXTS: Record<string, any> = {
1621
[PrefetchedDocumentLoader.CONTEXT_URL]:
1722
PrefetchedDocumentLoader.DEFAULT_CONTEXT,
18-
// TODO: temporarily also set old context versions for backwards-compatible.
19-
'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^4.0.0/components/context.jsonld':
20-
PrefetchedDocumentLoader.DEFAULT_CONTEXT,
21-
'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^3.0.0/components/context.jsonld':
22-
PrefetchedDocumentLoader.DEFAULT_CONTEXT,
2323
};
2424

25+
static {
26+
// TODO: temporarily also set old context versions for backwards-compatible.
27+
for (let i = 3; i < PrefetchedDocumentLoader.CJS_MAJOR_VERSION; i++) {
28+
PrefetchedDocumentLoader.DEFAULT_CONTEXTS[
29+
`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^${i}.0.0/components/context.jsonld`
30+
] = PrefetchedDocumentLoader.DEFAULT_CONTEXT;
31+
}
32+
}
33+
2534
private readonly contexts: Record<string, any>;
2635
private readonly path?: string;
2736
private readonly logger?: Logger;
@@ -37,10 +46,10 @@ export class PrefetchedDocumentLoader extends FetchDocumentLoader {
3746

3847
public async load(url: string): Promise<IJsonLdContext> {
3948
// Warn on deprecated context usage
40-
if (this.logger &&
41-
(url === 'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^4.0.0/components/context.jsonld' ||
42-
url === 'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^3.0.0/components/context.jsonld')) {
43-
this.logger.warn(`Detected deprecated context URL '${url}'${this.path ? ` in ${this.path}` : ''}. Prefer using version '^5.0.0' instead.`);
49+
// eslint-disable-next-line max-len
50+
const match = PrefetchedDocumentLoader.CONTEXT_PATTERN.exec(url);
51+
if (this.logger && match && Number.parseInt(match[1], 10) < PrefetchedDocumentLoader.CJS_MAJOR_VERSION) {
52+
this.logger.warn(`Detected deprecated context URL '${url}'${this.path ? ` in ${this.path}` : ''}. Prefer using version '^${PrefetchedDocumentLoader.CJS_MAJOR_VERSION}.0.0' instead.`);
4453
}
4554

4655
// Load prefetched contexts

test/unit/rdf/PrefetchedDocumentLoader-test.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('PrefetchedDocumentLoader', () => {
4646
});
4747
expect(await loader.load(`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^3.0.0/components/context.jsonld`))
4848
.toEqual(JSON.parse(fs.readFileSync(`${__dirname}/../../../components/context.jsonld`, 'utf8')));
49-
expect(logger.warn).toHaveBeenCalledWith(`Detected deprecated context URL 'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^3.0.0/components/context.jsonld' in PATH. Prefer using version '^5.0.0' instead.`);
49+
expect(logger.warn).toHaveBeenCalledWith(`Detected deprecated context URL 'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^3.0.0/components/context.jsonld' in PATH. Prefer using version '^${PrefetchedDocumentLoader.CJS_MAJOR_VERSION}.0.0' instead.`);
5050
});
5151

5252
it('for the built-in prefetched 4.0.0 context that is deprecated with a logger', async() => {
@@ -60,7 +60,21 @@ describe('PrefetchedDocumentLoader', () => {
6060
});
6161
expect(await loader.load(`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^4.0.0/components/context.jsonld`))
6262
.toEqual(JSON.parse(fs.readFileSync(`${__dirname}/../../../components/context.jsonld`, 'utf8')));
63-
expect(logger.warn).toHaveBeenCalledWith(`Detected deprecated context URL 'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^4.0.0/components/context.jsonld' in PATH. Prefer using version '^5.0.0' instead.`);
63+
expect(logger.warn).toHaveBeenCalledWith(`Detected deprecated context URL 'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^4.0.0/components/context.jsonld' in PATH. Prefer using version '^${PrefetchedDocumentLoader.CJS_MAJOR_VERSION}.0.0' instead.`);
64+
});
65+
66+
it('for the built-in prefetched latest context that is not deprecated with a logger', async() => {
67+
const logger: any = {
68+
warn: jest.fn(),
69+
};
70+
loader = new PrefetchedDocumentLoader({
71+
contexts: {},
72+
logger,
73+
path: 'PATH',
74+
});
75+
expect(await loader.load(`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^${PrefetchedDocumentLoader.CJS_MAJOR_VERSION}.0.0/components/context.jsonld`))
76+
.toEqual(JSON.parse(fs.readFileSync(`${__dirname}/../../../components/context.jsonld`, 'utf8')));
77+
expect(logger.warn).not.toHaveBeenCalled();
6478
});
6579

6680
it('for the built-in prefetched context that is deprecated with a logger without path', async() => {
@@ -73,7 +87,7 @@ describe('PrefetchedDocumentLoader', () => {
7387
});
7488
expect(await loader.load(`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^3.0.0/components/context.jsonld`))
7589
.toEqual(JSON.parse(fs.readFileSync(`${__dirname}/../../../components/context.jsonld`, 'utf8')));
76-
expect(logger.warn).toHaveBeenCalledWith(`Detected deprecated context URL 'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^3.0.0/components/context.jsonld'. Prefer using version '^5.0.0' instead.`);
90+
expect(logger.warn).toHaveBeenCalledWith(`Detected deprecated context URL 'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^3.0.0/components/context.jsonld'. Prefer using version '^${PrefetchedDocumentLoader.CJS_MAJOR_VERSION}.0.0' instead.`);
7791
});
7892

7993
it('for a non-prefetched context', async() => {

0 commit comments

Comments
 (0)