Files
SnowLuma/.pnpm-store/v11/files/7c/2cbc0b12d3f43c1da8f0c0cf962eaf5da6dc475ac5c9faddaf0ad92dabc75df787325e881c8e9461bf81b489a312df4c0fca7784b9eed623d9fd08e1644aa1
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

66 lines
1.6 KiB
Plaintext

import type { SerovalNode } from 'seroval';
import { createPlugin } from 'seroval';
import HeadersPlugin from './headers';
import ReadableStreamPlugin from './readable-stream';
function createResponseOptions(current: Response): ResponseInit {
return {
headers: current.headers,
status: current.status,
statusText: current.statusText,
};
}
type ResponseNode = {
body: SerovalNode;
options: SerovalNode;
};
const ResponsePlugin = /* @__PURE__ */ createPlugin<Response, ResponseNode>({
tag: 'seroval-plugins/web/Response',
extends: [ReadableStreamPlugin, HeadersPlugin],
test(value) {
if (typeof Response === 'undefined') {
return false;
}
return value instanceof Response;
},
parse: {
async async(value, ctx) {
return {
body: await ctx.parse(
value.body && !value.bodyUsed
? await value.clone().arrayBuffer()
: null,
),
options: await ctx.parse(createResponseOptions(value)),
};
},
stream(value, ctx) {
return {
body: ctx.parse(
value.body && !value.bodyUsed ? value.clone().body : null,
),
options: ctx.parse(createResponseOptions(value)),
};
},
},
serialize(node, ctx) {
return (
'new Response(' +
ctx.serialize(node.body) +
',' +
ctx.serialize(node.options) +
')'
);
},
deserialize(node, ctx) {
return new Response(
ctx.deserialize(node.body) as BodyInit,
ctx.deserialize(node.options) as ResponseInit,
);
},
});
export default ResponsePlugin;