@jini-ai/http-kit 0.6.12 → 0.6.15-rc.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @jini-ai/http-kit
2
2
 
3
+ ## 0.6.13
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [7479159]
8
+ - Updated dependencies [c31264d]
9
+ - Updated dependencies [0728914]
10
+ - Updated dependencies [cbce255]
11
+ - Updated dependencies [6ecbdb2]
12
+ - Updated dependencies [63bacc5]
13
+ - @jini-ai/core@0.6.13
14
+ - @jini-ai/protocol@0.6.13
15
+
3
16
  ## 0.6.12
4
17
 
5
18
  ### Patch Changes
package/dist/sse.d.ts CHANGED
@@ -55,7 +55,7 @@ export interface CreateSseChannelOptions<E extends SseEvent> {
55
55
  }
56
56
  export interface SseChannel<E extends SseEvent> {
57
57
  /**
58
- * Queues `event` for delivery. A no-op once the channel is closed. Before
58
+ * Queues `event` for delivery. A no-op once ending is requested or the channel is closed. Before
59
59
  * {@link SseChannel.open} is called, events accumulate in the same bounded
60
60
  * queue used for later backpressure, so a caller may safely `enqueue`
61
61
  * replay history before opening the stream — the client's very first
@@ -73,7 +73,8 @@ export interface SseChannel<E extends SseEvent> {
73
73
  /** True once the channel has closed for any reason (explicit `end()`, client disconnect, queue overflow, or a fatal write). */
74
74
  readonly isClosed: () => boolean;
75
75
  /**
76
- * Idempotent: marks the channel closed, unsubscribes nothing itself (a
76
+ * Idempotent: stops accepting events and drains an open channel's queued events before
77
+ * marking the channel closed. Before open(), it closes immediately. It unsubscribes nothing itself (a
77
78
  * caller's {@link SseChannel.onClose} callback does that), and ends the
78
79
  * underlying HTTP response if it has not already ended. Calling `res.end()`
79
80
  * a second time (e.g. after the client already disconnected) is safe and
package/dist/sse.js CHANGED
@@ -47,6 +47,7 @@ export function createSseChannel({ res }, options = {}) {
47
47
  let flowing = false;
48
48
  let writable = true;
49
49
  let closed = false;
50
+ let ending = false;
50
51
  const closeCallbacks = [];
51
52
  const markClosed = () => {
52
53
  if (closed)
@@ -58,11 +59,21 @@ export function createSseChannel({ res }, options = {}) {
58
59
  for (const callback of callbacks)
59
60
  callback();
60
61
  };
61
- const end = () => {
62
+ /** Close immediately for terminal events, overflow or a broken transport. */
63
+ const finish = () => {
62
64
  markClosed();
63
65
  if (!res.writableEnded)
64
66
  res.end();
65
67
  };
68
+ /** Stop production now; an open backlog finishes on drain. O(1) excluding close callbacks. */
69
+ const end = () => {
70
+ if (closed)
71
+ return;
72
+ ending = true;
73
+ if (flowing && queue.length > 0)
74
+ return;
75
+ finish();
76
+ };
66
77
  const pump = () => {
67
78
  if (!flowing || closed)
68
79
  return;
@@ -76,23 +87,25 @@ export function createSseChannel({ res }, options = {}) {
76
87
  // A dead/broken transport must never throw back through whatever is producing events —
77
88
  // stop this channel only.
78
89
  options.onWriteError?.({ error });
79
- end();
90
+ finish();
80
91
  return;
81
92
  }
82
93
  if (wroteOk === false)
83
94
  writable = false;
84
95
  if (options.isEndEvent?.({ event })) {
85
- end();
96
+ finish();
86
97
  return;
87
98
  }
88
99
  }
100
+ if (ending)
101
+ end();
89
102
  };
90
103
  const enqueue = ({ event }) => {
91
- if (closed)
104
+ if (closed || ending)
92
105
  return;
93
106
  if (queue.length >= maxQueuedEvents) {
94
107
  // Slow/stalled consumer — disconnect rather than grow memory without bound.
95
- end();
108
+ finish();
96
109
  return;
97
110
  }
98
111
  queue.push(event);
@@ -71,6 +71,18 @@ export function normalizeOriginCandidate({ rawUrl }) {
71
71
  function stripTrailingDot(host) {
72
72
  return host.endsWith(".") ? host.slice(0, -1) : host;
73
73
  }
74
+ /** Normalize a bare allowlisted host through the candidate's WHATWG/IDNA policy.
75
+ * URL delimiters and ports are not host entries; colons are valid only inside bracketed IPv6.
76
+ * Returns null for invalid entries; pure, O(host.length) time and space.
77
+ */
78
+ function normalizeAllowlistedHost({ host }, _optional = {}) {
79
+ const value = host.trim();
80
+ if (/[/\\?#@%]/u.test(value))
81
+ return null;
82
+ if (value.includes(":") && !value.endsWith("]"))
83
+ return null;
84
+ return normalizeOriginCandidate({ rawUrl: `https://${value}` })?.host ?? null;
85
+ }
74
86
  function effectivePort(origin) {
75
87
  return origin.port ?? (origin.scheme === "https" ? 443 : 80);
76
88
  }
@@ -138,7 +150,7 @@ export class OriginRegistry {
138
150
  if (target.scheme !== "https")
139
151
  return false;
140
152
  const allowlist = await loadAllowlist(ctx.workspaceId);
141
- const allowSet = new Set(allowlist.map((host) => stripTrailingDot(host.trim().toLowerCase())));
153
+ const allowSet = new Set(allowlist.map((host) => normalizeAllowlistedHost({ host })));
142
154
  return allowSet.has(target.host);
143
155
  }
144
156
  catch {
@@ -88,17 +88,20 @@ function resolveDotSegments(path) {
88
88
  * relative to a real origin, etc. are exactly what `new URL()` already did before that caller
89
89
  * reaches this. Do not export this directly.
90
90
  *
91
- * Order is load-bearing: decode ONCE, then resolve dot segments, then compare the first segment
92
- * case-folded. Decoding first is what catches `/%61dmin` (over-refusal for Express and browsers, a
93
- * real match for a decoding proxy — see the module doc); resolving dot segments after the decode is
91
+ * Check the undecoded first segment too: Express mounts `/admin/..%2fpublic` under `/admin`,
92
+ * even though decoding and resolving dot segments produces `/public`.
93
+ * Order is load-bearing for the decoded check: decode ONCE, then resolve dot segments, then compare
94
+ * the first segment case-folded. Decoding first is what catches `/%61dmin` (over-refusal for Express
95
+ * and browsers, a real match for a decoding proxy — see the module doc); resolving dot segments after the decode is
94
96
  * what catches `/blog/%2e%2e/admin`. Exactly one decode pass models one decoding intermediary:
95
97
  * `/%2561dmin` would need two, so it stays allowed.
96
98
  *
97
- * @param pathname - An already-parsed `URL`'s `.pathname` (no query, no fragment).
99
+ * @param required.pathname - An already-parsed `URL`'s `.pathname` (no query, no fragment).
100
+ * @param required.reservedSegments - First segments reserved by the host, stored case-folded.
98
101
  * @returns `{ kind: "ok" }` when it is ordinary site content; otherwise the arm naming the refusal.
99
102
  * @complexity O(n) in the pathname length.
100
103
  */
101
- function checkDecodedSitePathname(pathname, reservedSegments) {
104
+ function checkDecodedSitePathname({ pathname, reservedSegments }, _optional = {}) {
102
105
  let decoded;
103
106
  try {
104
107
  decoded = decodeURIComponent(pathname);
@@ -108,6 +111,9 @@ function checkDecodedSitePathname(pathname, reservedSegments) {
108
111
  }
109
112
  if (decoded.includes("\\") || hasControlCharacter({ value: decoded }, {}))
110
113
  return { kind: "disallowed-character" };
114
+ const rawFirst = pathname.slice(1).split("/", 1).join("").toLowerCase();
115
+ if (reservedSegments.has(rawFirst))
116
+ return { kind: "reserved", surface: rawFirst };
111
117
  // `resolveDotSegments` always returns a path with a leading "/", so the first segment is
112
118
  // everything after it up to the next "/" — `split(..., 1)` yields exactly that one element.
113
119
  const first = resolveDotSegments(decoded).slice(1).split("/", 1).join("").toLowerCase();
@@ -141,7 +147,7 @@ function checkDecodedSitePathname(pathname, reservedSegments) {
141
147
  * @example checkSitePathname({ url: new URL("/%61dmin/settings", "http://x.invalid"), reservedSegments: new Set(["admin", "api"]) }, {}); // => { kind: "reserved", surface: "admin" }
142
148
  */
143
149
  export function checkSitePathname({ url, reservedSegments }, _optional = {}) {
144
- return checkDecodedSitePathname(url.pathname, reservedSegments);
150
+ return checkDecodedSitePathname({ pathname: url.pathname, reservedSegments }, {});
145
151
  }
146
152
  /**
147
153
  * Classify a raw, site-relative reference (a stored redirect target, a caller-supplied path)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jini-ai/http-kit",
3
- "version": "0.6.12",
3
+ "version": "0.6.15-rc.1",
4
4
  "description": "Generic Express transport primitives, request parsing, response serialization, origin guards and route-pack mounting.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -71,16 +71,16 @@
71
71
  "@types/express": "^4.17.21",
72
72
  "@vitest/coverage-v8": "^2.1.9",
73
73
  "express": "^4.21.0",
74
- "@jini-ai/core": "0.6.12",
75
- "@jini-ai/platform": "0.6.12",
76
- "@jini-ai/protocol": "0.6.12"
74
+ "@jini-ai/protocol": "0.6.15-rc.1",
75
+ "@jini-ai/core": "0.6.15-rc.1",
76
+ "@jini-ai/platform": "0.6.15-rc.1"
77
77
  },
78
78
  "dependencies": {
79
- "@jini-ai/protocol": "^0.6.12",
80
- "@jini-ai/core": "^0.6.12"
79
+ "@jini-ai/core": "^0.6.15-rc.1",
80
+ "@jini-ai/protocol": "^0.6.15-rc.1"
81
81
  },
82
82
  "peerDependencies": {
83
- "@jini-ai/platform": "^0.6.10",
83
+ "@jini-ai/platform": "^0.6.15-rc.1",
84
84
  "express": "^4.21.0 || ^5.0.0"
85
85
  },
86
86
  "peerDependenciesMeta": {