cf-bindings-proxy
Advanced tools
Comparing version 0.3.3 to 0.4.0
{ | ||
"name": "cf-bindings-proxy", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"description": "Experimental proxy for interfacing with bindings in projects targeting Cloudflare Pages", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
76
proxy.js
@@ -1,16 +0,16 @@ | ||
import { transformData as _, prepareDataForProxy as u } from "./transform.js"; | ||
const h = async (e) => ({ | ||
...e, | ||
import { transformData as u, prepareDataForProxy as _ } from "./transform.js"; | ||
const f = async (i) => ({ | ||
...i, | ||
__calls: await Promise.all( | ||
e.__calls.map(async (i) => ({ | ||
...i, | ||
args: await Promise.all(i.args.map((n) => u(n.data, n))) | ||
i.__calls.map(async (o) => ({ | ||
...o, | ||
args: await Promise.all(o.args.map((n) => _(n.data, n))) | ||
})) | ||
) | ||
}), c = async (e) => { | ||
const i = await h(e); | ||
}), s = async (i) => { | ||
const o = await f(i); | ||
let n; | ||
try { | ||
n = await fetch("http://127.0.0.1:8799", { | ||
body: JSON.stringify(i), | ||
body: JSON.stringify(o), | ||
method: "POST", | ||
@@ -23,32 +23,38 @@ cache: "no-store", | ||
} | ||
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, i, n) => new Proxy(n, { | ||
get(a, r) { | ||
if (!n || ["then", Symbol.iterator, Symbol.toStringTag].includes(r)) | ||
const { success: c, data: t, transform: r } = await n.json(); | ||
if (!c) | ||
throw new Error(t || "Bad response from binding proxy"); | ||
return r ? u(t, r) : t; | ||
}, y = (i, o, n) => new Proxy(n, { | ||
get(c, t) { | ||
if (!n || ["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)]; | ||
if (["toJSON"].includes(r)) | ||
if (t in n || ["error", "results"].includes(t)) | ||
return n[t]; | ||
if (Array.isArray(n) && typeof t == "string" && !Number.isNaN(Number(t))) | ||
return n[Number(t)]; | ||
if (["toJSON"].includes(t)) | ||
return n; | ||
const t = l(e, !0); | ||
return t.__original_call = i, async (...s) => (t.__calls.push({ prop: r, args: s.map((o) => ({ data: o })) }), c(t)); | ||
if (t === "writeHttpMetadata" && n && typeof n == "object") { | ||
const e = n.httpMetadata || {}; | ||
return (a) => { | ||
e.cacheControl && a.set("cache-control", e.cacheControl), e.cacheExpiry && a.set("expires", e.cacheExpiry.toUTCString()), e.contentDisposition && a.set("content-disposition", e.contentDisposition), e.contentEncoding && a.set("content-encoding", e.contentEncoding), e.contentLanguage && a.set("content-language", e.contentLanguage), e.contentType && a.set("content-type", e.contentType); | ||
}; | ||
} | ||
const r = l(i, !0); | ||
return r.__original_call = o, async (...e) => (r.__calls.push({ prop: t, args: e.map((a) => ({ data: a })) }), s(r)); | ||
} | ||
}), f = (e) => ["prepare"].includes(e) ? ["first", "run", "all", "raw"] : [], l = (e, i = !1) => new Proxy({ __bindingId: e, __calls: [], __chainUntil: [] }, { | ||
get(n, a) { | ||
if (typeof a == "string" && a.startsWith("__")) | ||
return n[a]; | ||
if (a !== "toJSON" && !i) { | ||
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((s) => ({ data: s })) }), r); | ||
}), h = (i) => ["prepare"].includes(i) ? ["first", "run", "all", "raw"] : [], l = (i, o = !1) => new Proxy({ __bindingId: i, __calls: [], __chainUntil: [] }, { | ||
get(n, c) { | ||
if (typeof c == "string" && c.startsWith("__")) | ||
return n[c]; | ||
if (c !== "toJSON" && !o) { | ||
if (n.__chainUntil.length || (n.__chainUntil = h(c)), n.__chainUntil.length && !n.__chainUntil.includes(c)) { | ||
const t = l(i); | ||
return t.__chainUntil = n.__chainUntil, t.__calls = n.__calls, (...r) => (n.__calls.push({ prop: c, args: r.map((e) => ({ data: e })) }), t); | ||
} | ||
return async (...r) => { | ||
n.__calls.push({ prop: a, args: r.map((s) => ({ data: s })) }); | ||
const t = await c(n); | ||
return typeof t != "object" || !t ? t : y(e, n, t); | ||
return async (...t) => { | ||
n.__calls.push({ prop: c, args: t.map((e) => ({ data: e })) }); | ||
const r = await s(n); | ||
return typeof r != "object" || !r ? r : y(i, n, r); | ||
}; | ||
@@ -55,0 +61,0 @@ } |
@@ -99,5 +99,5 @@ <p align="center"> | ||
- [x] put | ||
- [ ] writeHttpMetadata | ||
- [x] writeHttpMetadata | ||
- [x] get | ||
- [ ] writeHttpMetadata | ||
- [x] writeHttpMetadata | ||
- [x] text | ||
@@ -110,7 +110,7 @@ - [x] json | ||
- [x] head | ||
- [ ] writeHttpMetadata | ||
- [x] writeHttpMetadata | ||
- [x] list | ||
- [ ] writeHttpMetadata | ||
- [x] writeHttpMetadata | ||
- [x] delete | ||
- [ ] createMultipartUpload | ||
- [ ] resumeMultipartUpload |
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
64426
310