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 6bb9669
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 18 deletions.
129 changes: 129 additions & 0 deletions packages/wrangler/src/api/integrations/platform/bindingNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import {
D1Database,

Check failure on line 2 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View workflow job for this annotation

GitHub Actions / Checks

'D1Database' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 2 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View workflow job for this annotation

GitHub Actions / Checks

'D1Database' is defined but never used
DurableObject,

Check failure on line 3 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View workflow job for this annotation

GitHub Actions / Checks

'DurableObject' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 3 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View workflow job for this annotation

GitHub Actions / Checks

'DurableObject' is defined but never used
DurableObjectNamespace,

Check failure on line 4 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View workflow job for this annotation

GitHub Actions / Checks

'DurableObjectNamespace' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 4 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View workflow job for this annotation

GitHub Actions / Checks

'DurableObjectNamespace' is defined but never used
Fetcher,

Check failure on line 5 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View workflow job for this annotation

GitHub Actions / Checks

'Fetcher' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 5 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View workflow job for this annotation

GitHub Actions / Checks

'Fetcher' is defined but never used
KVNamespace,

Check failure on line 6 in packages/wrangler/src/api/integrations/platform/bindingNames.ts

View workflow job for this annotation

GitHub Actions / Checks

'KVNamespace' is defined but never used. Allowed unused vars must match /^_/u
Queue,
R2Bucket,
} from "@cloudflare/workers-types";
import { deepFreeze } from "../utils";

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

export function getBindingNames(

Check warning on line 17 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#L17

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

Check warning on line 20 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#L19-L20

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

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

Check warning on line 30 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#L30

Added line #L30 was not covered by tests
if (isVar(binding)) {
return names.var.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 (isService(binding)) {
return names.service.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 (isKv(binding)) {
return names.kv.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 (isDo(binding)) {
return names.do.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
}
if (isR2(binding)) {
return names.r2.push(name);

Check warning on line 44 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#L44

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

Check warning on line 47 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#L47

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

Check warning on line 50 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#L50

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

deepFreeze(names);
return names;

Check warning on line 55 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#L54-L55

Added lines #L54 - L55 were not covered by tests
}

function isVar(binding: unknown): boolean {

Check warning on line 58 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#L58

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

Check warning on line 60 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

Added line #L60 was not covered by tests
}

// if(isfullyserializable) return true;

return false;

Check warning on line 65 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#L65

Added line #L65 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 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#L68-L70

Added lines #L68 - L70 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 78 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-L78

Added lines #L77 - L78 were not covered by tests

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

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#L80-L81

Added lines #L80 - L81 were not covered by tests
);
}

function isKv(binding: unknown): boolean {

Check warning on line 85 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#L85

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

Check warning on line 87 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#L87

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

Check warning on line 89 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#L89

Added line #L89 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 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#L92-L94

Added lines #L92 - L94 were not covered by tests
);
}

function isR2(binding: unknown): boolean {

Check warning on line 98 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#L98

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

Check warning on line 100 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#L100

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

Check warning on line 102 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#L102

Added line #L102 was not covered by tests
}

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

Check warning on line 107 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#L105-L107

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

Check warning on line 112 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#L112

Added line #L112 was not covered by tests
}

function isD1(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.batch, asRecord.dump, asRecord.exec, asRecord.prepare].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
);
}

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

Check warning on line 124 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#L123-L124

Added lines #L123 - L124 were not covered by tests

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

Check warning on line 127 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#L126-L127

Added lines #L126 - L127 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 6bb9669

Please sign in to comment.