@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/file.ts
ADDED
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
// One module compiled on its own (D-499): TypeScript and JSX stripped to the
|
|
2
|
+
// JavaScript a browser runs (./strip.ts `compiled`), every import kept as
|
|
3
|
+
// written and every line where it was, so a stack trace names the source's
|
|
4
|
+
// own line. A file compiles the same wherever it is and whichever app holds
|
|
5
|
+
// it, so its output is kept once, under the name `compiledName` gives it: the
|
|
6
|
+
// source's address (the SHA-256 of its bytes, the name a host already keeps
|
|
7
|
+
// it under), what kind of file it is, the options, and the compiler's
|
|
8
|
+
// version. A file seen before, anywhere on the platform, costs a lookup, and
|
|
9
|
+
// one the release before used costs nothing, not even a read of its source.
|
|
10
|
+
//
|
|
11
|
+
// This holds no compiler, so a host whose upload cannot carry sucrase (the
|
|
12
|
+
// yaks.app kernel) names and keeps output and compiles through a host that
|
|
13
|
+
// can (its compiler Worker).
|
|
14
|
+
import { parse } from 'es-module-lexer/js'
|
|
15
|
+
import { found } from './graph.js'
|
|
16
|
+
|
|
17
|
+
/** What the compile makes, as a number: output kept under another is made
|
|
18
|
+
* again. Raise it whenever the same source would compile to other bytes. */
|
|
19
|
+
export let COMPILER = 1
|
|
20
|
+
|
|
21
|
+
/** What compiling one of an app's files makes (./strip.ts `compiled`), as a
|
|
22
|
+
* number, apart from `COMPILER`, which names every package's modules too:
|
|
23
|
+
* a file kept under another is compiled again. */
|
|
24
|
+
export let FILE_COMPILER = 2
|
|
25
|
+
|
|
26
|
+
/** What a pack of an app's own modules makes (./pack.ts), as a number: a
|
|
27
|
+
* pack kept under another is made again. Raise it whenever the same modules
|
|
28
|
+
* would pack to other bytes. */
|
|
29
|
+
export let PACKER = 7
|
|
30
|
+
|
|
31
|
+
/** How a file compiles beyond what its path says: the package an app's JSX
|
|
32
|
+
* is automatic from (`preact`, `react`), or classic `React.createElement`
|
|
33
|
+
* where none. */
|
|
34
|
+
export type FileOptions = { jsxImportSource?: string }
|
|
35
|
+
|
|
36
|
+
/** What a file is by its path: `ts`, `tsx`, `jsx` and their kin compile,
|
|
37
|
+
* anything else is JavaScript as written. */
|
|
38
|
+
export let kindOf = (path: string): string =>
|
|
39
|
+
path.slice(path.lastIndexOf('.') + 1)
|
|
40
|
+
|
|
41
|
+
let hex = (bytes: ArrayBuffer) =>
|
|
42
|
+
Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, '0'))
|
|
43
|
+
.join('')
|
|
44
|
+
|
|
45
|
+
let COMPILED = new Set(['ts', 'mts', 'cts', 'tsx', 'jsx'])
|
|
46
|
+
|
|
47
|
+
let sha256 = async (bytes: Uint8Array<ArrayBuffer>) =>
|
|
48
|
+
hex(await crypto.subtle.digest('SHA-256', bytes))
|
|
49
|
+
|
|
50
|
+
/** A source's address: the SHA-256 of its bytes, in hex. */
|
|
51
|
+
export let addressOf = (source: string): Promise<string> =>
|
|
52
|
+
sha256(new TextEncoder().encode(source))
|
|
53
|
+
|
|
54
|
+
/** The name a file's compiled output is kept under, from its source's
|
|
55
|
+
* address: one name for one output, whichever app or path holds the source.
|
|
56
|
+
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { assertEquals, assertNotEquals } from '@std/assert'
|
|
59
|
+
* let x = await addressOf('x')
|
|
60
|
+
* assertEquals(await compiledName('a.ts', x), await compiledName('b/c.ts', x))
|
|
61
|
+
* assertNotEquals(await compiledName('a.ts', x), await compiledName('a.tsx', x))
|
|
62
|
+
* assertNotEquals(
|
|
63
|
+
* await compiledName('a.tsx', x),
|
|
64
|
+
* await compiledName('a.tsx', x, { jsxImportSource: 'preact' }),
|
|
65
|
+
* )
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export let compiledName = (
|
|
69
|
+
path: string,
|
|
70
|
+
address: string,
|
|
71
|
+
{ jsxImportSource = '' }: FileOptions = {},
|
|
72
|
+
): Promise<string> => {
|
|
73
|
+
let kind = COMPILED.has(kindOf(path)) ? kindOf(path) : 'js'
|
|
74
|
+
return sha256(
|
|
75
|
+
new TextEncoder().encode(
|
|
76
|
+
`${FILE_COMPILER}\0${kind}\0${jsxImportSource}\0${address}`,
|
|
77
|
+
),
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Where compiled output is kept: by name, written once, never changed. */
|
|
82
|
+
export type Keep = {
|
|
83
|
+
has: (name: string) => Promise<boolean>
|
|
84
|
+
put: (name: string, code: string) => Promise<void>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Where package modules are kept in a byte store, whichever host: every
|
|
88
|
+
* app's, under one prefix, since a name is the same output in any of them,
|
|
89
|
+
* and served to anyone at `MODULES_AT`. */
|
|
90
|
+
export let MODULES = 'modules/'
|
|
91
|
+
|
|
92
|
+
/** Where an app's own compiled scripts are kept: once for every app, as
|
|
93
|
+
* package modules are, but apart from them, so `MODULES_AT` never answers
|
|
94
|
+
* one. An app serves its own at its scripts' paths, behind its own access. */
|
|
95
|
+
export let APP_MODULES = 'app-modules/'
|
|
96
|
+
|
|
97
|
+
/** Where a host serves what it keeps under `MODULES`, from its one origin,
|
|
98
|
+
* so every app's pages share one cache of each. */
|
|
99
|
+
export let MODULES_AT = '/__m/'
|
|
100
|
+
|
|
101
|
+
/** A `Keep` over a byte store (@yaks/blob's, a bucket's), each module at
|
|
102
|
+
* `MODULES` + its name + `.js`. */
|
|
103
|
+
export let keptIn = (
|
|
104
|
+
store: {
|
|
105
|
+
has: (key: string) => Promise<boolean>
|
|
106
|
+
put: (key: string, bytes: Uint8Array) => Promise<unknown>
|
|
107
|
+
},
|
|
108
|
+
prefix = MODULES,
|
|
109
|
+
): Keep => ({
|
|
110
|
+
has: (name) => store.has(`${prefix}${name}.js`),
|
|
111
|
+
put: async (name, code) => {
|
|
112
|
+
await store.put(`${prefix}${name}.js`, new TextEncoder().encode(code))
|
|
113
|
+
},
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
/** A file to compile: its path, its source's address, and its source, read
|
|
117
|
+
* only when it compiles. */
|
|
118
|
+
export type Source = {
|
|
119
|
+
path: string
|
|
120
|
+
address: string
|
|
121
|
+
read: () => string | Promise<string>
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Sources held as text, each addressed. */
|
|
125
|
+
export let sourcesOf = (
|
|
126
|
+
files: { path: string; source: string }[],
|
|
127
|
+
): Promise<Source[]> =>
|
|
128
|
+
Promise.all(files.map(async ({ path, source }) => ({
|
|
129
|
+
path,
|
|
130
|
+
address: await addressOf(source),
|
|
131
|
+
read: () => source,
|
|
132
|
+
})))
|
|
133
|
+
|
|
134
|
+
/** Compiles one file (./strip.ts `compiled`, or a host that runs it). */
|
|
135
|
+
export type Compile = (
|
|
136
|
+
path: string,
|
|
137
|
+
source: string,
|
|
138
|
+
options: FileOptions,
|
|
139
|
+
) => string | Promise<string>
|
|
140
|
+
|
|
141
|
+
/** Each file's compiled name, compiling and keeping only output nobody kept
|
|
142
|
+
* before. `known` names output kept already (the release before's), taken at
|
|
143
|
+
* its word, its source never read; any other name costs one `has`, and a miss
|
|
144
|
+
* a read, a compile and a `put`. Six at once, as a Worker holds six
|
|
145
|
+
* connections. */
|
|
146
|
+
export let compiledFiles = async (
|
|
147
|
+
files: Source[],
|
|
148
|
+
keep: Keep,
|
|
149
|
+
compile: Compile,
|
|
150
|
+
{ known = new Set<string>(), options = {}, output }: {
|
|
151
|
+
known?: ReadonlySet<string>
|
|
152
|
+
options?: FileOptions
|
|
153
|
+
/** each file this call compiled, its output by path */
|
|
154
|
+
output?: Map<string, string>
|
|
155
|
+
} = {},
|
|
156
|
+
): Promise<Map<string, string>> => {
|
|
157
|
+
let names = new Map<string, string>()
|
|
158
|
+
let queue = [...files]
|
|
159
|
+
let work = async () => {
|
|
160
|
+
for (let file = queue.shift(); file; file = queue.shift()) {
|
|
161
|
+
let name = await compiledName(file.path, file.address, options)
|
|
162
|
+
if (!known.has(name) && !await keep.has(name)) {
|
|
163
|
+
let code = await compile(file.path, await file.read(), options)
|
|
164
|
+
output?.set(file.path, code)
|
|
165
|
+
await keep.put(name, code)
|
|
166
|
+
}
|
|
167
|
+
names.set(file.path, name)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
await Promise.all(Array.from({ length: 6 }, work))
|
|
171
|
+
return names
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** What names a package's compile: `name@version`, and after it the rest of
|
|
175
|
+
* a specifier a pattern export matches, built alone (./package.ts).
|
|
176
|
+
*
|
|
177
|
+
* ```ts
|
|
178
|
+
* import { assertEquals } from '@std/assert'
|
|
179
|
+
* assertEquals(packageKey('three', '0.180.0'), 'three@0.180.0')
|
|
180
|
+
* assertEquals(
|
|
181
|
+
* packageKey('three', '0.180.0', 'three/addons/controls/OrbitControls.js'),
|
|
182
|
+
* 'three@0.180.0/addons/controls/OrbitControls.js',
|
|
183
|
+
* )
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
export let packageKey = (
|
|
187
|
+
name: string,
|
|
188
|
+
version: string,
|
|
189
|
+
subpath?: string,
|
|
190
|
+
): string => `${name}@${version}${subpath ? subpath.slice(name.length) : ''}`
|
|
191
|
+
|
|
192
|
+
/** The name a package version's compile is kept under (./package.ts), from
|
|
193
|
+
* its key (`packageKey`): its record of entries is `<name>.json` beside its
|
|
194
|
+
* modules.
|
|
195
|
+
*
|
|
196
|
+
* ```ts
|
|
197
|
+
* import { assertNotEquals } from '@std/assert'
|
|
198
|
+
* assertNotEquals(
|
|
199
|
+
* await packageName('preact@10.26.0'),
|
|
200
|
+
* await packageName('preact@10.26.1'),
|
|
201
|
+
* )
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
export let packageName = (key: string): Promise<string> =>
|
|
205
|
+
sha256(new TextEncoder().encode(`${COMPILER}\0package\0${key}`))
|
|
206
|
+
|
|
207
|
+
/** What a package version's modules are kept in: a byte store's reads and
|
|
208
|
+
* writes, by key. */
|
|
209
|
+
export type Kept = {
|
|
210
|
+
read: (key: string) => Promise<Uint8Array | null>
|
|
211
|
+
put: (key: string, bytes: Uint8Array) => Promise<unknown>
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** A specifier no export of its package names: nothing to compile. */
|
|
215
|
+
export class Unexported extends Error {
|
|
216
|
+
override name = 'Unexported'
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** What is kept of a package's compile: each entry's specifier and its
|
|
220
|
+
* module file, the bare specifiers its modules import, and what each module
|
|
221
|
+
* imports statically (@yaks/esbuild/package `Compiled`). */
|
|
222
|
+
export type KeptPackage = {
|
|
223
|
+
entries: Record<string, string>
|
|
224
|
+
imports: string[]
|
|
225
|
+
links: Record<string, string[]>
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** A package version's entries and imports, compiled once for the platform:
|
|
229
|
+
* read from its record in `store`, or compiled, every module kept, and then
|
|
230
|
+
* the record, so a record names only modules that are there. A record from
|
|
231
|
+
* before it said its imports and links is compiled again. */
|
|
232
|
+
export let keptPackage = async (
|
|
233
|
+
store: Kept,
|
|
234
|
+
key: string,
|
|
235
|
+
compile: () => Promise<KeptPackage & { modules: Record<string, string> }>,
|
|
236
|
+
prefix = MODULES,
|
|
237
|
+
): Promise<KeptPackage> => {
|
|
238
|
+
let record = `${prefix}${await packageName(key)}.json`
|
|
239
|
+
let held = await store.read(record)
|
|
240
|
+
let was = held && JSON.parse(new TextDecoder().decode(held))
|
|
241
|
+
if (was?.entries && was.imports && was.links) return was
|
|
242
|
+
let { entries, modules, imports, links } = await compile()
|
|
243
|
+
let queue = Object.entries(modules)
|
|
244
|
+
let work = async () => {
|
|
245
|
+
for (let next = queue.shift(); next; next = queue.shift()) {
|
|
246
|
+
await store.put(prefix + next[0], new TextEncoder().encode(next[1]))
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
await Promise.all(Array.from({ length: 6 }, work))
|
|
250
|
+
let kept = { entries, imports, links }
|
|
251
|
+
await store.put(record, new TextEncoder().encode(JSON.stringify(kept)))
|
|
252
|
+
return kept
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
let TYPES: Record<string, string> = {
|
|
256
|
+
js: 'text/javascript; charset=utf-8',
|
|
257
|
+
css: 'text/css; charset=utf-8',
|
|
258
|
+
json: 'application/json',
|
|
259
|
+
svg: 'image/svg+xml',
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// A kept module's file: a name and its kind, or a package's resource under
|
|
263
|
+
// its name.
|
|
264
|
+
let SERVED =
|
|
265
|
+
/^[a-f0-9]{64}(?:\.(?:js|css)|\/(?:[\w@+~-][\w.@+~-]*\/)*[\w@+~-][\w.@+~-]*)$/
|
|
266
|
+
|
|
267
|
+
/** A kept module, or a package's resource, at `file` under `MODULES_AT`, for
|
|
268
|
+
* every app and every page: immutable, since a name is written once, and
|
|
269
|
+
* readable from any origin. Null when nothing is kept there. */
|
|
270
|
+
export let servedModule = async (
|
|
271
|
+
store: Pick<Kept, 'read'>,
|
|
272
|
+
file: string,
|
|
273
|
+
prefix = MODULES,
|
|
274
|
+
): Promise<Response | null> => {
|
|
275
|
+
if (!SERVED.test(file)) return null
|
|
276
|
+
let bytes = await store.read(prefix + file)
|
|
277
|
+
if (!bytes) return null
|
|
278
|
+
return new Response(bytes as Uint8Array<ArrayBuffer>, {
|
|
279
|
+
headers: {
|
|
280
|
+
'content-type': TYPES[kindOf(file)] ?? 'text/plain; charset=utf-8',
|
|
281
|
+
'cache-control': 'public, max-age=31536000, immutable',
|
|
282
|
+
'access-control-allow-origin': '*',
|
|
283
|
+
'x-content-type-options': 'nosniff',
|
|
284
|
+
},
|
|
285
|
+
})
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** What a page's import map names for the bare specifiers its modules
|
|
289
|
+
* import: each entry of each package an app's release uses (`packages`, as
|
|
290
|
+
* `keptPackage` keeps them), at `origin` + `MODULES_AT`, and each alias its
|
|
291
|
+
* renderer gives (`react` for `preact/compat`) at its target's address.
|
|
292
|
+
*
|
|
293
|
+
* ```ts
|
|
294
|
+
* import { assertEquals } from '@std/assert'
|
|
295
|
+
* assertEquals(
|
|
296
|
+
* bareMap(
|
|
297
|
+
* [{ entries: { preact: 'a.js', 'preact/compat': 'b.js' } }],
|
|
298
|
+
* { react: 'preact/compat', 'react-dom': 'preact/compat', x: 'gone' },
|
|
299
|
+
* 'https://yaks.app',
|
|
300
|
+
* ),
|
|
301
|
+
* {
|
|
302
|
+
* preact: 'https://yaks.app/__m/a.js',
|
|
303
|
+
* 'preact/compat': 'https://yaks.app/__m/b.js',
|
|
304
|
+
* react: 'https://yaks.app/__m/b.js',
|
|
305
|
+
* 'react-dom': 'https://yaks.app/__m/b.js',
|
|
306
|
+
* },
|
|
307
|
+
* )
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
310
|
+
export let bareMap = (
|
|
311
|
+
packages: Pick<KeptPackage, 'entries'>[],
|
|
312
|
+
aliases: Readonly<Record<string, string>> = {},
|
|
313
|
+
origin: string,
|
|
314
|
+
): Record<string, string> => {
|
|
315
|
+
let map: Record<string, string> = {}
|
|
316
|
+
for (let { entries } of packages) {
|
|
317
|
+
for (let [spec, file] of Object.entries(entries)) {
|
|
318
|
+
map[spec] = `${origin}${MODULES_AT}${file}`
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
for (let [spec, to] of Object.entries(aliases)) {
|
|
322
|
+
if (map[to] && !map[spec]) map[spec] = map[to]
|
|
323
|
+
}
|
|
324
|
+
return map
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Each relative import of `js`, the app's script at `path`: the path it
|
|
328
|
+
// names from the app's root, and the script it loads, as a compile finds it
|
|
329
|
+
// (./graph.ts `found`), or null. Static imports only where `all` is false.
|
|
330
|
+
type Relative = { spec: string; to: string | null; at: string | null }
|
|
331
|
+
|
|
332
|
+
let relatives = (
|
|
333
|
+
path: string,
|
|
334
|
+
js: string,
|
|
335
|
+
scripts: ReadonlySet<string>,
|
|
336
|
+
all: boolean,
|
|
337
|
+
): Relative[] | null => {
|
|
338
|
+
let lexed: readonly { n?: string; d: number }[]
|
|
339
|
+
try {
|
|
340
|
+
lexed = parse(js)[0]
|
|
341
|
+
} catch {
|
|
342
|
+
return null
|
|
343
|
+
}
|
|
344
|
+
return lexed.flatMap(({ n: spec, d }): Relative[] => {
|
|
345
|
+
if (spec == null || (!all && d != -1) || d == -2) return []
|
|
346
|
+
if (!/^\.\.?\//.test(spec)) return [{ spec, to: null, at: null }]
|
|
347
|
+
let to = decodeURIComponent(
|
|
348
|
+
new URL(spec, `https://app.invalid/${path}`).pathname.slice(1),
|
|
349
|
+
)
|
|
350
|
+
return [{ spec, to, at: found(to, (p) => scripts.has(p)) }]
|
|
351
|
+
})
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** What the app's script at `path` imports statically, read from its
|
|
355
|
+
* compiled module `js`, as a release's module graph names it: another of
|
|
356
|
+
* the app's `scripts` as `./<path>` from the app's root, found as a compile
|
|
357
|
+
* finds it (`./lib/n` loads lib/n.ts), and a bare specifier as written. The
|
|
358
|
+
* module is lexed, so an import in a comment or a string names nothing; it
|
|
359
|
+
* is the compiled module, so the JSX runtime the compiler writes in is named
|
|
360
|
+
* and an `import type` is gone. `import('x')` loads only when it runs. An
|
|
361
|
+
* address none of the app's scripts is, or a module that does not lex, names
|
|
362
|
+
* nothing.
|
|
363
|
+
*
|
|
364
|
+
* ```ts
|
|
365
|
+
* import { assertEquals } from '@std/assert'
|
|
366
|
+
* assertEquals(
|
|
367
|
+
* scriptLinks(
|
|
368
|
+
* 'ui/main.ts',
|
|
369
|
+
* "import { a } from '../lib/a.ts'\nexport * from 'preact'\n" +
|
|
370
|
+
* "import { b } from '../lib/b'\n" +
|
|
371
|
+
* "export let n = {}\n// import { f } from './f.ts'\n" +
|
|
372
|
+
* "let later = () => import('./late.ts')",
|
|
373
|
+
* new Set(['lib/a.ts', 'lib/b.tsx', 'ui/f.ts', 'ui/late.ts']),
|
|
374
|
+
* ),
|
|
375
|
+
* ['./lib/a.ts', 'preact', './lib/b.tsx'],
|
|
376
|
+
* )
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
379
|
+
export let scriptLinks = (
|
|
380
|
+
path: string,
|
|
381
|
+
js: string,
|
|
382
|
+
scripts: ReadonlySet<string>,
|
|
383
|
+
): string[] => {
|
|
384
|
+
let out = new Set<string>()
|
|
385
|
+
for (let { spec, to, at } of relatives(path, js, scripts, false) ?? []) {
|
|
386
|
+
if (to == null) {
|
|
387
|
+
if (!spec.startsWith('/') && !spec.includes(':')) out.add(spec)
|
|
388
|
+
} else if (at) out.add(`./${at}`)
|
|
389
|
+
}
|
|
390
|
+
return [...out]
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** Each path from the app's root that the script at `path` imports, by
|
|
394
|
+
* `import` or `import()`, naming none of the app's `scripts` but one a
|
|
395
|
+
* compile finds (./graph.ts `found`), with the script it loads: what a
|
|
396
|
+
* page's map names so the browser loads it there.
|
|
397
|
+
*
|
|
398
|
+
* ```ts
|
|
399
|
+
* import { assertEquals } from '@std/assert'
|
|
400
|
+
* assertEquals(
|
|
401
|
+
* looseImports('main.ts', "import './lib/n'\nimport('./ui')\n" +
|
|
402
|
+
* "import './lib/n.ts'", new Set(['lib/n.ts', 'ui/index.ts'])),
|
|
403
|
+
* { 'lib/n': 'lib/n.ts', ui: 'ui/index.ts' },
|
|
404
|
+
* )
|
|
405
|
+
* ```
|
|
406
|
+
*/
|
|
407
|
+
export let looseImports = (
|
|
408
|
+
path: string,
|
|
409
|
+
js: string,
|
|
410
|
+
scripts: ReadonlySet<string>,
|
|
411
|
+
): Record<string, string> =>
|
|
412
|
+
Object.fromEntries(
|
|
413
|
+
(relatives(path, js, scripts, true) ?? []).flatMap(({ to, at }) =>
|
|
414
|
+
to != null && at != null && at != to ? [[to, at] as const] : []
|
|
415
|
+
),
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
/** Each kept module of `packages` by its address under `origin`, and what it
|
|
419
|
+
* imports statically: the package's other modules by their addresses, and
|
|
420
|
+
* bare specifiers as written (@yaks/esbuild/package `Compiled`).
|
|
421
|
+
*
|
|
422
|
+
* ```ts
|
|
423
|
+
* import { assertEquals } from '@std/assert'
|
|
424
|
+
* assertEquals(
|
|
425
|
+
* packageLinks([{ links: { 'a.js': ['./b.js', 'preact'] } }], 'https://y'),
|
|
426
|
+
* { 'https://y/__m/a.js': ['https://y/__m/b.js', 'preact'] },
|
|
427
|
+
* )
|
|
428
|
+
* ```
|
|
429
|
+
*/
|
|
430
|
+
export let packageLinks = (
|
|
431
|
+
packages: Pick<KeptPackage, 'links'>[],
|
|
432
|
+
origin: string,
|
|
433
|
+
): Record<string, string[]> =>
|
|
434
|
+
Object.fromEntries(
|
|
435
|
+
packages.flatMap(({ links }) =>
|
|
436
|
+
Object.entries(links).map(([file, to]) => [
|
|
437
|
+
`${origin}${MODULES_AT}${file}`,
|
|
438
|
+
to.map((t) =>
|
|
439
|
+
t.startsWith('./') ? `${origin}${MODULES_AT}${t.slice(2)}` : t
|
|
440
|
+
),
|
|
441
|
+
])
|
|
442
|
+
),
|
|
443
|
+
)
|
package/forward.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// The service binding carries a complete compiler job from the kernel, and
|
|
2
|
+
// its answer goes back as it came. If the compiler stopped or was killed,
|
|
3
|
+
// replay the same job on another container before returning a failure. This
|
|
4
|
+
// gives the compile a fresh process and memory without publishing twice.
|
|
5
|
+
import { timed } from './timing.ts'
|
|
6
|
+
import { KILLED, REPLACED } from './container.ts'
|
|
7
|
+
|
|
8
|
+
let retryable = (e: unknown) => (e as { retryable?: boolean })?.retryable
|
|
9
|
+
|
|
10
|
+
// A Compiler still on the previous deployment answers a lost connection this
|
|
11
|
+
// way. Native containers use REPLACED.
|
|
12
|
+
let disconnected = async (response: Response): Promise<boolean> =>
|
|
13
|
+
response.status == 500 &&
|
|
14
|
+
await response.clone().text() == 'Container suddenly disconnected, try again'
|
|
15
|
+
|
|
16
|
+
export let forwarded = async (
|
|
17
|
+
req: Request,
|
|
18
|
+
send: (req: Request, retry: boolean) => Promise<Response>,
|
|
19
|
+
): Promise<Response> => {
|
|
20
|
+
if (req.method != 'POST') {
|
|
21
|
+
return new Response('POST a compiler job', { status: 405 })
|
|
22
|
+
}
|
|
23
|
+
if (req.headers.get('x-yak-compiler-job') != '1') {
|
|
24
|
+
return new Response('unsupported compiler job protocol', { status: 400 })
|
|
25
|
+
}
|
|
26
|
+
let job = await req.text()
|
|
27
|
+
let sent = (retry: boolean) =>
|
|
28
|
+
send(
|
|
29
|
+
new Request('http://compiler/', {
|
|
30
|
+
method: 'POST',
|
|
31
|
+
headers: { 'content-type': 'application/json' },
|
|
32
|
+
body: job,
|
|
33
|
+
}),
|
|
34
|
+
retry,
|
|
35
|
+
)
|
|
36
|
+
let began = Date.now()
|
|
37
|
+
try {
|
|
38
|
+
let answer = await sent(false)
|
|
39
|
+
if (
|
|
40
|
+
answer.status != REPLACED && answer.status != KILLED &&
|
|
41
|
+
!await disconnected(answer)
|
|
42
|
+
) return answer
|
|
43
|
+
await answer.body?.cancel()
|
|
44
|
+
} catch (e) {
|
|
45
|
+
if (!retryable(e)) throw e
|
|
46
|
+
}
|
|
47
|
+
let retry = Date.now() - began
|
|
48
|
+
return timed(await sent(true), { retry })
|
|
49
|
+
}
|