@procedures/client 0.3.0 → 0.5.0

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 (196) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/README.md +62 -7
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/adapters/http/client/transport.d.ts +2 -1
  5. package/dist/adapters/http/client/transport.d.ts.map +1 -1
  6. package/dist/adapters/http/client/transport.js +9 -2
  7. package/dist/adapters/http/client/transport.js.map +1 -1
  8. package/dist/adapters/http/server/transport.d.ts +3 -1
  9. package/dist/adapters/http/server/transport.d.ts.map +1 -1
  10. package/dist/adapters/http/server/transport.js +8 -6
  11. package/dist/adapters/http/server/transport.js.map +1 -1
  12. package/dist/adapters/local/client/transport.d.ts +2 -1
  13. package/dist/adapters/local/client/transport.d.ts.map +1 -1
  14. package/dist/adapters/local/client/transport.js +12 -4
  15. package/dist/adapters/local/client/transport.js.map +1 -1
  16. package/dist/adapters/local/client/types.d.ts +6 -0
  17. package/dist/adapters/local/client/types.d.ts.map +1 -1
  18. package/dist/adapters/local/client/types.js.map +1 -1
  19. package/dist/adapters/sequence.d.ts +36 -0
  20. package/dist/adapters/sequence.d.ts.map +1 -0
  21. package/dist/adapters/sequence.js +55 -0
  22. package/dist/adapters/sequence.js.map +1 -0
  23. package/dist/adapters/websocket/client/node.d.ts +30 -0
  24. package/dist/adapters/websocket/client/node.d.ts.map +1 -0
  25. package/dist/adapters/websocket/client/node.js +32 -0
  26. package/dist/adapters/websocket/client/node.js.map +1 -0
  27. package/dist/adapters/websocket/client/transport.d.ts +39 -1
  28. package/dist/adapters/websocket/client/transport.d.ts.map +1 -1
  29. package/dist/adapters/websocket/client/transport.js +119 -16
  30. package/dist/adapters/websocket/client/transport.js.map +1 -1
  31. package/dist/adapters/websocket/client/types.d.ts +15 -5
  32. package/dist/adapters/websocket/client/types.d.ts.map +1 -1
  33. package/dist/adapters/websocket/client/types.js.map +1 -1
  34. package/dist/adapters/websocket/server/transport.d.ts +22 -1
  35. package/dist/adapters/websocket/server/transport.d.ts.map +1 -1
  36. package/dist/adapters/websocket/server/transport.js +29 -1
  37. package/dist/adapters/websocket/server/transport.js.map +1 -1
  38. package/dist/adapters/websocket/server/types.d.ts +6 -0
  39. package/dist/adapters/websocket/server/types.d.ts.map +1 -1
  40. package/dist/adapters/websocket/shared/rpc.d.ts +4 -3
  41. package/dist/adapters/websocket/shared/rpc.d.ts.map +1 -1
  42. package/dist/adapters/websocket/shared/rpc.js +18 -9
  43. package/dist/adapters/websocket/shared/rpc.js.map +1 -1
  44. package/dist/browser.d.ts +3 -0
  45. package/dist/browser.d.ts.map +1 -1
  46. package/dist/browser.js +4 -0
  47. package/dist/browser.js.map +1 -1
  48. package/dist/client/errors/http-errors.d.ts.map +1 -1
  49. package/dist/client/errors/http-errors.js +6 -0
  50. package/dist/client/errors/http-errors.js.map +1 -1
  51. package/dist/client/errors/index.d.ts +1 -1
  52. package/dist/client/errors/index.js +1 -1
  53. package/dist/client/errors/registry.d.ts +2 -1
  54. package/dist/client/errors/registry.d.ts.map +1 -1
  55. package/dist/client/errors/registry.js +73 -22
  56. package/dist/client/errors/registry.js.map +1 -1
  57. package/dist/client/middleware/rate-limit.d.ts.map +1 -1
  58. package/dist/client/middleware/rate-limit.js +0 -1
  59. package/dist/client/middleware/rate-limit.js.map +1 -1
  60. package/dist/client/middleware/timeout.d.ts.map +1 -1
  61. package/dist/client/middleware/timeout.js +2 -9
  62. package/dist/client/middleware/timeout.js.map +1 -1
  63. package/dist/events/index.d.ts +4 -0
  64. package/dist/events/index.d.ts.map +1 -1
  65. package/dist/events/index.js +3 -0
  66. package/dist/events/index.js.map +1 -1
  67. package/dist/events/publisher.d.ts +95 -0
  68. package/dist/events/publisher.d.ts.map +1 -0
  69. package/dist/events/publisher.js +268 -0
  70. package/dist/events/publisher.js.map +1 -0
  71. package/dist/events/remote.d.ts +164 -0
  72. package/dist/events/remote.d.ts.map +1 -0
  73. package/dist/events/remote.js +250 -0
  74. package/dist/events/remote.js.map +1 -0
  75. package/dist/index.d.ts +4 -0
  76. package/dist/index.d.ts.map +1 -1
  77. package/dist/index.js +4 -0
  78. package/dist/index.js.map +1 -1
  79. package/dist/node.d.ts +11 -0
  80. package/dist/node.d.ts.map +1 -0
  81. package/dist/node.js +10 -0
  82. package/dist/node.js.map +1 -0
  83. package/dist/procedures/collection/index.d.ts +1 -1
  84. package/dist/procedures/collection/index.d.ts.map +1 -1
  85. package/dist/procedures/collection/index.js.map +1 -1
  86. package/dist/procedures/collection/procedures.d.ts.map +1 -1
  87. package/dist/procedures/collection/procedures.js +6 -1
  88. package/dist/procedures/collection/procedures.js.map +1 -1
  89. package/dist/procedures/collection/schemas.d.ts +16 -1
  90. package/dist/procedures/collection/schemas.d.ts.map +1 -1
  91. package/dist/procedures/collection/schemas.js +13 -2
  92. package/dist/procedures/collection/schemas.js.map +1 -1
  93. package/dist/procedures/collection/storage.d.ts +14 -2
  94. package/dist/procedures/collection/storage.d.ts.map +1 -1
  95. package/dist/procedures/core/array.d.ts +1 -1
  96. package/dist/procedures/core/array.d.ts.map +1 -1
  97. package/dist/procedures/core/array.js +56 -15
  98. package/dist/procedures/core/array.js.map +1 -1
  99. package/dist/procedures/core/index.d.ts.map +1 -1
  100. package/dist/procedures/core/index.js +9 -2
  101. package/dist/procedures/core/index.js.map +1 -1
  102. package/dist/procedures/core/logic.js +1 -1
  103. package/dist/procedures/core/logic.js.map +1 -1
  104. package/dist/procedures/core/math.d.ts.map +1 -1
  105. package/dist/procedures/core/math.js +5 -1
  106. package/dist/procedures/core/math.js.map +1 -1
  107. package/dist/procedures/core/meta.d.ts.map +1 -1
  108. package/dist/procedures/core/meta.js +41 -7
  109. package/dist/procedures/core/meta.js.map +1 -1
  110. package/dist/procedures/core/type.d.ts.map +1 -1
  111. package/dist/procedures/core/type.js +4 -2
  112. package/dist/procedures/core/type.js.map +1 -1
  113. package/dist/procedures/discovery.d.ts +4 -6
  114. package/dist/procedures/discovery.d.ts.map +1 -1
  115. package/dist/procedures/discovery.js +4 -6
  116. package/dist/procedures/discovery.js.map +1 -1
  117. package/dist/procedures/invoke.d.ts +4 -2
  118. package/dist/procedures/invoke.d.ts.map +1 -1
  119. package/dist/procedures/invoke.js +1 -1
  120. package/dist/procedures/invoke.js.map +1 -1
  121. package/dist/procedures/ref.d.ts +4 -2
  122. package/dist/procedures/ref.d.ts.map +1 -1
  123. package/dist/procedures/ref.js +2 -1
  124. package/dist/procedures/ref.js.map +1 -1
  125. package/dist/protocol.d.ts +10 -3
  126. package/dist/protocol.d.ts.map +1 -1
  127. package/dist/protocol.js +13 -2
  128. package/dist/protocol.js.map +1 -1
  129. package/dist/server/host-policy.d.ts +11 -0
  130. package/dist/server/host-policy.d.ts.map +1 -1
  131. package/dist/server/host-policy.js +5 -0
  132. package/dist/server/host-policy.js.map +1 -1
  133. package/dist/server/procedure-server.d.ts +26 -3
  134. package/dist/server/procedure-server.d.ts.map +1 -1
  135. package/dist/server/procedure-server.js +38 -6
  136. package/dist/server/procedure-server.js.map +1 -1
  137. package/package.json +7 -6
  138. package/src/adapters/http/client/transport.ts +9 -2
  139. package/src/adapters/http/server/transport.ts +8 -6
  140. package/src/adapters/local/client/transport.ts +12 -4
  141. package/src/adapters/local/client/types.ts +7 -0
  142. package/src/adapters/sequence.ts +55 -0
  143. package/src/adapters/websocket/client/node.ts +34 -0
  144. package/src/adapters/websocket/client/transport.ts +128 -16
  145. package/src/adapters/websocket/client/types.ts +16 -5
  146. package/src/adapters/websocket/server/transport.ts +43 -1
  147. package/src/adapters/websocket/server/types.ts +7 -0
  148. package/src/adapters/websocket/shared/rpc.ts +19 -9
  149. package/src/browser.ts +15 -0
  150. package/src/client/errors/README.md +2 -0
  151. package/src/client/errors/http-errors.ts +5 -0
  152. package/src/client/errors/index.ts +1 -1
  153. package/src/client/errors/registry.ts +80 -22
  154. package/src/client/middleware/rate-limit.ts +0 -2
  155. package/src/client/middleware/timeout.ts +2 -9
  156. package/src/events/index.ts +16 -0
  157. package/src/events/publisher.ts +314 -0
  158. package/src/events/remote.ts +369 -0
  159. package/src/index.ts +4 -0
  160. package/src/node.ts +11 -0
  161. package/src/procedures/collection/index.ts +1 -0
  162. package/src/procedures/collection/procedures.ts +6 -1
  163. package/src/procedures/collection/schemas.ts +28 -2
  164. package/src/procedures/collection/storage.ts +14 -2
  165. package/src/procedures/core/array.ts +53 -16
  166. package/src/procedures/core/index.ts +9 -2
  167. package/src/procedures/core/logic.ts +1 -1
  168. package/src/procedures/core/math.ts +5 -1
  169. package/src/procedures/core/meta.ts +43 -7
  170. package/src/procedures/core/type.ts +4 -2
  171. package/src/procedures/discovery.ts +4 -6
  172. package/src/procedures/invoke.ts +4 -2
  173. package/src/procedures/ref.ts +4 -2
  174. package/src/protocol.ts +16 -3
  175. package/src/server/host-policy.ts +14 -0
  176. package/src/server/procedure-server.ts +45 -8
  177. package/dist/procedures/cli/announce.d.ts +0 -67
  178. package/dist/procedures/cli/announce.d.ts.map +0 -1
  179. package/dist/procedures/cli/announce.js +0 -392
  180. package/dist/procedures/cli/announce.js.map +0 -1
  181. package/dist/procedures/cli/discover.d.ts +0 -66
  182. package/dist/procedures/cli/discover.d.ts.map +0 -1
  183. package/dist/procedures/cli/discover.js +0 -310
  184. package/dist/procedures/cli/discover.js.map +0 -1
  185. package/dist/procedures/cli/index.d.ts +0 -13
  186. package/dist/procedures/cli/index.d.ts.map +0 -1
  187. package/dist/procedures/cli/index.js +0 -203
  188. package/dist/procedures/cli/index.js.map +0 -1
  189. package/dist/procedures/cli/init.d.ts +0 -17
  190. package/dist/procedures/cli/init.d.ts.map +0 -1
  191. package/dist/procedures/cli/init.js +0 -336
  192. package/dist/procedures/cli/init.js.map +0 -1
  193. package/src/procedures/cli/announce.ts +0 -470
  194. package/src/procedures/cli/discover.ts +0 -390
  195. package/src/procedures/cli/index.ts +0 -231
  196. package/src/procedures/cli/init.ts +0 -370
@@ -0,0 +1,268 @@
1
+ /**
2
+ * The publisher of the remote events (`documentation/EVENTS.md`).
3
+ *
4
+ * An `EventPublisher` serves the procedure `events.subscribe` from an event bus. A host gives it
5
+ * to `ProcedureServer` (the option `events`) or to `LocalTransport`. Each subscription is a stream
6
+ * without an end: its first item confirms the listeners, and each other item is one event of the
7
+ * bus.
8
+ *
9
+ * - The topic rule (`topics`) is the expose rule of the topics. A topic outside it gives
10
+ * `TOPIC_NOT_FOUND`.
11
+ * - `allow` examines each topic with the capability of the call. A refusal gives `FORBIDDEN` with
12
+ * the reason `topic-not-allowed`.
13
+ * - The procedure declares that it reads the resource `events`, so the capability of the host
14
+ * applies to it as to each procedure.
15
+ *
16
+ * The delivery is at most once. The queue of a subscription holds at most `buffer` events. When
17
+ * it is full, the publisher drops the oldest event, and the next event tells the number of lost
18
+ * events (`dropped`). The bus never waits for a subscriber.
19
+ */
20
+ import { z } from "zod";
21
+ import { createProcedure } from "../procedures/define.js";
22
+ import { InvocationError } from "../procedures/invoke.js";
23
+ import { getGlobalEventBus } from "./bus.js";
24
+ import { EVENTS_SUBSCRIBE_PATH } from "./remote.js";
25
+ /** The number of topics that one subscription can name. */
26
+ export const MAX_SUBSCRIPTION_TOPICS = 64;
27
+ /** The number of characters of a topic name. */
28
+ export const MAX_TOPIC_LENGTH = 256;
29
+ /** This function gives the test of a topic rule. */
30
+ export function topicMatcher(rule) {
31
+ if (typeof rule === "function")
32
+ return rule;
33
+ const exact = new Set();
34
+ const prefixes = [];
35
+ let all = false;
36
+ for (const pattern of rule) {
37
+ if (pattern === "*")
38
+ all = true;
39
+ else if (pattern.endsWith("*"))
40
+ prefixes.push(pattern.slice(0, -1));
41
+ else
42
+ exact.add(pattern);
43
+ }
44
+ return (topic) => all || exact.has(topic) || prefixes.some((prefix) => topic.startsWith(prefix));
45
+ }
46
+ /** The error of a topic outside the topic rule. */
47
+ class TopicNotFoundError extends Error {
48
+ name = "TopicNotFoundError";
49
+ code = "TOPIC_NOT_FOUND";
50
+ retryable = false;
51
+ }
52
+ /** The error that ends each subscription when the publisher stops. A resubscribe can succeed later. */
53
+ function stoppedError() {
54
+ return Object.assign(new Error("The publisher of the events stops"), { code: "UNAVAILABLE", retryable: true });
55
+ }
56
+ const topicSchema = z.string().min(1).max(MAX_TOPIC_LENGTH);
57
+ const subscribeInput = z.object({
58
+ topics: z.array(topicSchema).min(1).max(MAX_SUBSCRIPTION_TOPICS).describe("The topics of the subscription"),
59
+ });
60
+ const eventItemSchema = z.discriminatedUnion("type", [
61
+ z.object({
62
+ type: z.literal("subscribed"),
63
+ topics: z.array(z.string()).describe("The topics that the publisher listens to for this subscription"),
64
+ }),
65
+ z.object({
66
+ type: z.literal("event"),
67
+ topic: z.string(),
68
+ data: z.unknown(),
69
+ seq: z.number().int().describe("The number of the event in the subscription, from 1"),
70
+ dropped: z.number().int().optional().describe("The events that the publisher dropped immediately before this one"),
71
+ }),
72
+ ]);
73
+ /**
74
+ * One subscription: the listeners on the bus and the queue of its events. It is the async
75
+ * iterator of the stream of the call. `return()`, the abort of the signal of the call and
76
+ * `fail()` remove the listeners.
77
+ */
78
+ class TopicSubscription {
79
+ buffer;
80
+ signal;
81
+ onClose;
82
+ confirmation;
83
+ queue = [];
84
+ seq = 0;
85
+ dropped = 0;
86
+ waiter;
87
+ closed = false;
88
+ failure;
89
+ unsubscribes;
90
+ constructor(bus, topics, buffer, signal, onClose) {
91
+ this.buffer = buffer;
92
+ this.signal = signal;
93
+ this.onClose = onClose;
94
+ this.confirmation = { type: "subscribed", topics };
95
+ this.unsubscribes = topics.map((topic) => bus.on(topic, (data) => this.push(topic, data)));
96
+ signal?.addEventListener("abort", this.onAbort, { once: true });
97
+ }
98
+ /** This function closes a subscription whose call aborted before it started. */
99
+ closeIfAborted() {
100
+ if (this.signal?.aborted)
101
+ this.close();
102
+ return this;
103
+ }
104
+ onAbort = () => this.close();
105
+ /** This function takes one event of the bus. It never waits. */
106
+ push(topic, data) {
107
+ if (this.closed)
108
+ return;
109
+ const event = { type: "event", topic, data, seq: ++this.seq };
110
+ const waiter = this.waiter;
111
+ if (waiter && this.queue.length === 0) {
112
+ this.waiter = undefined;
113
+ waiter.resolve({ value: this.withDropped(event), done: false });
114
+ return;
115
+ }
116
+ if (this.queue.length >= this.buffer) {
117
+ // A slow subscriber: the oldest event goes, and the next event tells the loss
118
+ this.queue.shift();
119
+ this.dropped++;
120
+ }
121
+ this.queue.push(event);
122
+ }
123
+ /** The event with the number of events that the publisher dropped before it. */
124
+ withDropped(event) {
125
+ if (this.dropped === 0)
126
+ return event;
127
+ const dropped = this.dropped;
128
+ this.dropped = 0;
129
+ return { ...event, dropped };
130
+ }
131
+ next() {
132
+ if (this.confirmation) {
133
+ const confirmation = this.confirmation;
134
+ this.confirmation = undefined;
135
+ return Promise.resolve({ value: confirmation, done: false });
136
+ }
137
+ const event = this.queue.shift();
138
+ if (event)
139
+ return Promise.resolve({ value: this.withDropped(event), done: false });
140
+ if (this.failure !== undefined) {
141
+ const failure = this.failure;
142
+ this.failure = undefined;
143
+ return Promise.reject(failure);
144
+ }
145
+ if (this.closed)
146
+ return Promise.resolve({ value: undefined, done: true });
147
+ return new Promise((resolve, reject) => {
148
+ this.waiter = { resolve, reject };
149
+ });
150
+ }
151
+ return() {
152
+ this.close();
153
+ return Promise.resolve({ value: undefined, done: true });
154
+ }
155
+ [Symbol.asyncIterator]() {
156
+ return this;
157
+ }
158
+ /** This function ends the subscription with an error: the reader gets it at its next item. */
159
+ fail(error) {
160
+ if (this.closed)
161
+ return;
162
+ const waiter = this.waiter;
163
+ this.waiter = undefined;
164
+ this.release();
165
+ this.queue.length = 0;
166
+ this.confirmation = undefined;
167
+ if (waiter)
168
+ waiter.reject(error);
169
+ else
170
+ this.failure = error;
171
+ }
172
+ /** This function ends the subscription without an error. */
173
+ close() {
174
+ if (this.closed)
175
+ return;
176
+ const waiter = this.waiter;
177
+ this.waiter = undefined;
178
+ this.release();
179
+ this.queue.length = 0;
180
+ this.confirmation = undefined;
181
+ waiter?.resolve({ value: undefined, done: true });
182
+ }
183
+ /** This function removes the listeners from the bus and from the signal. */
184
+ release() {
185
+ this.closed = true;
186
+ for (const unsubscribe of this.unsubscribes)
187
+ unsubscribe();
188
+ this.signal?.removeEventListener("abort", this.onAbort);
189
+ this.onClose(this);
190
+ }
191
+ }
192
+ /**
193
+ * The publisher of the remote events: it serves `events.subscribe` from a bus, under a topic
194
+ * rule. `ProcedureServer` makes one from its option `events`.
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * const publisher = new EventPublisher({ bus, topics: ["orders.*"] });
199
+ * const server = new ProcedureServer({ registry, autoRegister: true, events: publisher });
200
+ * publisher.publish("orders.created", { order: 1 });
201
+ * ```
202
+ */
203
+ export class EventPublisher {
204
+ /** The bus that the subscriptions read. */
205
+ bus;
206
+ /** The procedure `events.subscribe` of this publisher. A host pins it, outside its registry. */
207
+ procedure;
208
+ known;
209
+ allow;
210
+ buffer;
211
+ open = new Set();
212
+ constructor(options) {
213
+ this.bus = options.bus ?? getGlobalEventBus();
214
+ this.known = topicMatcher(options.topics);
215
+ this.allow = options.allow;
216
+ this.buffer = Math.max(1, Math.floor(options.buffer ?? 1024));
217
+ this.procedure = createProcedure()
218
+ .path([...EVENTS_SUBSCRIBE_PATH])
219
+ .input(subscribeInput)
220
+ .output(eventItemSchema)
221
+ .meta({
222
+ description: "A subscription to topics of the event bus of the host: a stream without an end, with one item for each event",
223
+ effects: { reads: ["events"], writes: [] },
224
+ stream: { unbounded: true },
225
+ })
226
+ .handler((async (input, ctx) => this.subscribe(input.topics, ctx)))
227
+ .build();
228
+ }
229
+ /** This function publishes an event on the bus: each subscription of the topic gets it. */
230
+ publish(topic, data) {
231
+ this.bus.emit(topic, data);
232
+ }
233
+ /** The number of open subscriptions. */
234
+ get subscriptions() {
235
+ return this.open.size;
236
+ }
237
+ /**
238
+ * This function ends each open subscription with a retryable `UNAVAILABLE`: the publisher stops.
239
+ * A subscriber resubscribes after its backoff. `ProcedureServer.stop()` calls it first.
240
+ */
241
+ end() {
242
+ for (const subscription of [...this.open])
243
+ subscription.fail(stoppedError());
244
+ }
245
+ /**
246
+ * The stream of one subscription. The publisher examines each topic before it adds a listener:
247
+ * the topic rule first, then `allow`. A call with one bad topic makes no subscription.
248
+ */
249
+ subscribe(topics, ctx) {
250
+ const unique = [...new Set(topics)];
251
+ for (const topic of unique) {
252
+ if (!this.known(topic))
253
+ throw new TopicNotFoundError(`The host has no topic "${topic}"`);
254
+ }
255
+ if (this.allow) {
256
+ const call = { capability: ctx.capability, metadata: ctx.metadata };
257
+ for (const topic of unique) {
258
+ if (!this.allow(topic, call)) {
259
+ throw new InvocationError("FORBIDDEN", `The capability of the call does not permit the topic "${topic}"`, ctx.path, "topic-not-allowed");
260
+ }
261
+ }
262
+ }
263
+ const subscription = new TopicSubscription(this.bus, unique, this.buffer, ctx.signal, (closed) => this.open.delete(closed));
264
+ this.open.add(subscription);
265
+ return subscription.closeIfAborted();
266
+ }
267
+ }
268
+ //# sourceMappingURL=publisher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publisher.js","sourceRoot":"","sources":["../../src/events/publisher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAG1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,qBAAqB,EAAuC,MAAM,aAAa,CAAC;AAEzF,2DAA2D;AAC3D,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,CAAC;AAE1C,gDAAgD;AAChD,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAoCpC,oDAAoD;AACpD,MAAM,UAAU,YAAY,CAAC,IAAe;IAC1C,IAAI,OAAO,IAAI,KAAK,UAAU;QAAE,OAAO,IAAI,CAAC;IAC5C,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YAAY,GAAG,IAAI,iBAAiB,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;QAC5H,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QAC5B,OAAO,YAAY,CAAC,cAAc,EAAE,CAAC;IACvC,CAAC;CACF"}
@@ -0,0 +1,164 @@
1
+ /**
2
+ * The subscriber of the remote events (`documentation/EVENTS.md`).
3
+ *
4
+ * A peer subscribes to the topics of the bus of another peer with a streaming call of the
5
+ * procedure `events.subscribe`. The first item of the stream confirms the subscription, and each
6
+ * other item is one event. The frames are the frames of each call: credit is the backpressure,
7
+ * and a cancel is the unsubscribe. Thus each transport with streams carries a subscription:
8
+ * WebSocket in the two directions, HTTP with NDJSON, a message port and the local transport.
9
+ *
10
+ * - `remoteEvents()` gives an async iterator of the events.
11
+ * - `subscribeRemote()` calls a handler for each event, and gives the unsubscribe function.
12
+ *
13
+ * After a failure that a retry can repair (a closed connection, `UNAVAILABLE`, `TIMEOUT`), the
14
+ * subscriber sends the call again after a backoff. The delivery is at most once: the events
15
+ * between the failure and the new confirmation are lost. Each other error ends the subscription.
16
+ *
17
+ * This file imports only types and the backoff, so a page or a worker that subscribes stays small.
18
+ */
19
+ import type { Transport } from "../client/types.js";
20
+ /** The path of the procedure of a subscription. */
21
+ export declare const EVENTS_SUBSCRIBE_PATH: readonly ["events", "subscribe"];
22
+ /** The input of `events.subscribe`. */
23
+ export interface SubscribeInput {
24
+ /** The topics: from 1 to 64 names of 1 to 256 characters. */
25
+ readonly topics: readonly string[];
26
+ }
27
+ /**
28
+ * An item of the stream of `events.subscribe`. The first item is `subscribed`. Each other item is
29
+ * an `event`. A subscriber ignores an item of another type: it comes from a newer publisher.
30
+ */
31
+ export type EventItem = {
32
+ readonly type: "subscribed";
33
+ readonly topics: readonly string[];
34
+ } | {
35
+ readonly type: "event";
36
+ readonly topic: string;
37
+ readonly data: unknown;
38
+ /** The number of the event in its subscription, from 1. A gap shows lost events. */
39
+ readonly seq: number;
40
+ /** The number of events that the publisher dropped immediately before this one. */
41
+ readonly dropped?: number | undefined;
42
+ };
43
+ /** One event that a subscriber gets. */
44
+ export interface RemoteEvent<T = unknown> {
45
+ /** The topic (the channel of the bus of the publisher). */
46
+ readonly topic: string;
47
+ /** The data of the event, after a trip as JSON. */
48
+ readonly data: T;
49
+ /** The number of the event in its subscription, from 1. A resubscribe starts again at 1. */
50
+ readonly seq: number;
51
+ /** The number of events that the publisher dropped immediately before this one: 0 without a loss. */
52
+ readonly dropped: number;
53
+ /** The id of the subscription. It stays the same after a resubscribe. */
54
+ readonly subscription: string;
55
+ }
56
+ /**
57
+ * The handler of the events of a subscription. When it gives a promise, the subscriber waits for
58
+ * it before the next event, so a slow handler holds the credit of its own subscription only.
59
+ */
60
+ export type RemoteEventHandler<T = unknown> = (data: T, event: RemoteEvent<T>) => void | Promise<void>;
61
+ /** The options of a subscription. */
62
+ export interface RemoteEventsOptions {
63
+ /** When this signal aborts, the subscription ends: the transport sends a cancel. */
64
+ signal?: AbortSignal | undefined;
65
+ /**
66
+ * True: the subscriber sends the call again after a failure that a retry can repair. False: such
67
+ * a failure ends the subscription.
68
+ * @default true
69
+ */
70
+ resubscribe?: boolean | undefined;
71
+ /**
72
+ * This function gives the time before resubscribe try `attempt` (from 1), in milliseconds. The
73
+ * default is an exponential backoff from 250 ms to 30 s with full jitter. The WebSocket
74
+ * transport uses its reconnect backoff.
75
+ */
76
+ backoff?: ((attempt: number) => number) | undefined;
77
+ /** This function gets the topics of each confirmation: the first one, and one after each resubscribe. */
78
+ onSubscribed?: ((topics: readonly string[]) => void) | undefined;
79
+ /** The metadata of the call, for example a trace. */
80
+ metadata?: Record<string, unknown> | undefined;
81
+ }
82
+ /** The options of `subscribeRemote()`. */
83
+ export interface SubscribeOptions extends RemoteEventsOptions {
84
+ /**
85
+ * This function gets the error that ends the subscription: a refusal (`TOPIC_NOT_FOUND`,
86
+ * `FORBIDDEN`), an older publisher (`NOT_FOUND`), or a transport that closed. An unsubscribe is
87
+ * not an error.
88
+ */
89
+ onError?: ((error: SubscriptionError) => void) | undefined;
90
+ }
91
+ /**
92
+ * The unsubscribe function of a subscription. A second call does nothing. `ready` resolves at the
93
+ * first confirmation of the publisher. It rejects when the subscription ends before it.
94
+ */
95
+ export interface Subscription {
96
+ (): void;
97
+ /** This promise resolves when the publisher confirms the subscription the first time. */
98
+ readonly ready: Promise<void>;
99
+ /** The id of the subscription: the `subscription` of its events. */
100
+ readonly id: string;
101
+ }
102
+ /** The error that ends a subscription. */
103
+ export declare class SubscriptionError extends Error {
104
+ /** The code: a code of the protocol, for example `TOPIC_NOT_FOUND`. */
105
+ readonly code: string;
106
+ /** True when a resubscribe can repair the failure. */
107
+ readonly retryable: boolean;
108
+ /** The reason of a `FORBIDDEN` error, for example `topic-not-allowed`. */
109
+ readonly reason?: string | undefined;
110
+ readonly name = "SubscriptionError";
111
+ constructor(
112
+ /** The code: a code of the protocol, for example `TOPIC_NOT_FOUND`. */
113
+ code: string, message: string,
114
+ /** True when a resubscribe can repair the failure. */
115
+ retryable: boolean,
116
+ /** The reason of a `FORBIDDEN` error, for example `topic-not-allowed`. */
117
+ reason?: string | undefined);
118
+ }
119
+ /**
120
+ * The parts that a transport adds to its subscriptions. The WebSocket transport waits for its
121
+ * connection, uses its reconnect backoff, ends its subscriptions at `close()`, and gives each
122
+ * event to its `onEvent` option.
123
+ */
124
+ export interface SubscriptionHooks {
125
+ /** When this signal aborts, each subscription ends without an error. */
126
+ readonly lifetime?: AbortSignal | undefined;
127
+ /**
128
+ * This function resolves when the transport can send the call. It rejects when the transport
129
+ * closed. After a lost connection, the subscriber waits for it, not for a backoff.
130
+ */
131
+ readonly ready?: ((signal: AbortSignal) => Promise<void>) | undefined;
132
+ /** The backoff, when the options give none. */
133
+ readonly backoff?: ((attempt: number) => number) | undefined;
134
+ /** This function gets each event of each subscription. */
135
+ readonly onEvent?: ((event: RemoteEvent) => void) | undefined;
136
+ /** False: no resubscribe, whatever the options say (a server cannot reach a closed connection). */
137
+ readonly resubscribe?: boolean | undefined;
138
+ }
139
+ /** The part of a transport that a subscription uses. */
140
+ export type EventTransport = Pick<Transport, "send">;
141
+ /**
142
+ * This function gives the events of a subscription to topics of the bus of the other side. The
143
+ * iteration sends the call. A `break` of the reader, or the abort of the signal, sends the cancel.
144
+ * An error that ends the subscription ends the iteration with a `SubscriptionError`.
145
+ *
146
+ * @param transport - The transport to the publisher: HTTP, WebSocket, a message port, local
147
+ * @param topics - One topic, or a list of 1 to 64 topics
148
+ * @param options - The signal, the resubscribe, the backoff and the confirmation handler
149
+ * @param hooks - The parts that a transport adds (its connection, its backoff, its `onEvent`)
150
+ */
151
+ export declare function remoteEvents<T = unknown>(transport: EventTransport, topics: string | readonly string[], options?: RemoteEventsOptions, hooks?: SubscriptionHooks): AsyncIterable<RemoteEvent<T>>;
152
+ /**
153
+ * This function subscribes to topics of the bus of the other side, and calls the handler for each
154
+ * event in order. It gives the unsubscribe function, with the `ready` promise of the first
155
+ * confirmation. An error of the handler goes to `console.error`, and the subscription goes on.
156
+ *
157
+ * @param transport - The transport to the publisher
158
+ * @param topics - One topic, or a list of 1 to 64 topics
159
+ * @param handler - The handler of each event: it gets the data and the event
160
+ * @param options - The options of `remoteEvents()`, and the `onError` handler of the end
161
+ * @param hooks - The parts that a transport adds
162
+ */
163
+ export declare function subscribeRemote<T = unknown>(transport: EventTransport, topics: string | readonly string[], handler: RemoteEventHandler<T>, options?: SubscribeOptions, hooks?: SubscriptionHooks): Subscription;
164
+ //# sourceMappingURL=remote.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remote.d.ts","sourceRoot":"","sources":["../../src/events/remote.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,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