@bowl42/keywire 0.0.0-stage → 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.
Files changed (384) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +118 -2
  3. package/dist/app.d.ts +2 -0
  4. package/dist/app.js +13 -0
  5. package/dist/app.js.map +1 -0
  6. package/dist/bytes.d.ts +1 -0
  7. package/dist/bytes.js +7 -0
  8. package/dist/bytes.js.map +1 -0
  9. package/dist/client.d.ts +66 -0
  10. package/dist/client.js +846 -0
  11. package/dist/client.js.map +1 -0
  12. package/dist/compress/block.d.ts +51 -0
  13. package/dist/compress/block.js +667 -0
  14. package/dist/compress/block.js.map +1 -0
  15. package/dist/compress/deflate.d.ts +7 -0
  16. package/dist/compress/deflate.js +603 -0
  17. package/dist/compress/deflate.js.map +1 -0
  18. package/dist/compress/index.d.ts +6 -0
  19. package/dist/compress/index.js +13 -0
  20. package/dist/compress/index.js.map +1 -0
  21. package/dist/compress/inflate.d.ts +21 -0
  22. package/dist/compress/inflate.js +380 -0
  23. package/dist/compress/inflate.js.map +1 -0
  24. package/dist/compress/wasm.d.ts +50 -0
  25. package/dist/compress/wasm.js +192 -0
  26. package/dist/compress/wasm.js.map +1 -0
  27. package/dist/compress/zlib-wasm.d.ts +15 -0
  28. package/dist/compress/zlib-wasm.js +18 -0
  29. package/dist/compress/zlib-wasm.js.map +1 -0
  30. package/dist/conn.d.ts +126 -0
  31. package/dist/conn.js +685 -0
  32. package/dist/conn.js.map +1 -0
  33. package/dist/dial/index.d.ts +3 -0
  34. package/dist/dial/index.js +10 -0
  35. package/dist/dial/index.js.map +1 -0
  36. package/dist/discovery/index.d.ts +16 -0
  37. package/dist/discovery/index.js +173 -0
  38. package/dist/discovery/index.js.map +1 -0
  39. package/dist/discovery/reach.d.ts +4 -0
  40. package/dist/discovery/reach.js +36 -0
  41. package/dist/discovery/reach.js.map +1 -0
  42. package/dist/domain.d.ts +3 -0
  43. package/dist/domain.js +12 -0
  44. package/dist/domain.js.map +1 -0
  45. package/dist/errors.d.ts +7 -0
  46. package/dist/errors.js +49 -0
  47. package/dist/errors.js.map +1 -0
  48. package/dist/extension.d.ts +53 -0
  49. package/dist/extension.js +158 -0
  50. package/dist/extension.js.map +1 -0
  51. package/dist/forward.d.ts +11 -0
  52. package/dist/forward.js +125 -0
  53. package/dist/forward.js.map +1 -0
  54. package/dist/framed-stream.d.ts +14 -0
  55. package/dist/framed-stream.js +113 -0
  56. package/dist/framed-stream.js.map +1 -0
  57. package/dist/gen/keywire/discovery/v1/discovery_pb.d.ts +105 -0
  58. package/dist/gen/keywire/discovery/v1/discovery_pb.js +57 -0
  59. package/dist/gen/keywire/discovery/v1/discovery_pb.js.map +1 -0
  60. package/dist/gen/keywire/state/v1/state_pb.d.ts +988 -0
  61. package/dist/gen/keywire/state/v1/state_pb.js +184 -0
  62. package/dist/gen/keywire/state/v1/state_pb.js.map +1 -0
  63. package/dist/gen/keywire/v1/forward_pb.d.ts +53 -0
  64. package/dist/gen/keywire/v1/forward_pb.js +58 -0
  65. package/dist/gen/keywire/v1/forward_pb.js.map +1 -0
  66. package/dist/gen/keywire/v1/guest_pb.d.ts +322 -0
  67. package/dist/gen/keywire/v1/guest_pb.js +100 -0
  68. package/dist/gen/keywire/v1/guest_pb.js.map +1 -0
  69. package/dist/gen/keywire/v1/pair_pb.d.ts +106 -0
  70. package/dist/gen/keywire/v1/pair_pb.js +45 -0
  71. package/dist/gen/keywire/v1/pair_pb.js.map +1 -0
  72. package/dist/gen/keywire/v1/relay_pb.d.ts +116 -0
  73. package/dist/gen/keywire/v1/relay_pb.js +20 -0
  74. package/dist/gen/keywire/v1/relay_pb.js.map +1 -0
  75. package/dist/gen/keywire/v1/roster_pb.d.ts +213 -0
  76. package/dist/gen/keywire/v1/roster_pb.js +77 -0
  77. package/dist/gen/keywire/v1/roster_pb.js.map +1 -0
  78. package/dist/gen/keywire/v1/sync_pb.d.ts +68 -0
  79. package/dist/gen/keywire/v1/sync_pb.js +24 -0
  80. package/dist/gen/keywire/v1/sync_pb.js.map +1 -0
  81. package/dist/guest/equal-bytes.d.ts +1 -0
  82. package/dist/guest/equal-bytes.js +10 -0
  83. package/dist/guest/equal-bytes.js.map +1 -0
  84. package/dist/guest/hello.d.ts +4 -0
  85. package/dist/guest/hello.js +42 -0
  86. package/dist/guest/hello.js.map +1 -0
  87. package/dist/guest/invite-frame.d.ts +8 -0
  88. package/dist/guest/invite-frame.js +44 -0
  89. package/dist/guest/invite-frame.js.map +1 -0
  90. package/dist/guest/invite.d.ts +17 -0
  91. package/dist/guest/invite.js +86 -0
  92. package/dist/guest/invite.js.map +1 -0
  93. package/dist/guest/relay-hello.d.ts +2 -0
  94. package/dist/guest/relay-hello.js +38 -0
  95. package/dist/guest/relay-hello.js.map +1 -0
  96. package/dist/guest/revocations.d.ts +12 -0
  97. package/dist/guest/revocations.js +134 -0
  98. package/dist/guest/revocations.js.map +1 -0
  99. package/dist/guest/roster.d.ts +2 -0
  100. package/dist/guest/roster.js +14 -0
  101. package/dist/guest/roster.js.map +1 -0
  102. package/dist/guest/token-payload.d.ts +2 -0
  103. package/dist/guest/token-payload.js +71 -0
  104. package/dist/guest/token-payload.js.map +1 -0
  105. package/dist/guest-conn.d.ts +20 -0
  106. package/dist/guest-conn.js +99 -0
  107. package/dist/guest-conn.js.map +1 -0
  108. package/dist/guest-relay.d.ts +7 -0
  109. package/dist/guest-relay.js +45 -0
  110. package/dist/guest-relay.js.map +1 -0
  111. package/dist/guest.d.ts +20 -0
  112. package/dist/guest.js +99 -0
  113. package/dist/guest.js.map +1 -0
  114. package/dist/index.d.ts +31 -0
  115. package/dist/index.js +28 -0
  116. package/dist/index.js.map +1 -0
  117. package/dist/msg-path.d.ts +9 -0
  118. package/dist/msg-path.js +2 -0
  119. package/dist/msg-path.js.map +1 -0
  120. package/dist/mux/byte-queue.d.ts +9 -0
  121. package/dist/mux/byte-queue.js +62 -0
  122. package/dist/mux/byte-queue.js.map +1 -0
  123. package/dist/mux/codec.d.ts +30 -0
  124. package/dist/mux/codec.js +23 -0
  125. package/dist/mux/codec.js.map +1 -0
  126. package/dist/mux/errors.d.ts +23 -0
  127. package/dist/mux/errors.js +47 -0
  128. package/dist/mux/errors.js.map +1 -0
  129. package/dist/mux/frame.d.ts +44 -0
  130. package/dist/mux/frame.js +161 -0
  131. package/dist/mux/frame.js.map +1 -0
  132. package/dist/mux/nexttask.d.ts +1 -0
  133. package/dist/mux/nexttask.js +19 -0
  134. package/dist/mux/nexttask.js.map +1 -0
  135. package/dist/mux/session.d.ts +69 -0
  136. package/dist/mux/session.js +586 -0
  137. package/dist/mux/session.js.map +1 -0
  138. package/dist/mux/signal.d.ts +5 -0
  139. package/dist/mux/signal.js +14 -0
  140. package/dist/mux/signal.js.map +1 -0
  141. package/dist/mux/stream.d.ts +34 -0
  142. package/dist/mux/stream.js +620 -0
  143. package/dist/mux/stream.js.map +1 -0
  144. package/dist/nodekit/device.d.ts +40 -0
  145. package/dist/nodekit/device.js +188 -0
  146. package/dist/nodekit/device.js.map +1 -0
  147. package/dist/nodekit/index.d.ts +5 -0
  148. package/dist/nodekit/index.js +8 -0
  149. package/dist/nodekit/index.js.map +1 -0
  150. package/dist/nodekit/indexeddb.d.ts +2 -0
  151. package/dist/nodekit/indexeddb.js +114 -0
  152. package/dist/nodekit/indexeddb.js.map +1 -0
  153. package/dist/nodekit/pair-device.d.ts +27 -0
  154. package/dist/nodekit/pair-device.js +44 -0
  155. package/dist/nodekit/pair-device.js.map +1 -0
  156. package/dist/nodekit/sealed-signer.d.ts +22 -0
  157. package/dist/nodekit/sealed-signer.js +74 -0
  158. package/dist/nodekit/sealed-signer.js.map +1 -0
  159. package/dist/nodekit/store.d.ts +22 -0
  160. package/dist/nodekit/store.js +21 -0
  161. package/dist/nodekit/store.js.map +1 -0
  162. package/dist/noise/backend.d.ts +25 -0
  163. package/dist/noise/backend.js +2 -0
  164. package/dist/noise/backend.js.map +1 -0
  165. package/dist/noise/backends.d.ts +4 -0
  166. package/dist/noise/backends.js +19 -0
  167. package/dist/noise/backends.js.map +1 -0
  168. package/dist/noise/cipher.d.ts +18 -0
  169. package/dist/noise/cipher.js +69 -0
  170. package/dist/noise/cipher.js.map +1 -0
  171. package/dist/noise/crypto.d.ts +11 -0
  172. package/dist/noise/crypto.js +47 -0
  173. package/dist/noise/crypto.js.map +1 -0
  174. package/dist/noise/handshake.d.ts +28 -0
  175. package/dist/noise/handshake.js +183 -0
  176. package/dist/noise/handshake.js.map +1 -0
  177. package/dist/noise/import.d.ts +2 -0
  178. package/dist/noise/import.js +16 -0
  179. package/dist/noise/import.js.map +1 -0
  180. package/dist/noise/noble.d.ts +2 -0
  181. package/dist/noise/noble.js +51 -0
  182. package/dist/noise/noble.js.map +1 -0
  183. package/dist/noise/pair-code.d.ts +2 -0
  184. package/dist/noise/pair-code.js +11 -0
  185. package/dist/noise/pair-code.js.map +1 -0
  186. package/dist/noise/pkcs8.d.ts +3 -0
  187. package/dist/noise/pkcs8.js +18 -0
  188. package/dist/noise/pkcs8.js.map +1 -0
  189. package/dist/noise/sealed.d.ts +21 -0
  190. package/dist/noise/sealed.js +40 -0
  191. package/dist/noise/sealed.js.map +1 -0
  192. package/dist/noise/symmetric.d.ts +17 -0
  193. package/dist/noise/symmetric.js +56 -0
  194. package/dist/noise/symmetric.js.map +1 -0
  195. package/dist/noise/webcrypto.d.ts +3 -0
  196. package/dist/noise/webcrypto.js +98 -0
  197. package/dist/noise/webcrypto.js.map +1 -0
  198. package/dist/pair-relay.d.ts +4 -0
  199. package/dist/pair-relay.js +46 -0
  200. package/dist/pair-relay.js.map +1 -0
  201. package/dist/pair.d.ts +7 -0
  202. package/dist/pair.js +50 -0
  203. package/dist/pair.js.map +1 -0
  204. package/dist/paths/link.d.ts +33 -0
  205. package/dist/paths/link.js +132 -0
  206. package/dist/paths/link.js.map +1 -0
  207. package/dist/paths/node-paths.d.ts +10 -0
  208. package/dist/paths/node-paths.js +41 -0
  209. package/dist/paths/node-paths.js.map +1 -0
  210. package/dist/paths/path.d.ts +11 -0
  211. package/dist/paths/path.js +25 -0
  212. package/dist/paths/path.js.map +1 -0
  213. package/dist/paths/race.d.ts +18 -0
  214. package/dist/paths/race.js +157 -0
  215. package/dist/paths/race.js.map +1 -0
  216. package/dist/paths/relay-sessions.d.ts +11 -0
  217. package/dist/paths/relay-sessions.js +93 -0
  218. package/dist/paths/relay-sessions.js.map +1 -0
  219. package/dist/paths/upgrades.d.ts +12 -0
  220. package/dist/paths/upgrades.js +58 -0
  221. package/dist/paths/upgrades.js.map +1 -0
  222. package/dist/relay-server/admission.d.ts +24 -0
  223. package/dist/relay-server/admission.js +140 -0
  224. package/dist/relay-server/admission.js.map +1 -0
  225. package/dist/relay-server/config.d.ts +28 -0
  226. package/dist/relay-server/config.js +47 -0
  227. package/dist/relay-server/config.js.map +1 -0
  228. package/dist/relay-server/discovery/addr.d.ts +18 -0
  229. package/dist/relay-server/discovery/addr.js +211 -0
  230. package/dist/relay-server/discovery/addr.js.map +1 -0
  231. package/dist/relay-server/discovery/directory.d.ts +42 -0
  232. package/dist/relay-server/discovery/directory.js +101 -0
  233. package/dist/relay-server/discovery/directory.js.map +1 -0
  234. package/dist/relay-server/discovery/http.d.ts +10 -0
  235. package/dist/relay-server/discovery/http.js +172 -0
  236. package/dist/relay-server/discovery/http.js.map +1 -0
  237. package/dist/relay-server/discovery/index.d.ts +6 -0
  238. package/dist/relay-server/discovery/index.js +15 -0
  239. package/dist/relay-server/discovery/index.js.map +1 -0
  240. package/dist/relay-server/discovery/listing.d.ts +24 -0
  241. package/dist/relay-server/discovery/listing.js +115 -0
  242. package/dist/relay-server/discovery/listing.js.map +1 -0
  243. package/dist/relay-server/discovery/options.d.ts +39 -0
  244. package/dist/relay-server/discovery/options.js +97 -0
  245. package/dist/relay-server/discovery/options.js.map +1 -0
  246. package/dist/relay-server/discovery/service.d.ts +21 -0
  247. package/dist/relay-server/discovery/service.js +409 -0
  248. package/dist/relay-server/discovery/service.js.map +1 -0
  249. package/dist/relay-server/forward.d.ts +3 -0
  250. package/dist/relay-server/forward.js +97 -0
  251. package/dist/relay-server/forward.js.map +1 -0
  252. package/dist/relay-server/handshake.d.ts +10 -0
  253. package/dist/relay-server/handshake.js +36 -0
  254. package/dist/relay-server/handshake.js.map +1 -0
  255. package/dist/relay-server/index.d.ts +6 -0
  256. package/dist/relay-server/index.js +10 -0
  257. package/dist/relay-server/index.js.map +1 -0
  258. package/dist/relay-server/relay.d.ts +25 -0
  259. package/dist/relay-server/relay.js +392 -0
  260. package/dist/relay-server/relay.js.map +1 -0
  261. package/dist/relay-server/service-read.d.ts +4 -0
  262. package/dist/relay-server/service-read.js +16 -0
  263. package/dist/relay-server/service-read.js.map +1 -0
  264. package/dist/relay-server/service.d.ts +25 -0
  265. package/dist/relay-server/service.js +8 -0
  266. package/dist/relay-server/service.js.map +1 -0
  267. package/dist/relay-server/session.d.ts +60 -0
  268. package/dist/relay-server/session.js +35 -0
  269. package/dist/relay-server/session.js.map +1 -0
  270. package/dist/relay-server/url.d.ts +1 -0
  271. package/dist/relay-server/url.js +8 -0
  272. package/dist/relay-server/url.js.map +1 -0
  273. package/dist/relay-url.d.ts +2 -0
  274. package/dist/relay-url.js +11 -0
  275. package/dist/relay-url.js.map +1 -0
  276. package/dist/relay.d.ts +5 -0
  277. package/dist/relay.js +60 -0
  278. package/dist/relay.js.map +1 -0
  279. package/dist/roster/admission.d.ts +17 -0
  280. package/dist/roster/admission.js +85 -0
  281. package/dist/roster/admission.js.map +1 -0
  282. package/dist/roster/credentials.d.ts +5 -0
  283. package/dist/roster/credentials.js +72 -0
  284. package/dist/roster/credentials.js.map +1 -0
  285. package/dist/roster/digest.d.ts +3 -0
  286. package/dist/roster/digest.js +23 -0
  287. package/dist/roster/digest.js.map +1 -0
  288. package/dist/roster/envelope.d.ts +11 -0
  289. package/dist/roster/envelope.js +35 -0
  290. package/dist/roster/envelope.js.map +1 -0
  291. package/dist/roster/errors.d.ts +6 -0
  292. package/dist/roster/errors.js +11 -0
  293. package/dist/roster/errors.js.map +1 -0
  294. package/dist/roster/limits.d.ts +25 -0
  295. package/dist/roster/limits.js +26 -0
  296. package/dist/roster/limits.js.map +1 -0
  297. package/dist/roster/merge.d.ts +10 -0
  298. package/dist/roster/merge.js +101 -0
  299. package/dist/roster/merge.js.map +1 -0
  300. package/dist/roster/name.d.ts +1 -0
  301. package/dist/roster/name.js +7 -0
  302. package/dist/roster/name.js.map +1 -0
  303. package/dist/roster/order.d.ts +5 -0
  304. package/dist/roster/order.js +22 -0
  305. package/dist/roster/order.js.map +1 -0
  306. package/dist/roster/ownership.d.ts +9 -0
  307. package/dist/roster/ownership.js +42 -0
  308. package/dist/roster/ownership.js.map +1 -0
  309. package/dist/roster/retain.d.ts +2 -0
  310. package/dist/roster/retain.js +55 -0
  311. package/dist/roster/retain.js.map +1 -0
  312. package/dist/roster/revocations.d.ts +5 -0
  313. package/dist/roster/revocations.js +37 -0
  314. package/dist/roster/revocations.js.map +1 -0
  315. package/dist/roster/scan.d.ts +16 -0
  316. package/dist/roster/scan.js +194 -0
  317. package/dist/roster/scan.js.map +1 -0
  318. package/dist/roster/state.d.ts +34 -0
  319. package/dist/roster/state.js +204 -0
  320. package/dist/roster/state.js.map +1 -0
  321. package/dist/roster/statement.d.ts +38 -0
  322. package/dist/roster/statement.js +176 -0
  323. package/dist/roster/statement.js.map +1 -0
  324. package/dist/roster/types.d.ts +35 -0
  325. package/dist/roster/types.js +2 -0
  326. package/dist/roster/types.js.map +1 -0
  327. package/dist/roster/verify.d.ts +3 -0
  328. package/dist/roster/verify.js +63 -0
  329. package/dist/roster/verify.js.map +1 -0
  330. package/dist/roster-sync/apply.d.ts +2 -0
  331. package/dist/roster-sync/apply.js +23 -0
  332. package/dist/roster-sync/apply.js.map +1 -0
  333. package/dist/roster-sync/exchange.d.ts +3 -0
  334. package/dist/roster-sync/exchange.js +25 -0
  335. package/dist/roster-sync/exchange.js.map +1 -0
  336. package/dist/roster-sync/frame.d.ts +17 -0
  337. package/dist/roster-sync/frame.js +66 -0
  338. package/dist/roster-sync/frame.js.map +1 -0
  339. package/dist/roster-sync/records.d.ts +8 -0
  340. package/dist/roster-sync/records.js +78 -0
  341. package/dist/roster-sync/records.js.map +1 -0
  342. package/dist/roster-sync/sync.d.ts +12 -0
  343. package/dist/roster-sync/sync.js +69 -0
  344. package/dist/roster-sync/sync.js.map +1 -0
  345. package/dist/rtc.d.ts +15 -0
  346. package/dist/rtc.js +275 -0
  347. package/dist/rtc.js.map +1 -0
  348. package/dist/state/errors.d.ts +30 -0
  349. package/dist/state/errors.js +72 -0
  350. package/dist/state/errors.js.map +1 -0
  351. package/dist/state/index.d.ts +6 -0
  352. package/dist/state/index.js +11 -0
  353. package/dist/state/index.js.map +1 -0
  354. package/dist/state/limits.d.ts +34 -0
  355. package/dist/state/limits.js +115 -0
  356. package/dist/state/limits.js.map +1 -0
  357. package/dist/state/remote.d.ts +217 -0
  358. package/dist/state/remote.js +1934 -0
  359. package/dist/state/remote.js.map +1 -0
  360. package/dist/state/shared.d.ts +56 -0
  361. package/dist/state/shared.js +300 -0
  362. package/dist/state/shared.js.map +1 -0
  363. package/dist/state/state.d.ts +118 -0
  364. package/dist/state/state.js +1048 -0
  365. package/dist/state/state.js.map +1 -0
  366. package/dist/state/store.d.ts +31 -0
  367. package/dist/state/store.js +375 -0
  368. package/dist/state/store.js.map +1 -0
  369. package/dist/state/wire.d.ts +43 -0
  370. package/dist/state/wire.js +179 -0
  371. package/dist/state/wire.js.map +1 -0
  372. package/dist/strict-proto.d.ts +10 -0
  373. package/dist/strict-proto.js +167 -0
  374. package/dist/strict-proto.js.map +1 -0
  375. package/dist/token.d.ts +18 -0
  376. package/dist/token.js +60 -0
  377. package/dist/token.js.map +1 -0
  378. package/dist/urls.d.ts +7 -0
  379. package/dist/urls.js +117 -0
  380. package/dist/urls.js.map +1 -0
  381. package/dist/ws.d.ts +19 -0
  382. package/dist/ws.js +143 -0
  383. package/dist/ws.js.map +1 -0
  384. package/package.json +72 -3
@@ -0,0 +1,34 @@
1
+ import type { Bytes } from "../noise/crypto.ts";
2
+ import { type CodecBudget, type Compression } from "./codec.ts";
3
+ import { ResetError } from "./errors.ts";
4
+ import { type RecordWriter } from "./frame.ts";
5
+ import type { Session } from "./session.ts";
6
+ export declare class Stream {
7
+ #private;
8
+ readonly id: number;
9
+ peer: Bytes | undefined;
10
+ queued: boolean;
11
+ removed: boolean;
12
+ constructor(s: Session, id: number, needOpen: boolean);
13
+ get unopened(): boolean;
14
+ get hasOutput(): boolean;
15
+ setEncoding(encoding: number, compression: Compression | undefined, budget: CodecBudget | undefined): void;
16
+ get compressed(): boolean;
17
+ setWriteCompression(on: boolean): void;
18
+ writePlainOnly(): void;
19
+ read(): Promise<Bytes | null>;
20
+ write(data: Bytes): Promise<void>;
21
+ writeHead(head: Bytes): Promise<void>;
22
+ closeWrite(): Promise<void>;
23
+ flushStage(): void;
24
+ replyAndClose(status: number): void;
25
+ close(): Promise<void>;
26
+ reset(code: number): void;
27
+ wake(): void;
28
+ fail(err: Error): void;
29
+ frameOutput(w: RecordWriter, quantum: number): void;
30
+ receiveData(data: Bytes, consumed?: number): void;
31
+ receiveWindow(n: number): void;
32
+ receiveClose(): void;
33
+ receiveReset(err: ResetError): void;
34
+ }
@@ -0,0 +1,620 @@
1
+ import { ByteQueue } from "./byte-queue.js";
2
+ import { CompressionError, CompressionMemoryError, MAX_BLOCK, } from "./codec.js";
3
+ import { ProtocolError, ResetError, StreamClosed, WriteQueueFull } from "./errors.js";
4
+ import { CODE_CANCEL, CODE_REFUSED, DATA_HEADER, FrameType, grantBatch, MAX_CONTROL, MAX_STREAM_WINDOW, STREAM_WINDOW, widen, } from "./frame.js";
5
+ import { Signal } from "./signal.js";
6
+ // WriteMode is how a write goes: as it is (an uncompressed stream, or a
7
+ // stream's head), or in plain or deflate blocks, as setWriteCompression
8
+ // said when it was called.
9
+ var WriteMode;
10
+ (function (WriteMode) {
11
+ WriteMode[WriteMode["Raw"] = 0] = "Raw";
12
+ WriteMode[WriteMode["Plain"] = 1] = "Plain";
13
+ WriteMode[WriteMode["Deflate"] = 2] = "Deflate";
14
+ })(WriteMode || (WriteMode = {}));
15
+ const DONE = {
16
+ data: undefined,
17
+ off: 0,
18
+ mode: WriteMode.Raw,
19
+ charge: 0,
20
+ resolve: () => { },
21
+ reject: () => { },
22
+ };
23
+ // Stages gather a compressed stream's deflate writes, MAX_BLOCK bytes at
24
+ // most, until the session flushes or the stage is full, so that the
25
+ // writes of one task go in one block; spare ones are kept, a few.
26
+ const spareStages = [];
27
+ const SPARE_STAGES = 4;
28
+ // WAITING_COST is what a waiting write counts against the write queue's
29
+ // limit beyond its bytes, for the queue's own record of it: without it,
30
+ // many tiny writes would hold far more than the limit.
31
+ const WAITING_COST = 64;
32
+ // Stream is a reliable, ordered byte stream. write resolves once the data is
33
+ // buffered within the flow-control windows, like a socket; connection errors
34
+ // surface on later calls. Concurrent writes are serialized in call order.
35
+ // Writes that wait for credit hold the session's writeQueue bytes at most,
36
+ // beyond one write waiting alone; one more resets the stream and is
37
+ // rejected with WriteQueueFull, rather than leave a gap in what it carries.
38
+ export class Stream {
39
+ id;
40
+ // peer is the static key of the device at the other end, once known.
41
+ peer;
42
+ queued = false;
43
+ removed = false;
44
+ #s;
45
+ #signal = new Signal();
46
+ #needOpen;
47
+ #out = new ByteQueue();
48
+ #sendWindow = STREAM_WINDOW;
49
+ #closeWrite = false;
50
+ #finWritten = false;
51
+ #recv = [];
52
+ #recvLen = 0;
53
+ #recvWindow = STREAM_WINDOW;
54
+ #recvLimit = STREAM_WINDOW;
55
+ #credit = 0;
56
+ #recvFin = false;
57
+ // #closed gives up reading: data that comes is dropped and its credit
58
+ // returned, until the peer closes.
59
+ #closed = false;
60
+ #err;
61
+ // Writes waiting for credit, in call order; one with no data closes the
62
+ // write side.
63
+ #waiting = [];
64
+ #waitHead = 0;
65
+ #waitingCharge = 0;
66
+ #closing;
67
+ #pumping = false;
68
+ // encoding is the stream's encoding byte (spec/compression.md): 0, or 1
69
+ // for deflate blocks, which #reader and #writer decode and encode.
70
+ #encoding = 0;
71
+ #reader;
72
+ #writer;
73
+ #budget;
74
+ #compressWrites = true;
75
+ #plainOnly = false;
76
+ // #stage holds deflate writes not yet encoded, #staged bytes of it;
77
+ // while it does, nothing is waiting.
78
+ #stage;
79
+ #staged = 0;
80
+ // #creditWaited is set when a block had to wait for credit, until it
81
+ // is encoded.
82
+ #creditWaited = false;
83
+ #reading = Promise.resolve();
84
+ #compressionErr;
85
+ constructor(s, id, needOpen) {
86
+ this.#s = s;
87
+ this.id = id;
88
+ this.#needOpen = needOpen;
89
+ }
90
+ // unopened reports whether the stream's OPEN is still to be written.
91
+ get unopened() {
92
+ return this.#needOpen;
93
+ }
94
+ get hasOutput() {
95
+ return this.#out.length > 0 || this.#needOpen || (this.#closeWrite && !this.#finWritten);
96
+ }
97
+ // setEncoding gives the stream its encoding, before any data of the
98
+ // application is read or written; for encoding 1, compression is the
99
+ // registered codec, and budget the connection's, which bounds what its
100
+ // compressing streams hold.
101
+ setEncoding(encoding, compression, budget) {
102
+ if (encoding !== 1)
103
+ return;
104
+ if (!compression || !budget)
105
+ throw new ProtocolError("compressed stream without a codec");
106
+ this.#encoding = encoding;
107
+ this.#reader = compression.reader(() => this.#readRaw(), budget);
108
+ this.#writer = compression.writer(budget);
109
+ this.#budget = budget;
110
+ }
111
+ // compressed says the stream carries deflate blocks (encoding 1) both
112
+ // ways: it was opened asking for compression, on a connection whose
113
+ // peers both support it.
114
+ get compressed() {
115
+ return this.#encoding === 1;
116
+ }
117
+ // setWriteCompression turns compression of later writes on or off, on a
118
+ // compressed stream; data written with it off goes as plain blocks,
119
+ // outside the compression context, as secrets should
120
+ // (spec/compression.md, Security). It is on from the start, unless this
121
+ // side compresses nothing it sends (writePlainOnly).
122
+ setWriteCompression(on) {
123
+ this.#compressWrites = on && !this.#plainOnly;
124
+ }
125
+ // writePlainOnly has every later write go plain, whatever
126
+ // setWriteCompression asks: this side compresses nothing it sends.
127
+ writePlainOnly() {
128
+ this.#plainOnly = true;
129
+ this.#compressWrites = false;
130
+ }
131
+ // read resolves with the next chunk, or null once the peer has closed. A
132
+ // compressed stream whose peer sent a malformed block is reset with code
133
+ // 0, and its reads reject with CompressionError.
134
+ read() {
135
+ if (!this.#reader)
136
+ return this.#readRaw();
137
+ const next = this.#reading.then(() => this.#readDecoded());
138
+ this.#reading = next.catch(() => { });
139
+ return next;
140
+ }
141
+ async #readDecoded() {
142
+ if (this.#compressionErr)
143
+ throw this.#compressionErr;
144
+ if (this.#err)
145
+ throw this.#err;
146
+ if (this.#closed)
147
+ throw new StreamClosed();
148
+ try {
149
+ const data = await this.#reader.read();
150
+ this.#endIfDrained();
151
+ return data;
152
+ }
153
+ catch (e) {
154
+ if (e instanceof CompressionError && !this.#compressionErr) {
155
+ this.#compressionErr = e;
156
+ this.reset(CODE_CANCEL);
157
+ }
158
+ if (e instanceof CompressionMemoryError && !this.#compressionErr) {
159
+ this.#compressionErr = e;
160
+ this.reset(CODE_REFUSED);
161
+ }
162
+ if (e instanceof CompressionError || e instanceof CompressionMemoryError)
163
+ throw e;
164
+ // The reader was closed under a waiting read: the stream's own
165
+ // error is the one to report.
166
+ if (this.#err)
167
+ throw this.#err;
168
+ if (this.#closed)
169
+ throw new StreamClosed();
170
+ throw e;
171
+ }
172
+ }
173
+ async #readRaw() {
174
+ for (;;) {
175
+ if (this.#err)
176
+ throw this.#err;
177
+ if (this.#closed)
178
+ throw new StreamClosed();
179
+ const chunk = this.#recv.shift();
180
+ if (chunk) {
181
+ this.#recvLen -= chunk.length;
182
+ this.#grant(chunk.length);
183
+ return chunk;
184
+ }
185
+ if (this.#recvFin)
186
+ return null;
187
+ await this.#signal.wait();
188
+ }
189
+ }
190
+ // write buffers synchronously when nothing is waiting ahead of it, so
191
+ // writes in one task share records; otherwise it queues behind them.
192
+ write(data) {
193
+ return this.#write(data, false);
194
+ }
195
+ // writeHead writes the stream's service and encoding bytes, as they are.
196
+ writeHead(head) {
197
+ return this.#write(head, true);
198
+ }
199
+ #write(data, raw) {
200
+ if (this.#err)
201
+ return Promise.reject(this.#err);
202
+ // The mode is fixed now, however long the write waits.
203
+ const mode = raw || !this.#writer
204
+ ? WriteMode.Raw
205
+ : this.#compressWrites
206
+ ? WriteMode.Deflate
207
+ : WriteMode.Plain;
208
+ // A compressed write is encoded a block at a time as credit allows
209
+ // (#encode); while it waits, it holds only the caller's data.
210
+ if (this.#writer && (this.#closeWrite || this.#closing))
211
+ return Promise.reject(new Error("mux: write side closed"));
212
+ let off = 0;
213
+ let alone = this.#waitHead === this.#waiting.length;
214
+ if (alone) {
215
+ try {
216
+ if (mode === WriteMode.Deflate)
217
+ off = this.#gather(data);
218
+ else if (this.#flushStage())
219
+ off = this.#put(data, 0, mode);
220
+ else
221
+ alone = false;
222
+ }
223
+ catch (e) {
224
+ return Promise.reject(e);
225
+ }
226
+ if (off === data.length)
227
+ return Promise.resolve();
228
+ }
229
+ const charge = data.length - off + WAITING_COST;
230
+ if (!alone && this.#waitingCharge + charge > this.#s.writeQueue) {
231
+ this.reset(CODE_CANCEL);
232
+ return Promise.reject(new WriteQueueFull());
233
+ }
234
+ return this.#wait(data, off, charge, mode);
235
+ }
236
+ // closeWrite tells the peer no more data follows, after buffered data.
237
+ closeWrite() {
238
+ try {
239
+ this.#flushStage();
240
+ }
241
+ catch (e) {
242
+ return Promise.reject(e);
243
+ }
244
+ if (this.#waitHead < this.#waiting.length) {
245
+ this.#closing ??= this.#wait(undefined, 0, 0, WriteMode.Raw);
246
+ return this.#closing;
247
+ }
248
+ try {
249
+ this.#close();
250
+ return Promise.resolve();
251
+ }
252
+ catch (e) {
253
+ return Promise.reject(e);
254
+ }
255
+ }
256
+ #close() {
257
+ if (this.#err)
258
+ throw this.#err;
259
+ if (this.#closeWrite)
260
+ return;
261
+ this.#closeWrite = true;
262
+ this.#writer?.close();
263
+ this.#s.markReady(this);
264
+ }
265
+ // #wait queues a write behind the others, to finish as credit arrives.
266
+ #wait(data, off, charge, mode) {
267
+ const { promise, resolve, reject } = Promise.withResolvers();
268
+ this.#waitingCharge += charge;
269
+ this.#waiting.push({ data, off, mode, charge, resolve, reject });
270
+ void this.#pump();
271
+ return promise;
272
+ }
273
+ // #pump finishes the waiting writes in order, each once credit lets it.
274
+ async #pump() {
275
+ if (this.#pumping)
276
+ return;
277
+ this.#pumping = true;
278
+ while (this.#waitHead < this.#waiting.length) {
279
+ const w = this.#waiting[this.#waitHead];
280
+ try {
281
+ if (!w.data)
282
+ this.#close();
283
+ else {
284
+ const off = this.#put(w.data, w.off, w.mode);
285
+ this.#waitingCharge -= off - w.off;
286
+ w.charge -= off - w.off;
287
+ w.off = off;
288
+ if (w.off < w.data.length) {
289
+ await this.#signal.wait();
290
+ continue;
291
+ }
292
+ }
293
+ w.resolve();
294
+ }
295
+ catch (e) {
296
+ w.reject(e);
297
+ }
298
+ this.#waitingCharge -= w.charge;
299
+ // Shifting a long array costs its length; drop finished writes in
300
+ // bulk instead.
301
+ this.#waiting[this.#waitHead++] = DONE;
302
+ if (this.#waitHead === this.#waiting.length) {
303
+ this.#waiting = [];
304
+ this.#waitHead = 0;
305
+ }
306
+ else if (this.#waitHead >= 1024 && this.#waitHead * 2 >= this.#waiting.length) {
307
+ this.#waiting = this.#waiting.slice(this.#waitHead);
308
+ this.#waitHead = 0;
309
+ }
310
+ }
311
+ this.#pumping = false;
312
+ }
313
+ // #gather takes a deflate write in: whole blocks of it are encoded at
314
+ // once when nothing is gathered, the rest is copied into the stage,
315
+ // encoded once full or as the session flushes. It returns how much of
316
+ // data it took; what it did not must wait, behind what the stage could
317
+ // not send.
318
+ #gather(data) {
319
+ if (this.#err)
320
+ throw this.#err;
321
+ if (this.#closeWrite)
322
+ throw new Error("mux: write side closed");
323
+ let off = 0;
324
+ while (off < data.length) {
325
+ if (this.#staged === 0 && data.length - off >= MAX_BLOCK) {
326
+ const end = off + Math.floor((data.length - off) / MAX_BLOCK) * MAX_BLOCK;
327
+ const next = this.#encode(this.#writer, data.subarray(0, end), off, true);
328
+ if (next < end)
329
+ return next;
330
+ off = next;
331
+ continue;
332
+ }
333
+ if (!this.#stage) {
334
+ // The stage counts in the connection's budget; without room, the
335
+ // write is encoded at once, a block of its own.
336
+ if (!this.#budget.reserve(MAX_BLOCK))
337
+ return this.#encode(this.#writer, data, off, true);
338
+ this.#stage = spareStages.pop() ?? new Uint8Array(MAX_BLOCK);
339
+ }
340
+ const n = Math.min(data.length - off, MAX_BLOCK - this.#staged);
341
+ this.#stage.set(data.subarray(off, off + n), this.#staged);
342
+ this.#staged += n;
343
+ off += n;
344
+ if (this.#staged === MAX_BLOCK && !this.#flushStage())
345
+ return off;
346
+ }
347
+ if (this.#staged > 0)
348
+ this.#s.markStaged(this);
349
+ return off;
350
+ }
351
+ // flushStage encodes what the stage gathered, as the session flushes.
352
+ flushStage() {
353
+ try {
354
+ this.#flushStage();
355
+ }
356
+ catch {
357
+ // The stream was reset: its writes see the error.
358
+ }
359
+ }
360
+ // #flushStage encodes the stage as one block, if credit allows, and
361
+ // reports whether it is empty; what credit does not allow waits, first
362
+ // in line.
363
+ #flushStage() {
364
+ if (this.#staged === 0)
365
+ return true;
366
+ const stage = this.#stage;
367
+ const size = this.#staged;
368
+ let off;
369
+ try {
370
+ off = this.#encode(this.#writer, stage.subarray(0, size), 0, true);
371
+ }
372
+ catch (e) {
373
+ this.#dropStage();
374
+ throw e;
375
+ }
376
+ if (off < size) {
377
+ const rest = stage.slice(off, size);
378
+ const charge = rest.length + WAITING_COST;
379
+ this.#waitingCharge += charge;
380
+ this.#waiting.splice(this.#waitHead, 0, {
381
+ data: rest,
382
+ off: 0,
383
+ mode: WriteMode.Deflate,
384
+ charge,
385
+ resolve: () => { },
386
+ reject: () => { },
387
+ });
388
+ void this.#pump();
389
+ }
390
+ this.#dropStage();
391
+ return off === size;
392
+ }
393
+ #dropStage() {
394
+ if (this.#stage) {
395
+ if (spareStages.length < SPARE_STAGES)
396
+ spareStages.push(this.#stage);
397
+ this.#budget.release(MAX_BLOCK);
398
+ }
399
+ this.#stage = undefined;
400
+ this.#staged = 0;
401
+ }
402
+ // #put buffers as much of data from off as the windows allow, encoded
403
+ // where the stream is compressed, and returns the new offset.
404
+ #put(data, off, mode) {
405
+ return mode !== WriteMode.Raw && this.#writer
406
+ ? this.#encode(this.#writer, data, off, mode === WriteMode.Deflate)
407
+ : this.#reserve(data, off);
408
+ }
409
+ // #encode encodes blocks of data from off, each only once the stream and
410
+ // connection credit cover the most it can take, so that a block goes
411
+ // whole and nothing is encoded ahead of credit; the block is then
412
+ // charged what it took. It returns the new offset.
413
+ #encode(w, data, off, compress) {
414
+ if (this.#err)
415
+ throw this.#err;
416
+ if (this.#closeWrite)
417
+ throw new Error("mux: write side closed");
418
+ let wrote = false;
419
+ while (off < data.length) {
420
+ const size = Math.min(MAX_BLOCK, data.length - off);
421
+ const need = w.plan(size, compress);
422
+ if (this.#sendWindow < need || this.#s.connSend < need) {
423
+ if (this.#s.connSend < need)
424
+ this.#s.armStall();
425
+ this.#creditWaited = true;
426
+ break;
427
+ }
428
+ let parts;
429
+ try {
430
+ parts = w.block(data.subarray(off, off + size), !this.#creditWaited);
431
+ this.#creditWaited = false;
432
+ }
433
+ catch (e) {
434
+ this.reset(CODE_CANCEL);
435
+ throw e;
436
+ }
437
+ for (const p of parts) {
438
+ this.#sendWindow -= p.length;
439
+ this.#s.connSend -= p.length;
440
+ this.#out.write(p);
441
+ }
442
+ off += size;
443
+ wrote = true;
444
+ }
445
+ if (wrote)
446
+ this.#s.markReady(this);
447
+ return off;
448
+ }
449
+ // #reserve buffers as much of data from off as the windows allow and
450
+ // returns the new offset.
451
+ #reserve(data, off) {
452
+ if (this.#err)
453
+ throw this.#err;
454
+ if (this.#closeWrite)
455
+ throw new Error("mux: write side closed");
456
+ const n = Math.min(data.length - off, this.#sendWindow, this.#s.connSend);
457
+ if (this.#s.connSend === 0)
458
+ this.#s.armStall();
459
+ if (n === 0)
460
+ return off;
461
+ this.#sendWindow -= n;
462
+ this.#s.connSend -= n;
463
+ this.#out.write(n === data.length ? data : data.subarray(off, off + n));
464
+ this.#s.markReady(this);
465
+ return off + n;
466
+ }
467
+ // replyAndClose sends a one-byte answer and closes, without waiting for
468
+ // credit: a peer withholding it gets a reset instead.
469
+ replyAndClose(status) {
470
+ if (this.#err || this.#closeWrite)
471
+ return;
472
+ if (this.#sendWindow === 0 || this.#s.connSend === 0) {
473
+ this.reset(CODE_REFUSED);
474
+ return;
475
+ }
476
+ this.#sendWindow--;
477
+ this.#s.connSend--;
478
+ this.#out.write(Uint8Array.of(status));
479
+ this.#closeWrite = true;
480
+ this.#s.markReady(this);
481
+ }
482
+ // close ends the stream gracefully: what was written still reaches the
483
+ // peer, followed by the end of the stream, while data from the peer is
484
+ // dropped until it closes too. It resolves once the end is queued behind
485
+ // waiting writes. reset ends a stream at once instead, discarding what
486
+ // was not yet sent.
487
+ close() {
488
+ if (!this.#closed && !this.#err) {
489
+ this.#closed = true;
490
+ const n = this.#recvLen;
491
+ this.#recv = [];
492
+ this.#recvLen = 0;
493
+ if (n > 0)
494
+ this.#grant(n);
495
+ // Nothing more is read: the decoder's state goes.
496
+ this.#reader?.close();
497
+ this.#signal.notify();
498
+ }
499
+ return this.closeWrite();
500
+ }
501
+ // reset aborts both directions; code is an integer from 0 to MAX_VARINT.
502
+ reset(code) {
503
+ if (!Number.isSafeInteger(code) || code < 0)
504
+ throw new RangeError("mux: reset code must be an integer from 0 to 2^53-1");
505
+ if (this.#err || this.removed)
506
+ return;
507
+ this.receiveReset(new ResetError(code, false));
508
+ // A stream whose open was never written is unknown to the peer, and a
509
+ // reset for it would be a protocol violation.
510
+ if (!this.#needOpen)
511
+ this.#s.queueReset(this.id, code);
512
+ }
513
+ wake() {
514
+ this.#signal.notify();
515
+ }
516
+ fail(err) {
517
+ if (this.#err)
518
+ return;
519
+ this.#err = err;
520
+ this.#dropStage();
521
+ this.#writer?.close();
522
+ // The decoder's state goes too, and what the bound counts for it; a
523
+ // read waiting on it fails with the stream's error.
524
+ this.#reader?.close();
525
+ this.#signal.notify();
526
+ }
527
+ // frameOutput writes OPEN, up to quantum bytes of DATA and a due CLOSE.
528
+ frameOutput(w, quantum) {
529
+ if (this.#needOpen) {
530
+ w.frame({ type: FrameType.Open, stream: this.id });
531
+ this.#needOpen = false;
532
+ }
533
+ for (let sent = 0; this.#out.length > 0 && sent < quantum && w.room > DATA_HEADER;) {
534
+ const data = this.#out.peek(Math.min(quantum - sent, w.room - DATA_HEADER));
535
+ w.frame({ type: FrameType.Data, stream: this.id, data });
536
+ this.#out.discard(data.length);
537
+ sent += data.length;
538
+ }
539
+ if (this.#out.length === 0 && this.#closeWrite && !this.#finWritten && w.room >= MAX_CONTROL) {
540
+ w.frame({ type: FrameType.Close, stream: this.id });
541
+ this.#finWritten = true;
542
+ this.#maybeRemove();
543
+ }
544
+ }
545
+ receiveData(data, consumed = 0) {
546
+ if (this.#recvFin)
547
+ throw new ProtocolError("data after close");
548
+ if (data.length + consumed > this.#recvWindow)
549
+ throw new ProtocolError("data beyond stream window");
550
+ this.#recvWindow -= data.length + consumed;
551
+ if (consumed)
552
+ this.#grant(consumed);
553
+ if (this.#closed) {
554
+ // close gave up reading: drop the data, returning its credit.
555
+ if (data.length > 0)
556
+ this.#grant(data.length);
557
+ }
558
+ else if (data.length > 0) {
559
+ this.#recv.push(data);
560
+ this.#recvLen += data.length;
561
+ }
562
+ this.#signal.notify();
563
+ }
564
+ receiveWindow(n) {
565
+ if (this.#sendWindow + n > MAX_STREAM_WINDOW)
566
+ throw new ProtocolError("stream window beyond maximum");
567
+ this.#sendWindow += n;
568
+ this.#signal.notify();
569
+ }
570
+ receiveClose() {
571
+ if (this.#recvFin)
572
+ throw new ProtocolError("duplicate close");
573
+ this.#recvFin = true;
574
+ this.#endIfDrained();
575
+ this.#maybeRemove();
576
+ this.#signal.notify();
577
+ }
578
+ // receiveReset ends the stream and returns the credit its buffers held:
579
+ // unsent output was never counted by the peer, unread input was.
580
+ receiveReset(err) {
581
+ this.fail(err);
582
+ if (this.#out.length > 0)
583
+ this.#s.addConnSend(this.#out.length);
584
+ if (this.#recvLen > 0)
585
+ this.#s.creditConn(this.#recvLen);
586
+ this.#out.clear();
587
+ this.#recv = [];
588
+ this.#recvLen = 0;
589
+ this.#remove();
590
+ }
591
+ #grant(n) {
592
+ this.#s.creditConn(n);
593
+ this.#credit += n;
594
+ if (this.#credit < grantBatch(this.#recvLimit) || this.#recvFin || this.removed)
595
+ return;
596
+ const grant = widen(this.#credit, this.#recvLimit, this.#s.streamWindow);
597
+ this.#recvLimit = this.#s.streamWindow;
598
+ this.#s.queueWindow(this.id, grant);
599
+ this.#recvWindow += grant;
600
+ this.#credit = 0;
601
+ }
602
+ #maybeRemove() {
603
+ if (this.#finWritten && this.#recvFin)
604
+ this.#remove();
605
+ }
606
+ #remove() {
607
+ this.removed = true;
608
+ this.#writer?.close();
609
+ this.#endIfDrained();
610
+ this.#s.remove(this.id);
611
+ }
612
+ // #endIfDrained has the decoder give its state up where no block can
613
+ // come any more: the peer closed, and all it sent was taken. Reads then
614
+ // return what was decoded, and the end.
615
+ #endIfDrained() {
616
+ if (this.#recvFin && this.#recvLen === 0)
617
+ this.#reader?.endIfIdle();
618
+ }
619
+ }
620
+ //# sourceMappingURL=stream.js.map