Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depcache spec and reference implementation #210

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions reference-implementation/__tests__/helpers/common-test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const assert = require('assert');
const { URL } = require('url');
const { parseFromString } = require('../../lib/parser.js');
const { resolve } = require('../../lib/resolver.js');
const { traceDepcache } = require('../../lib/depcache.js');

function assertNoExtraProperties(object, expectedProperties, description) {
for (const actualProperty in object) {
Expand All @@ -22,6 +23,14 @@ function assertOwnProperty(j, name) {
// expected import maps (taken from JSONs) uses strings.
// This function converts `m` (expected import maps or its part)
// into URL-based, for comparison.
function replaceImportMapStringWithURL(m) {
return {
depcache: m.depcache,
imports: replaceStringWithURL(m.imports),
scopes: replaceStringWithURL(m.scopes)
};
}

function replaceStringWithURL(m) {
if (typeof m === 'string') {
return new URL(m);
Expand Down Expand Up @@ -59,7 +68,7 @@ function runTests(j) {
assertNoExtraProperties(
j,
[
'expectedResults', 'expectedParsedImportMap',
'expectedResults', 'expectedParsedImportMap', 'expectedDepcache',
'baseURL', 'name', 'parsedImportMap',
'importMap', 'importMapBaseURL',
'link', 'details'
Expand All @@ -85,8 +94,9 @@ function runTests(j) {
}
assert(
'expectedResults' in j ||
'expectedParsedImportMap' in j,
'expectedResults or expectedParsedImportMap should exist'
'expectedParsedImportMap' in j ||
'expectedDepcache' in j,
'expectedResults, expectedParsedImportMap or expectedDepcache should exist'
);

// Resolution tests.
Expand Down Expand Up @@ -119,7 +129,31 @@ function runTests(j) {
expect(j.parsedImportMap).toBeInstanceOf(TypeError);
} else {
expect(j.parsedImportMap)
.toEqual(replaceStringWithURL(j.expectedParsedImportMap));
.toEqual(replaceImportMapStringWithURL(j.expectedParsedImportMap));
}
});
}

// Depcache tests
if ('expectedDepcache' in j) {
it(j.name, () => {
assertOwnProperty(j, 'baseURL');
describe(
'Import map registration should be successful for resolution tests',
() => {
expect(j.parsedImportMap).not.toBeInstanceOf(Error);
}
);

for (const specifier in j.expectedDepcache) {
const expected = j.expectedDepcache[specifier];
const resolved = resolve(specifier, j.parsedImportMap, new URL(j.baseURL));
if (expected === null) {
expect(() => traceDepcache(resolved, j.parsedImportMap)).toThrow(TypeError);
} else {
const traced = traceDepcache(resolved, j.parsedImportMap);
expect(traced).toEqual(j.expectedDepcache[specifier]);
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion reference-implementation/__tests__/helpers/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ expect.extend({
}

received = received.href;
expected = (new URL(expected)).href;
expected = new URL(expected).href;

const pass = received === expected;

Expand Down
144 changes: 144 additions & 0 deletions reference-implementation/__tests__/json/depcache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{
"importMapBaseURL": "https://example.com/app/index.html",
"tests": {
"Invalid depcache": {
"importMap": {
"depcache": []
},
"tests": {
"should report invalid depcache": {
"expectedParsedImportMap": null
}
}
},
"Invalid depcache URL": {
"importMap": {
"depcache": {
"http://[www.example.com]/": [],
"/a.js": ["/b.js"]
}
},
"tests": {
"should ignore invalid depcache URL": {
"baseURL": "https://example.com/",
"expectedDepcache": {
"/a.js": [
"https://example.com/a.js",
"https://example.com/b.js"
]
}
}
}
},
"Invalid depcache keys and specifiers": {
"importMap": {
"depcache": {
"/a.js": ["/b.js"],
"/c.js": ["http://[www.example.com]/"]
}
},
"tests": {
"should ignore invalid depcache URL": {
"baseURL": "https://example.com/",
"expectedParsedImportMap": {
"depcache": {
"https://example.com/a.js": ["/b.js"],
"https://example.com/c.js": ["http://[www.example.com]/"]
},
"imports": {},
"scopes": {}
},
"expectedDepcache": {
"/a.js": [
"https://example.com/a.js",
"https://example.com/b.js"
]
}
},
"invalid depcache key resolution": {
"baseURL": "https://example.com/",
"expectedDepcache": {
"/c.js": null
}
}
}
},
"Invalid depcache dependencies list": {
"importMap": {
"depcache": {
"/valid.js": ["/a.js"],
"/a.js": {}
}
},
"tests": {
"should ignore invalid depcache items": {
"baseURL": "https://example.com/",
"expectedParsedImportMap": {
"depcache": {
"https://example.com/valid.js": ["/a.js"]
},
"imports": {},
"scopes": {}
}
}
}
},
"Invalid depcache list items": {
"importMap": {
"depcache": {
"/valid.js": ["/a.js"],
"/a.js": ["/b.js", null, {}]
}
},
"tests": {
"should ignore invalid depcache items": {
"baseURL": "https://example.com/",
"expectedParsedImportMap": {
"depcache": {
"https://example.com/valid.js": ["/a.js"]
},
"imports": {},
"scopes": {}
}
}
}
},
"Depcache resolution": {
"importMap": {
"imports": {
"a": "/a-1.mjs",
"b": "/scope/b-1.mjs"
},
"scopes": {
"/a-1.mjs": {
"a": "/a-2.mjs"
},
"/scope/": {
"a": "/scope/a-3.mjs",
"b": "./b-2.mjs"
}
},
"depcache": {
"/a-1.mjs": ["a"],
"/a-2.mjs": ["b", "a"],
"/scope/b-1.mjs": ["./b-2.mjs"],
"/scope/b-2.mjs": ["a"]
}
},
"tests": {
"should trace full depcache": {
"baseURL": "https://example.com/",
"expectedDepcache": {
"a": [
"https://example.com/a-1.mjs",
"https://example.com/a-2.mjs",
"https://example.com/scope/b-1.mjs",
"https://example.com/scope/b-2.mjs",
"https://example.com/scope/a-3.mjs"
]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"importMapBaseURL": "https://base.example/path1/path2/path3",
"expectedParsedImportMap": {
"depcache": {},
"imports": {
"about": "about:good",
"blob": "blob:good",
Expand Down Expand Up @@ -50,6 +51,7 @@
},
"importMapBaseURL": "https://base.example/path1/path2/path3",
"expectedParsedImportMap": {
"depcache": {},
"imports": {
"unparseable2": null,
"unparseable3": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"importMapBaseURL": "https://base.example/path1/path2/path3",
"expectedParsedImportMap": {
"depcache": {},
"imports": {
"foo1": null,
"foo2": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"tests": {
"should accept strings prefixed with ./, ../, or /": {
"importMap": {
"depcache": {},
"imports": {
"dotSlash": "./foo",
"dotDotSlash": "../foo",
Expand All @@ -11,6 +12,7 @@
},
"importMapBaseURL": "https://base.example/path1/path2/path3",
"expectedParsedImportMap": {
"depcache": {},
"imports": {
"dotSlash": "https://base.example/path1/path2/foo",
"dotDotSlash": "https://base.example/path1/foo",
Expand All @@ -21,6 +23,7 @@
},
"should not accept strings prefixed with ./, ../, or / for data: base URLs": {
"importMap": {
"depcache": {},
"imports": {
"dotSlash": "./foo",
"dotDotSlash": "../foo",
Expand All @@ -29,6 +32,7 @@
},
"importMapBaseURL": "data:text/html,test",
"expectedParsedImportMap": {
"depcache": {},
"imports": {
"dotSlash": null,
"dotDotSlash": null,
Expand All @@ -39,6 +43,7 @@
},
"should accept the literal strings ./, ../, or / with no suffix": {
"importMap": {
"depcache": {},
"imports": {
"dotSlash": "./",
"dotDotSlash": "../",
Expand All @@ -47,6 +52,7 @@
},
"importMapBaseURL": "https://base.example/path1/path2/path3",
"expectedParsedImportMap": {
"depcache": {},
"imports": {
"dotSlash": "https://base.example/path1/path2/",
"dotDotSlash": "https://base.example/path1/",
Expand All @@ -57,6 +63,7 @@
},
"should ignore percent-encoded variants of ./, ../, or /": {
"importMap": {
"depcache": {},
"imports": {
"dotSlash1": "%2E/",
"dotDotSlash1": "%2E%2E/",
Expand All @@ -69,6 +76,7 @@
},
"importMapBaseURL": "https://base.example/path1/path2/path3",
"expectedParsedImportMap": {
"depcache": {},
"imports": {
"dotSlash1": null,
"dotDotSlash1": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"should normalize empty import maps to have imports and scopes keys": {
"importMap": {},
"expectedParsedImportMap": {
"depcache": {},
"imports": {},
"scopes": {}
}
Expand All @@ -14,6 +15,7 @@
"scopes": {}
},
"expectedParsedImportMap": {
"depcache": {},
"imports": {},
"scopes": {}
}
Expand All @@ -23,6 +25,7 @@
"imports": {}
},
"expectedParsedImportMap": {
"depcache": {},
"imports": {},
"scopes": {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
}
},
"expectedParsedImportMap": {
"depcache": {},
"imports": {
"null": null,
"boolean": null,
Expand All @@ -36,6 +37,7 @@
}
},
"expectedParsedImportMap": {
"depcache": {},
"imports": {},
"scopes": {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"scops": {}
},
"expectedParsedImportMap": {
"depcache": {},
"imports": {},
"scopes": {}
}
Expand Down
Loading