forked from developit/web-worker
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader.js
88 lines (78 loc) · 2.31 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { get } from 'node:https'
import { BroadcastChannel } from 'node:worker_threads'
import { resolveObjectURL } from 'node:buffer'
const bc = new BroadcastChannel('blob: loader')
bc.addEventListener('message', async evt => {
const url = evt.data
const blob = resolveObjectURL(url)
if (blob) {
const source = await blob.text()
bc.postMessage({url, source})
}
})
bc?.unref()
export function resolve (specifier, context, nextResolve) {
const { parentURL = null } = context;
// Normally Node.js would error on specifiers starting with 'http', so
// this hook intercepts them and converts them into absolute URLs to be
// passed along to the later hooks below.
if (specifier.startsWith('https:')) {
return {
shortCircuit: true,
url: specifier
}
} else if (parentURL && parentURL.startsWith('https:')) {
return {
shortCircuit: true,
url: new URL(specifier, parentURL).href,
}
} else if (specifier.startsWith('blob:')) {
return {
shortCircuit: true,
url: specifier,
}
}
// Let Node.js handle all other specifiers.
return nextResolve(specifier, context, nextResolve);
}
export async function load(url, context, nextLoad) {
// For JavaScript to be loaded over the network
if (url.startsWith('https://')) {
return {
format: 'module',
shortCircuit: true,
source: await fetch(url).then(r => r.text())
}
}
if (url.startsWith('blob:')) {
// use broadcast channel to ask main thread to resolve blob
bc.postMessage(url)
const source = await new Promise(rs => {
// Don't remove or or hell will break loose
setTimeout(() => {}, 100)
bc.addEventListener('message', evt => {
if (evt.data.url === url) {
rs(evt.data.source)
}
})
})
return {
// This example assumes all network-provided JavaScript is ES module code.
format: 'module',
shortCircuit: true,
source
}
}
// Let Node.js handle all other URLs.
return nextLoad(url, context, nextLoad);
}
const fetch = globalThis.fetch || async function fetch (url) {
return new Promise((resolve, reject) => {
get(url, res => {
let out = ''
res.on('data', chunk => out += chunk)
res.on('end', () => resolve({ text: () => out }))
res.on('error', reject)
})
})
}