@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/container.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { timed } from "./timing.js";
|
|
2
|
+
import { sha256 } from "./sha256.js";
|
|
3
|
+
/** The job can be retried on a different container. */ export let REPLACED = 503;
|
|
4
|
+
/** The runtime observed the container exit after a SIGKILL. */ export let KILLED = 507;
|
|
5
|
+
/** The container knows no such job: its image is older than the host
|
|
6
|
+
* asking, as while a deploy rolls containers out after the host. */ export let UNKNOWN = 501;
|
|
7
|
+
/** The header a pack's answer names its packer in (./file.ts `PACKER`): a
|
|
8
|
+
* container a deploy has not reached yet packs by the rules before, and its
|
|
9
|
+
* answer is kept by nobody whose packer differs. */ export let PACKED_BY = 'x-yak-packer';
|
|
10
|
+
/** The header an answer names the SHA-256 of its body in ({@link sealed}). */ export let DIGEST = 'x-yak-digest';
|
|
11
|
+
/** The answer, its body's SHA-256 beside it. Bytes between the container and
|
|
12
|
+
* its caller have arrived with 32 of them moved elsewhere in the body (bug
|
|
13
|
+
* 7d531594): still JSON, now a worker that fails to load. The caller checks
|
|
14
|
+
* what arrived ({@link opened}). */ export let sealed = async (answer)=>{
|
|
15
|
+
let bytes = new Uint8Array(await answer.arrayBuffer());
|
|
16
|
+
let headers = new Headers(answer.headers);
|
|
17
|
+
headers.set(DIGEST, await sha256(bytes));
|
|
18
|
+
return new Response(bytes, {
|
|
19
|
+
status: answer.status,
|
|
20
|
+
statusText: answer.statusText,
|
|
21
|
+
headers
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
/** What an answer {@link sealed} holds, or null when it arrived otherwise. An
|
|
25
|
+
* answer with no seal, from a container older than seals, is taken as it
|
|
26
|
+
* came. */ export let opened = async (answer)=>{
|
|
27
|
+
let bytes = new Uint8Array(await answer.arrayBuffer());
|
|
28
|
+
let seal = answer.headers.get(DIGEST);
|
|
29
|
+
return !seal || seal == await sha256(bytes) ? bytes : null;
|
|
30
|
+
};
|
|
31
|
+
let WARM = 120_000;
|
|
32
|
+
let PORT = 8080;
|
|
33
|
+
let READY = 20_000;
|
|
34
|
+
let POLL = 300;
|
|
35
|
+
/** Where the container reaches its shelf (./shelf.ts): a name no DNS
|
|
36
|
+
* answers, which the host intercepts. */ export let SHELF = 'shelf.compiler';
|
|
37
|
+
let pause = (ms)=>new Promise((done)=>setTimeout(done, ms));
|
|
38
|
+
let message = (e)=>e instanceof Error ? e.message : String(e);
|
|
39
|
+
let exitCode = async (container)=>{
|
|
40
|
+
try {
|
|
41
|
+
await container.monitor();
|
|
42
|
+
} catch (error) {
|
|
43
|
+
return typeof error == 'number' ? error : error?.exitCode;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
// An exit lookup is useful only after a broken request and a stopped
|
|
47
|
+
// container. Monitoring a running container pins its Durable Object.
|
|
48
|
+
let failure = async (container, error)=>{
|
|
49
|
+
if (!container.running && await exitCode(container) == 137) {
|
|
50
|
+
return new Response(`the compiler's container was killed: ${message(error)}`, {
|
|
51
|
+
status: KILLED
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return new Response(message(error), {
|
|
55
|
+
status: REPLACED
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
/** Keep the compiler warm with the runtime's idle timeout. Only a caller
|
|
59
|
+
* starting or using the compiler holds its Durable Object awake. A `shelf`
|
|
60
|
+
* answers what the container keeps beyond its life, at {@link SHELF}. */ export let compiler = (container, now = Date.now, wait = pause, shelf)=>{
|
|
61
|
+
let prepared = false;
|
|
62
|
+
let opening;
|
|
63
|
+
let port = ()=>container.getTcpPort(PORT);
|
|
64
|
+
let listening = async ()=>{
|
|
65
|
+
let until = now() + READY;
|
|
66
|
+
let running = container.running;
|
|
67
|
+
for(;;){
|
|
68
|
+
try {
|
|
69
|
+
// Any HTTP response confirms the port is ready. The compiler only
|
|
70
|
+
// accepts POST, so this probe never starts a job.
|
|
71
|
+
let response = await port().fetch(new Request('http://compiler/', {
|
|
72
|
+
method: 'HEAD',
|
|
73
|
+
signal: AbortSignal.timeout(POLL * 3)
|
|
74
|
+
}));
|
|
75
|
+
await response.body?.cancel();
|
|
76
|
+
return;
|
|
77
|
+
} catch (error) {
|
|
78
|
+
// Allocation can still be pending on the first probe. Once we saw
|
|
79
|
+
// the container running, an exit ends this startup attempt.
|
|
80
|
+
let alive = container.running;
|
|
81
|
+
if (running && !alive || now() >= until) throw error;
|
|
82
|
+
running = alive;
|
|
83
|
+
await wait(POLL);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
let open = async ()=>{
|
|
88
|
+
let cold = !container.running;
|
|
89
|
+
// An intercept lasts as long as the container, and is set before it
|
|
90
|
+
// starts so its first request finds it.
|
|
91
|
+
// A container whose shelf could not be reached compiles without it.
|
|
92
|
+
if (shelf) {
|
|
93
|
+
await container.interceptOutboundHttp(SHELF, shelf).catch((error)=>console.error(`the compiler's shelf is out of reach: ${message(error)}`));
|
|
94
|
+
}
|
|
95
|
+
if (cold) {
|
|
96
|
+
container.start({
|
|
97
|
+
enableInternet: true,
|
|
98
|
+
...shelf ? {
|
|
99
|
+
env: {
|
|
100
|
+
SHELF: `http://${SHELF}`
|
|
101
|
+
}
|
|
102
|
+
} : {}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
await container.setInactivityTimeout(WARM);
|
|
106
|
+
await listening();
|
|
107
|
+
prepared = true;
|
|
108
|
+
return cold ? now() : undefined;
|
|
109
|
+
};
|
|
110
|
+
let ready = ()=>{
|
|
111
|
+
if (opening) return opening;
|
|
112
|
+
if (prepared && container.running) return;
|
|
113
|
+
return opening = open().finally(()=>opening = undefined);
|
|
114
|
+
};
|
|
115
|
+
return async (request)=>{
|
|
116
|
+
let began = now();
|
|
117
|
+
let started, answer;
|
|
118
|
+
try {
|
|
119
|
+
started = await ready();
|
|
120
|
+
answer = await port().fetch(request);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
prepared = false;
|
|
123
|
+
if (request.signal.aborted) throw error;
|
|
124
|
+
return await failure(container, error);
|
|
125
|
+
}
|
|
126
|
+
// A previous compiler image used 507 for a subprocess exit too. A
|
|
127
|
+
// container that returned an HTTP response survived the interruption.
|
|
128
|
+
if (answer.status == KILLED) {
|
|
129
|
+
answer = new Response(answer.body, {
|
|
130
|
+
status: REPLACED,
|
|
131
|
+
headers: answer.headers
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return timed(answer, {
|
|
135
|
+
container: now() - began,
|
|
136
|
+
...started == null ? {} : {
|
|
137
|
+
start: Math.max(0, started - began)
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
//# sourceMappingURL=container.js.map
|
package/container.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"container.js","sources":["./container.ts"],"names":[],"mappings":"AAAA,SAAS,KAAK,sBAAqB;AACnC,SAAS,MAAM,sBAAqB;AAEpC,qDAAqD,GACrD,OAAO,IAAI,WAAW,IAAG;AACzB,6DAA6D,GAC7D,OAAO,IAAI,SAAS,IAAG;AACvB;kEACkE,GAClE,OAAO,IAAI,UAAU,IAAG;AACxB;;kDAEkD,GAClD,OAAO,IAAI,YAAY,eAAc;AAErC,4EAA4E,GAC5E,OAAO,IAAI,SAAS,eAAc;AAElC;;;kCAGkC,GAClC,OAAO,IAAI,SAAS,OAAO;EACzB,IAAI,QAAQ,IAAI,WAAW,MAAM,OAAO,WAAW;EACnD,IAAI,UAAU,IAAI,QAAQ,OAAO,OAAO;EACxC,QAAQ,GAAG,CAAC,QAAQ,MAAM,OAAO;EACjC,OAAO,IAAI,SAAS,OAAO;IACzB,QAAQ,OAAO,MAAM;IACrB,YAAY,OAAO,UAAU;IAC7B;EACF;AACF,EAAC;AAED;;SAES,GACT,OAAO,IAAI,SAAS,OAClB;EAEA,IAAI,QAAQ,IAAI,WAAW,MAAM,OAAO,WAAW;EACnD,IAAI,OAAO,OAAO,OAAO,CAAC,GAAG,CAAC;EAC9B,OAAO,CAA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CAAC,GAAG,UAAU;MAAO,CAAC;IACnE;EACF;AACF,EAAC"}
|
package/container.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { timed } from './timing.js'
|
|
2
|
+
import { sha256 } from './sha256.js'
|
|
3
|
+
|
|
4
|
+
/** The job can be retried on a different container. */
|
|
5
|
+
export let REPLACED = 503
|
|
6
|
+
/** The runtime observed the container exit after a SIGKILL. */
|
|
7
|
+
export let KILLED = 507
|
|
8
|
+
/** The container knows no such job: its image is older than the host
|
|
9
|
+
* asking, as while a deploy rolls containers out after the host. */
|
|
10
|
+
export let UNKNOWN = 501
|
|
11
|
+
/** The header a pack's answer names its packer in (./file.ts `PACKER`): a
|
|
12
|
+
* container a deploy has not reached yet packs by the rules before, and its
|
|
13
|
+
* answer is kept by nobody whose packer differs. */
|
|
14
|
+
export let PACKED_BY = 'x-yak-packer'
|
|
15
|
+
|
|
16
|
+
/** The header an answer names the SHA-256 of its body in ({@link sealed}). */
|
|
17
|
+
export let DIGEST = 'x-yak-digest'
|
|
18
|
+
|
|
19
|
+
/** The answer, its body's SHA-256 beside it. Bytes between the container and
|
|
20
|
+
* its caller have arrived with 32 of them moved elsewhere in the body (bug
|
|
21
|
+
* 7d531594): still JSON, now a worker that fails to load. The caller checks
|
|
22
|
+
* what arrived ({@link opened}). */
|
|
23
|
+
export let sealed = async (answer: Response): Promise<Response> => {
|
|
24
|
+
let bytes = new Uint8Array(await answer.arrayBuffer())
|
|
25
|
+
let headers = new Headers(answer.headers)
|
|
26
|
+
headers.set(DIGEST, await sha256(bytes))
|
|
27
|
+
return new Response(bytes, {
|
|
28
|
+
status: answer.status,
|
|
29
|
+
statusText: answer.statusText,
|
|
30
|
+
headers,
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** What an answer {@link sealed} holds, or null when it arrived otherwise. An
|
|
35
|
+
* answer with no seal, from a container older than seals, is taken as it
|
|
36
|
+
* came. */
|
|
37
|
+
export let opened = async (
|
|
38
|
+
answer: Response,
|
|
39
|
+
): Promise<Uint8Array<ArrayBuffer> | null> => {
|
|
40
|
+
let bytes = new Uint8Array(await answer.arrayBuffer())
|
|
41
|
+
let seal = answer.headers.get(DIGEST)
|
|
42
|
+
return !seal || seal == await sha256(bytes) ? bytes : null
|
|
43
|
+
}
|
|
44
|
+
let WARM = 120_000
|
|
45
|
+
let PORT = 8080
|
|
46
|
+
let READY = 20_000
|
|
47
|
+
let POLL = 300
|
|
48
|
+
|
|
49
|
+
/** Where the container reaches its shelf (./shelf.ts): a name no DNS
|
|
50
|
+
* answers, which the host intercepts. */
|
|
51
|
+
export let SHELF = 'shelf.compiler'
|
|
52
|
+
|
|
53
|
+
type Door = { fetch(request: Request): Promise<Response> }
|
|
54
|
+
|
|
55
|
+
/** The native container operations the compiler needs. */
|
|
56
|
+
export type Container = {
|
|
57
|
+
readonly running: boolean
|
|
58
|
+
start(
|
|
59
|
+
options: { enableInternet: boolean; env?: Record<string, string> },
|
|
60
|
+
): void
|
|
61
|
+
setInactivityTimeout(ms: number): Promise<void>
|
|
62
|
+
getTcpPort(port: number): Door
|
|
63
|
+
interceptOutboundHttp(host: string, door: Door): Promise<void>
|
|
64
|
+
monitor(): Promise<void>
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let pause = (ms: number) => new Promise<void>((done) => setTimeout(done, ms))
|
|
68
|
+
let message = (e: unknown) => e instanceof Error ? e.message : String(e)
|
|
69
|
+
|
|
70
|
+
let exitCode = async (container: Container) => {
|
|
71
|
+
try {
|
|
72
|
+
await container.monitor()
|
|
73
|
+
} catch (error) {
|
|
74
|
+
return typeof error == 'number'
|
|
75
|
+
? error
|
|
76
|
+
: (error as { exitCode?: number } | null)?.exitCode
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// An exit lookup is useful only after a broken request and a stopped
|
|
81
|
+
// container. Monitoring a running container pins its Durable Object.
|
|
82
|
+
let failure = async (container: Container, error: unknown) => {
|
|
83
|
+
if (!container.running && await exitCode(container) == 137) {
|
|
84
|
+
return new Response(
|
|
85
|
+
`the compiler's container was killed: ${message(error)}`,
|
|
86
|
+
{
|
|
87
|
+
status: KILLED,
|
|
88
|
+
},
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
return new Response(message(error), { status: REPLACED })
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Keep the compiler warm with the runtime's idle timeout. Only a caller
|
|
95
|
+
* starting or using the compiler holds its Durable Object awake. A `shelf`
|
|
96
|
+
* answers what the container keeps beyond its life, at {@link SHELF}. */
|
|
97
|
+
export let compiler = (
|
|
98
|
+
container: Container,
|
|
99
|
+
now = Date.now,
|
|
100
|
+
wait = pause,
|
|
101
|
+
shelf?: Door,
|
|
102
|
+
) => {
|
|
103
|
+
let prepared = false
|
|
104
|
+
let opening: Promise<number | undefined> | undefined
|
|
105
|
+
|
|
106
|
+
let port = () => container.getTcpPort(PORT)
|
|
107
|
+
let listening = async () => {
|
|
108
|
+
let until = now() + READY
|
|
109
|
+
let running = container.running
|
|
110
|
+
for (;;) {
|
|
111
|
+
try {
|
|
112
|
+
// Any HTTP response confirms the port is ready. The compiler only
|
|
113
|
+
// accepts POST, so this probe never starts a job.
|
|
114
|
+
let response = await port().fetch(
|
|
115
|
+
new Request('http://compiler/', {
|
|
116
|
+
method: 'HEAD',
|
|
117
|
+
signal: AbortSignal.timeout(POLL * 3),
|
|
118
|
+
}),
|
|
119
|
+
)
|
|
120
|
+
await response.body?.cancel()
|
|
121
|
+
return
|
|
122
|
+
} catch (error) {
|
|
123
|
+
// Allocation can still be pending on the first probe. Once we saw
|
|
124
|
+
// the container running, an exit ends this startup attempt.
|
|
125
|
+
let alive = container.running
|
|
126
|
+
if ((running && !alive) || now() >= until) throw error
|
|
127
|
+
running = alive
|
|
128
|
+
await wait(POLL)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
let open = async () => {
|
|
133
|
+
let cold = !container.running
|
|
134
|
+
// An intercept lasts as long as the container, and is set before it
|
|
135
|
+
// starts so its first request finds it.
|
|
136
|
+
// A container whose shelf could not be reached compiles without it.
|
|
137
|
+
if (shelf) {
|
|
138
|
+
await container.interceptOutboundHttp(SHELF, shelf).catch((error) =>
|
|
139
|
+
console.error(`the compiler's shelf is out of reach: ${message(error)}`)
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
if (cold) {
|
|
143
|
+
container.start({
|
|
144
|
+
enableInternet: true,
|
|
145
|
+
...shelf ? { env: { SHELF: `http://${SHELF}` } } : {},
|
|
146
|
+
})
|
|
147
|
+
}
|
|
148
|
+
await container.setInactivityTimeout(WARM)
|
|
149
|
+
await listening()
|
|
150
|
+
prepared = true
|
|
151
|
+
return cold ? now() : undefined
|
|
152
|
+
}
|
|
153
|
+
let ready = () => {
|
|
154
|
+
if (opening) return opening
|
|
155
|
+
if (prepared && container.running) return
|
|
156
|
+
return opening = open().finally(() => opening = undefined)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return async (request: Request): Promise<Response> => {
|
|
160
|
+
let began = now()
|
|
161
|
+
let started, answer
|
|
162
|
+
try {
|
|
163
|
+
started = await ready()
|
|
164
|
+
answer = await port().fetch(request)
|
|
165
|
+
} catch (error) {
|
|
166
|
+
prepared = false
|
|
167
|
+
if (request.signal.aborted) throw error
|
|
168
|
+
return await failure(container, error)
|
|
169
|
+
}
|
|
170
|
+
// A previous compiler image used 507 for a subprocess exit too. A
|
|
171
|
+
// container that returned an HTTP response survived the interruption.
|
|
172
|
+
if (answer.status == KILLED) {
|
|
173
|
+
answer = new Response(answer.body, {
|
|
174
|
+
status: REPLACED,
|
|
175
|
+
headers: answer.headers,
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
return timed(answer, {
|
|
179
|
+
container: now() - began,
|
|
180
|
+
...started == null ? {} : { start: Math.max(0, started - began) },
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
}
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/esbuild","version":"0.2.36","license":"Apache-2.0","description":"Compiles an app's TypeScript and npm imports: the module graph of its files, what a deploy must compile, and the compile itself with native esbuild, which yaks.app runs in a container.","exports":{".":"./mod.ts","./plan":"./plan.ts","./compile":"./compile.ts","./unpack":"./unpack.ts","./strip":"./strip.ts","./file":"./file.ts","./link":"./link.ts","./pack":"./pack.ts","./resolve":"./resolve.ts","./dependencies":"./dependencies.ts"},"imports":{"@yaks/esbuild":"./mod.ts","@yaks/esbuild/plan":"./plan.ts","@yaks/esbuild/compile":"./compile.ts","@yaks/esbuild/unpack":"./unpack.ts","@yaks/esbuild/strip":"./strip.ts","@yaks/esbuild/file":"./file.ts","@yaks/esbuild/link":"./link.ts","@yaks/esbuild/pack":"./pack.ts","@yaks/esbuild/resolve":"./resolve.ts","@yaks/esbuild/dependencies":"./dependencies.ts"}}
|
package/dependencies.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// What an app's package.json declares, read without the module graph: a
|
|
2
|
+
// host asking which packages an app names (a store composing their words)
|
|
3
|
+
// loads none of the compile.
|
|
4
|
+
/** A plan that cannot be made, in a sentence the agent can act on. */ export class Unplanned extends Error {
|
|
5
|
+
name = 'Unplanned';
|
|
6
|
+
}
|
|
7
|
+
/** The packages package.json declares, name to version range; none without
|
|
8
|
+
* one.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { assertEquals } from '@std/assert'
|
|
12
|
+
* assertEquals(dependencies('{"dependencies": {"three": "^0.180.0"}}'), {
|
|
13
|
+
* three: '^0.180.0',
|
|
14
|
+
* })
|
|
15
|
+
* assertEquals(dependencies(null), {})
|
|
16
|
+
* ```
|
|
17
|
+
*/ export let dependencies = (text)=>{
|
|
18
|
+
if (text == null) return {};
|
|
19
|
+
let pkg;
|
|
20
|
+
try {
|
|
21
|
+
pkg = JSON.parse(text);
|
|
22
|
+
} catch (e) {
|
|
23
|
+
throw new Unplanned(`package.json is not JSON: ${e.message}`);
|
|
24
|
+
}
|
|
25
|
+
let deps = pkg?.dependencies ?? {};
|
|
26
|
+
if (typeof deps != 'object' || deps == null || Array.isArray(deps) || Object.values(deps).some((v)=>typeof v != 'string')) {
|
|
27
|
+
throw new Unplanned('package.json: dependencies is an object of package name to version, ' + 'e.g. {"dependencies": {"three": "^0.180.0"}}');
|
|
28
|
+
}
|
|
29
|
+
return deps;
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=dependencies.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dependencies.js","sources":["./dependencies.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,0EAA0E;AAC1E,6BAA6B;AAE7B,oEAAoE,GACpE,OAAO,MAAM,kBAAkB;EACpB,OAAO,YAAW;AAC7B;AAEA;;;;;;;;;;CAUC,GACD,OAAO,IAAI,eAAe,CAAC;EACzB,IAAI,QAAQ,MAAM,OAAO,CAAC;EAC1B,IAAI;EACJ,IAAI;IACF,MAAM,KAAK,KAAK,CAAC;EACnB,EAAE,OAAO,GAAG;IACV,MAAM,IAAI,UAAU,CAAC,0BAA0B,EAAE,AAAC,EAAY,OAAO,EAAE;EACzE;EACA,IAAI,OAAO,KAAK,gBAAgB,CAAC;EACjC,IACE,OAAO,QAAQ,YAAY,QAAQ,QAAQ,MAAM,OAAO,CAAC,SACzD,OAAO,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC,IAAM,OAAO,KAAK,WAC5C;IACA,MAAM,IAAI,UACR,yEACE;EAEN;EACA,OAAO;AACT,EAAC"}
|
package/dependencies.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// What an app's package.json declares, read without the module graph: a
|
|
2
|
+
// host asking which packages an app names (a store composing their words)
|
|
3
|
+
// loads none of the compile.
|
|
4
|
+
|
|
5
|
+
/** A plan that cannot be made, in a sentence the agent can act on. */
|
|
6
|
+
export class Unplanned extends Error {
|
|
7
|
+
override name = 'Unplanned'
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** The packages package.json declares, name to version range; none without
|
|
11
|
+
* one.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { assertEquals } from '@std/assert'
|
|
15
|
+
* assertEquals(dependencies('{"dependencies": {"three": "^0.180.0"}}'), {
|
|
16
|
+
* three: '^0.180.0',
|
|
17
|
+
* })
|
|
18
|
+
* assertEquals(dependencies(null), {})
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export let dependencies = (text: string | null): Record<string, string> => {
|
|
22
|
+
if (text == null) return {}
|
|
23
|
+
let pkg: { dependencies?: unknown }
|
|
24
|
+
try {
|
|
25
|
+
pkg = JSON.parse(text)
|
|
26
|
+
} catch (e) {
|
|
27
|
+
throw new Unplanned(`package.json is not JSON: ${(e as Error).message}`)
|
|
28
|
+
}
|
|
29
|
+
let deps = pkg?.dependencies ?? {}
|
|
30
|
+
if (
|
|
31
|
+
typeof deps != 'object' || deps == null || Array.isArray(deps) ||
|
|
32
|
+
Object.values(deps).some((v) => typeof v != 'string')
|
|
33
|
+
) {
|
|
34
|
+
throw new Unplanned(
|
|
35
|
+
'package.json: dependencies is an object of package name to version, ' +
|
|
36
|
+
'e.g. {"dependencies": {"three": "^0.180.0"}}',
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
return deps as Record<string, string>
|
|
40
|
+
}
|