kite-x402-hono 0.3.0 → 0.3.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,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.1
4
+
5
+ - Make rejected-body and redirect stream cleanup non-blocking so stalled producer
6
+ cancellation cannot retain a request slot. Preserve original errors when cleanup fails.
7
+ - Cancel unfinished reads, including already-aborted requests, and release reader locks.
8
+ - Add nine regression tests for stalled/rejected cleanup, capacity recovery, aborts
9
+ and byte-preserving successful reads.
10
+
3
11
  ## 0.3.0
4
12
 
5
13
  - Add `FACILITATOR_TIMEOUT_MS` (default 90000, range 1–300000) and validate
package/README.md CHANGED
@@ -164,6 +164,16 @@ settlement before retrying. Slots are released when requests finish, and health
164
164
  checks remain available. Lowering the timeout increases indeterminate outcomes
165
165
  for slow settlements; choose a deadline suitable for your deployment.
166
166
 
167
+ ### Stream cleanup (fixed in 0.3.1)
168
+
169
+ Rejected oversized bodies, refused redirects and aborted reads request stream
170
+ cancellation without waiting for producer cleanup. A stalled or rejected cleanup
171
+ promise cannot hold a concurrency slot or replace the original HTTP error.
172
+ Readers release their locks on success and failure; successful bodies retain their
173
+ original bytes. Cleanup is best-effort: an injected stream producer still owns its
174
+ underlying resources. This does not cancel or undo payment settlement, and does
175
+ not add payment retries.
176
+
167
177
  ### Commands
168
178
 
169
179
  ```sh
package/README.zh-CN.md CHANGED
@@ -136,6 +136,14 @@ Bun.serve({ port: config.port, fetch: app.fetch });
136
136
  不自动重试验证和结算;结果不明时先核对再重试。请求结束后释放并发名额,健康检查保持可用。
137
137
  超时设得过短会增加结算结果不明的情况,应按实际部署选择。
138
138
 
139
+ ### 数据流清理(0.3.1 修复)
140
+
141
+ 拒绝超大请求/响应、拒绝上游重定向或取消读取时,会发起数据流取消,
142
+ 但不会等待生产者完成清理。清理卡住或报错不会占住并发名额,也不会覆盖原始错误。
143
+ 读取器在成功和失败时均释放锁,正常响应内容保持不变。
144
+ 清理属于尽力执行:自定义流生产者仍须管理底层资源。
145
+ 此改动不会取消或撤销付款结算,也不增加付款重试。
146
+
139
147
  ### 检查命令
140
148
 
141
149
  ```sh
package/dist/proxy.js CHANGED
@@ -44,7 +44,13 @@ export async function readLimited(stream, limit, signal) {
44
44
  if (!stream)
45
45
  return new Uint8Array();
46
46
  const reader = stream.getReader();
47
+ let completed = false;
48
+ let cancelled = false;
47
49
  const cancel = () => {
50
+ if (cancelled)
51
+ return;
52
+ cancelled = true;
53
+ // Cleanup belongs to the stream producer and must not block a failure response.
48
54
  void reader.cancel().catch(() => { });
49
55
  };
50
56
  signal?.addEventListener("abort", cancel, { once: true });
@@ -55,11 +61,12 @@ export async function readLimited(stream, limit, signal) {
55
61
  signal?.throwIfAborted();
56
62
  const { done, value } = await reader.read();
57
63
  signal?.throwIfAborted();
58
- if (done)
64
+ if (done) {
65
+ completed = true;
59
66
  break;
67
+ }
60
68
  size += value.byteLength;
61
69
  if (size > limit) {
62
- await reader.cancel();
63
70
  throw new BodyTooLarge();
64
71
  }
65
72
  chunks.push(value);
@@ -67,6 +74,8 @@ export async function readLimited(stream, limit, signal) {
67
74
  }
68
75
  finally {
69
76
  signal?.removeEventListener("abort", cancel);
77
+ if (!completed)
78
+ cancel();
70
79
  reader.releaseLock();
71
80
  }
72
81
  const output = new Uint8Array(size);
@@ -121,7 +130,7 @@ export async function proxy(request, config, fetchUpstream) {
121
130
  }));
122
131
  // No redirect following: an upstream cannot forward the injected credential to another host.
123
132
  if (upstream.status >= 300 && upstream.status < 400) {
124
- await upstream.body?.cancel();
133
+ void upstream.body?.cancel().catch(() => { });
125
134
  return Response.json({ error: "upstream_redirect_refused" }, { status: 502 });
126
135
  }
127
136
  const bytes = await readLimited(upstream.body, config.maxBodyBytes, controller.signal);
package/docs/week-4.md ADDED
@@ -0,0 +1,38 @@
1
+ # Week 4: non-blocking stream cleanup
2
+
3
+ Continue the Kite x402 Bun/Hono wrapper in the same repository.
4
+
5
+ ## Problem
6
+
7
+ Version 0.3.0 awaited stream cancellation when rejecting oversized bodies and
8
+ upstream redirects. An asynchronous producer cleanup that never resolves could
9
+ hold the entire request and concurrency slot indefinitely. A rejecting cleanup
10
+ could also replace the intended error. Already-aborted reads released the reader
11
+ lock but did not cancel the source.
12
+
13
+ ## Change and evidence
14
+
15
+ Initiate cancellation without awaiting producer cleanup, handle cleanup rejection,
16
+ cancel unfinished reads once, and always release the reader lock. Successful
17
+ reads keep their exact bytes and do not cancel completed streams.
18
+
19
+ Nine regression tests cover pending and rejecting cancellation for oversized
20
+ requests, oversized responses and redirects; they verify original HTTP errors,
21
+ no settlement or payment receipt, health availability and capacity recovery using
22
+ an unpaid challenge. Additional cases cover already-aborted reads, abort during a
23
+ pending read, and successful multi-chunk reads at the exact size limit.
24
+
25
+ Against the previous source, seven of these nine tests fail. With the fix all nine
26
+ pass. Test payment verification is simulated; no real on-chain payment is claimed.
27
+ The fix is included in version 0.3.1; registry publication is verified separately.
28
+
29
+ Cancellation is best-effort, not proof the producer released external resources.
30
+ An injected stream must manage its own resources. This change neither aborts an
31
+ in-flight settlement nor retries payment or undoes an upstream action.
32
+
33
+ ## 可提交说明
34
+
35
+ 第四周修复数据流清理阻塞请求的问题:超大请求/响应和上游重定向被拒绝时,
36
+ 发起取消但不等待生产者清理,防止清理挂起占住并发名额;清理异常不再覆盖原始错误。
37
+ 补齐已取消请求的流清理,确保读取器释放锁。新增 9 项回归测试,覆盖挂起/失败清理、
38
+ 原始错误保留、不结算、不返回付款收据、健康检查、并发恢复以及正常响应字节完整性。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kite-x402-hono",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Bun and Hono reverse proxy for x402 payments on Kite",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",