alchemy 0.55.3 → 0.56.0
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/bin/alchemy.js +46 -246
- package/lib/alchemy.d.ts.map +1 -1
- package/lib/alchemy.js +7 -0
- package/lib/alchemy.js.map +1 -1
- package/lib/cloudflare/bundle/plugin-hot-reload.d.ts +1 -3
- package/lib/cloudflare/bundle/plugin-hot-reload.d.ts.map +1 -1
- package/lib/cloudflare/bundle/plugin-hot-reload.js +4 -1
- package/lib/cloudflare/bundle/plugin-hot-reload.js.map +1 -1
- package/lib/cloudflare/compatibility-date.gen.d.ts +1 -1
- package/lib/cloudflare/compatibility-date.gen.js +1 -1
- package/lib/cloudflare/compatibility-presets.d.ts +1 -1
- package/lib/cloudflare/compatibility-presets.js +1 -1
- package/lib/cloudflare/compatibility-presets.js.map +1 -1
- package/lib/cloudflare/local-dev-cloudflare-shim.d.ts +3 -1
- package/lib/cloudflare/local-dev-cloudflare-shim.d.ts.map +1 -1
- package/lib/cloudflare/local-dev-cloudflare-shim.js +10 -5
- package/lib/cloudflare/local-dev-cloudflare-shim.js.map +1 -1
- package/lib/cloudflare/miniflare/build-worker-options.d.ts +22 -0
- package/lib/cloudflare/miniflare/build-worker-options.d.ts.map +1 -0
- package/lib/cloudflare/miniflare/build-worker-options.js +394 -0
- package/lib/cloudflare/miniflare/build-worker-options.js.map +1 -0
- package/lib/cloudflare/miniflare/miniflare-controller.d.ts +23 -0
- package/lib/cloudflare/miniflare/miniflare-controller.d.ts.map +1 -0
- package/lib/cloudflare/miniflare/miniflare-controller.js +110 -0
- package/lib/cloudflare/miniflare/miniflare-controller.js.map +1 -0
- package/lib/cloudflare/miniflare/miniflare-worker-proxy.d.ts.map +1 -1
- package/lib/cloudflare/miniflare/miniflare-worker-proxy.js +5 -15
- package/lib/cloudflare/miniflare/miniflare-worker-proxy.js.map +1 -1
- package/lib/cloudflare/miniflare/remote-binding-proxy.d.ts +1 -5
- package/lib/cloudflare/miniflare/remote-binding-proxy.d.ts.map +1 -1
- package/lib/cloudflare/miniflare/remote-binding-proxy.js +17 -22
- package/lib/cloudflare/miniflare/remote-binding-proxy.js.map +1 -1
- package/lib/cloudflare/website.js +4 -4
- package/lib/cloudflare/website.js.map +1 -1
- package/lib/cloudflare/worker-bundle.d.ts +2 -0
- package/lib/cloudflare/worker-bundle.d.ts.map +1 -1
- package/lib/cloudflare/worker-bundle.js +19 -3
- package/lib/cloudflare/worker-bundle.js.map +1 -1
- package/lib/cloudflare/worker.d.ts.map +1 -1
- package/lib/cloudflare/worker.js +76 -97
- package/lib/cloudflare/worker.js.map +1 -1
- package/lib/context.d.ts +11 -0
- package/lib/context.d.ts.map +1 -1
- package/lib/context.js +8 -0
- package/lib/context.js.map +1 -1
- package/lib/scope.d.ts +12 -0
- package/lib/scope.d.ts.map +1 -1
- package/lib/scope.js +25 -0
- package/lib/scope.js.map +1 -1
- package/package.json +3 -2
- package/src/alchemy.ts +7 -0
- package/src/cloudflare/bundle/plugin-hot-reload.ts +4 -3
- package/src/cloudflare/compatibility-date.gen.ts +1 -1
- package/src/cloudflare/compatibility-presets.ts +1 -1
- package/src/cloudflare/local-dev-cloudflare-shim.ts +11 -5
- package/src/cloudflare/miniflare/build-worker-options.ts +454 -0
- package/src/cloudflare/miniflare/miniflare-controller.ts +128 -0
- package/src/cloudflare/miniflare/miniflare-worker-proxy.ts +6 -15
- package/src/cloudflare/miniflare/remote-binding-proxy.ts +18 -23
- package/src/cloudflare/website.ts +4 -4
- package/src/cloudflare/worker-bundle.ts +28 -3
- package/src/cloudflare/worker.ts +90 -125
- package/src/context.ts +19 -0
- package/src/scope.ts +27 -0
- package/lib/cloudflare/miniflare/miniflare-worker-options.d.ts +0 -14
- package/lib/cloudflare/miniflare/miniflare-worker-options.d.ts.map +0 -1
- package/lib/cloudflare/miniflare/miniflare-worker-options.js +0 -468
- package/lib/cloudflare/miniflare/miniflare-worker-options.js.map +0 -1
- package/lib/cloudflare/miniflare/miniflare.d.ts +0 -40
- package/lib/cloudflare/miniflare/miniflare.d.ts.map +0 -1
- package/lib/cloudflare/miniflare/miniflare.js +0 -142
- package/lib/cloudflare/miniflare/miniflare.js.map +0 -1
- package/src/cloudflare/miniflare/miniflare-worker-options.ts +0 -599
- package/src/cloudflare/miniflare/miniflare.ts +0 -183
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import * as miniflare from "miniflare";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { findOpenPort } from "../../util/find-open-port.ts";
|
|
4
|
-
import {
|
|
5
|
-
promiseWithResolvers,
|
|
6
|
-
type PromiseWithResolvers,
|
|
7
|
-
} from "../../util/promise-with-resolvers.ts";
|
|
8
|
-
import {
|
|
9
|
-
buildMiniflareWorkerOptions,
|
|
10
|
-
buildRemoteBindings,
|
|
11
|
-
type MiniflareWorkerOptions,
|
|
12
|
-
} from "./miniflare-worker-options.ts";
|
|
13
|
-
import { MiniflareWorkerProxy } from "./miniflare-worker-proxy.ts";
|
|
14
|
-
import {
|
|
15
|
-
createRemoteProxyWorker,
|
|
16
|
-
type RemoteBindingProxy,
|
|
17
|
-
} from "./remote-binding-proxy.ts";
|
|
18
|
-
|
|
19
|
-
class MiniflareServer {
|
|
20
|
-
miniflare?: miniflare.Miniflare;
|
|
21
|
-
workers = new Map<string, miniflare.WorkerOptions>();
|
|
22
|
-
workerProxies = new Map<string, MiniflareWorkerProxy>();
|
|
23
|
-
remoteBindingProxies = new Map<string, RemoteBindingProxy>();
|
|
24
|
-
|
|
25
|
-
stream = new WritableStream<{
|
|
26
|
-
worker: MiniflareWorkerOptions;
|
|
27
|
-
promise: PromiseWithResolvers<{ url: string }>;
|
|
28
|
-
}>({
|
|
29
|
-
write: async ({ worker, promise }) => {
|
|
30
|
-
try {
|
|
31
|
-
const server = await this.set(worker);
|
|
32
|
-
promise.resolve(server);
|
|
33
|
-
} catch (error) {
|
|
34
|
-
promise.reject(error);
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
close: async () => {
|
|
38
|
-
await this.dispose();
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
|
-
writer = this.stream.getWriter();
|
|
42
|
-
|
|
43
|
-
async push(worker: MiniflareWorkerOptions) {
|
|
44
|
-
const promise = promiseWithResolvers<{ url: string }>();
|
|
45
|
-
const [, server] = await Promise.all([
|
|
46
|
-
this.writer.write({ worker, promise }),
|
|
47
|
-
promise.promise,
|
|
48
|
-
]);
|
|
49
|
-
return server;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async close() {
|
|
53
|
-
await this.writer.close();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private async set(worker: MiniflareWorkerOptions) {
|
|
57
|
-
this.workers.set(
|
|
58
|
-
worker.name,
|
|
59
|
-
buildMiniflareWorkerOptions({
|
|
60
|
-
...worker,
|
|
61
|
-
remoteProxyConnectionString: await this.maybeCreateRemoteProxy(worker),
|
|
62
|
-
}),
|
|
63
|
-
);
|
|
64
|
-
if (this.miniflare) {
|
|
65
|
-
await withErrorRewrite(
|
|
66
|
-
this.miniflare.setOptions(await this.miniflareOptions()),
|
|
67
|
-
);
|
|
68
|
-
} else {
|
|
69
|
-
// Miniflare intercepts SIGINT and exits with 130, which is not a failure.
|
|
70
|
-
// No one likes to see a non-zero exit code when they Ctrl+C, so here's our workaround.
|
|
71
|
-
process.on("exit", (code) => {
|
|
72
|
-
process.exit(code === 130 ? 0 : code);
|
|
73
|
-
});
|
|
74
|
-
this.miniflare = new miniflare.Miniflare(await this.miniflareOptions());
|
|
75
|
-
await withErrorRewrite(this.miniflare.ready);
|
|
76
|
-
}
|
|
77
|
-
const existingProxy = this.workerProxies.get(worker.name);
|
|
78
|
-
if (existingProxy) {
|
|
79
|
-
return existingProxy;
|
|
80
|
-
}
|
|
81
|
-
const newProxy = new MiniflareWorkerProxy({
|
|
82
|
-
name: worker.name,
|
|
83
|
-
port: worker.port ?? (await findOpenPort()),
|
|
84
|
-
miniflare: this.miniflare,
|
|
85
|
-
});
|
|
86
|
-
this.workerProxies.set(worker.name, newProxy);
|
|
87
|
-
await newProxy.ready;
|
|
88
|
-
return newProxy;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private async dispose() {
|
|
92
|
-
await Promise.all([
|
|
93
|
-
this.miniflare?.dispose(),
|
|
94
|
-
...Array.from(this.workerProxies.values()).map((server) =>
|
|
95
|
-
server.close(),
|
|
96
|
-
),
|
|
97
|
-
...Array.from(this.remoteBindingProxies.values()).map((proxy) =>
|
|
98
|
-
proxy.server.close(),
|
|
99
|
-
),
|
|
100
|
-
]);
|
|
101
|
-
this.miniflare = undefined;
|
|
102
|
-
this.workers.clear();
|
|
103
|
-
this.workerProxies.clear();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
private async maybeCreateRemoteProxy(
|
|
107
|
-
worker: MiniflareWorkerOptions,
|
|
108
|
-
): Promise<miniflare.RemoteProxyConnectionString | undefined> {
|
|
109
|
-
const bindings = buildRemoteBindings(worker);
|
|
110
|
-
if (bindings.length === 0) {
|
|
111
|
-
return undefined;
|
|
112
|
-
}
|
|
113
|
-
const existing = this.remoteBindingProxies.get(worker.name);
|
|
114
|
-
if (
|
|
115
|
-
existing?.bindings.every((b) =>
|
|
116
|
-
bindings.find((b2) => b2.name === b.name && b2.type === b.type),
|
|
117
|
-
)
|
|
118
|
-
) {
|
|
119
|
-
return existing.connectionString;
|
|
120
|
-
} else if (existing) {
|
|
121
|
-
await existing.server.close();
|
|
122
|
-
}
|
|
123
|
-
const proxy = await createRemoteProxyWorker({
|
|
124
|
-
name: `mixed-mode-proxy-${crypto.randomUUID()}`,
|
|
125
|
-
bindings,
|
|
126
|
-
});
|
|
127
|
-
this.remoteBindingProxies.set(worker.name, proxy);
|
|
128
|
-
return proxy.connectionString;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
private async miniflareOptions(): Promise<miniflare.MiniflareOptions> {
|
|
132
|
-
return {
|
|
133
|
-
workers: Array.from(this.workers.values()),
|
|
134
|
-
defaultPersistRoot: path.join(process.cwd(), ".alchemy/miniflare"),
|
|
135
|
-
unsafeDevRegistryPath: miniflare.getDefaultDevRegistryPath(),
|
|
136
|
-
analyticsEngineDatasetsPersist: true,
|
|
137
|
-
cachePersist: true,
|
|
138
|
-
d1Persist: true,
|
|
139
|
-
durableObjectsPersist: true,
|
|
140
|
-
kvPersist: true,
|
|
141
|
-
r2Persist: true,
|
|
142
|
-
secretsStorePersist: true,
|
|
143
|
-
workflowsPersist: true,
|
|
144
|
-
log: process.env.DEBUG
|
|
145
|
-
? new miniflare.Log(miniflare.LogLevel.DEBUG)
|
|
146
|
-
: undefined,
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export class ExternalDependencyError extends Error {
|
|
152
|
-
constructor() {
|
|
153
|
-
super(
|
|
154
|
-
'Miniflare detected an external dependency that could not be resolved. This typically occurs when the "nodejs_compat" or "nodejs_als" compatibility flag is not enabled.',
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
async function withErrorRewrite<T>(promise: Promise<T>) {
|
|
160
|
-
try {
|
|
161
|
-
return await promise;
|
|
162
|
-
} catch (error) {
|
|
163
|
-
if (
|
|
164
|
-
error instanceof miniflare.MiniflareCoreError &&
|
|
165
|
-
error.code === "ERR_MODULE_STRING_SCRIPT"
|
|
166
|
-
) {
|
|
167
|
-
throw new ExternalDependencyError();
|
|
168
|
-
} else {
|
|
169
|
-
throw error;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
declare global {
|
|
175
|
-
var _ALCHEMY_MINIFLARE_SERVER: MiniflareServer | undefined;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export const miniflareServer = new Proxy({} as MiniflareServer, {
|
|
179
|
-
get: (_, prop: keyof MiniflareServer) => {
|
|
180
|
-
globalThis._ALCHEMY_MINIFLARE_SERVER ??= new MiniflareServer();
|
|
181
|
-
return globalThis._ALCHEMY_MINIFLARE_SERVER[prop];
|
|
182
|
-
},
|
|
183
|
-
});
|