feat: remove cloudflare adapter dependency to nodejs_compat

- adjusted bindings extraction to not rely on node inspect anymore
- added new vite export that uses nodejs apis
This commit is contained in:
dswbx
2025-11-11 13:33:04 +01:00
parent 3f55c0f2c5
commit 4859b99954
11 changed files with 119 additions and 31 deletions
+31 -14
View File
@@ -1,5 +1,3 @@
import { inspect } from "node:util";
export type BindingTypeMap = {
D1Database: D1Database;
KVNamespace: KVNamespace;
@@ -10,21 +8,40 @@ export type BindingTypeMap = {
export type GetBindingType = keyof BindingTypeMap;
export type BindingMap<T extends GetBindingType> = { key: string; value: BindingTypeMap[T] };
export const bindingMatchers = {
// D1Database instance doesn't stringify to [object D1Database] (yet)
D1Database: (value: object): value is D1Database => {
if (typeof value !== "object" || value === null) return false;
if (String(value) === "[object D1Database]") return true;
if (Object.keys(value).length !== 2) return false;
return ["alwaysPrimarySession", "fetcher"].every((key) => key in value);
},
KVNamespace: (value: object): value is KVNamespace =>
String(value) === "[object KvNamespace]" && Object.keys(value).length === 0,
DurableObjectNamespace: (value: object): value is DurableObjectNamespace =>
String(value) === "[object DurableObjectNamespace]" && Object.keys(value).length === 0,
R2Bucket: (value: object): value is R2Bucket =>
String(value) === "[object R2Bucket]" && Object.keys(value).length === 0,
};
export function getBindings<T extends GetBindingType>(env: any, type: T): BindingMap<T>[] {
const bindings: BindingMap<T>[] = [];
if (!(type in bindingMatchers)) {
console.error(`No binding matcher for type ${type}`);
return [];
}
const matcher = bindingMatchers[type];
for (const key in env) {
try {
if (
(env[key] as any).constructor.name === type ||
String(env[key]) === `[object ${type}]` ||
inspect(env[key]).includes(type)
) {
bindings.push({
key,
value: env[key] as BindingTypeMap[T],
});
}
} catch (e) {}
const value = env[key];
if (typeof value !== "object" || value === null) continue;
if (!matcher(value)) continue;
bindings.push({
key,
value,
} as any);
}
return bindings;
}