-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnode_loader.js
49 lines (41 loc) · 1.29 KB
/
node_loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// A Node.js "loader" module that imbues a Node.js process with the ability to
// import modules over HTTP.
// Unlike the --experimental-network-imports flag, this loader permits modules
// imported over the network to import built-in modules such as "node:fs", and
// it ignores CORS.
/*jslint node */
const rx_http = /^https?:\/\//;
function resolve(specifier, context, next_resolve) {
if (rx_http.test(specifier)) {
return {
url: specifier,
shortCircuit: true
};
}
if (context.parentURL && rx_http.test(context.parentURL)) {
return {
url: new URL(specifier, context.parentURL).href,
shortCircuit: true
};
}
return next_resolve(specifier, context);
}
function load(url, context, next_load) {
if (rx_http.test(url)) {
// Load the module's source code from the network.
return fetch(url).then(function (response) {
if (!response.ok) {
throw new Error("Failed to load " + url + ".");
}
return response.text();
}).then(function (source) {
return {
format: "module",
source,
shortCircuit: true
};
});
}
return next_load(url, context);
}
export {resolve, load};