Skip to content

Commit

Permalink
add bindingNames to getPlatformProxy result
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz committed Mar 12, 2024
1 parent aabae0f commit 8ff4604
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 18 deletions.
121 changes: 121 additions & 0 deletions packages/wrangler/src/api/integrations/platform/bindingNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { deepFreeze } from "../utils";

export type BindingNames = Record<
"var" | "service" | "kv" | "do" | "d1" | "r2" | "queue",
string[]
>;

export function getBindingNames(

Check warning on line 8 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L8

Added line #L8 was not covered by tests
env: Record<string, unknown>
): Readonly<BindingNames> {
const names: BindingNames = {

Check warning on line 11 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L10-L11

Added lines #L10 - L11 were not covered by tests
var: [],
service: [],
kv: [],
do: [],
d1: [],
r2: [],
queue: [],
};

Object.entries(env).forEach(([name, binding]) => {

Check warning on line 21 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L21

Added line #L21 was not covered by tests
if (isVar(binding)) {
return names.var.push(name);

Check warning on line 23 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L23

Added line #L23 was not covered by tests
}
if (isService(binding)) {
return names.service.push(name);

Check warning on line 26 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L26

Added line #L26 was not covered by tests
}
if (isKv(binding)) {
return names.kv.push(name);

Check warning on line 29 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L29

Added line #L29 was not covered by tests
}
if (isDo(binding)) {
return names.do.push(name);

Check warning on line 32 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L32

Added line #L32 was not covered by tests
}
if (isR2(binding)) {
return names.r2.push(name);

Check warning on line 35 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L35

Added line #L35 was not covered by tests
}
if (isD1(binding)) {
return names.d1.push(name);

Check warning on line 38 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L38

Added line #L38 was not covered by tests
}
if (isQueue(binding)) {
return names.queue.push(name);

Check warning on line 41 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L41

Added line #L41 was not covered by tests
}
});

deepFreeze(names);
return names;

Check warning on line 46 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L45-L46

Added lines #L45 - L46 were not covered by tests
}

function isVar(binding: unknown): boolean {

Check warning on line 49 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L49

Added line #L49 was not covered by tests
if (typeof binding === "string") {
return true;

Check warning on line 51 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L51

Added line #L51 was not covered by tests
}

// TO IMPLEMENT
// if(isfullyserializable) return true;

return false;

Check warning on line 57 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L57

Added line #L57 was not covered by tests
}

function containsCrudLikeMethods(binding: Record<string, unknown>): boolean {
return [binding.get, binding.put, binding.delete, binding.list].every(
(field) => typeof field === "function"

Check warning on line 62 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L60-L62

Added lines #L60 - L62 were not covered by tests
);
}

// NOTE: unfortunately the AI bindings is considered a service here, since
// it is implemented using a fetcher and so it is completely
// indistinguishable from a standard service binding
function isService(binding: unknown): boolean {
const asRecord = binding as Record<string, unknown>;

Check warning on line 70 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L69-L70

Added lines #L69 - L70 were not covered by tests

return [asRecord.fetch, asRecord.connect].every(
(field) => typeof field === "function"

Check warning on line 73 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L72-L73

Added lines #L72 - L73 were not covered by tests
);
}

function isKv(binding: unknown): boolean {

Check warning on line 77 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L77

Added line #L77 was not covered by tests
if (!containsCrudLikeMethods(binding as Record<string, unknown>)) {
return false;

Check warning on line 79 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L79

Added line #L79 was not covered by tests
}
return !containsR2Methods(binding as Record<string, unknown>);

Check warning on line 81 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L81

Added line #L81 was not covered by tests
}

function containsR2Methods(binding: Record<string, unknown>): boolean {
return [binding.createMultipartUpload, binding.resumeMultipartUpload].every(
(field) => typeof field === "function"

Check warning on line 86 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L84-L86

Added lines #L84 - L86 were not covered by tests
);
}

function isR2(binding: unknown): boolean {

Check warning on line 90 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L90

Added line #L90 was not covered by tests
if (!containsCrudLikeMethods(binding as Record<string, unknown>)) {
return false;

Check warning on line 92 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L92

Added line #L92 was not covered by tests
}
return containsR2Methods(binding as Record<string, unknown>);

Check warning on line 94 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L94

Added line #L94 was not covered by tests
}

function isDo(binding: unknown): boolean {
const asRecord = binding as Record<string, unknown>;
return [

Check warning on line 99 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L97-L99

Added lines #L97 - L99 were not covered by tests
asRecord.get,
asRecord.idFromName,
asRecord.idFromString,
asRecord.newUniqueId,
].every((field) => typeof field === "function");

Check warning on line 104 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L104

Added line #L104 was not covered by tests
}

function isD1(binding: unknown): boolean {
const asRecord = binding as Record<string, unknown>;

Check warning on line 108 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L107-L108

Added lines #L107 - L108 were not covered by tests

return [asRecord.batch, asRecord.dump, asRecord.exec, asRecord.prepare].every(
(field) => typeof field === "function"

Check warning on line 111 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L110-L111

Added lines #L110 - L111 were not covered by tests
);
}

function isQueue(binding: unknown): boolean {
const asRecord = binding as Record<string, unknown>;

Check warning on line 116 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L115-L116

Added lines #L115 - L116 were not covered by tests

return [asRecord.send, asRecord.sendBatch].every(
(field) => typeof field === "function"

Check warning on line 119 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/bindingNames.ts#L118-L119

Added lines #L118 - L119 were not covered by tests
);
}
35 changes: 17 additions & 18 deletions packages/wrangler/src/api/integrations/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import {
buildSitesOptions,
} from "../../../dev/miniflare";
import { getAssetPaths, getSiteAssetPaths } from "../../../sites";
import { deepFreeze } from "../utils";
import { getBindingNames } from "./bindingNames";
import { CacheStorage } from "./caches";
import { ExecutionContext } from "./executionContext";
import { getServiceBindings } from "./services";
import type { Config } from "../../../config";
import type { BindingNames } from "./bindingNames";
import type { IncomingRequestCfProperties } from "@cloudflare/workers-types/experimental";
import type { MiniflareOptions, WorkerOptions } from "miniflare";

Expand Down Expand Up @@ -49,6 +52,10 @@ export type PlatformProxy<
* Environment object containing the various Cloudflare bindings
*/
env: Env;
/**
* Name of the available bindings, grouped by type
*/
bindingNames: Readonly<BindingNames>;
/**
* Mock of the context object that Workers received in their request handler, all the object's methods are no-op
*/
Expand Down Expand Up @@ -86,11 +93,11 @@ export async function getPlatformProxy<
});

// getBindingsProxy doesn't currently support selecting an environment
const env = undefined;
const environment = undefined;

Check warning on line 96 in packages/wrangler/src/api/integrations/platform/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/index.ts#L96

Added line #L96 was not covered by tests

const miniflareOptions = await getMiniflareOptionsFromConfig(
rawConfig,
env,
environment,
options
);

Expand All @@ -102,16 +109,19 @@ export async function getPlatformProxy<

const bindings: Env = await mf.getBindings();

const vars = getVarsForDev(rawConfig, env);
const vars = getVarsForDev(rawConfig, environment);

Check warning on line 112 in packages/wrangler/src/api/integrations/platform/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/index.ts#L112

Added line #L112 was not covered by tests

const cf = await mf.getCf();
deepFreeze(cf);

const env = {

Check warning on line 117 in packages/wrangler/src/api/integrations/platform/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/platform/index.ts#L117

Added line #L117 was not covered by tests
...vars,
...bindings,
};

return {
env: {
...vars,
...bindings,
},
env,
bindingNames: getBindingNames(env),
cf: cf as CfProperties,
ctx: new ExecutionContext(),
caches: new CacheStorage(),
Expand Down Expand Up @@ -198,17 +208,6 @@ function getMiniflarePersistOptions(
};
}

function deepFreeze<T extends Record<string | number | symbol, unknown>>(
obj: T
): void {
Object.freeze(obj);
Object.entries(obj).forEach(([, prop]) => {
if (prop !== null && typeof prop === "object" && !Object.isFrozen(prop)) {
deepFreeze(prop as Record<string | number | symbol, unknown>);
}
});
}

export type SourcelessWorkerOptions = Omit<
WorkerOptions,
"script" | "scriptPath" | "modules" | "modulesRoot" | "modulesRule"
Expand Down
10 changes: 10 additions & 0 deletions packages/wrangler/src/api/integrations/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function deepFreeze<T extends Record<string | number | symbol, unknown>>(
obj: T
): void {
Object.freeze(obj);
Object.entries(obj).forEach(([, prop]) => {

Check warning on line 5 in packages/wrangler/src/api/integrations/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/utils.ts#L3-L5

Added lines #L3 - L5 were not covered by tests
if (prop !== null && typeof prop === "object" && !Object.isFrozen(prop)) {
deepFreeze(prop as Record<string | number | symbol, unknown>);

Check warning on line 7 in packages/wrangler/src/api/integrations/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/api/integrations/utils.ts#L7

Added line #L7 was not covered by tests
}
});
}

0 comments on commit 8ff4604

Please sign in to comment.