cf-bindings-proxy
Advanced tools
Comparing version
@@ -1,21 +0,36 @@ | ||
import { prepareDataForProxy as y, transformData as d } from "../../transform.js"; | ||
const i = async (n, s) => s.reduce(async (t, { prop: e, args: f }) => (await t)[e]( | ||
import { prepareDataForProxy as l, transformData as b } from "../../transform.js"; | ||
const y = async (o, u) => u.reduce(async (t, { prop: s, args: d }) => (await t)[s]( | ||
...await Promise.all( | ||
f.map(async (o) => Array.isArray(o.data) ? Promise.all( | ||
o.data.map((a) => "__bindingId" in a ? i(n, a.__calls) : a) | ||
) : o.transform ? d(o.data, o.transform) : o.data) | ||
d.map(async (e) => Array.isArray(e.data) ? Promise.all( | ||
e.data.map((n) => "__bindingId" in n ? y(o, n.__calls) : n) | ||
) : e.transform ? b(e.data, e.transform) : e.data) | ||
) | ||
), Promise.resolve(n)), m = { | ||
async fetch(n, s) { | ||
if (n.method !== "POST") | ||
), Promise.resolve(o)), w = { | ||
async fetch(o, u) { | ||
if (o.method !== "POST") | ||
return new Response("Method not allowed", { status: 405 }); | ||
try { | ||
const { __original_call: t, __bindingId: e, __calls: f } = await n.json(), o = t ? await i(s[t.__bindingId], t.__calls) : s[e], a = await i(o, f), r = { success: !0, data: a, functions: {} }; | ||
const { __original_call: t, __proxyType: s, __bindingId: d, __calls: e } = await o.json(), n = t ? t.__bindingId : d; | ||
let i; | ||
switch (s) { | ||
case "caches": { | ||
const f = caches; | ||
i = n === "default" ? f.default : await f.open(n); | ||
break; | ||
} | ||
case "binding": { | ||
i = u[n]; | ||
break; | ||
} | ||
default: | ||
throw new Error("Unknown proxy type"); | ||
} | ||
const p = t ? await y(i, t.__calls) : i, a = await y(p, e), r = { success: !0, data: a, functions: {} }; | ||
if (r.success) { | ||
const c = await y(a, { data: a }); | ||
if (r.transform = c.transform, r.data = c.data, a && typeof a == "object" && !Array.isArray(a)) { | ||
const f = await l(a, { data: a }); | ||
if (r.transform = f.transform, r.data = f.data, a && typeof a == "object" && !Array.isArray(a) && ![Response, Request, URL].find((c) => a instanceof c)) { | ||
if ("arrayBuffer" in a && typeof a.arrayBuffer == "function") { | ||
const u = await a.arrayBuffer(); | ||
r.functions.arrayBuffer = await y(u, { | ||
data: u | ||
const c = await a.arrayBuffer(); | ||
r.functions.arrayBuffer = await l(c, { | ||
data: c | ||
}); | ||
@@ -44,7 +59,7 @@ } | ||
console.error(t); | ||
const e = JSON.stringify({ | ||
const s = JSON.stringify({ | ||
success: !1, | ||
data: t instanceof Error ? t.message : "Failed to access binding" | ||
}); | ||
return new Response(e, { | ||
return new Response(s, { | ||
status: 500, | ||
@@ -57,4 +72,4 @@ headers: { "Content-Type": "application/json" } | ||
export { | ||
m as default | ||
w as default | ||
}; | ||
//# sourceMappingURL=_worker.js.map |
@@ -0,2 +1,13 @@ | ||
import type { Cache } from '@cloudflare/workers-types'; | ||
/** | ||
* Whether the bindings proxy is enabled and currently active. | ||
* | ||
* The proxy is enabled by default in development mode, but can be disabled by setting | ||
* `DISABLE_BINDINGS_PROXY` to `true`. | ||
* | ||
* Alternatively, it can be enabled in other environments by setting `ENABLE_BINDINGS_PROXY` to | ||
* `true`. | ||
* */ | ||
export declare const isProxyEnabled: () => string | boolean; | ||
/** | ||
* Interfaces with a binding from the environment. | ||
@@ -20,2 +31,23 @@ * | ||
export declare const binding: <T>(id: string, opts?: BindingOpts) => T; | ||
type DeriveCacheReturnType<T> = T extends 'default' | undefined ? Cache : Promise<Cache>; | ||
/** | ||
* Interfaces with the Cloudflare Cache API. | ||
* | ||
* By default, the `default` cache is used, however, a custom cache can be provided by passing a | ||
* cache name as the first argument. | ||
* | ||
* @example | ||
* ```ts | ||
* const value = await cacheApi().put(..., ...); | ||
* ``` | ||
* | ||
* @example | ||
* ```ts | ||
* const value = await cacheApi('custom').put(..., ...); | ||
* ``` | ||
* | ||
* @param cacheName Name of the cache to open, or `undefined` to open the default cache. | ||
* @returns Cache instance. | ||
*/ | ||
export declare const cacheApi: <T extends string | undefined = undefined>(cacheName?: T | undefined) => DeriveCacheReturnType<T>; | ||
type BindingOpts = { | ||
@@ -22,0 +54,0 @@ fallback: Record<string, unknown>; |
29
index.js
@@ -1,15 +0,30 @@ | ||
import { createBindingProxy as _ } from "./proxy.js"; | ||
import { createBindingProxy as t } from "./proxy.js"; | ||
import "./transform.js"; | ||
const B = (r, e) => { | ||
var n, o, c, N; | ||
return (n = process == null ? void 0 : process.env) != null && n.ENABLE_BINDINGS_PROXY || !((o = process == null ? void 0 : process.env) != null && o.DISABLE_BINDINGS_PROXY) && ((c = process == null ? void 0 : process.env) == null ? void 0 : c.NODE_ENV) === "development" ? new Proxy( | ||
const c = () => { | ||
var e, n, r; | ||
return ((e = process == null ? void 0 : process.env) == null ? void 0 : e.ENABLE_BINDINGS_PROXY) || !((n = process == null ? void 0 : process.env) != null && n.DISABLE_BINDINGS_PROXY) && ((r = process == null ? void 0 : process.env) == null ? void 0 : r.NODE_ENV) === "development"; | ||
}, d = (e, n) => { | ||
var r; | ||
return c() ? new Proxy( | ||
{}, | ||
{ | ||
get: (s, i) => _(r)[i] | ||
get: (o, s) => t(e, { proxyType: "binding" })[s] | ||
} | ||
) : (N = (e == null ? void 0 : e.fallback) ?? (process == null ? void 0 : process.env)) == null ? void 0 : N[r]; | ||
) : (r = (n == null ? void 0 : n.fallback) ?? (process == null ? void 0 : process.env)) == null ? void 0 : r[e]; | ||
}, y = (e) => { | ||
if (c()) | ||
return new Proxy( | ||
{}, | ||
{ | ||
get: (r, o) => t(e ?? "default", { proxyType: "caches" })[o] | ||
} | ||
); | ||
const n = caches; | ||
return e === "default" || e === void 0 ? n.default : n.open(e); | ||
}; | ||
export { | ||
B as binding | ||
d as binding, | ||
y as cacheApi, | ||
c as isProxyEnabled | ||
}; | ||
//# sourceMappingURL=index.js.map |
127
package.json
{ | ||
"name": "cf-bindings-proxy", | ||
"version": "0.5.0", | ||
"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.6.0" | ||
}, | ||
"peerDependencies": { | ||
"@cloudflare/workers-types": ">=4", | ||
"wrangler": ">=3.0.0" | ||
} | ||
"name": "cf-bindings-proxy", | ||
"version": "0.6.0-dc52fd6", | ||
"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; pkill workerd", | ||
"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.6.0" | ||
}, | ||
"peerDependencies": { | ||
"@cloudflare/workers-types": ">=4", | ||
"wrangler": ">=3.0.0" | ||
}, | ||
"versionMetadata": { | ||
"type": "BETA" | ||
} | ||
} |
@@ -22,4 +22,6 @@ import type { FunctionInfo, Functions, ParseType, TransformRule } from './transform'; | ||
}; | ||
export type ProxyType = 'binding' | 'caches'; | ||
export type BindingRequest = { | ||
__original_call?: BindingRequest; | ||
__proxyType: ProxyType; | ||
__bindingId: string; | ||
@@ -29,2 +31,6 @@ __calls: PropertyCall[]; | ||
}; | ||
type CreateBindingOpts = { | ||
notChainable?: boolean; | ||
proxyType?: ProxyType; | ||
}; | ||
/** | ||
@@ -37,3 +43,4 @@ * Creates a proxy object for the binding. | ||
*/ | ||
export declare const createBindingProxy: <T>(bindingId: string, notChainable?: boolean) => T; | ||
export declare const createBindingProxy: <T>(bindingId: string, { notChainable, proxyType }?: CreateBindingOpts) => T; | ||
export {}; | ||
//# sourceMappingURL=proxy.d.ts.map |
71
proxy.js
@@ -1,3 +0,3 @@ | ||
import { transformData as g, transformFunctionInfo as b, prepareDataForProxy as w } from "./transform.js"; | ||
const x = async (o) => ({ | ||
import { transformData as b, transformFunctionInfo as g, prepareDataForProxy as w } from "./transform.js"; | ||
const P = async (o) => ({ | ||
...o, | ||
@@ -7,11 +7,11 @@ __calls: await Promise.all( | ||
...a, | ||
args: await Promise.all(a.args.map((n) => w(n.data, n))) | ||
args: await Promise.all(a.args.map((c) => w(c.data, c))) | ||
})) | ||
) | ||
}), d = async (o) => { | ||
const a = await x(o), n = JSON.stringify(a); | ||
let i; | ||
const a = await P(o), c = JSON.stringify(a); | ||
let n; | ||
try { | ||
i = await fetch("http://127.0.0.1:8799", { | ||
body: n, | ||
n = await fetch("http://127.0.0.1:8799", { | ||
body: c, | ||
method: "POST", | ||
@@ -24,9 +24,9 @@ cache: "no-store", | ||
} | ||
const { success: e, data: r, transform: t, functions: s } = await i.json(); | ||
if (!e) | ||
throw new Error(r || "Bad response from binding proxy"); | ||
const c = t ? g(r, t) : r; | ||
if (s && c && typeof c == "object" && !Array.isArray(c)) | ||
for (const [f, u] of Object.entries(s)) { | ||
const l = await b(u, s); | ||
const { success: s, data: e, transform: r, functions: t } = await n.json(); | ||
if (!s) | ||
throw new Error(e || "Bad response from binding proxy"); | ||
const i = r ? b(e, r) : e; | ||
if (t && i && typeof i == "object" && !Array.isArray(i)) | ||
for (const [f, u] of Object.entries(t)) { | ||
const l = await g(u, t); | ||
if (u.asAccessor) { | ||
@@ -40,3 +40,3 @@ const y = typeof l == "function" && !(l instanceof Blob) ? await l() : l; | ||
}); | ||
Object.defineProperties(c, { | ||
Object.defineProperties(i, { | ||
body: { | ||
@@ -54,3 +54,3 @@ get() { | ||
} else | ||
Object.defineProperty(c, f, { | ||
Object.defineProperty(i, f, { | ||
get() { | ||
@@ -61,7 +61,7 @@ return y; | ||
} else | ||
c[f] = l; | ||
i[f] = l; | ||
} | ||
return c; | ||
}, p = (o, a, n) => new Proxy(n, { | ||
get(i, e) { | ||
return i; | ||
}, U = (o, a, c, n) => new Proxy(n, { | ||
get(s, e) { | ||
if (!n || ["then", Symbol.iterator, Symbol.toStringTag].includes(e)) | ||
@@ -77,22 +77,25 @@ return; | ||
const t = n.httpMetadata || {}; | ||
return (s) => { | ||
t.cacheControl && s.set("cache-control", t.cacheControl), t.cacheExpiry && s.set("expires", t.cacheExpiry.toUTCString()), t.contentDisposition && s.set("content-disposition", t.contentDisposition), t.contentEncoding && s.set("content-encoding", t.contentEncoding), t.contentLanguage && s.set("content-language", t.contentLanguage), t.contentType && s.set("content-type", t.contentType); | ||
return (i) => { | ||
t.cacheControl && i.set("cache-control", t.cacheControl), t.cacheExpiry && i.set("expires", t.cacheExpiry.toUTCString()), t.contentDisposition && i.set("content-disposition", t.contentDisposition), t.contentEncoding && i.set("content-encoding", t.contentEncoding), t.contentLanguage && i.set("content-language", t.contentLanguage), t.contentType && i.set("content-type", t.contentType); | ||
}; | ||
} | ||
const r = m(o, !0); | ||
return r.__original_call = a, async (...t) => (r.__calls.push({ prop: e, args: t.map((s) => ({ data: s })) }), d(r)); | ||
const r = m(a, { | ||
notChainable: !0, | ||
proxyType: o | ||
}); | ||
return r.__original_call = c, async (...t) => (r.__calls.push({ prop: e, args: t.map((i) => ({ data: i })) }), d(r)); | ||
} | ||
}), P = (o) => ["prepare"].includes(o) ? ["first", "run", "all", "raw"] : [], m = (o, a = !1) => new Proxy({ __bindingId: o, __calls: [], __chainUntil: [] }, { | ||
get(n, i) { | ||
if (typeof i == "string" && i.startsWith("__")) | ||
return n[i]; | ||
if (i !== "toJSON" && !a && !(n.__calls.length === 0 && i === "then")) { | ||
if (n.__chainUntil.length || (n.__chainUntil = P(i)), n.__chainUntil.length && !n.__chainUntil.includes(i)) { | ||
const e = m(o); | ||
return e.__chainUntil = n.__chainUntil, e.__calls = n.__calls, (...r) => (n.__calls.push({ prop: i, args: r.map((t) => ({ data: t })) }), e); | ||
}), x = (o) => ["prepare"].includes(o) ? ["first", "run", "all", "raw"] : [], p = (o, a) => ({ __proxyType: o, __bindingId: a, __calls: [], __chainUntil: [] }), m = (o, { notChainable: a = !1, proxyType: c = "binding" } = {}) => new Proxy(p(c, o), { | ||
get(n, s) { | ||
if (typeof s == "string" && s.startsWith("__")) | ||
return n[s]; | ||
if (s !== "toJSON" && !a && !(n.__calls.length === 0 && s === "then")) { | ||
if (n.__chainUntil.length || (n.__chainUntil = x(s)), n.__chainUntil.length && !n.__chainUntil.includes(s)) { | ||
const e = m(o, { proxyType: c }); | ||
return e.__chainUntil = n.__chainUntil, e.__calls = n.__calls, (...r) => (n.__calls.push({ prop: s, args: r.map((t) => ({ data: t })) }), e); | ||
} | ||
return async (...e) => { | ||
n.__calls.push({ prop: i, args: e.map((t) => ({ data: t })) }); | ||
n.__calls.push({ prop: s, args: e.map((t) => ({ data: t })) }); | ||
const r = await d(n); | ||
return typeof r != "object" || !r || Array.isArray(r) ? r : p(o, n, r); | ||
return typeof r != "object" || !r || Array.isArray(r) || [URL, Request, Response].find((t) => r instanceof t) ? r : U(c, o, n, r); | ||
}; | ||
@@ -99,0 +102,0 @@ } |
@@ -119,1 +119,7 @@ <p align="center"> | ||
- [ ] resumeMultipartUpload (needs more tests) | ||
#### Cache API | ||
- [x] put | ||
- [x] match | ||
- [x] delete |
import type { PropertyCall } from './proxy'; | ||
export type TransformDataType = 'buffer' | 'blob' | 'stream' | 'base64' | 'text' | 'json'; | ||
export type TransformRawType = ArrayBuffer | Blob | string | NonNullable<object>; | ||
type ParseTransformFrom<T extends TransformDataType> = T extends 'buffer' ? Extract<TransformDataType, 'base64' | 'text' | 'json'> : T extends 'blob' ? Extract<TransformDataType, 'base64'> : T extends 'stream' ? Extract<TransformDataType, 'base64'> : T extends 'base64' ? Extract<TransformDataType, 'buffer' | 'blob' | 'stream'> : never; | ||
export type TransformDataType = 'buffer' | 'blob' | 'stream' | 'base64' | 'text' | 'json' | 'url' | 'request' | 'response'; | ||
export type TransformRawType = ArrayBuffer | Blob | string | NonNullable<object> | URL | Request | Response; | ||
type ParseTransformFrom<T extends TransformDataType> = T extends 'buffer' ? Extract<TransformDataType, 'base64' | 'text' | 'json'> : T extends 'blob' | 'stream' ? Extract<TransformDataType, 'base64'> : T extends 'base64' ? Extract<TransformDataType, 'buffer' | 'blob' | 'stream'> : T extends 'text' ? Extract<TransformDataType, 'url' | 'request' | 'response'> : T extends 'url' | 'request' | 'response' ? Extract<TransformDataType, 'text'> : never; | ||
export type TransformRule<From extends TransformDataType = TransformDataType, To extends ParseTransformFrom<From> = ParseTransformFrom<From>> = { | ||
@@ -9,3 +9,3 @@ from: From; | ||
}; | ||
export type ParseType<T extends TransformDataType> = T extends 'buffer' ? ArrayBuffer : T extends 'blob' ? Blob : T extends 'stream' ? ReadableStream : T extends 'base64' | 'text' ? string : T extends 'json' ? NonNullable<object> : never; | ||
export type ParseType<T extends TransformDataType> = T extends 'buffer' ? ArrayBuffer : T extends 'blob' ? Blob : T extends 'stream' ? ReadableStream : T extends 'base64' | 'text' ? string : T extends 'json' ? NonNullable<object> : T extends 'url' ? URL : T extends 'request' ? Request : T extends 'response' ? Response : never; | ||
export type Functions = 'arrayBuffer' | 'blob' | 'json' | 'text' | 'body'; | ||
@@ -38,10 +38,10 @@ export type FunctionInfo<DataTransformRule extends TransformRule | undefined = TransformRule | undefined, Data = DataTransformRule extends TransformRule ? ParseType<DataTransformRule['to']> : undefined> = ({ | ||
type PropertyCallArg = PropertyCall['args'][0]; | ||
export declare const transformFunctionInfo: <Fn extends FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | undefined>>({ data, takeDataFrom, transform }: Fn, fns: { | ||
blob?: FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | undefined> | undefined; | ||
text?: FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | undefined> | undefined; | ||
json?: FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | undefined> | undefined; | ||
arrayBuffer?: FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | undefined> | undefined; | ||
body?: FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | undefined> | undefined; | ||
export declare const transformFunctionInfo: <Fn extends FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json" | "url" | "request" | "response"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | URL | Request | Response | undefined>>({ data, takeDataFrom, transform }: Fn, fns: { | ||
blob?: FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json" | "url" | "request" | "response"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | URL | Request | Response | undefined> | undefined; | ||
text?: FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json" | "url" | "request" | "response"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | URL | Request | Response | undefined> | undefined; | ||
json?: FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json" | "url" | "request" | "response"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | URL | Request | Response | undefined> | undefined; | ||
arrayBuffer?: FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json" | "url" | "request" | "response"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | URL | Request | Response | undefined> | undefined; | ||
body?: FunctionInfo<TransformRule<TransformDataType, "buffer" | "blob" | "stream" | "base64" | "text" | "json" | "url" | "request" | "response"> | undefined, string | object | ArrayBuffer | Blob | ReadableStream<any> | URL | Request | Response | undefined> | undefined; | ||
}) => Promise<TransformRawType | (() => TransformRawType | Promise<TransformRawType>)>; | ||
export {}; | ||
//# sourceMappingURL=transform.d.ts.map |
159
transform.js
@@ -1,24 +0,24 @@ | ||
const f = async (e, t) => { | ||
switch (t.from) { | ||
const o = async (t, r) => { | ||
switch (r.from) { | ||
case "buffer": { | ||
if (t.to === "blob") | ||
return new Blob([e]); | ||
if (t.to === "base64") { | ||
const n = new Uint8Array(e); | ||
let o = ""; | ||
for (let b = 0; b < n.byteLength; b++) | ||
o += String.fromCharCode(n[b]); | ||
return btoa(o); | ||
if (r.to === "blob") | ||
return new Blob([t]); | ||
if (r.to === "base64") { | ||
const f = new Uint8Array(t); | ||
let s = ""; | ||
for (let n = 0; n < f.byteLength; n++) | ||
s += String.fromCharCode(f[n]); | ||
return btoa(s); | ||
} | ||
const r = new TextDecoder().decode(e); | ||
if (t.to === "text") | ||
return r; | ||
if (t.to === "json") | ||
return JSON.parse(r); | ||
const e = new TextDecoder().decode(t); | ||
if (r.to === "text") | ||
return e; | ||
if (r.to === "json") | ||
return JSON.parse(e); | ||
break; | ||
} | ||
case "blob": { | ||
if (t.to === "base64") { | ||
const r = await e.arrayBuffer(); | ||
return f(r, { from: "buffer", to: "base64" }); | ||
if (r.to === "base64") { | ||
const e = await t.arrayBuffer(); | ||
return o(e, { from: "buffer", to: "base64" }); | ||
} | ||
@@ -28,5 +28,5 @@ break; | ||
case "stream": { | ||
if (t.to === "base64") { | ||
const r = await e.getReader().read(); | ||
return f(r.value, { | ||
if (r.to === "base64") { | ||
const e = await t.getReader().read(); | ||
return o(e.value, { | ||
from: "buffer", | ||
@@ -39,43 +39,110 @@ to: "base64" | ||
case "base64": { | ||
if (t.to === "buffer") | ||
return Uint8Array.from(atob(e), (r) => r.charCodeAt(0)).buffer; | ||
if (t.to === "blob") { | ||
const r = await f(e, { | ||
if (r.to === "buffer") | ||
return Uint8Array.from(atob(t), (e) => e.charCodeAt(0)).buffer; | ||
if (r.to === "blob") { | ||
const e = await o(t, { | ||
from: "base64", | ||
to: "buffer" | ||
}); | ||
return new Blob([r]); | ||
return new Blob([e]); | ||
} | ||
if (t.to === "stream") { | ||
const r = await f(e, { | ||
if (r.to === "stream") { | ||
const e = await o(t, { | ||
from: "base64", | ||
to: "buffer" | ||
}), { readable: n, writable: o } = new FixedLengthStream(r.byteLength), b = o.getWriter(); | ||
return b.write(r), b.close(), n; | ||
}), { readable: f, writable: s } = new FixedLengthStream(e.byteLength), n = s.getWriter(); | ||
return n.write(e), n.close(), f; | ||
} | ||
if (r.to === "url") | ||
return new URL(t); | ||
break; | ||
} | ||
case "url": { | ||
if (r.to === "text") | ||
return t.toString(); | ||
break; | ||
} | ||
case "request": { | ||
if (r.to === "text") { | ||
const e = t; | ||
return JSON.stringify({ | ||
url: e.url, | ||
method: e.method, | ||
headers: [...e.headers.entries()], | ||
body: await o(await e.arrayBuffer(), { | ||
from: "buffer", | ||
to: "base64" | ||
}) | ||
}); | ||
} | ||
break; | ||
} | ||
case "response": { | ||
if (r.to === "text") { | ||
const e = t; | ||
return JSON.stringify({ | ||
status: e.status, | ||
statusText: e.statusText, | ||
headers: [...e.headers.entries()], | ||
body: await o(await e.arrayBuffer(), { from: "buffer", to: "base64" }) | ||
}); | ||
} | ||
break; | ||
} | ||
case "text": { | ||
if (r.to === "url") | ||
return new URL(t); | ||
if (r.to === "request") { | ||
const e = JSON.parse(t); | ||
return new Request(e.url, { | ||
method: e.method, | ||
headers: Object.fromEntries(e.headers), | ||
body: e.body ? await o(e.body, { from: "base64", to: "buffer" }) : void 0 | ||
}); | ||
} | ||
if (r.to === "response") { | ||
const e = JSON.parse(t); | ||
return new Response( | ||
e.body ? await o(e.body, { from: "base64", to: "buffer" }) : void 0, | ||
{ | ||
status: e.status, | ||
statusText: e.statusText, | ||
headers: Object.fromEntries(e.headers) | ||
} | ||
); | ||
} | ||
break; | ||
} | ||
} | ||
return e; | ||
}, i = async (e, t) => e instanceof ArrayBuffer ? { | ||
return t; | ||
}, u = async (t, r) => t instanceof ArrayBuffer ? { | ||
transform: { from: "base64", to: "buffer" }, | ||
data: await f(e, { from: "buffer", to: "base64" }) | ||
} : e instanceof Blob ? { | ||
data: await o(t, { from: "buffer", to: "base64" }) | ||
} : t instanceof Blob ? { | ||
transform: { from: "base64", to: "blob" }, | ||
data: await f(e, { from: "blob", to: "base64" }) | ||
} : e !== null && typeof e == "object" && "getReader" in e && typeof e.getReader == "function" ? { | ||
data: await o(t, { from: "blob", to: "base64" }) | ||
} : t instanceof URL ? { | ||
transform: { from: "text", to: "url" }, | ||
data: await o(t, { from: "url", to: "text" }) | ||
} : t instanceof Request ? { | ||
transform: { from: "text", to: "request" }, | ||
data: await o(t, { from: "request", to: "text" }) | ||
} : t instanceof Response ? { | ||
transform: { from: "text", to: "response" }, | ||
data: await o(t, { from: "response", to: "text" }) | ||
} : t !== null && typeof t == "object" && "getReader" in t && typeof t.getReader == "function" ? { | ||
transform: { from: "base64", to: "stream" }, | ||
data: await f(e, { from: "stream", to: "base64" }) | ||
} : t, a = async ({ data: e, takeDataFrom: t, transform: r }, n) => { | ||
const o = t ? await a(n[t], n) : e; | ||
return o && r ? async () => { | ||
const s = typeof o == "function" && !(o instanceof Blob) ? await Promise.resolve(o()) : o; | ||
return Promise.resolve(f(s, r)); | ||
} : o ?? e; | ||
data: await o(t, { from: "stream", to: "base64" }) | ||
} : r, b = async ({ data: t, takeDataFrom: r, transform: e }, f) => { | ||
const s = r ? await b(f[r], f) : t; | ||
return s && e ? async () => { | ||
const i = typeof s == "function" && !(s instanceof Blob) ? await Promise.resolve(s()) : s; | ||
return Promise.resolve(o(i, e)); | ||
} : s ?? t; | ||
}; | ||
export { | ||
i as prepareDataForProxy, | ||
f as transformData, | ||
a as transformFunctionInfo | ||
u as prepareDataForProxy, | ||
o as transformData, | ||
b as transformFunctionInfo | ||
}; | ||
//# 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
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
121966
26.78%590
33.79%125
5.04%