Files
discord-clone/node_modules/unenv/runtime/fetch/call.mjs
2023-01-03 09:29:04 -06:00

35 lines
1.2 KiB
JavaScript

import { IncomingMessage } from "../node/http/_request.mjs";
import { ServerResponse } from "../node/http/_response.mjs";
export function createCall(handle) {
return function callHandle(context) {
const req = new IncomingMessage();
const res = new ServerResponse(req);
req.url = context.url || "/";
req.method = context.method || "GET";
req.headers = {};
if (context.headers) {
const headerEntries = typeof context.headers.entries === "function" ? context.headers.entries() : Object.entries(context.headers);
for (const [name, value] of headerEntries) {
if (!value) {
continue;
}
req.headers[name.toLowerCase()] = value;
}
}
req.headers.host = req.headers.host || context.host || "localhost";
req.connection.encrypted = req.connection.encrypted || context.protocol === "https";
req.body = context.body || null;
return handle(req, res).then(() => {
const r = {
body: res._data || "",
headers: res._headers,
status: res.statusCode,
statusText: res.statusMessage
};
req.destroy();
res.destroy();
return r;
});
};
}