Aktueller Stand
This commit is contained in:
10
backend/node_modules/hono/dist/helper/streaming/index.js
generated
vendored
Normal file
10
backend/node_modules/hono/dist/helper/streaming/index.js
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
// src/helper/streaming/index.ts
|
||||
import { stream } from "./stream.js";
|
||||
import { streamSSE, SSEStreamingApi } from "./sse.js";
|
||||
import { streamText } from "./text.js";
|
||||
export {
|
||||
SSEStreamingApi,
|
||||
stream,
|
||||
streamSSE,
|
||||
streamText
|
||||
};
|
||||
62
backend/node_modules/hono/dist/helper/streaming/sse.js
generated
vendored
Normal file
62
backend/node_modules/hono/dist/helper/streaming/sse.js
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
// src/helper/streaming/sse.ts
|
||||
import { HtmlEscapedCallbackPhase, resolveCallback } from "../../utils/html.js";
|
||||
import { StreamingApi } from "../../utils/stream.js";
|
||||
import { isOldBunVersion } from "./utils.js";
|
||||
var SSEStreamingApi = class extends StreamingApi {
|
||||
constructor(writable, readable) {
|
||||
super(writable, readable);
|
||||
}
|
||||
async writeSSE(message) {
|
||||
const data = await resolveCallback(message.data, HtmlEscapedCallbackPhase.Stringify, false, {});
|
||||
const dataLines = data.split("\n").map((line) => {
|
||||
return `data: ${line}`;
|
||||
}).join("\n");
|
||||
const sseData = [
|
||||
message.event && `event: ${message.event}`,
|
||||
dataLines,
|
||||
message.id && `id: ${message.id}`,
|
||||
message.retry && `retry: ${message.retry}`
|
||||
].filter(Boolean).join("\n") + "\n\n";
|
||||
await this.write(sseData);
|
||||
}
|
||||
};
|
||||
var run = async (stream, cb, onError) => {
|
||||
try {
|
||||
await cb(stream);
|
||||
} catch (e) {
|
||||
if (e instanceof Error && onError) {
|
||||
await onError(e, stream);
|
||||
await stream.writeSSE({
|
||||
event: "error",
|
||||
data: e.message
|
||||
});
|
||||
} else {
|
||||
console.error(e);
|
||||
}
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
};
|
||||
var contextStash = /* @__PURE__ */ new WeakMap();
|
||||
var streamSSE = (c, cb, onError) => {
|
||||
const { readable, writable } = new TransformStream();
|
||||
const stream = new SSEStreamingApi(writable, readable);
|
||||
if (isOldBunVersion()) {
|
||||
c.req.raw.signal.addEventListener("abort", () => {
|
||||
if (!stream.closed) {
|
||||
stream.abort();
|
||||
}
|
||||
});
|
||||
}
|
||||
contextStash.set(stream.responseReadable, c);
|
||||
c.header("Transfer-Encoding", "chunked");
|
||||
c.header("Content-Type", "text/event-stream");
|
||||
c.header("Cache-Control", "no-cache");
|
||||
c.header("Connection", "keep-alive");
|
||||
run(stream, cb, onError);
|
||||
return c.newResponse(stream.responseReadable);
|
||||
};
|
||||
export {
|
||||
SSEStreamingApi,
|
||||
streamSSE
|
||||
};
|
||||
34
backend/node_modules/hono/dist/helper/streaming/stream.js
generated
vendored
Normal file
34
backend/node_modules/hono/dist/helper/streaming/stream.js
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
// src/helper/streaming/stream.ts
|
||||
import { StreamingApi } from "../../utils/stream.js";
|
||||
import { isOldBunVersion } from "./utils.js";
|
||||
var contextStash = /* @__PURE__ */ new WeakMap();
|
||||
var stream = (c, cb, onError) => {
|
||||
const { readable, writable } = new TransformStream();
|
||||
const stream2 = new StreamingApi(writable, readable);
|
||||
if (isOldBunVersion()) {
|
||||
c.req.raw.signal.addEventListener("abort", () => {
|
||||
if (!stream2.closed) {
|
||||
stream2.abort();
|
||||
}
|
||||
});
|
||||
}
|
||||
contextStash.set(stream2.responseReadable, c);
|
||||
(async () => {
|
||||
try {
|
||||
await cb(stream2);
|
||||
} catch (e) {
|
||||
if (e === void 0) {
|
||||
} else if (e instanceof Error && onError) {
|
||||
await onError(e, stream2);
|
||||
} else {
|
||||
console.error(e);
|
||||
}
|
||||
} finally {
|
||||
stream2.close();
|
||||
}
|
||||
})();
|
||||
return c.newResponse(stream2.responseReadable);
|
||||
};
|
||||
export {
|
||||
stream
|
||||
};
|
||||
12
backend/node_modules/hono/dist/helper/streaming/text.js
generated
vendored
Normal file
12
backend/node_modules/hono/dist/helper/streaming/text.js
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
// src/helper/streaming/text.ts
|
||||
import { TEXT_PLAIN } from "../../context.js";
|
||||
import { stream } from "./index.js";
|
||||
var streamText = (c, cb, onError) => {
|
||||
c.header("Content-Type", TEXT_PLAIN);
|
||||
c.header("X-Content-Type-Options", "nosniff");
|
||||
c.header("Transfer-Encoding", "chunked");
|
||||
return stream(c, cb, onError);
|
||||
};
|
||||
export {
|
||||
streamText
|
||||
};
|
||||
13
backend/node_modules/hono/dist/helper/streaming/utils.js
generated
vendored
Normal file
13
backend/node_modules/hono/dist/helper/streaming/utils.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
// src/helper/streaming/utils.ts
|
||||
var isOldBunVersion = () => {
|
||||
const version = typeof Bun !== "undefined" ? Bun.version : void 0;
|
||||
if (version === void 0) {
|
||||
return false;
|
||||
}
|
||||
const result = version.startsWith("1.1") || version.startsWith("1.0") || version.startsWith("0.");
|
||||
isOldBunVersion = () => result;
|
||||
return result;
|
||||
};
|
||||
export {
|
||||
isOldBunVersion
|
||||
};
|
||||
Reference in New Issue
Block a user