Work on #508 (thin client)
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { init } from "https://esm.sh/v131/node_events.js";
|
||||
import type {
|
||||
ProxyFetchRequest,
|
||||
ProxyFetchResponse,
|
||||
@@ -8,21 +7,44 @@ import {
|
||||
base64Encode,
|
||||
} from "../../plugos/asset_bundle/base64.ts";
|
||||
|
||||
async function readStream(
|
||||
stream: ReadableStream<Uint8Array>,
|
||||
): Promise<Uint8Array> {
|
||||
const arrays: Uint8Array[] = [];
|
||||
let totalRead = 0;
|
||||
const reader = stream.getReader();
|
||||
while (true) {
|
||||
// The `read()` method returns a promise that
|
||||
// resolves when a value has been received.
|
||||
const { done, value } = await reader.read();
|
||||
// Result objects contain two properties:
|
||||
// `done` - `true` if the stream has already given you all its data.
|
||||
// `value` - Some data. Always `undefined` when `done` is `true`.
|
||||
if (done) {
|
||||
const resultArray = new Uint8Array(totalRead);
|
||||
let offset = 0;
|
||||
for (const array of arrays) {
|
||||
resultArray.set(array, offset);
|
||||
offset += array.length;
|
||||
}
|
||||
return resultArray;
|
||||
}
|
||||
arrays.push(value);
|
||||
totalRead += value.length;
|
||||
}
|
||||
}
|
||||
|
||||
export async function sandboxFetch(
|
||||
reqInfo: RequestInfo,
|
||||
options?: ProxyFetchRequest,
|
||||
): Promise<ProxyFetchResponse> {
|
||||
if (typeof reqInfo !== "string") {
|
||||
// Request as first argument, let's deconstruct it
|
||||
// console.log("fetch", reqInfo);
|
||||
const body = new Uint8Array(await reqInfo.arrayBuffer());
|
||||
const encodedBody = body.length > 0 ? base64Encode(body) : undefined;
|
||||
options = {
|
||||
method: reqInfo.method,
|
||||
headers: Object.fromEntries(reqInfo.headers.entries()),
|
||||
base64Body: reqInfo.body
|
||||
? base64Encode(
|
||||
new Uint8Array(await (new Response(reqInfo.body)).arrayBuffer()),
|
||||
)
|
||||
: undefined,
|
||||
base64Body: encodedBody,
|
||||
};
|
||||
reqInfo = reqInfo.url;
|
||||
}
|
||||
@@ -30,6 +52,21 @@ export async function sandboxFetch(
|
||||
return syscall("sandboxFetch.fetch", reqInfo, options);
|
||||
}
|
||||
|
||||
async function bodyInitToUint8Array(init: BodyInit): Promise<Uint8Array> {
|
||||
if (init instanceof Blob) {
|
||||
const buffer = await init.arrayBuffer();
|
||||
return new Uint8Array(buffer);
|
||||
} else if (init instanceof ArrayBuffer) {
|
||||
return new Uint8Array(init);
|
||||
} else if (init instanceof ReadableStream) {
|
||||
return readStream(init);
|
||||
} else if (typeof init === "string") {
|
||||
return new TextEncoder().encode(init);
|
||||
} else {
|
||||
throw new Error("Unknown body init type");
|
||||
}
|
||||
}
|
||||
|
||||
export function monkeyPatchFetch() {
|
||||
// @ts-ignore: monkey patching fetch
|
||||
globalThis.nativeFetch = globalThis.fetch;
|
||||
@@ -38,16 +75,18 @@ export function monkeyPatchFetch() {
|
||||
reqInfo: RequestInfo,
|
||||
init?: RequestInit,
|
||||
): Promise<Response> {
|
||||
const encodedBody = init && init.body
|
||||
? base64Encode(
|
||||
new Uint8Array(await (new Response(init.body)).arrayBuffer()),
|
||||
)
|
||||
: undefined;
|
||||
// console.log("Encoded this body", encodedBody);
|
||||
const r = await sandboxFetch(
|
||||
reqInfo,
|
||||
init && {
|
||||
method: init.method,
|
||||
headers: init.headers as Record<string, string>,
|
||||
base64Body: init.body
|
||||
? base64Encode(
|
||||
new Uint8Array(await (new Response(init.body)).arrayBuffer()),
|
||||
)
|
||||
: undefined,
|
||||
base64Body: encodedBody,
|
||||
},
|
||||
);
|
||||
return new Response(r.base64Body ? base64Decode(r.base64Body) : null, {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { syscall } from "./syscall.ts";
|
||||
|
||||
export function set(key: string, value: any): Promise<void> {
|
||||
return syscall("clientStore.set", key, value);
|
||||
}
|
||||
|
||||
export function get(key: string): Promise<any> {
|
||||
return syscall("clientStore.get", key);
|
||||
}
|
||||
|
||||
export function del(key: string): Promise<void> {
|
||||
return syscall("clientStore.delete", key);
|
||||
}
|
||||
@@ -3,7 +3,6 @@ export * as index from "./index.ts";
|
||||
export * as markdown from "./markdown.ts";
|
||||
export * as space from "./space.ts";
|
||||
export * as system from "./system.ts";
|
||||
// Legacy redirect, use "store" in $sb/plugos-syscall/mod.ts instead
|
||||
export * as clientStore from "./store.ts";
|
||||
export * as clientStore from "./clientStore.ts";
|
||||
export * as sync from "./sync.ts";
|
||||
export * as debug from "./debug.ts";
|
||||
|
||||
Reference in New Issue
Block a user