Skip to content

Commit

Permalink
Remove hardcoded mentions of CJS 5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Mar 4, 2024
1 parent 0d5c097 commit 017aa58
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
29 changes: 19 additions & 10 deletions lib/rdf/PrefetchedDocumentLoader.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import type { IJsonLdContext } from 'jsonld-context-parser';
import { FetchDocumentLoader } from 'jsonld-context-parser';
import semverMajor = require('semver/functions/major');
import type { Logger } from 'winston';

/**
* A document loader that first loads from a precomputed set of contexts,
* and only then does an HTTP(S) lookup for the context.
*/
export class PrefetchedDocumentLoader extends FetchDocumentLoader {
public static readonly CJS_MAJOR_VERSION: number = semverMajor(require('../../package.json').version);
public static readonly CONTEXT_URL: string =
'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^5.0.0/components/context.jsonld';
`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^${PrefetchedDocumentLoader.CJS_MAJOR_VERSION}.0.0/components/context.jsonld`;

public static readonly CONTEXT_PATTERN: RegExp =
/https:\/\/linkedsoftwaredependencies.org\/bundles\/npm\/componentsjs\/\^([0-9]+).0.0\/components\/context.jsonld/u;

Check failure on line 16 in lib/rdf/PrefetchedDocumentLoader.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 2 spaces but found 4

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

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

static {
// TODO: temporarily also set old context versions for backwards-compatible.
for (let i = 3; i < PrefetchedDocumentLoader.CJS_MAJOR_VERSION; i++) {
PrefetchedDocumentLoader.DEFAULT_CONTEXTS[
`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^${i}.0.0/components/context.jsonld`
] = PrefetchedDocumentLoader.DEFAULT_CONTEXT;
}
}

private readonly contexts: Record<string, any>;
private readonly path?: string;
private readonly logger?: Logger;
Expand All @@ -37,10 +46,10 @@ export class PrefetchedDocumentLoader extends FetchDocumentLoader {

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

// Load prefetched contexts
Expand Down
20 changes: 17 additions & 3 deletions test/unit/rdf/PrefetchedDocumentLoader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('PrefetchedDocumentLoader', () => {
});
expect(await loader.load(`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^3.0.0/components/context.jsonld`))
.toEqual(JSON.parse(fs.readFileSync(`${__dirname}/../../../components/context.jsonld`, 'utf8')));
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.`);
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.`);
});

it('for the built-in prefetched 4.0.0 context that is deprecated with a logger', async() => {
Expand All @@ -60,7 +60,21 @@ describe('PrefetchedDocumentLoader', () => {
});
expect(await loader.load(`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^4.0.0/components/context.jsonld`))
.toEqual(JSON.parse(fs.readFileSync(`${__dirname}/../../../components/context.jsonld`, 'utf8')));
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.`);
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.`);
});

it('for the built-in prefetched latest context that is not deprecated with a logger', async() => {
const logger: any = {
warn: jest.fn(),
};
loader = new PrefetchedDocumentLoader({
contexts: {},
logger,
path: 'PATH',
});
expect(await loader.load(`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^${PrefetchedDocumentLoader.CJS_MAJOR_VERSION}.0.0/components/context.jsonld`))
.toEqual(JSON.parse(fs.readFileSync(`${__dirname}/../../../components/context.jsonld`, 'utf8')));
expect(logger.warn).not.toHaveBeenCalled();
});

it('for the built-in prefetched context that is deprecated with a logger without path', async() => {
Expand All @@ -73,7 +87,7 @@ describe('PrefetchedDocumentLoader', () => {
});
expect(await loader.load(`https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^3.0.0/components/context.jsonld`))
.toEqual(JSON.parse(fs.readFileSync(`${__dirname}/../../../components/context.jsonld`, 'utf8')));
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.`);
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.`);
});

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

0 comments on commit 017aa58

Please sign in to comment.