cf-bindings-proxy
Advanced tools
Comparing version 0.3.1-3c3f882 to 0.3.1-fc01172
@@ -1,11 +0,19 @@ | ||
import { transformData as f } from "../../transform.js"; | ||
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") | ||
import { prepareDataForProxy as l, transformData as p } from "../../transform.js"; | ||
const c = async (e, n) => n.reduce(async (a, { prop: r, args: i }) => (await a)[r]( | ||
...await Promise.all( | ||
i.map(async (s) => Array.isArray(s.data) ? Promise.all( | ||
s.data.map((t) => "__bindingId" in t ? c(e, t.__calls) : t) | ||
) : s.transform ? p(s.data, s.transform) : s.data) | ||
) | ||
), Promise.resolve(e)), u = { | ||
async fetch(e, n) { | ||
if (e.method !== "POST") | ||
return new Response("Method not allowed", { status: 405 }); | ||
try { | ||
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), { | ||
const { __original_call: a, __bindingId: r, __calls: i } = await e.json(), s = a ? await c(n[a.__bindingId], a.__calls) : n[r], t = await c(s, i), o = { success: !0, data: t }; | ||
if (o.success) { | ||
const d = await l(t, { data: t }); | ||
o.transform = d.transform, o.data = d.data; | ||
} | ||
return new Response(JSON.stringify(o), { | ||
status: 200, | ||
@@ -16,7 +24,7 @@ headers: { "Content-Type": "application/json" } | ||
console.error(a); | ||
const n = JSON.stringify({ | ||
const r = JSON.stringify({ | ||
success: !1, | ||
data: a instanceof Error ? a.message : "Failed to access binding" | ||
}); | ||
return new Response(n, { | ||
return new Response(r, { | ||
status: 500, | ||
@@ -29,4 +37,4 @@ headers: { "Content-Type": "application/json" } | ||
export { | ||
l as default | ||
u as default | ||
}; | ||
//# sourceMappingURL=_worker.js.map |
{ | ||
"name": "cf-bindings-proxy", | ||
"version": "0.3.1-3c3f882", | ||
"version": "0.3.1-fc01172", | ||
"description": "Experimental proxy for interfacing with bindings in projects targeting Cloudflare Pages", | ||
@@ -58,3 +58,3 @@ "license": "MIT", | ||
"vitest-environment-miniflare": "^2.14.0", | ||
"wrangler": "^3.5.1" | ||
"wrangler": "^3.6.0" | ||
}, | ||
@@ -61,0 +61,0 @@ "peerDependencies": { |
74
proxy.js
@@ -1,24 +0,16 @@ | ||
import { transformData as o } from "./transform.js"; | ||
const u = async (n) => n.data instanceof ArrayBuffer ? { | ||
data: o(n.data, { 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, | ||
import { transformData as _, prepareDataForProxy as u } from "./transform.js"; | ||
const h = async (e) => ({ | ||
...e, | ||
__calls: await Promise.all( | ||
n.__calls.map(async (s) => ({ | ||
e.__calls.map(async (s) => ({ | ||
...s, | ||
args: await Promise.all(s.args.map(u)) | ||
args: await Promise.all(s.args.map((n) => u(n.data, n))) | ||
})) | ||
) | ||
}), c = async (n) => { | ||
let s; | ||
}), c = async (e) => { | ||
const s = await h(e); | ||
let n; | ||
try { | ||
s = await fetch("http://127.0.0.1:8799", { | ||
body: JSON.stringify(await _(n)), | ||
n = await fetch("http://127.0.0.1:8799", { | ||
body: JSON.stringify(s), | ||
method: "POST", | ||
@@ -31,30 +23,30 @@ cache: "no-store", | ||
} | ||
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)) | ||
const { success: a, data: r, transform: t } = await n.json(); | ||
if (!a) | ||
throw new Error(r || "Bad response from binding proxy"); | ||
return t ? _(r, t) : r; | ||
}, y = (e, s, n) => new Proxy(n, { | ||
get(a, r) { | ||
if (!n || ["then", Symbol.iterator, Symbol.toStringTag].includes(r)) | ||
return; | ||
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)); | ||
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 t = l(e, !0); | ||
return t.__original_call = s, async (...i) => (t.__calls.push({ prop: r, args: i.map((o) => ({ data: o })) }), c(t)); | ||
} | ||
}), h = (n) => ["prepare"].includes(n) ? ["first", "run", "all", "raw"] : [], l = (n, s = !1) => new Proxy({ __bindingId: n, __calls: [], __chainUntil: [] }, { | ||
get(r, a) { | ||
}), f = (e) => ["prepare"].includes(e) ? ["first", "run", "all", "raw"] : [], l = (e, s = !1) => new Proxy({ __bindingId: e, __calls: [], __chainUntil: [] }, { | ||
get(n, a) { | ||
if (typeof a == "string" && a.startsWith("__")) | ||
return r[a]; | ||
return n[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); | ||
if (n.__chainUntil.length || (n.__chainUntil = f(a)), n.__chainUntil.length && !n.__chainUntil.includes(a)) { | ||
const r = l(e); | ||
return r.__chainUntil = n.__chainUntil, r.__calls = n.__calls, (...t) => (n.__calls.push({ prop: a, args: t.map((i) => ({ data: i })) }), r); | ||
} | ||
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); | ||
return async (...r) => { | ||
n.__calls.push({ prop: a, args: r.map((i) => ({ data: i })) }); | ||
const t = await c(n); | ||
return typeof t != "object" || !t ? t : y(e, n, t); | ||
}; | ||
@@ -61,0 +53,0 @@ } |
@@ -0,1 +1,2 @@ | ||
import type { PropertyCall } from './proxy'; | ||
/** | ||
@@ -11,3 +12,12 @@ * Transforms data from one format to another. | ||
to: string; | ||
}) => unknown; | ||
}) => Promise<unknown>; | ||
/** | ||
* Prepares the argument's data to be sent over HTTP via the binding proxy. | ||
* This will transform any `ArrayBuffer` or `Blob` to `base64` and add the `transform` property. | ||
* | ||
* @param data The data to prepare. | ||
*/ | ||
export declare const prepareDataForProxy: (rawData: PropertyCallArg['data'], fallback: PropertyCallArg) => Promise<PropertyCallArg>; | ||
type PropertyCallArg = PropertyCall['args'][0]; | ||
export {}; | ||
//# sourceMappingURL=transform.d.ts.map |
@@ -1,19 +0,31 @@ | ||
const b = (r, e) => { | ||
if (e.from === "buffer" && e.to === "base64") | ||
return btoa(String.fromCharCode(...new Uint8Array(r))); | ||
const o = async (r, e) => { | ||
if (e.from === "buffer" && e.to === "base64") { | ||
const f = new Uint8Array(r); | ||
let t = ""; | ||
for (let b = 0; b < f.byteLength; b++) | ||
t += String.fromCharCode(f[b]); | ||
return btoa(t); | ||
} | ||
if (e.from === "base64" && e.to === "buffer") | ||
return Uint8Array.from(atob(r), (f) => f.charCodeAt(0)).buffer; | ||
if (e.from === "blob" && e.to === "base64") { | ||
const f = r.arrayBuffer(); | ||
return b(f, { from: "buffer", to: "base64" }); | ||
const f = await r.arrayBuffer(); | ||
return o(f, { from: "buffer", to: "base64" }); | ||
} | ||
if (e.from === "base64" && e.to === "blob") { | ||
const f = b(r, { from: "base64", to: "buffer" }); | ||
const f = await o(r, { from: "base64", to: "buffer" }); | ||
return new Blob([f]); | ||
} | ||
return r; | ||
}; | ||
}, n = async (r, e) => r instanceof ArrayBuffer ? { | ||
transform: { from: "base64", to: "buffer" }, | ||
data: await o(r, { from: "buffer", to: "base64" }) | ||
} : r instanceof Blob ? { | ||
transform: { from: "base64", to: "blob" }, | ||
data: await o(r, { from: "blob", to: "base64" }) | ||
} : e; | ||
export { | ||
b as transformData | ||
n as prepareDataForProxy, | ||
o as transformData | ||
}; | ||
//# sourceMappingURL=transform.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
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
60642
299