Skip to content

Commit 0f3968c

Browse files
committed
Emit warning when a remote context lookup is being done
1 parent eb7303a commit 0f3968c

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/rdf/PrefetchedDocumentLoader.ts

+8
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,21 @@ export class PrefetchedDocumentLoader extends FetchDocumentLoader {
3535
}
3636

3737
public async load(url: string): Promise<IJsonLdContext> {
38+
// Warn on deprecated context usage
3839
if (this.logger &&
3940
url === 'https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^3.0.0/components/context.jsonld') {
4041
this.logger.warn(`Detected deprecated context URL '${url}'${this.path ? ` in ${this.path}` : ''}. Prefer using version '^4.0.0' instead.`);
4142
}
43+
44+
// Load prefetched contexts
4245
if (url in this.contexts) {
4346
return this.contexts[url];
4447
}
48+
49+
// Warn before doing a remote context lookup
50+
if (this.logger) {
51+
this.logger.warn(`Detected remote context lookup for '${url}'${this.path ? ` in ${this.path}` : ''}. This may indicate a missing or invalid dependency, or an invalid context URL.`);
52+
}
4553
return super.load(url);
4654
}
4755
}

test/unit/rdf/PrefetchedDocumentLoader-test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,32 @@ describe('PrefetchedDocumentLoader', () => {
6666
expect(await loader.load('http://remote.org/context'))
6767
.toEqual({ x: 'y' });
6868
});
69+
70+
it('for a non-prefetched context with a logger', async() => {
71+
const logger: any = {
72+
warn: jest.fn(),
73+
};
74+
loader = new PrefetchedDocumentLoader({
75+
contexts: {},
76+
logger,
77+
path: 'PATH',
78+
});
79+
expect(await loader.load('http://remote.org/context'))
80+
.toEqual({ x: 'y' });
81+
expect(logger.warn).toHaveBeenCalledWith(`Detected remote context lookup for 'http://remote.org/context' in PATH. This may indicate a missing or invalid dependency, or an invalid context URL.`);
82+
});
83+
84+
it('for a non-prefetched context with a logger without path', async() => {
85+
const logger: any = {
86+
warn: jest.fn(),
87+
};
88+
loader = new PrefetchedDocumentLoader({
89+
contexts: {},
90+
logger,
91+
});
92+
expect(await loader.load('http://remote.org/context'))
93+
.toEqual({ x: 'y' });
94+
expect(logger.warn).toHaveBeenCalledWith(`Detected remote context lookup for 'http://remote.org/context'. This may indicate a missing or invalid dependency, or an invalid context URL.`);
95+
});
6996
});
7097
});

0 commit comments

Comments
 (0)