@yaks/esbuild 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +450 -2
- package/_dist/bundle.d.ts +6 -0
- package/_dist/bundle.d.ts.map +1 -0
- package/_dist/compile.d.ts +72 -0
- package/_dist/compile.d.ts.map +1 -0
- package/_dist/dependencies.d.ts +15 -0
- package/_dist/dependencies.d.ts.map +1 -0
- package/_dist/file.d.ts +185 -0
- package/_dist/file.d.ts.map +1 -0
- package/_dist/graph.d.ts +104 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/home.d.ts +15 -0
- package/_dist/home.d.ts.map +1 -0
- package/_dist/link.d.ts +192 -0
- package/_dist/link.d.ts.map +1 -0
- package/_dist/lock.d.ts +30 -0
- package/_dist/lock.d.ts.map +1 -0
- package/_dist/mod.d.ts +6 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/pack.d.ts +42 -0
- package/_dist/pack.d.ts.map +1 -0
- package/_dist/package.d.ts +11 -0
- package/_dist/package.d.ts.map +1 -0
- package/_dist/plan.d.ts +135 -0
- package/_dist/plan.d.ts.map +1 -0
- package/_dist/platform.d.ts +27 -0
- package/_dist/platform.d.ts.map +1 -0
- package/_dist/resolve.d.ts +42 -0
- package/_dist/resolve.d.ts.map +1 -0
- package/_dist/shelf.d.ts +11 -0
- package/_dist/shelf.d.ts.map +1 -0
- package/_dist/strip.d.ts +51 -0
- package/_dist/strip.d.ts.map +1 -0
- package/_dist/timing.d.ts +6 -0
- package/_dist/timing.d.ts.map +1 -0
- package/_dist/unpack.d.ts +14 -0
- package/_dist/unpack.d.ts.map +1 -0
- package/analysis.js +72 -0
- package/analysis.js.map +1 -0
- package/analysis.ts +82 -0
- package/bundle.js +320 -0
- package/bundle.js.map +1 -0
- package/bundle.ts +415 -0
- package/common.js +129 -0
- package/common.js.map +1 -0
- package/common.ts +138 -0
- package/compile.js +500 -0
- package/compile.js.map +1 -0
- package/compile.ts +622 -0
- package/container.js +142 -0
- package/container.js.map +1 -0
- package/container.ts +183 -0
- package/deno.json +1 -0
- package/dependencies.js +31 -0
- package/dependencies.js.map +1 -0
- package/dependencies.ts +40 -0
- package/file.js +303 -0
- package/file.js.map +1 -0
- package/file.ts +443 -0
- package/forward.ts +49 -0
- package/graph.js +297 -0
- package/graph.js.map +1 -0
- package/graph.ts +360 -0
- package/home.js +40 -0
- package/home.js.map +1 -0
- package/home.ts +52 -0
- package/link.js +455 -0
- package/link.js.map +1 -0
- package/link.ts +596 -0
- package/lock.js +129 -0
- package/lock.js.map +1 -0
- package/lock.ts +161 -0
- package/mod.js +14 -0
- package/mod.js.map +1 -0
- package/mod.ts +43 -0
- package/npm.js +362 -0
- package/npm.js.map +1 -0
- package/npm.ts +429 -0
- package/pack.js +188 -0
- package/pack.js.map +1 -0
- package/pack.ts +225 -0
- package/package.js +199 -0
- package/package.js.map +1 -0
- package/package.json +60 -4
- package/package.ts +255 -0
- package/plan.js +273 -0
- package/plan.js.map +1 -0
- package/plan.ts +395 -0
- package/platform.js +160 -0
- package/platform.js.map +1 -0
- package/platform.ts +238 -0
- package/resolve.js +86 -0
- package/resolve.js.map +1 -0
- package/resolve.ts +144 -0
- package/said.js +44 -0
- package/said.js.map +1 -0
- package/said.ts +61 -0
- package/serve.ts +37 -0
- package/sha256.js +7 -0
- package/sha256.js.map +1 -0
- package/sha256.ts +10 -0
- package/shared.js +646 -0
- package/shared.js.map +1 -0
- package/shared.ts +813 -0
- package/shelf.js +73 -0
- package/shelf.js.map +1 -0
- package/shelf.ts +110 -0
- package/strip.js +114 -0
- package/strip.js.map +1 -0
- package/strip.ts +117 -0
- package/tar.js +87 -0
- package/tar.js.map +1 -0
- package/tar.ts +99 -0
- package/timing.js +37 -0
- package/timing.js.map +1 -0
- package/timing.ts +71 -0
- package/unpack.js +40 -0
- package/unpack.js.map +1 -0
- package/unpack.ts +60 -0
- package/used.js +91 -0
- package/used.js.map +1 -0
- package/used.ts +116 -0
package/link.ts
ADDED
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
// A module graph linked for a place no import map reaches (D-499): a module
|
|
2
|
+
// worker, which a page's map never reaches, or a Worker's upload, whose
|
|
3
|
+
// modules find each other only by name. Each module's specifiers are written
|
|
4
|
+
// as the addresses its graph resolves them to, so the module as kept
|
|
5
|
+
// (./file.ts) runs there unchanged in meaning.
|
|
6
|
+
//
|
|
7
|
+
// A linked module is another module, kept under a name of its own. That name
|
|
8
|
+
// is its module's and what everything it reaches resolves to, so the same
|
|
9
|
+
// graph links to the same names in any app, and an edit renames only the
|
|
10
|
+
// modules that reach it. A package module that reaches no bare specifier
|
|
11
|
+
// needs no linking: it is itself.
|
|
12
|
+
import { parse } from 'es-module-lexer/js'
|
|
13
|
+
import type { Keep } from './file.ts'
|
|
14
|
+
import { startsIn } from './graph.js'
|
|
15
|
+
|
|
16
|
+
/** A module in a graph: an app's script by `./<path>` from the app's root,
|
|
17
|
+
* or a package module by its address. */
|
|
18
|
+
export type Node = string
|
|
19
|
+
|
|
20
|
+
/** What each module's specifiers resolve to, by node: a specifier as written
|
|
21
|
+
* and the node it names. A specifier missing from a module's entry is left as
|
|
22
|
+
* written (`cloudflare:workers`, an address). */
|
|
23
|
+
export type Edges = Record<Node, Record<string, Node>>
|
|
24
|
+
|
|
25
|
+
/** Each literal specifier a module imports, statically or by `import()`, in
|
|
26
|
+
* reading order.
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { assertEquals } from '@std/assert'
|
|
30
|
+
* assertEquals(
|
|
31
|
+
* specifiersOf(
|
|
32
|
+
* "import a from './a.js'\nexport * from 'b'\n" +
|
|
33
|
+
* "let c = import('./c.js'), d = import(name)",
|
|
34
|
+
* ),
|
|
35
|
+
* ['./a.js', 'b', './c.js'],
|
|
36
|
+
* )
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export let specifiersOf = (code: string): string[] =>
|
|
40
|
+
parse(code)[0].flatMap(({ n }) => n == null ? [] : [n])
|
|
41
|
+
|
|
42
|
+
/** `code` with each specifier `to` answers for written as its answer, and
|
|
43
|
+
* every other one as it was.
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { assertEquals } from '@std/assert'
|
|
47
|
+
* assertEquals(
|
|
48
|
+
* rewritten("import a from 'a'\nimport('./b.js')\nimport 'c'", (spec) =>
|
|
49
|
+
* spec == 'c' ? undefined : `/x/${spec}`),
|
|
50
|
+
* "import a from '/x/a'\nimport(\"/x/./b.js\")\nimport 'c'",
|
|
51
|
+
* )
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export let rewritten = (
|
|
55
|
+
code: string,
|
|
56
|
+
to: (spec: string) => string | undefined,
|
|
57
|
+
): string => {
|
|
58
|
+
let out = '', at = 0
|
|
59
|
+
for (let { n, s, e, d } of parse(code)[0]) {
|
|
60
|
+
let next = n == null ? undefined : to(n)
|
|
61
|
+
if (next == null) continue
|
|
62
|
+
// A dynamic import's span holds its quotes; a static one's does not.
|
|
63
|
+
out += code.slice(at, s) + (d > -1 ? JSON.stringify(next) : next)
|
|
64
|
+
at = e
|
|
65
|
+
}
|
|
66
|
+
return out + code.slice(at)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Whether a specifier is bare: no path and no scheme, so only a map or a
|
|
70
|
+
* linker says what it names. */
|
|
71
|
+
export let bare = (spec: string): boolean =>
|
|
72
|
+
!/^(?:\.{0,2}\/|[a-z][a-z0-9+.-]*:)/i.test(spec)
|
|
73
|
+
|
|
74
|
+
let hex = (bytes: ArrayBuffer) =>
|
|
75
|
+
Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, '0'))
|
|
76
|
+
.join('')
|
|
77
|
+
let sha256 = async (text: string) =>
|
|
78
|
+
hex(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)))
|
|
79
|
+
|
|
80
|
+
/** What linking makes, as a number: raise it whenever the same graph would
|
|
81
|
+
* link to other bytes. */
|
|
82
|
+
export let LINKER = 1
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Each module `roots` reach, by node, and the name of it linked: a digest of
|
|
86
|
+
* the module's own name (`nameOf`, the name its bytes are kept under) and of
|
|
87
|
+
* everything it reaches, edges included. A module that must be linked is an
|
|
88
|
+
* app's (`own`), one with a bare specifier, or one that reaches either; any
|
|
89
|
+
* other keeps its own name. Cycles are named together, each member by the
|
|
90
|
+
* whole cycle.
|
|
91
|
+
*
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { assertEquals, assertNotEquals } from '@std/assert'
|
|
94
|
+
* let own = (node: string) => node.startsWith('./')
|
|
95
|
+
* let edges = {
|
|
96
|
+
* './a.ts': { './b.ts': './b.ts', p: 'https://y/p.js' },
|
|
97
|
+
* './b.ts': {},
|
|
98
|
+
* 'https://y/p.js': {},
|
|
99
|
+
* }
|
|
100
|
+
* let names = await linkNames(['./a.ts'], edges, (n) => n.length + n, own)
|
|
101
|
+
* assertEquals(names.get('https://y/p.js'), '14https://y/p.js')
|
|
102
|
+
* let moved = await linkNames(['./a.ts'], edges, (n) => n + '!', own)
|
|
103
|
+
* assertNotEquals(names.get('./a.ts'), moved.get('./a.ts'))
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export let linkNames = async (
|
|
107
|
+
roots: Node[],
|
|
108
|
+
edges: Edges,
|
|
109
|
+
nameOf: (node: Node) => string,
|
|
110
|
+
own: (node: Node) => boolean,
|
|
111
|
+
): Promise<Map<Node, string>> => {
|
|
112
|
+
// Tarjan's components, each found after every component it reaches.
|
|
113
|
+
let index = new Map<Node, number>()
|
|
114
|
+
let low = new Map<Node, number>()
|
|
115
|
+
let stack: Node[] = []
|
|
116
|
+
let held = new Set<Node>()
|
|
117
|
+
let components: Node[][] = []
|
|
118
|
+
let componentOf = new Map<Node, number>()
|
|
119
|
+
let visit = (node: Node) => {
|
|
120
|
+
index.set(node, index.size)
|
|
121
|
+
low.set(node, index.get(node)!)
|
|
122
|
+
stack.push(node)
|
|
123
|
+
held.add(node)
|
|
124
|
+
for (let to of Object.values(edges[node] ?? {})) {
|
|
125
|
+
if (!index.has(to)) {
|
|
126
|
+
visit(to)
|
|
127
|
+
low.set(node, Math.min(low.get(node)!, low.get(to)!))
|
|
128
|
+
} else if (held.has(to)) {
|
|
129
|
+
low.set(node, Math.min(low.get(node)!, index.get(to)!))
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (low.get(node) != index.get(node)) return
|
|
133
|
+
let members: Node[] = []
|
|
134
|
+
for (let top = stack.pop()!;; top = stack.pop()!) {
|
|
135
|
+
held.delete(top)
|
|
136
|
+
members.push(top)
|
|
137
|
+
componentOf.set(top, components.length)
|
|
138
|
+
if (top == node) break
|
|
139
|
+
}
|
|
140
|
+
components.push(members.sort())
|
|
141
|
+
}
|
|
142
|
+
for (let root of roots) if (!index.has(root)) visit(root)
|
|
143
|
+
let digests: string[] = []
|
|
144
|
+
let linking: boolean[] = []
|
|
145
|
+
let names = new Map<Node, string>()
|
|
146
|
+
for (let [at, members] of components.entries()) {
|
|
147
|
+
let outside = (to: Node) => componentOf.get(to) != at
|
|
148
|
+
linking[at] = members.some((node) =>
|
|
149
|
+
own(node) ||
|
|
150
|
+
Object.entries(edges[node] ?? {}).some(([spec, to]) =>
|
|
151
|
+
bare(spec) || outside(to) && linking[componentOf.get(to)!]
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
digests[at] = await sha256(JSON.stringify([
|
|
155
|
+
LINKER,
|
|
156
|
+
members.map((node) => [
|
|
157
|
+
node,
|
|
158
|
+
nameOf(node),
|
|
159
|
+
Object.entries(edges[node] ?? {}).sort(([a], [b]) => a < b ? -1 : 1)
|
|
160
|
+
.map(([spec, to]) => [
|
|
161
|
+
spec,
|
|
162
|
+
to,
|
|
163
|
+
outside(to) ? digests[componentOf.get(to)!] : '',
|
|
164
|
+
]),
|
|
165
|
+
]),
|
|
166
|
+
]))
|
|
167
|
+
for (let node of members) {
|
|
168
|
+
names.set(
|
|
169
|
+
node,
|
|
170
|
+
linking[at]
|
|
171
|
+
? await sha256(`${LINKER}\0${node}\0${digests[at]}`)
|
|
172
|
+
: nameOf(node),
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return names
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// What a file imported with a type becomes in an upload, which links only
|
|
180
|
+
// modules: an ES module whose default is what the import reads.
|
|
181
|
+
let VALUES: Record<string, (text: string) => string> = {
|
|
182
|
+
json: (text) => `export default ${text}`,
|
|
183
|
+
text: (text) => `export default ${JSON.stringify(text)}`,
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** `code` with the attributes of its static imports dropped, and the type
|
|
187
|
+
* each of those imports named (`json`, `text`), by specifier: the file it
|
|
188
|
+
* names is uploaded as a module of that value. Null when an import names
|
|
189
|
+
* another type, or a dynamic import names any.
|
|
190
|
+
*
|
|
191
|
+
* ```ts
|
|
192
|
+
* import { assertEquals } from '@std/assert'
|
|
193
|
+
* assertEquals(
|
|
194
|
+
* unattributed("import w from './w.json' with { type: 'json' }\nlet n = 1"),
|
|
195
|
+
* { code: "import w from './w.json'\nlet n = 1", typed: { './w.json': 'json' } },
|
|
196
|
+
* )
|
|
197
|
+
* assertEquals(unattributed("import a from './a.css' with { type: 'css' }"), null)
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
export let unattributed = (
|
|
201
|
+
code: string,
|
|
202
|
+
): { code: string; typed: Record<string, string> } | null => {
|
|
203
|
+
let out = '', at = 0
|
|
204
|
+
let typed: Record<string, string> = {}
|
|
205
|
+
for (let { n, e, d, a, at: said } of parse(code)[0]) {
|
|
206
|
+
if (!said?.length) continue
|
|
207
|
+
let type = said.find(([key]) => key == 'type')?.[1]
|
|
208
|
+
if (n == null || d != -1 || a < 0 || !type || !VALUES[type]) return null
|
|
209
|
+
typed[n] = type
|
|
210
|
+
// From after the specifier's closing quote through the attributes' brace.
|
|
211
|
+
let close = code.indexOf('}', a)
|
|
212
|
+
out += code.slice(at, e + 1)
|
|
213
|
+
at = close + 1
|
|
214
|
+
}
|
|
215
|
+
return { code: out + code.slice(at), typed }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** The graph `roots` reach: each module's specifiers, as `specs` answers
|
|
219
|
+
* them (from what a host kept before, or by reading the module), and what
|
|
220
|
+
* each resolves to, as `resolve` says. A specifier it leaves undefined is
|
|
221
|
+
* left as written and followed no further. */
|
|
222
|
+
export let graphOf = async (
|
|
223
|
+
roots: Node[],
|
|
224
|
+
specs: (node: Node) => Promise<string[]>,
|
|
225
|
+
resolve: (from: Node, spec: string) => Node | undefined,
|
|
226
|
+
): Promise<{ edges: Edges; specs: Record<Node, string[]> }> => {
|
|
227
|
+
let edges: Edges = {}
|
|
228
|
+
let read: Record<Node, string[]> = {}
|
|
229
|
+
let seen = new Set(roots)
|
|
230
|
+
let next = [...seen]
|
|
231
|
+
while (next.length) {
|
|
232
|
+
let round = next
|
|
233
|
+
next = []
|
|
234
|
+
await Promise.all(round.map(async (node) => {
|
|
235
|
+
let out: Record<string, Node> = {}
|
|
236
|
+
for (let spec of read[node] = await specs(node)) {
|
|
237
|
+
let to = resolve(node, spec)
|
|
238
|
+
if (to == null) continue
|
|
239
|
+
out[spec] = to
|
|
240
|
+
if (!seen.has(to)) {
|
|
241
|
+
seen.add(to)
|
|
242
|
+
next.push(to)
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
edges[node] = out
|
|
246
|
+
}))
|
|
247
|
+
}
|
|
248
|
+
return { edges, specs: read }
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Whether a node is one of the app's own files. */
|
|
252
|
+
export let own = (node: Node): boolean => node.startsWith('./')
|
|
253
|
+
|
|
254
|
+
// A specifier against a package module's address, in the address's form: a
|
|
255
|
+
// URL from a URL, a path from a path (a host serving on the page's origin).
|
|
256
|
+
let against = (from: string, spec: string) => {
|
|
257
|
+
let to = new URL(spec, new URL(from, 'https://host.invalid/'))
|
|
258
|
+
return /^[a-z][a-z0-9+.-]*:/i.test(from) ? to.href : to.pathname
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* How a graph of an app's scripts and package modules resolves: from an app
|
|
263
|
+
* script (`./<path>`), a relative specifier names the app's file at that
|
|
264
|
+
* path exactly, as a runtime links it, when `files` holds it; from a package
|
|
265
|
+
* module, a relative specifier is the address it makes. A bare one is what
|
|
266
|
+
* `imports` names for it, from either.
|
|
267
|
+
*
|
|
268
|
+
* ```ts
|
|
269
|
+
* import { assertEquals } from '@std/assert'
|
|
270
|
+
* let resolve = resolver(new Set(['a/b.ts', 'c.ts']), {
|
|
271
|
+
* p: 'https://y/__m/p.js',
|
|
272
|
+
* })
|
|
273
|
+
* assertEquals(resolve('./a/b.ts', '../c.ts'), './c.ts')
|
|
274
|
+
* assertEquals(resolve('./a/b.ts', './nope.ts'), undefined)
|
|
275
|
+
* assertEquals(resolve('./a/b.ts', 'p'), 'https://y/__m/p.js')
|
|
276
|
+
* assertEquals(resolve('https://y/__m/p.js', './q.js'), 'https://y/__m/q.js')
|
|
277
|
+
* assertEquals(resolve('/__m/p.js', './q.js'), '/__m/q.js')
|
|
278
|
+
* assertEquals(resolve('./c.ts', 'cloudflare:workers'), undefined)
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
export let resolver = (
|
|
282
|
+
files: ReadonlySet<string>,
|
|
283
|
+
imports: Readonly<Record<string, string>>,
|
|
284
|
+
) =>
|
|
285
|
+
(from: Node, spec: string): Node | undefined => {
|
|
286
|
+
if (bare(spec)) {
|
|
287
|
+
return Object.hasOwn(imports, spec) ? imports[spec] : undefined
|
|
288
|
+
}
|
|
289
|
+
if (!own(from)) {
|
|
290
|
+
return spec.startsWith('.') ? against(from, spec) : undefined
|
|
291
|
+
}
|
|
292
|
+
if (!spec.startsWith('./') && !spec.startsWith('../')) return undefined
|
|
293
|
+
let path = decodeURIComponent(
|
|
294
|
+
new URL(spec, `https://app.invalid/${from.slice(2)}`).pathname.slice(1),
|
|
295
|
+
)
|
|
296
|
+
return files.has(path) ? `./${path}` : undefined
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** `to` as a relative specifier from `from`, both paths from one root.
|
|
300
|
+
*
|
|
301
|
+
* ```ts
|
|
302
|
+
* import { assertEquals } from '@std/assert'
|
|
303
|
+
* assertEquals(relativeTo('a/b.ts', 'a/c.ts'), './c.ts')
|
|
304
|
+
* assertEquals(relativeTo('a/b/c.ts', '__m/x.js'), '../../__m/x.js')
|
|
305
|
+
* assertEquals(relativeTo('w.ts', '__m/x.js'), './__m/x.js')
|
|
306
|
+
* ```
|
|
307
|
+
*/
|
|
308
|
+
export let relativeTo = (from: string, to: string): string => {
|
|
309
|
+
let at = from.split('/').slice(0, -1)
|
|
310
|
+
let parts = to.split('/')
|
|
311
|
+
let same = 0
|
|
312
|
+
while (
|
|
313
|
+
same < at.length && same < parts.length - 1 && at[same] == parts[same]
|
|
314
|
+
) same++
|
|
315
|
+
let up = at.length - same
|
|
316
|
+
return (up ? '../'.repeat(up) : './') + parts.slice(same).join('/')
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Each of `nodes` written with its edges' addresses, as `address` names a
|
|
320
|
+
* module to the module importing it. Six read at once. */
|
|
321
|
+
export let linkedTexts = async (
|
|
322
|
+
nodes: Node[],
|
|
323
|
+
edges: Edges,
|
|
324
|
+
read: (node: Node) => Promise<string>,
|
|
325
|
+
address: (from: Node, to: Node) => string,
|
|
326
|
+
): Promise<Map<Node, string>> => {
|
|
327
|
+
let out = new Map<Node, string>()
|
|
328
|
+
let queue = [...nodes]
|
|
329
|
+
let work = async () => {
|
|
330
|
+
for (let node = queue.shift(); node; node = queue.shift()) {
|
|
331
|
+
let to = edges[node] ?? {}
|
|
332
|
+
out.set(
|
|
333
|
+
node,
|
|
334
|
+
rewritten(
|
|
335
|
+
await read(node),
|
|
336
|
+
(spec) =>
|
|
337
|
+
Object.hasOwn(to, spec) ? address(node, to[spec]) : undefined,
|
|
338
|
+
),
|
|
339
|
+
)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
await Promise.all(Array.from({ length: 6 }, work))
|
|
343
|
+
return out
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** How a release's module workers were linked, carried to the next release
|
|
347
|
+
* so it reads only what changed. */
|
|
348
|
+
export type Linking = {
|
|
349
|
+
/** Each worker's entry, by its path from the app's root, and the linked
|
|
350
|
+
* module that is the worker. */
|
|
351
|
+
workers: Record<string, string>
|
|
352
|
+
/** What each script starts as a worker, by `./<path>`, for those that
|
|
353
|
+
* start any. */
|
|
354
|
+
starts: Record<string, string[]>
|
|
355
|
+
/** Each module a worker reaches, by node, and its specifiers. */
|
|
356
|
+
specs: Record<Node, string[]>
|
|
357
|
+
/** Each linked module's name, by node. */
|
|
358
|
+
linked: Record<Node, string>
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** A release's scripts as `linkWorkers` reads them. */
|
|
362
|
+
export type Release = {
|
|
363
|
+
/** Each script by its path from the app's root, and its compiled module's
|
|
364
|
+
* name (./file.ts). */
|
|
365
|
+
modules: Readonly<Record<string, string>>
|
|
366
|
+
/** What its pages' map names for bare specifiers. */
|
|
367
|
+
imports: Readonly<Record<string, string>>
|
|
368
|
+
linking?: Linking
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// A package module's name: its file's, as it is kept and served.
|
|
372
|
+
let stem = (address: Node) =>
|
|
373
|
+
address.slice(address.lastIndexOf('/') + 1).replace(/\.js$/, '')
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Each module worker a release's scripts start
|
|
377
|
+
* (`new Worker(new URL('./grow.ts', import.meta.url))`), linked: no import
|
|
378
|
+
* map reaches a worker, so every module it reaches is kept again with each
|
|
379
|
+
* specifier written as the address the page's map would resolve it to, an
|
|
380
|
+
* app's script at its own path with `?v=` naming its linked module, a
|
|
381
|
+
* package module at its own address. So a worker imports what its page
|
|
382
|
+
* can.
|
|
383
|
+
*
|
|
384
|
+
* What the release before (`previous`) linked is taken at its word: a
|
|
385
|
+
* script whose module is the same starts and imports what it did, a package
|
|
386
|
+
* module imports what it did, and a linked module it kept is not kept again.
|
|
387
|
+
* Only the rest is read (`source`, a script's own text; `read`, a module's
|
|
388
|
+
* compiled text by node), and only what nobody kept is written to `keep`,
|
|
389
|
+
* which says where a node's linked module is kept: an app's apart from a
|
|
390
|
+
* package's.
|
|
391
|
+
*/
|
|
392
|
+
export let linkWorkers = async (
|
|
393
|
+
release: Release,
|
|
394
|
+
{ source, read, keep, previous }: {
|
|
395
|
+
source: (path: string) => Promise<string>
|
|
396
|
+
read: (node: Node, name: string) => Promise<string>
|
|
397
|
+
keep: (node: Node) => Keep
|
|
398
|
+
previous?: Release | null
|
|
399
|
+
},
|
|
400
|
+
): Promise<Linking> => {
|
|
401
|
+
let { modules, imports } = release
|
|
402
|
+
let was = previous?.linking
|
|
403
|
+
let same = (path: string) =>
|
|
404
|
+
!!previous && previous.modules[path] == modules[path]
|
|
405
|
+
let scripts = Object.keys(modules)
|
|
406
|
+
let starts: Linking['starts'] = {}
|
|
407
|
+
await Promise.all(scripts.map(async (path) => {
|
|
408
|
+
let known = was && same(path) ? was.starts[`./${path}`] ?? [] : undefined
|
|
409
|
+
let found = known ?? startsIn(await source(path))
|
|
410
|
+
if (found.length) starts[`./${path}`] = found
|
|
411
|
+
}))
|
|
412
|
+
let resolve = resolver(new Set(scripts), imports)
|
|
413
|
+
let entries = [
|
|
414
|
+
...new Set(
|
|
415
|
+
Object.entries(starts).flatMap(([from, specs]) =>
|
|
416
|
+
specs.flatMap((spec) => {
|
|
417
|
+
let to = resolve(from, spec)
|
|
418
|
+
return to && own(to) ? [to] : []
|
|
419
|
+
})
|
|
420
|
+
),
|
|
421
|
+
),
|
|
422
|
+
].sort()
|
|
423
|
+
if (!entries.length) return { workers: {}, starts, specs: {}, linked: {} }
|
|
424
|
+
let texts = new Map<Node, string>()
|
|
425
|
+
let nameOf = (node: Node) => own(node) ? modules[node.slice(2)] : stem(node)
|
|
426
|
+
let text = async (node: Node) => {
|
|
427
|
+
let got = texts.get(node)
|
|
428
|
+
if (got == null) texts.set(node, got = await read(node, nameOf(node)))
|
|
429
|
+
return got
|
|
430
|
+
}
|
|
431
|
+
let { edges, specs } = await graphOf(entries, async (node) => {
|
|
432
|
+
let kept = was?.specs[node]
|
|
433
|
+
if (kept && (!own(node) || same(node.slice(2)))) return kept
|
|
434
|
+
return specifiersOf(await text(node))
|
|
435
|
+
}, resolve)
|
|
436
|
+
let names = await linkNames(entries, edges, nameOf, own)
|
|
437
|
+
let linked: Linking['linked'] = {}
|
|
438
|
+
for (let [node, name] of names) {
|
|
439
|
+
if (own(node) || name != nameOf(node)) linked[node] = name
|
|
440
|
+
}
|
|
441
|
+
let held = new Set(Object.values(was?.linked ?? {}))
|
|
442
|
+
let missing = (await Promise.all(
|
|
443
|
+
Object.entries(linked).map(async ([node, name]) =>
|
|
444
|
+
held.has(name) || await keep(node).has(name) ? [] : [node]
|
|
445
|
+
),
|
|
446
|
+
)).flat()
|
|
447
|
+
let address = (from: Node, to: Node) => {
|
|
448
|
+
let name = names.get(to)!
|
|
449
|
+
return own(to)
|
|
450
|
+
? `${relativeTo(from.slice(2), to.slice(2))}?v=${name}`
|
|
451
|
+
: `${to.slice(0, to.lastIndexOf('/') + 1)}${name}.js`
|
|
452
|
+
}
|
|
453
|
+
let written = await linkedTexts(missing, edges, text, address)
|
|
454
|
+
await Promise.all(
|
|
455
|
+
[...written].map(([node, code]) => keep(node).put(linked[node], code)),
|
|
456
|
+
)
|
|
457
|
+
return {
|
|
458
|
+
workers: Object.fromEntries(
|
|
459
|
+
entries.map((node) => [node.slice(2), linked[node]]),
|
|
460
|
+
),
|
|
461
|
+
starts,
|
|
462
|
+
specs,
|
|
463
|
+
linked,
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
let TYPED = /\.(?:ts|tsx|jsx|mts|cts)$/
|
|
468
|
+
|
|
469
|
+
/** `code` with each `import.meta.url` written as `url`: where a runtime
|
|
470
|
+
* gives a module no address of its own (a Worker's upload), a package module
|
|
471
|
+
* still finds the files beside it where it is served.
|
|
472
|
+
*
|
|
473
|
+
* ```ts
|
|
474
|
+
* import { assertEquals } from '@std/assert'
|
|
475
|
+
* assertEquals(
|
|
476
|
+
* located("let u = new URL('./x.css', import.meta.url), m = import.meta", 'https://y/__m/a.js'),
|
|
477
|
+
* "let u = new URL('./x.css', \"https://y/__m/a.js\"), m = import.meta",
|
|
478
|
+
* )
|
|
479
|
+
* ```
|
|
480
|
+
*/
|
|
481
|
+
export let located = (code: string, url: string): string => {
|
|
482
|
+
let out = '', at = 0
|
|
483
|
+
for (let { s, e, d } of parse(code)[0]) {
|
|
484
|
+
// -2 is `import.meta`.
|
|
485
|
+
if (
|
|
486
|
+
d != -2 || code.slice(e, e + 4) != '.url' ||
|
|
487
|
+
/[\w$]/.test(code[e + 4] ?? '')
|
|
488
|
+
) {
|
|
489
|
+
continue
|
|
490
|
+
}
|
|
491
|
+
out += code.slice(at, s) + JSON.stringify(url)
|
|
492
|
+
at = e + 4
|
|
493
|
+
}
|
|
494
|
+
return out + code.slice(at)
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/** Where a Worker's upload keeps a package module: under one folder, by its
|
|
498
|
+
* file's name, so its imports of its own package's modules resolve as
|
|
499
|
+
* written. */
|
|
500
|
+
export let UPLOADED = '__m/'
|
|
501
|
+
|
|
502
|
+
/** A Worker's upload: each module by its name, as JavaScript, and each file
|
|
503
|
+
* a module imports as it is (`.wasm`, `.txt`) by its path, to read raw. */
|
|
504
|
+
export type Upload = {
|
|
505
|
+
modules: { name: string; code: string }[]
|
|
506
|
+
carry: string[]
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* The modules a Worker's server source (`main`, a path from the app's root)
|
|
511
|
+
* reaches, compiled one file at a time and linked by name (T-5555): an app's
|
|
512
|
+
* script under its own path, a package module under `UPLOADED`, every
|
|
513
|
+
* specifier written as the name it links to, since an upload has no map.
|
|
514
|
+
* `files` is every file the app holds; `read` is a module's compiled text,
|
|
515
|
+
* by node and name, as `linkWorkers` reads it, and `source` a file's own
|
|
516
|
+
* text, for a JSON or text file a module imports with its type, which uploads
|
|
517
|
+
* as a module of its value.
|
|
518
|
+
*
|
|
519
|
+
* Undefined when nothing reached is TypeScript or JSX and no package is
|
|
520
|
+
* imported: the source uploads as written. Null when the compiler must build
|
|
521
|
+
* it: an import with a type other than `json` or `text`, or a static import
|
|
522
|
+
* of a bare specifier neither the map nor the runtime (`node:`,
|
|
523
|
+
* `cloudflare:`) names. A dynamic one is left as written, failing only if it
|
|
524
|
+
* runs, as it would in a page.
|
|
525
|
+
*/
|
|
526
|
+
export let serverUpload = async (
|
|
527
|
+
main: string,
|
|
528
|
+
release: Release,
|
|
529
|
+
files: ReadonlySet<string>,
|
|
530
|
+
read: (node: Node, name: string) => Promise<string>,
|
|
531
|
+
source: (path: string) => Promise<string>,
|
|
532
|
+
): Promise<Upload | null | undefined> => {
|
|
533
|
+
let { modules, imports } = release
|
|
534
|
+
let nameOf = (node: Node) => own(node) ? modules[node.slice(2)] : stem(node)
|
|
535
|
+
let resolve = resolver(files, imports)
|
|
536
|
+
let texts = new Map<Node, string>()
|
|
537
|
+
let values = new Map<Node, string>()
|
|
538
|
+
let refused = false
|
|
539
|
+
let { edges } = await graphOf([`./${main}`], async (node) => {
|
|
540
|
+
if (own(node) && !modules[node.slice(2)]) return []
|
|
541
|
+
let plain = unattributed(await read(node, nameOf(node)))
|
|
542
|
+
if (!plain) {
|
|
543
|
+
refused = true
|
|
544
|
+
return []
|
|
545
|
+
}
|
|
546
|
+
texts.set(node, plain.code)
|
|
547
|
+
for (let [spec, type] of Object.entries(plain.typed)) {
|
|
548
|
+
let to = resolve(node, spec)
|
|
549
|
+
if (to && own(to)) values.set(to, type)
|
|
550
|
+
else refused = true
|
|
551
|
+
}
|
|
552
|
+
let found = parse(plain.code)[0]
|
|
553
|
+
if (
|
|
554
|
+
found.some(({ n, d }) =>
|
|
555
|
+
n != null && d == -1 && bare(n) && !Object.hasOwn(imports, n)
|
|
556
|
+
)
|
|
557
|
+
) refused = true
|
|
558
|
+
return found.flatMap(({ n }) => n == null ? [] : [n])
|
|
559
|
+
}, resolve)
|
|
560
|
+
if (refused) return null
|
|
561
|
+
await Promise.all(
|
|
562
|
+
[...values].map(async ([node, type]) =>
|
|
563
|
+
texts.set(node, VALUES[type](await source(node.slice(2))))
|
|
564
|
+
),
|
|
565
|
+
)
|
|
566
|
+
let nodes = Object.keys(edges)
|
|
567
|
+
if (!nodes.some((node) => !own(node) || TYPED.test(node))) return undefined
|
|
568
|
+
let named = (node: Node) =>
|
|
569
|
+
own(node) ? node.slice(2) : UPLOADED + node.slice(node.lastIndexOf('/') + 1)
|
|
570
|
+
let linked = nodes.filter((node) => texts.has(node) && !values.has(node))
|
|
571
|
+
let written = await linkedTexts(
|
|
572
|
+
linked,
|
|
573
|
+
edges,
|
|
574
|
+
(node) => Promise.resolve(texts.get(node)!),
|
|
575
|
+
(from, to) => relativeTo(named(from), named(to)),
|
|
576
|
+
)
|
|
577
|
+
return {
|
|
578
|
+
// A Worker gives a module no `import.meta.url`: a package module's is
|
|
579
|
+
// its address, where the files it reads beside it are served.
|
|
580
|
+
modules: [
|
|
581
|
+
...linked.map((node) => ({
|
|
582
|
+
name: named(node),
|
|
583
|
+
code: own(node)
|
|
584
|
+
? written.get(node)!
|
|
585
|
+
: located(written.get(node)!, node),
|
|
586
|
+
})),
|
|
587
|
+
...[...values.keys()].map((node) => ({
|
|
588
|
+
name: named(node),
|
|
589
|
+
code: texts.get(node)!,
|
|
590
|
+
})),
|
|
591
|
+
],
|
|
592
|
+
carry: nodes.filter((node) => !texts.has(node)).map((node) =>
|
|
593
|
+
node.slice(2)
|
|
594
|
+
),
|
|
595
|
+
}
|
|
596
|
+
}
|