@blamejs/core 0.5.9 → 0.5.10

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.
package/CHANGELOG.md CHANGED
@@ -8,6 +8,7 @@ upgrading across more than a few patches at a time.
8
8
 
9
9
  ## v0.5.x
10
10
 
11
+ - **0.5.9** (2026-04-30) — b.csv: RFC 4180 parser + serializer
11
12
  - **0.5.8** (2026-04-30) — b.uuid: RFC 4122 v4 + RFC 9562 v7
12
13
  - **0.5.7** (2026-04-30) — defensive validation + queue closure capture + audit context
13
14
  - **0.5.6** (2026-04-30) — break-glass: trustProxy honored, cache require hoisted
@@ -30,6 +30,7 @@ module.exports = {
30
30
  health: require("./health").create,
31
31
  compression: require("./compression").create,
32
32
  cspNonce: require("./csp-nonce").create,
33
+ sse: require("./sse").create,
33
34
  apiEncrypt: require("./api-encrypt"),
34
35
 
35
36
  // Module exports for advanced use (constants, raw factory access)
@@ -47,6 +48,7 @@ module.exports = {
47
48
  health: require("./health"),
48
49
  compression: require("./compression"),
49
50
  cspNonce: require("./csp-nonce"),
51
+ sse: require("./sse"),
50
52
  apiEncrypt: require("./api-encrypt"),
51
53
  },
52
54
  };
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ /**
3
+ * sse — Server-Sent Events middleware. One-way streaming from server
4
+ * to browser over a single HTTP response with `Content-Type:
5
+ * text/event-stream`. Browsers reconnect automatically with the
6
+ * `Last-Event-ID` header so the operator's handler can resume from
7
+ * the last delivered event.
8
+ *
9
+ * Use case: live dashboards, log tailing, progress updates, real-time
10
+ * counters. SSE is the right tool when the server pushes and the
11
+ * client doesn't need to send anything back. For bidirectional, use
12
+ * `b.websocket`.
13
+ *
14
+ * router.get("/events", b.middleware.sse(async function (channel, req) {
15
+ * channel.send({ id: 1, event: "tick", data: { count: 1 } });
16
+ * channel.send({ id: 2, event: "tick", data: { count: 2 } });
17
+ * // resume support — read req.headers["last-event-id"] and
18
+ * // resume from that point.
19
+ * }));
20
+ *
21
+ * `channel`:
22
+ * send({ id?, event?, data, retry? }) — emit one SSE message
23
+ * ping(comment?) — emit a comment line (keeps
24
+ * intermediate proxies happy)
25
+ * close() — end the stream
26
+ * onAbort(fn) — register cleanup when the
27
+ * client disconnects (browser
28
+ * tab close, network drop)
29
+ *
30
+ * Heartbeat: the middleware sends a comment line every `heartbeatMs`
31
+ * (default 15s) automatically so corporate proxies / Heroku-style
32
+ * idle-timeouts don't kill the stream. Operators with strict
33
+ * deployments override `heartbeatMs: false` to disable.
34
+ *
35
+ * Compression: SSE streams typically should NOT be compressed —
36
+ * `b.middleware.compression` skips `text/event-stream` by default.
37
+ */
38
+ var C = require("../constants");
39
+ var requestHelpers = require("../request-helpers");
40
+ var validateOpts = require("../validate-opts");
41
+
42
+ var DEFAULT_HEARTBEAT_MS = C.TIME.seconds(15);
43
+
44
+ function _formatEvent(msg) {
45
+ // RFC 6455... wait, that's WebSocket. SSE: WHATWG HTML §9.2.5.
46
+ // Lines: "id: <n>\n", "event: <name>\n", "data: <line>\n" (multi-line
47
+ // data is multiple "data: " lines), "retry: <ms>\n", blank line ends.
48
+ var out = "";
49
+ if (msg.id !== undefined && msg.id !== null) out += "id: " + String(msg.id).replace(/[\r\n]/g, "") + "\n";
50
+ if (msg.event) out += "event: " + String(msg.event).replace(/[\r\n]/g, "") + "\n";
51
+ if (msg.retry !== undefined && msg.retry !== null) {
52
+ if (typeof msg.retry !== "number" || !isFinite(msg.retry) || msg.retry < 0) {
53
+ throw new Error("sse: retry must be a non-negative finite number of milliseconds");
54
+ }
55
+ out += "retry: " + Math.floor(msg.retry) + "\n";
56
+ }
57
+ var dataStr;
58
+ if (msg.data === undefined || msg.data === null) dataStr = "";
59
+ else if (typeof msg.data === "string") dataStr = msg.data;
60
+ else dataStr = JSON.stringify(msg.data);
61
+ // Multi-line data → one `data:` line per source line (per spec).
62
+ var lines = dataStr.split(/\r?\n/);
63
+ for (var i = 0; i < lines.length; i++) out += "data: " + lines[i] + "\n";
64
+ out += "\n"; // dispatch
65
+ return out;
66
+ }
67
+
68
+ function create(handler, opts) {
69
+ if (typeof handler !== "function") {
70
+ throw new Error("middleware.sse: handler must be a function (channel, req) => ...");
71
+ }
72
+ opts = opts || {};
73
+ validateOpts(opts, ["heartbeatMs", "headers"], "middleware.sse");
74
+ var heartbeatMs = opts.heartbeatMs === false ? 0
75
+ : (opts.heartbeatMs != null ? opts.heartbeatMs : DEFAULT_HEARTBEAT_MS);
76
+ if (heartbeatMs !== 0 && (typeof heartbeatMs !== "number" || !isFinite(heartbeatMs) || heartbeatMs <= 0)) {
77
+ throw new Error("middleware.sse: heartbeatMs must be a positive finite number or false");
78
+ }
79
+ var extraHeaders = opts.headers || {};
80
+
81
+ return async function sseMiddleware(req, res) {
82
+ if (typeof res.writeHead !== "function" || typeof res.write !== "function") {
83
+ // Not an http.ServerResponse — operator wired this onto something
84
+ // unusual. Fail closed rather than silently dropping the handler.
85
+ throw new Error("middleware.sse: res does not support writeHead/write — wire SSE only on HTTP routes");
86
+ }
87
+ var headers = Object.assign({
88
+ "Content-Type": "text/event-stream; charset=utf-8",
89
+ "Cache-Control": "no-cache, no-transform",
90
+ "Connection": "keep-alive",
91
+ // Disable nginx response buffering when terminating behind it.
92
+ "X-Accel-Buffering": "no",
93
+ }, extraHeaders);
94
+ // Append Vary: Accept so a proxy doesn't serve a cached non-SSE
95
+ // response on the same URL to a future client.
96
+ res.writeHead(200, headers);
97
+ requestHelpers.appendVary(res, "Accept");
98
+ // Initial flush — some proxies hold the headers until first byte.
99
+ res.write(":\n\n");
100
+
101
+ var closed = false;
102
+ var heartbeatTimer = null;
103
+ var abortHandlers = [];
104
+
105
+ function _scheduleHeartbeat() {
106
+ if (heartbeatMs === 0) return;
107
+ heartbeatTimer = setTimeout(function () {
108
+ if (closed) return;
109
+ try { res.write(": heartbeat\n\n"); } catch (_e) { /* socket closed */ }
110
+ _scheduleHeartbeat();
111
+ }, heartbeatMs);
112
+ if (typeof heartbeatTimer.unref === "function") heartbeatTimer.unref();
113
+ }
114
+ _scheduleHeartbeat();
115
+
116
+ var channel = {
117
+ send: function (msg) {
118
+ if (closed) return false;
119
+ try { res.write(_formatEvent(msg || {})); return true; }
120
+ catch (_e) { return false; }
121
+ },
122
+ ping: function (comment) {
123
+ if (closed) return false;
124
+ var safe = comment ? String(comment).replace(/[\r\n]/g, " ") : "ping";
125
+ try { res.write(": " + safe + "\n\n"); return true; }
126
+ catch (_e) { return false; }
127
+ },
128
+ close: function () {
129
+ if (closed) return;
130
+ closed = true;
131
+ if (heartbeatTimer) { clearTimeout(heartbeatTimer); heartbeatTimer = null; }
132
+ try { res.end(); } catch (_e) { /* already ended */ }
133
+ },
134
+ onAbort: function (fn) {
135
+ if (typeof fn === "function") abortHandlers.push(fn);
136
+ },
137
+ get closed() { return closed; },
138
+ };
139
+
140
+ function _onClose() {
141
+ if (closed) return;
142
+ closed = true;
143
+ if (heartbeatTimer) { clearTimeout(heartbeatTimer); heartbeatTimer = null; }
144
+ for (var i = 0; i < abortHandlers.length; i++) {
145
+ try { abortHandlers[i](); } catch (_e) { /* operator handler error — drop */ }
146
+ }
147
+ }
148
+ res.once("close", _onClose);
149
+ res.once("error", _onClose);
150
+ if (req && typeof req.once === "function") req.once("aborted", _onClose);
151
+
152
+ try {
153
+ await handler(channel, req);
154
+ } catch (e) {
155
+ _onClose();
156
+ try { res.end(); } catch (_ignored) { /* */ }
157
+ throw e;
158
+ }
159
+ };
160
+ }
161
+
162
+ module.exports = {
163
+ create: create,
164
+ _formatEvent: _formatEvent, // test-only export
165
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blamejs/core",
3
- "version": "0.5.9",
3
+ "version": "0.5.10",
4
4
  "description": "The Node framework that owns its stack.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "blamejs contributors",