@domain.js/main 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,12 +46,37 @@ function Router(deps) {
46
46
  const send = async (res, results, isEventStream = false) => {
47
47
  if ((0, utils_1.isStream)(results)) {
48
48
  if (isEventStream) {
49
- res.type("text/event-stream");
49
+ // 告知 Fastify 由我们自行通过 res.raw 发送响应,避免 handler 返回后框架再发一次
50
+ res.hijack();
51
+ // 使用 res.raw 直接写响应时,Fastify 不会把 reply.type/header 同步到 raw,
52
+ // 必须在 Node 原生 response 上设置头,否则 Content-Type 等不会生效。
53
+ res.raw.setHeader("Content-Type", "text/event-stream");
54
+ res.raw.setHeader("Cache-Control", "no-cache");
55
+ res.raw.setHeader("Pragma", "no-cache");
56
+ res.raw.setHeader("Connection", "keep-alive");
57
+ if (typeof res.raw.flushHeaders === "function") {
58
+ res.raw.flushHeaders();
59
+ }
50
60
  await new Promise((resolve) => {
51
- results.on("data", (chunk) => {
61
+ const onEnd = () => {
62
+ cleanup();
63
+ resolve();
64
+ };
65
+ const onError = () => {
66
+ cleanup();
67
+ resolve();
68
+ };
69
+ const cleanup = () => {
70
+ results.off("data", onData);
71
+ results.off("end", onEnd);
72
+ results.off("error", onError);
73
+ };
74
+ const onData = (chunk) => {
52
75
  res.raw.write(chunk);
53
- });
54
- results.on("end", resolve);
76
+ };
77
+ results.on("data", onData);
78
+ results.on("end", onEnd);
79
+ results.on("error", onError);
55
80
  });
56
81
  }
57
82
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {