Skip to content

Commit

Permalink
bindings passthrough + persistence path
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed May 30, 2024
1 parent f1ff546 commit 4a87a51
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 262 deletions.
125 changes: 13 additions & 112 deletions packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { randomUUID } from "node:crypto";
import { readFile } from "node:fs/promises";
import getPort from "get-port";
import { Miniflare, Mutex } from "miniflare";
import { DEFAULT_INSPECTOR_PORT } from "../..";
import { getLocalPersistencePath } from "../../dev/get-local-persistence-path";
import * as MF from "../../dev/miniflare";
import { RuntimeController } from "./BaseController";
import { castErrorCause } from "./events";
import type { CfWorkerInit } from "../../deployment-bundle/worker";
import { convertBindingsToCfWorkerInitBindings } from "./utils";
import type { WorkerEntrypointsDefinition } from "../../dev-registry";
import type {
BundleCompleteEvent,
Expand Down Expand Up @@ -48,118 +46,21 @@ function getName(config: StartDevWorkerOptions) {
async function convertToConfigBundle(
event: BundleCompleteEvent
): Promise<MF.ConfigBundle> {
const bindings: CfWorkerInit["bindings"] = {
vars: undefined,
kv_namespaces: undefined,
send_email: undefined,
wasm_modules: undefined,
text_blobs: undefined,
browser: undefined,
ai: undefined,
version_metadata: undefined,
data_blobs: undefined,
durable_objects: undefined,
queues: undefined,
r2_buckets: undefined,
d1_databases: undefined,
vectorize: undefined,
constellation: undefined,
hyperdrive: undefined,
services: undefined,
analytics_engine_datasets: undefined,
dispatch_namespaces: undefined,
mtls_certificates: undefined,
logfwdr: undefined,
unsafe: undefined,
};

const fetchers: Record<string, ServiceFetch> = {};
let { bindings, fetchers } = await convertBindingsToCfWorkerInitBindings(

Check failure on line 49 in packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts

View workflow job for this annotation

GitHub Actions / Checks (macos-13)

'fetchers' is never reassigned. Use 'const' instead

Check failure on line 49 in packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts

View workflow job for this annotation

GitHub Actions / Checks (ubuntu-latest)

'fetchers' is never reassigned. Use 'const' instead
event.config.bindings
);

for (const [name, binding] of Object.entries(event.config.bindings ?? {})) {
binding.type;
if (binding.type === "plain_text") {
bindings.vars ??= {};
bindings.vars[name] = binding.value;
} else if (binding.type === "json") {
bindings.vars ??= {};
bindings.vars[name] = binding.value;
} else if (binding.type === "kv_namespace") {
bindings.kv_namespaces ??= [];
bindings.kv_namespaces.push({ ...binding, binding: name });
} else if (binding.type === "send_email") {
bindings.send_email ??= [];
bindings.send_email.push({ ...binding, name: name });
} else if (binding.type === "wasm_module") {
bindings.wasm_modules ??= {};
bindings.wasm_modules[name] = await getBinaryFileContents(binding.source);
} else if (binding.type === "text_blob") {
bindings.text_blobs ??= {};
bindings.text_blobs[name] = binding.source.path as string;
} else if (binding.type === "data_blob") {
bindings.data_blobs ??= {};
bindings.data_blobs[name] = await getBinaryFileContents(binding.source);
} else if (binding.type === "browser") {
bindings.browser = { binding: name };
} else if (binding.type === "ai") {
bindings.ai = { binding: name };
} else if (binding.type === "version_metadata") {
bindings.version_metadata = { binding: name };
} else if (binding.type === "durable_object_namespace") {
bindings.durable_objects ??= { bindings: [] };
bindings.durable_objects.bindings.push({ ...binding, name: name });
} else if (binding.type === "queue") {
bindings.queues ??= [];
bindings.queues.push({ ...binding, binding: name });
} else if (binding.type === "r2_bucket") {
bindings.r2_buckets ??= [];
bindings.r2_buckets.push({ ...binding, binding: name });
} else if (binding.type === "d1") {
bindings.d1_databases ??= [];
bindings.d1_databases.push({ ...binding, binding: name });
} else if (binding.type === "vectorize") {
bindings.vectorize ??= [];
bindings.vectorize.push({ ...binding, binding: name });
} else if (binding.type === "constellation") {
bindings.constellation ??= [];
bindings.constellation.push({ ...binding, binding: name });
} else if (binding.type === "hyperdrive") {
bindings.hyperdrive ??= [];
bindings.hyperdrive.push({ ...binding, binding: name });
} else if (binding.type === "service") {
bindings.services ??= [];
bindings.services.push({ ...binding, binding: name });
} else if (binding.type === "fetcher") {
fetchers[name] = binding.fetcher;
} else if (binding.type === "analytics_engine") {
bindings.analytics_engine_datasets ??= [];
bindings.analytics_engine_datasets.push({ ...binding, binding: name });
} else if (binding.type === "dispatch_namespace") {
bindings.dispatch_namespaces ??= [];
bindings.dispatch_namespaces.push({ ...binding, binding: name });
} else if (binding.type === "mtls_certificate") {
bindings.mtls_certificates ??= [];
bindings.mtls_certificates.push({ ...binding, binding: name });
} else if (binding.type === "logfwdr") {
bindings.logfwdr ??= { bindings: [] };
bindings.logfwdr.bindings.push({ ...binding, name: name });
} else if (binding.type.startsWith("unsafe-")) {
bindings.unsafe ??= {
bindings: [],
metadata: undefined,
capnp: undefined,
};
bindings.unsafe.bindings?.push({ ...binding, name: name });
}
// TODO: Remove this passthrough
if (event.config._bindings) {
bindings = event.config._bindings;
}

const persistence = event.config.dev?.persist
? getLocalPersistencePath(
typeof event.config.dev?.persist === "object"
? event.config.dev?.persist.path
: undefined,
event.config.config?.path
)
: null;
const persistence = getLocalPersistencePath(
typeof event.config.dev?.persist === "object"
? event.config.dev?.persist.path
: undefined,
event.config.config?.path
);

const crons = [];
const queueConsumers = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ export class RemoteRuntimeController extends RuntimeController {
sendMetrics: config.sendMetrics,
});

const bindings = (
await convertBindingsToCfWorkerInitBindings(config.bindings)
).bindings;

const token = await this.#previewToken({
bundle,
modules: bundle.modules,
Expand All @@ -191,8 +195,8 @@ export class RemoteRuntimeController extends RuntimeController {
}
: undefined,
format: bundle.entry.format,
bindings: (await convertBindingsToCfWorkerInitBindings(config.bindings))
.bindings,
// TODO: Remove this passthrough
bindings: config._bindings ? config._bindings : bindings,
compatibilityDate: config.compatibilityDate,
compatibilityFlags: config.compatibilityFlags,
usageModel: config.usageModel,
Expand Down
Loading

0 comments on commit 4a87a51

Please sign in to comment.