cf-bindings-proxy
Advanced tools
Comparing version 0.2.7 to 0.2.8-18a25d1
import { transformData as f } from "../../transform.js"; | ||
const c = (t, o) => o.reduce((e, { prop: r, args: i }) => e[r]( | ||
...i.map((n) => Array.isArray(n) ? n.map((s) => "__bindingId" in s ? c(t, s.__calls) : s) : n) | ||
), t), u = { | ||
async fetch(t, o) { | ||
if (t.method !== "POST") | ||
const c = (r, o) => o.reduce((a, { prop: n, args: i }) => a[n]( | ||
...i.map((s) => Array.isArray(s.data) ? s.data.map((t) => "__bindingId" in t ? c(r, t.__calls) : t) : s.transform ? f(s.data, s.transform) : s.data) | ||
), r), l = { | ||
async fetch(r, o) { | ||
if (r.method !== "POST") | ||
return new Response("Method not allowed", { status: 405 }); | ||
try { | ||
const { __original_call: e, __bindingId: r, __calls: i } = await t.json(), n = e ? await c(o[e.__bindingId], e.__calls) : o[r], s = await c(n, i), a = { success: !0, data: s }; | ||
return a.success && (s instanceof ArrayBuffer ? (a.transform = { from: "base64", to: "buffer" }, a.data = f(s, { from: "buffer", to: "base64" })) : s instanceof Blob && (a.transform = { from: "base64", to: "blob" }, a.data = f(await s.arrayBuffer(), { from: "buffer", to: "base64" }))), new Response(JSON.stringify(a), { | ||
const { __original_call: a, __bindingId: n, __calls: i } = await r.json(), s = a ? await c(o[a.__bindingId], a.__calls) : o[n], t = await c(s, i), e = { success: !0, data: t }; | ||
return e.success && (t instanceof ArrayBuffer ? (e.transform = { from: "base64", to: "buffer" }, e.data = f(t, { from: "buffer", to: "base64" })) : t instanceof Blob && (e.transform = { from: "base64", to: "blob" }, e.data = f(await t.arrayBuffer(), { from: "buffer", to: "base64" }))), new Response(JSON.stringify(e), { | ||
status: 200, | ||
headers: { "Content-Type": "application/json" } | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
const r = JSON.stringify({ | ||
} catch (a) { | ||
console.error(a); | ||
const n = JSON.stringify({ | ||
success: !1, | ||
data: e instanceof Error ? e.message : "Failed to access binding" | ||
data: a instanceof Error ? a.message : "Failed to access binding" | ||
}); | ||
return new Response(r, { | ||
return new Response(n, { | ||
status: 500, | ||
@@ -28,4 +28,4 @@ headers: { "Content-Type": "application/json" } | ||
export { | ||
u as default | ||
l as default | ||
}; | ||
//# sourceMappingURL=_worker.js.map |
127
package.json
{ | ||
"name": "cf-bindings-proxy", | ||
"version": "0.2.7", | ||
"description": "Experimental proxy for interfacing with bindings in projects targeting Cloudflare Pages", | ||
"license": "MIT", | ||
"author": "James", | ||
"type": "module", | ||
"bin": "./cli/index.js", | ||
"exports": { | ||
".": { | ||
"import": "./index.js", | ||
"require": "./index.cjs" | ||
} | ||
}, | ||
"publishConfig": { | ||
"directory": "dist" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/james-elicx/cf-bindings-proxy" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/james-elicx/cf-bindings-proxy/issues" | ||
}, | ||
"homepage": "https://github.com/james-elicx/cf-bindings-proxy#readme", | ||
"scripts": { | ||
"dev": "node ./cli/index.js", | ||
"build": "vite build && node ./scripts/build.js", | ||
"build:watch": "npm run build --watch", | ||
"lint": "eslint \"./**/*.{cjs,js,jsx,ts,tsx}\"", | ||
"prettier": "prettier --ignore-unknown --ignore-path=.gitignore --check .", | ||
"prettier:format": "prettier --ignore-unknown --ignore-path=.gitignore --write .", | ||
"tsc": "tsc --noEmit", | ||
"test": "vitest run", | ||
"test:kill": "rm -rf .wrangler; sudo kill -9 `sudo lsof -i :8799 -t`", | ||
"test:watch": "vitest", | ||
"test:coverage": "vitest run --coverage", | ||
"alter-version": "node ./scripts/alter-version.js", | ||
"release": "changeset publish" | ||
}, | ||
"devDependencies": { | ||
"@changesets/cli": "^2.26.2", | ||
"@cloudflare/workers-types": "^4.20230814.0", | ||
"@tsconfig/strictest": "^2.0.1", | ||
"@types/node": "^20.3.3", | ||
"eslint": "^8.41.0", | ||
"eslint-config-ixn": "^1.4.2", | ||
"kysely": "^0.26.1", | ||
"kysely-d1": "^0.3.0", | ||
"nodemon": "^2.0.22", | ||
"prettier": "^2.8.8", | ||
"typescript": "^5.0.4", | ||
"vite": "^4.3.5", | ||
"vite-plugin-dts": "^2.3.0", | ||
"vite-plugin-externalize-deps": "^0.6.0", | ||
"vitest": "^0.31.0", | ||
"vitest-environment-miniflare": "^2.14.0", | ||
"wrangler": "^3.5.1" | ||
}, | ||
"peerDependencies": { | ||
"@cloudflare/workers-types": ">=4", | ||
"wrangler": ">=3.0.0" | ||
} | ||
"name": "cf-bindings-proxy", | ||
"version": "0.2.8-18a25d1", | ||
"description": "Experimental proxy for interfacing with bindings in projects targeting Cloudflare Pages", | ||
"license": "MIT", | ||
"author": "James", | ||
"type": "module", | ||
"bin": "./cli/index.js", | ||
"exports": { | ||
".": { | ||
"import": "./index.js", | ||
"require": "./index.cjs" | ||
} | ||
}, | ||
"publishConfig": { | ||
"directory": "dist" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/james-elicx/cf-bindings-proxy" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/james-elicx/cf-bindings-proxy/issues" | ||
}, | ||
"homepage": "https://github.com/james-elicx/cf-bindings-proxy#readme", | ||
"scripts": { | ||
"dev": "node ./cli/index.js", | ||
"build": "vite build && node ./scripts/build.js", | ||
"build:watch": "npm run build --watch", | ||
"lint": "eslint \"./**/*.{cjs,js,jsx,ts,tsx}\"", | ||
"prettier": "prettier --ignore-unknown --ignore-path=.gitignore --check .", | ||
"prettier:format": "prettier --ignore-unknown --ignore-path=.gitignore --write .", | ||
"tsc": "tsc --noEmit", | ||
"test": "vitest run", | ||
"test:kill": "rm -rf .wrangler; sudo kill -9 `sudo lsof -i :8799 -t`", | ||
"test:watch": "vitest", | ||
"test:coverage": "vitest run --coverage", | ||
"alter-version": "node ./scripts/alter-version.js", | ||
"release": "changeset publish" | ||
}, | ||
"devDependencies": { | ||
"@changesets/cli": "^2.26.2", | ||
"@cloudflare/workers-types": "^4.20230814.0", | ||
"@tsconfig/strictest": "^2.0.1", | ||
"@types/node": "^20.3.3", | ||
"eslint": "^8.41.0", | ||
"eslint-config-ixn": "^1.4.2", | ||
"kysely": "^0.26.1", | ||
"kysely-d1": "^0.3.0", | ||
"nodemon": "^2.0.22", | ||
"prettier": "^2.8.8", | ||
"typescript": "^5.0.4", | ||
"vite": "^4.3.5", | ||
"vite-plugin-dts": "^2.3.0", | ||
"vite-plugin-externalize-deps": "^0.6.0", | ||
"vitest": "^0.31.0", | ||
"vitest-environment-miniflare": "^2.14.0", | ||
"wrangler": "^3.5.1" | ||
}, | ||
"peerDependencies": { | ||
"@cloudflare/workers-types": ">=4", | ||
"wrangler": ">=3.0.0" | ||
}, | ||
"versionMetadata": { | ||
"type": "BETA" | ||
} | ||
} |
@@ -15,3 +15,9 @@ export type BindingResponse = { | ||
prop: string; | ||
args: (unknown | BindingRequest[])[]; | ||
args: { | ||
data: unknown | BindingRequest[]; | ||
transform?: { | ||
from: string; | ||
to: string; | ||
}; | ||
}[]; | ||
}; | ||
@@ -18,0 +24,0 @@ export type BindingRequest = { |
75
proxy.js
@@ -1,7 +0,24 @@ | ||
import { transformData as a } from "./transform.js"; | ||
const c = async (i) => { | ||
import { transformData as o } from "./transform.js"; | ||
const u = async (n) => n.data instanceof ArrayBuffer ? { | ||
data: o(n, { from: "buffer", to: "base64" }), | ||
transform: { from: "base64", to: "buffer" } | ||
} : n.data instanceof Blob ? { | ||
data: o(await n.data.arrayBuffer(), { | ||
from: "buffer", | ||
to: "base64" | ||
}), | ||
transform: { from: "base64", to: "blob" } | ||
} : n, _ = async (n) => ({ | ||
...n, | ||
__calls: await Promise.all( | ||
n.__calls.map(async (s) => ({ | ||
...s, | ||
args: await Promise.all(s.args.map(u)) | ||
})) | ||
) | ||
}), c = async (n) => { | ||
let s; | ||
try { | ||
s = await fetch("http://127.0.0.1:8799", { | ||
body: JSON.stringify(i), | ||
body: JSON.stringify(await _(n)), | ||
method: "POST", | ||
@@ -14,30 +31,30 @@ cache: "no-store", | ||
} | ||
const { success: n, data: t, transform: r } = await s.json(); | ||
if (!n) | ||
throw new Error(t || "Bad response from binding proxy"); | ||
return r ? a(t, r) : t; | ||
}, _ = (i, s, n) => new Proxy(n, { | ||
get(t, r) { | ||
if (!n || ["then", Symbol.iterator, Symbol.toStringTag].includes(r)) | ||
const { success: r, data: a, transform: t } = await s.json(); | ||
if (!r) | ||
throw new Error(a || "Bad response from binding proxy"); | ||
return t ? o(a, t) : a; | ||
}, y = (n, s, r) => new Proxy(r, { | ||
get(a, t) { | ||
if (!r || ["then", Symbol.iterator, Symbol.toStringTag].includes(t)) | ||
return; | ||
if (r in n || ["error", "results"].includes(r)) | ||
return n[r]; | ||
if (Array.isArray(n) && typeof r == "string" && !Number.isNaN(Number(r))) | ||
return n[Number(r)]; | ||
const e = o(i, !0); | ||
return e.__original_call = s, async (...l) => (e.__calls.push({ prop: r, args: l }), c(e)); | ||
if (t in r || ["error", "results"].includes(t)) | ||
return r[t]; | ||
if (Array.isArray(r) && typeof t == "string" && !Number.isNaN(Number(t))) | ||
return r[Number(t)]; | ||
const e = l(n, !0); | ||
return e.__original_call = s, async (...i) => (e.__calls.push({ prop: t, args: i.map((f) => ({ data: f })) }), c(e)); | ||
} | ||
}), u = (i) => ["prepare"].includes(i) ? ["first", "run", "all", "raw"] : [], o = (i, s = !1) => new Proxy({ __bindingId: i, __calls: [], __chainUntil: [] }, { | ||
get(n, t) { | ||
if (typeof t == "string" && t.startsWith("__")) | ||
return n[t]; | ||
if (t !== "toJSON" && !s) { | ||
if (n.__chainUntil.length || (n.__chainUntil = u(t)), n.__chainUntil.length && !n.__chainUntil.includes(t)) { | ||
const r = o(i); | ||
return r.__chainUntil = n.__chainUntil, r.__calls = n.__calls, (...e) => (n.__calls.push({ prop: t, args: e }), r); | ||
}), h = (n) => ["prepare"].includes(n) ? ["first", "run", "all", "raw"] : [], l = (n, s = !1) => new Proxy({ __bindingId: n, __calls: [], __chainUntil: [] }, { | ||
get(r, a) { | ||
if (typeof a == "string" && a.startsWith("__")) | ||
return r[a]; | ||
if (a !== "toJSON" && !s) { | ||
if (r.__chainUntil.length || (r.__chainUntil = h(a)), r.__chainUntil.length && !r.__chainUntil.includes(a)) { | ||
const t = l(n); | ||
return t.__chainUntil = r.__chainUntil, t.__calls = r.__calls, (...e) => (r.__calls.push({ prop: a, args: e.map((i) => ({ data: i })) }), t); | ||
} | ||
return async (...r) => { | ||
n.__calls.push({ prop: t, args: r }); | ||
const e = await c(n); | ||
return typeof e != "object" || !e ? e : _(i, n, e); | ||
return async (...t) => { | ||
r.__calls.push({ prop: a, args: t.map((i) => ({ data: i })) }); | ||
const e = await c(r); | ||
return typeof e != "object" || !e ? e : y(n, r, e); | ||
}; | ||
@@ -48,4 +65,4 @@ } | ||
export { | ||
o as createBindingProxy | ||
l as createBindingProxy | ||
}; | ||
//# sourceMappingURL=proxy.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
56239
263