kernel / core / mind-worker / src / library / connection.tsmain at 949894f
import { RESERVED_SEGMENT_NAMES } from "iterate/expression";
/** Prototype members are what Workers RPC and capnweb traverse. A reserved name such as `then` is
* skipped: a thenable connection would be adopted as a promise and never settle. */
export function subclassWithMethods<Base extends abstract new (...args: never[]) => object>(
base: Base,
names: string[],
call: (self: InstanceType<Base>, name: string, input: unknown) => unknown,
): Base {
const Subclass = class extends (base as abstract new (...args: never[]) => object) {};
for (const name of names) {
if (
RESERVED_SEGMENT_NAMES.has(name) ||
name in Subclass.prototype ||
!/^[A-Za-z_$][\w$]*$/.test(name)
)
continue;
Object.defineProperty(Subclass.prototype, name, {
value(this: InstanceType<Base>, input?: unknown) {
return call(this, name, input);
},
writable: true,
configurable: true,
});
}
return Subclass as unknown as Base;
}
export async function responseRefusal(response: Response, what: string): Promise<Error> {
const reader = response.body?.getReader();
let snippet = "";
if (reader) {
const decoder = new TextDecoder();
try {
while (snippet.length < 300) {
const { done, value } = await reader.read();
if (done) break;
snippet += decoder.decode(value, { stream: true });
}
} catch {
/* a body that cannot be read adds nothing to the refusal */
} finally {
reader.cancel().catch(() => undefined);
}
snippet = snippet.slice(0, 300);
}
return new Error(`${what} returned ${response.status}${snippet ? `: ${snippet}` : ""}`);
}
export async function refuseUnlessOk(response: Response, what: string): Promise<Response> {
if (response.ok) return response;
throw await responseRefusal(response, what);
}