@orpc/experimental-durable-iterator 0.0.0-next.01f0b7a
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/LICENSE +21 -0
- package/README.md +81 -0
- package/dist/client/index.d.mts +69 -0
- package/dist/client/index.d.ts +69 -0
- package/dist/client/index.mjs +160 -0
- package/dist/durable-object/index.d.mts +286 -0
- package/dist/durable-object/index.d.ts +286 -0
- package/dist/durable-object/index.mjs +536 -0
- package/dist/index.d.mts +85 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.mjs +105 -0
- package/dist/shared/experimental-durable-iterator.B3M42lLK.mjs +29 -0
- package/dist/shared/experimental-durable-iterator.BRB0hiXN.mjs +15 -0
- package/dist/shared/experimental-durable-iterator.C6YPLbUA.d.ts +28 -0
- package/dist/shared/experimental-durable-iterator.DQjHfIr1.d.mts +42 -0
- package/dist/shared/experimental-durable-iterator.DQjHfIr1.d.ts +42 -0
- package/dist/shared/experimental-durable-iterator.DZOLL3sf.mjs +47 -0
- package/dist/shared/experimental-durable-iterator.DrenXxND.d.mts +28 -0
- package/package.json +55 -0
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { RPCHandlerOptions } from '@orpc/server/websocket';
|
|
2
|
+
import { DurableObject } from 'cloudflare:workers';
|
|
3
|
+
import { b as DurableIteratorTokenPayload, a as DurableIteratorObjectDef, D as DurableIteratorObject$1 } from '../shared/experimental-durable-iterator.DQjHfIr1.mjs';
|
|
4
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
|
5
|
+
import { Interceptor } from '@orpc/shared';
|
|
6
|
+
export { withEventMeta } from '@orpc/server';
|
|
7
|
+
import '@orpc/client';
|
|
8
|
+
import 'valibot';
|
|
9
|
+
|
|
10
|
+
interface DurableIteratorWebsocketInternal {
|
|
11
|
+
/**
|
|
12
|
+
* Access the original websocket instance
|
|
13
|
+
*
|
|
14
|
+
* @warning Be careful when using original because you can accidentally modifying internal state.
|
|
15
|
+
*/
|
|
16
|
+
original: WebSocket;
|
|
17
|
+
/**
|
|
18
|
+
* Serialize the websocket id
|
|
19
|
+
*
|
|
20
|
+
* @warning this method should be called when client established connection
|
|
21
|
+
*/
|
|
22
|
+
serializeId(id: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Deserialize the websocket id
|
|
25
|
+
*
|
|
26
|
+
* @warning this method assumes that the id is already set when client established connection
|
|
27
|
+
*/
|
|
28
|
+
deserializeId(): string;
|
|
29
|
+
/**
|
|
30
|
+
* Serialize the token payload usually when client connected
|
|
31
|
+
*
|
|
32
|
+
* @warning this method should be called when client established connection or when token payload is updated
|
|
33
|
+
*/
|
|
34
|
+
serializeTokenPayload(payload: DurableIteratorTokenPayload): void;
|
|
35
|
+
/**
|
|
36
|
+
* Deserialize the payload attached when client connected
|
|
37
|
+
*
|
|
38
|
+
* @warning this method assumes that the token payload is already set when client established connection
|
|
39
|
+
*/
|
|
40
|
+
deserializeTokenPayload(): DurableIteratorTokenPayload;
|
|
41
|
+
/**
|
|
42
|
+
* Serialize the hibernation id used for publishing events to the client
|
|
43
|
+
*/
|
|
44
|
+
serializeHibernationId(id: string): void;
|
|
45
|
+
/**
|
|
46
|
+
* Deserialize the hibernation id used for publishing events to the client
|
|
47
|
+
*/
|
|
48
|
+
deserializeHibernationId(): string | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Close the websocket connection if expired
|
|
51
|
+
*
|
|
52
|
+
* @warning this method assumes that the token payload is already set when client established connection
|
|
53
|
+
*/
|
|
54
|
+
closeIfExpired(): void;
|
|
55
|
+
}
|
|
56
|
+
interface DurableIteratorWebsocket extends WebSocket {
|
|
57
|
+
/**
|
|
58
|
+
* Durable Event internal apis
|
|
59
|
+
*/
|
|
60
|
+
['~orpc']: DurableIteratorWebsocketInternal;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Create a Durable Iterator WebSocket from a regular WebSocket
|
|
64
|
+
*
|
|
65
|
+
* @info The websocket automatically closes if expired before sending data
|
|
66
|
+
*/
|
|
67
|
+
declare function toDurableIteratorWebsocket(original: WebSocket): DurableIteratorWebsocket;
|
|
68
|
+
|
|
69
|
+
interface DurableIteratorObjectStateInternal {
|
|
70
|
+
/**
|
|
71
|
+
* The original DurableObjectState
|
|
72
|
+
*
|
|
73
|
+
* @warning Be careful when using original because you can accidentally modifying internal state.
|
|
74
|
+
*/
|
|
75
|
+
original: DurableObjectState;
|
|
76
|
+
}
|
|
77
|
+
interface DurableIteratorObjectState<TProps> extends DurableObjectState<TProps> {
|
|
78
|
+
/**
|
|
79
|
+
* DurableIteratorObjectState internal apis
|
|
80
|
+
*/
|
|
81
|
+
'~orpc': DurableIteratorObjectStateInternal;
|
|
82
|
+
/**
|
|
83
|
+
* Get all WebSockets connected to this Durable Object
|
|
84
|
+
* And convert them to DurableIteratorWebsocket to avoid accidentally modifying internal state
|
|
85
|
+
*/
|
|
86
|
+
'getWebSockets'(...args: Parameters<DurableObjectState['getWebSockets']>): DurableIteratorWebsocket[];
|
|
87
|
+
}
|
|
88
|
+
declare function toDurableIteratorObjectState<TProps>(original: DurableObjectState<TProps>): DurableIteratorObjectState<TProps>;
|
|
89
|
+
|
|
90
|
+
interface EventResumeStorageOptions extends StandardRPCJsonSerializerOptions {
|
|
91
|
+
/**
|
|
92
|
+
* How long (in seconds) to retain events for reconnection replay.
|
|
93
|
+
*
|
|
94
|
+
* When a client reconnects, stored events within this window can be replayed
|
|
95
|
+
* to ensure no data is lost. Outside this window, missed events are dropped.
|
|
96
|
+
*
|
|
97
|
+
* @remarks
|
|
98
|
+
* - Use infinite values to disable
|
|
99
|
+
*
|
|
100
|
+
* @default NaN (disabled)
|
|
101
|
+
*/
|
|
102
|
+
resumeRetentionSeconds?: number;
|
|
103
|
+
/**
|
|
104
|
+
* Prefix for the resume storage table schema.
|
|
105
|
+
* This is used to avoid naming conflicts with other tables in the same Durable Object.
|
|
106
|
+
*
|
|
107
|
+
* @default 'orpc:durable-iterator:resume:'
|
|
108
|
+
*/
|
|
109
|
+
resumeSchemaPrefix?: string;
|
|
110
|
+
}
|
|
111
|
+
interface ResumeEventFilter {
|
|
112
|
+
/** Only websockets with these tags will receive the event */
|
|
113
|
+
tags?: readonly string[];
|
|
114
|
+
/** Only websockets that are in this list will receive the event */
|
|
115
|
+
targets?: readonly DurableIteratorWebsocket[];
|
|
116
|
+
/** Websockets that are in this list will not receive the event */
|
|
117
|
+
exclude?: readonly DurableIteratorWebsocket[];
|
|
118
|
+
}
|
|
119
|
+
declare class EventResumeStorage<T extends object> {
|
|
120
|
+
private readonly durableState;
|
|
121
|
+
private readonly serializer;
|
|
122
|
+
private readonly retentionSeconds;
|
|
123
|
+
private readonly schemaPrefix;
|
|
124
|
+
get isEnabled(): boolean;
|
|
125
|
+
constructor(durableState: DurableObjectState, options?: EventResumeStorageOptions);
|
|
126
|
+
/**
|
|
127
|
+
* Store an payload for resume capability.
|
|
128
|
+
*
|
|
129
|
+
* @returns The updated meta of the stored payload
|
|
130
|
+
*/
|
|
131
|
+
store(payload: T, resumeFilter: ResumeEventFilter): T;
|
|
132
|
+
/**
|
|
133
|
+
* Get events after lastEventId for a specific websocket
|
|
134
|
+
*/
|
|
135
|
+
get(websocket: DurableIteratorWebsocket, lastEventId: string): T[];
|
|
136
|
+
private initSchema;
|
|
137
|
+
private resetSchema;
|
|
138
|
+
private cleanupExpiredEvents;
|
|
139
|
+
private serializeEventPayload;
|
|
140
|
+
private deserializeEventPayload;
|
|
141
|
+
private withEventId;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
type DurableIteratorObjectRouterContext = {
|
|
145
|
+
object: DurableObject<any, any>;
|
|
146
|
+
resumeStorage: EventResumeStorage<any>;
|
|
147
|
+
websocket: DurableIteratorWebsocket;
|
|
148
|
+
options: DurableIteratorObjectHandlerOptions;
|
|
149
|
+
};
|
|
150
|
+
interface PublishEventOptions {
|
|
151
|
+
/**
|
|
152
|
+
* Deliver the event only to websockets that have the specified tags.
|
|
153
|
+
*/
|
|
154
|
+
tags?: readonly string[];
|
|
155
|
+
/**
|
|
156
|
+
* Restrict the event to a specific set of websockets.
|
|
157
|
+
*
|
|
158
|
+
* Accept a list of websockets or a filter function.
|
|
159
|
+
*
|
|
160
|
+
* Use this when security is important — only the listed websockets
|
|
161
|
+
* will ever receive the event. Newly connected websockets are not
|
|
162
|
+
* included unless explicitly added here.
|
|
163
|
+
*/
|
|
164
|
+
targets?: readonly WebSocket[] | ((ws: DurableIteratorWebsocket) => boolean);
|
|
165
|
+
/**
|
|
166
|
+
* Exclude certain websockets from receiving the event.
|
|
167
|
+
*
|
|
168
|
+
* Accept a list of websockets or a filter function.
|
|
169
|
+
*
|
|
170
|
+
* Use this when broadcasting widely but skipping a few clients
|
|
171
|
+
* (e.g., the sender). Newly connected websockets may still receive
|
|
172
|
+
* the event if not listed here, so this is less strict than `targets`.
|
|
173
|
+
*/
|
|
174
|
+
exclude?: readonly WebSocket[] | ((ws: DurableIteratorWebsocket) => boolean);
|
|
175
|
+
}
|
|
176
|
+
interface DurableIteratorObjectHandlerOptions extends RPCHandlerOptions<DurableIteratorObjectRouterContext>, EventResumeStorageOptions {
|
|
177
|
+
/**
|
|
178
|
+
* The signing key to use verify the token.
|
|
179
|
+
*/
|
|
180
|
+
signingKey: string;
|
|
181
|
+
/**
|
|
182
|
+
* Called after a client successfully subscribes to the main iterator.
|
|
183
|
+
* You can start sending events to the client here.
|
|
184
|
+
*
|
|
185
|
+
* @param websocket Corresponding WebSocket connection.
|
|
186
|
+
* @param lastEventId Can be `undefined` if this is the first connection (not a resumed session).
|
|
187
|
+
*/
|
|
188
|
+
onSubscribed?: (websocket: DurableIteratorWebsocket, lastEventId: string | undefined) => void;
|
|
189
|
+
}
|
|
190
|
+
declare class DurableIteratorObjectHandler<T extends object, TProps> implements DurableIteratorObjectDef<T> {
|
|
191
|
+
private readonly object;
|
|
192
|
+
private readonly options;
|
|
193
|
+
'~eventPayloadType'?: {
|
|
194
|
+
type: T;
|
|
195
|
+
};
|
|
196
|
+
private readonly handler;
|
|
197
|
+
private readonly resumeStorage;
|
|
198
|
+
/**
|
|
199
|
+
* Proxied, ensure you don't accidentally change internal state, and auto close if expired websockets before .send is called
|
|
200
|
+
*/
|
|
201
|
+
ctx: DurableIteratorObjectState<TProps>;
|
|
202
|
+
constructor(ctx: DurableObjectState<TProps>, object: DurableObject<any, TProps>, options: DurableIteratorObjectHandlerOptions);
|
|
203
|
+
/**
|
|
204
|
+
* Publish an event to a set of clients.
|
|
205
|
+
*/
|
|
206
|
+
publishEvent(payload: T, options?: PublishEventOptions): void;
|
|
207
|
+
/**
|
|
208
|
+
* This method is called when a HTTP request is received for upgrading to a WebSocket connection.
|
|
209
|
+
* Should mapping with corresponding `fetch` inside durable object
|
|
210
|
+
*/
|
|
211
|
+
fetch(request: Request): Promise<Response>;
|
|
212
|
+
/**
|
|
213
|
+
* This method is called when a WebSocket message is received.
|
|
214
|
+
* Should mapping with corresponding `webSocketMessage` inside durable object
|
|
215
|
+
*/
|
|
216
|
+
webSocketMessage(websocket_: WebSocket, message: string | ArrayBuffer): Promise<void>;
|
|
217
|
+
/**
|
|
218
|
+
* This method is called when a WebSocket connection is closed.
|
|
219
|
+
* Should mapping with corresponding `webSocketClose` inside durable object
|
|
220
|
+
*/
|
|
221
|
+
webSocketClose(ws_: WebSocket, _code: number, _reason: string, _wasClean: boolean): void | Promise<void>;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
declare class DurableIteratorObject<T extends object, TEnv = Cloudflare.Env, TProps = {}> extends DurableObject<TEnv, TProps> implements DurableIteratorObject$1<T> {
|
|
225
|
+
'~orpc': DurableIteratorObjectHandler<T, TProps>;
|
|
226
|
+
/**
|
|
227
|
+
* Proxied, ensure you don't accidentally change internal state, and auto close if expired websockets before .send is called
|
|
228
|
+
*/
|
|
229
|
+
protected ctx: DurableIteratorObjectState<TProps>;
|
|
230
|
+
constructor(ctx: DurableObjectState<TProps>, env: TEnv, options: DurableIteratorObjectHandlerOptions);
|
|
231
|
+
/**
|
|
232
|
+
* Publish an event to clients
|
|
233
|
+
*/
|
|
234
|
+
publishEvent(payload: T, options?: PublishEventOptions): void;
|
|
235
|
+
/**
|
|
236
|
+
* Upgrades websocket connection
|
|
237
|
+
*
|
|
238
|
+
* @info You can safety intercept non-upgrade requests
|
|
239
|
+
* @warning No verification is done here, you should verify the token payload before calling this method.
|
|
240
|
+
*/
|
|
241
|
+
fetch(request: Request): Promise<Response>;
|
|
242
|
+
/**
|
|
243
|
+
* Handle WebSocket messages
|
|
244
|
+
*
|
|
245
|
+
* @warning Use `toDurableIteratorWebsocket` to proxy the WebSocket when interacting
|
|
246
|
+
* to avoid accidentally modifying internal state, and auto close if expired before .send is called
|
|
247
|
+
*/
|
|
248
|
+
webSocketMessage(websocket: WebSocket, message: string | ArrayBuffer): Promise<void>;
|
|
249
|
+
/**
|
|
250
|
+
* Handle WebSocket close event
|
|
251
|
+
*
|
|
252
|
+
* @warning Use `toDurableIteratorWebsocket` to proxy the WebSocket when interacting
|
|
253
|
+
* to avoid accidentally modifying internal state, and auto close if expired before .send is called
|
|
254
|
+
*/
|
|
255
|
+
webSocketClose(websocket: WebSocket, code: number, reason: string, wasClean: boolean): void | Promise<void>;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
interface UpgradeDurableIteratorRequestOptions {
|
|
259
|
+
/**
|
|
260
|
+
* The signing key used to verify the token
|
|
261
|
+
*/
|
|
262
|
+
signingKey: string;
|
|
263
|
+
/**
|
|
264
|
+
* The durable object namespace
|
|
265
|
+
*/
|
|
266
|
+
namespace: DurableObjectNamespace<any>;
|
|
267
|
+
/**
|
|
268
|
+
* The options to use when getting the durable object stub
|
|
269
|
+
*/
|
|
270
|
+
namespaceGetOptions?: DurableObjectNamespaceGetDurableObjectOptions;
|
|
271
|
+
/**
|
|
272
|
+
* intercept upgrade process
|
|
273
|
+
*/
|
|
274
|
+
interceptors?: Interceptor<{
|
|
275
|
+
payload: DurableIteratorTokenPayload;
|
|
276
|
+
}, Promise<Response>>[];
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Verifies and upgrades a durable iterator request.
|
|
280
|
+
*
|
|
281
|
+
* @info Verify token before forwarding to durable object to prevent DDoS attacks
|
|
282
|
+
*/
|
|
283
|
+
declare function upgradeDurableIteratorRequest(request: Request, options: UpgradeDurableIteratorRequestOptions): Promise<Response>;
|
|
284
|
+
|
|
285
|
+
export { DurableIteratorObject, DurableIteratorObjectHandler, EventResumeStorage, toDurableIteratorObjectState, toDurableIteratorWebsocket, upgradeDurableIteratorRequest };
|
|
286
|
+
export type { DurableIteratorObjectHandlerOptions, DurableIteratorObjectState, DurableIteratorObjectStateInternal, DurableIteratorWebsocket, DurableIteratorWebsocketInternal, EventResumeStorageOptions, PublishEventOptions, ResumeEventFilter, UpgradeDurableIteratorRequestOptions };
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { RPCHandlerOptions } from '@orpc/server/websocket';
|
|
2
|
+
import { DurableObject } from 'cloudflare:workers';
|
|
3
|
+
import { b as DurableIteratorTokenPayload, a as DurableIteratorObjectDef, D as DurableIteratorObject$1 } from '../shared/experimental-durable-iterator.DQjHfIr1.js';
|
|
4
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
|
5
|
+
import { Interceptor } from '@orpc/shared';
|
|
6
|
+
export { withEventMeta } from '@orpc/server';
|
|
7
|
+
import '@orpc/client';
|
|
8
|
+
import 'valibot';
|
|
9
|
+
|
|
10
|
+
interface DurableIteratorWebsocketInternal {
|
|
11
|
+
/**
|
|
12
|
+
* Access the original websocket instance
|
|
13
|
+
*
|
|
14
|
+
* @warning Be careful when using original because you can accidentally modifying internal state.
|
|
15
|
+
*/
|
|
16
|
+
original: WebSocket;
|
|
17
|
+
/**
|
|
18
|
+
* Serialize the websocket id
|
|
19
|
+
*
|
|
20
|
+
* @warning this method should be called when client established connection
|
|
21
|
+
*/
|
|
22
|
+
serializeId(id: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Deserialize the websocket id
|
|
25
|
+
*
|
|
26
|
+
* @warning this method assumes that the id is already set when client established connection
|
|
27
|
+
*/
|
|
28
|
+
deserializeId(): string;
|
|
29
|
+
/**
|
|
30
|
+
* Serialize the token payload usually when client connected
|
|
31
|
+
*
|
|
32
|
+
* @warning this method should be called when client established connection or when token payload is updated
|
|
33
|
+
*/
|
|
34
|
+
serializeTokenPayload(payload: DurableIteratorTokenPayload): void;
|
|
35
|
+
/**
|
|
36
|
+
* Deserialize the payload attached when client connected
|
|
37
|
+
*
|
|
38
|
+
* @warning this method assumes that the token payload is already set when client established connection
|
|
39
|
+
*/
|
|
40
|
+
deserializeTokenPayload(): DurableIteratorTokenPayload;
|
|
41
|
+
/**
|
|
42
|
+
* Serialize the hibernation id used for publishing events to the client
|
|
43
|
+
*/
|
|
44
|
+
serializeHibernationId(id: string): void;
|
|
45
|
+
/**
|
|
46
|
+
* Deserialize the hibernation id used for publishing events to the client
|
|
47
|
+
*/
|
|
48
|
+
deserializeHibernationId(): string | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Close the websocket connection if expired
|
|
51
|
+
*
|
|
52
|
+
* @warning this method assumes that the token payload is already set when client established connection
|
|
53
|
+
*/
|
|
54
|
+
closeIfExpired(): void;
|
|
55
|
+
}
|
|
56
|
+
interface DurableIteratorWebsocket extends WebSocket {
|
|
57
|
+
/**
|
|
58
|
+
* Durable Event internal apis
|
|
59
|
+
*/
|
|
60
|
+
['~orpc']: DurableIteratorWebsocketInternal;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Create a Durable Iterator WebSocket from a regular WebSocket
|
|
64
|
+
*
|
|
65
|
+
* @info The websocket automatically closes if expired before sending data
|
|
66
|
+
*/
|
|
67
|
+
declare function toDurableIteratorWebsocket(original: WebSocket): DurableIteratorWebsocket;
|
|
68
|
+
|
|
69
|
+
interface DurableIteratorObjectStateInternal {
|
|
70
|
+
/**
|
|
71
|
+
* The original DurableObjectState
|
|
72
|
+
*
|
|
73
|
+
* @warning Be careful when using original because you can accidentally modifying internal state.
|
|
74
|
+
*/
|
|
75
|
+
original: DurableObjectState;
|
|
76
|
+
}
|
|
77
|
+
interface DurableIteratorObjectState<TProps> extends DurableObjectState<TProps> {
|
|
78
|
+
/**
|
|
79
|
+
* DurableIteratorObjectState internal apis
|
|
80
|
+
*/
|
|
81
|
+
'~orpc': DurableIteratorObjectStateInternal;
|
|
82
|
+
/**
|
|
83
|
+
* Get all WebSockets connected to this Durable Object
|
|
84
|
+
* And convert them to DurableIteratorWebsocket to avoid accidentally modifying internal state
|
|
85
|
+
*/
|
|
86
|
+
'getWebSockets'(...args: Parameters<DurableObjectState['getWebSockets']>): DurableIteratorWebsocket[];
|
|
87
|
+
}
|
|
88
|
+
declare function toDurableIteratorObjectState<TProps>(original: DurableObjectState<TProps>): DurableIteratorObjectState<TProps>;
|
|
89
|
+
|
|
90
|
+
interface EventResumeStorageOptions extends StandardRPCJsonSerializerOptions {
|
|
91
|
+
/**
|
|
92
|
+
* How long (in seconds) to retain events for reconnection replay.
|
|
93
|
+
*
|
|
94
|
+
* When a client reconnects, stored events within this window can be replayed
|
|
95
|
+
* to ensure no data is lost. Outside this window, missed events are dropped.
|
|
96
|
+
*
|
|
97
|
+
* @remarks
|
|
98
|
+
* - Use infinite values to disable
|
|
99
|
+
*
|
|
100
|
+
* @default NaN (disabled)
|
|
101
|
+
*/
|
|
102
|
+
resumeRetentionSeconds?: number;
|
|
103
|
+
/**
|
|
104
|
+
* Prefix for the resume storage table schema.
|
|
105
|
+
* This is used to avoid naming conflicts with other tables in the same Durable Object.
|
|
106
|
+
*
|
|
107
|
+
* @default 'orpc:durable-iterator:resume:'
|
|
108
|
+
*/
|
|
109
|
+
resumeSchemaPrefix?: string;
|
|
110
|
+
}
|
|
111
|
+
interface ResumeEventFilter {
|
|
112
|
+
/** Only websockets with these tags will receive the event */
|
|
113
|
+
tags?: readonly string[];
|
|
114
|
+
/** Only websockets that are in this list will receive the event */
|
|
115
|
+
targets?: readonly DurableIteratorWebsocket[];
|
|
116
|
+
/** Websockets that are in this list will not receive the event */
|
|
117
|
+
exclude?: readonly DurableIteratorWebsocket[];
|
|
118
|
+
}
|
|
119
|
+
declare class EventResumeStorage<T extends object> {
|
|
120
|
+
private readonly durableState;
|
|
121
|
+
private readonly serializer;
|
|
122
|
+
private readonly retentionSeconds;
|
|
123
|
+
private readonly schemaPrefix;
|
|
124
|
+
get isEnabled(): boolean;
|
|
125
|
+
constructor(durableState: DurableObjectState, options?: EventResumeStorageOptions);
|
|
126
|
+
/**
|
|
127
|
+
* Store an payload for resume capability.
|
|
128
|
+
*
|
|
129
|
+
* @returns The updated meta of the stored payload
|
|
130
|
+
*/
|
|
131
|
+
store(payload: T, resumeFilter: ResumeEventFilter): T;
|
|
132
|
+
/**
|
|
133
|
+
* Get events after lastEventId for a specific websocket
|
|
134
|
+
*/
|
|
135
|
+
get(websocket: DurableIteratorWebsocket, lastEventId: string): T[];
|
|
136
|
+
private initSchema;
|
|
137
|
+
private resetSchema;
|
|
138
|
+
private cleanupExpiredEvents;
|
|
139
|
+
private serializeEventPayload;
|
|
140
|
+
private deserializeEventPayload;
|
|
141
|
+
private withEventId;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
type DurableIteratorObjectRouterContext = {
|
|
145
|
+
object: DurableObject<any, any>;
|
|
146
|
+
resumeStorage: EventResumeStorage<any>;
|
|
147
|
+
websocket: DurableIteratorWebsocket;
|
|
148
|
+
options: DurableIteratorObjectHandlerOptions;
|
|
149
|
+
};
|
|
150
|
+
interface PublishEventOptions {
|
|
151
|
+
/**
|
|
152
|
+
* Deliver the event only to websockets that have the specified tags.
|
|
153
|
+
*/
|
|
154
|
+
tags?: readonly string[];
|
|
155
|
+
/**
|
|
156
|
+
* Restrict the event to a specific set of websockets.
|
|
157
|
+
*
|
|
158
|
+
* Accept a list of websockets or a filter function.
|
|
159
|
+
*
|
|
160
|
+
* Use this when security is important — only the listed websockets
|
|
161
|
+
* will ever receive the event. Newly connected websockets are not
|
|
162
|
+
* included unless explicitly added here.
|
|
163
|
+
*/
|
|
164
|
+
targets?: readonly WebSocket[] | ((ws: DurableIteratorWebsocket) => boolean);
|
|
165
|
+
/**
|
|
166
|
+
* Exclude certain websockets from receiving the event.
|
|
167
|
+
*
|
|
168
|
+
* Accept a list of websockets or a filter function.
|
|
169
|
+
*
|
|
170
|
+
* Use this when broadcasting widely but skipping a few clients
|
|
171
|
+
* (e.g., the sender). Newly connected websockets may still receive
|
|
172
|
+
* the event if not listed here, so this is less strict than `targets`.
|
|
173
|
+
*/
|
|
174
|
+
exclude?: readonly WebSocket[] | ((ws: DurableIteratorWebsocket) => boolean);
|
|
175
|
+
}
|
|
176
|
+
interface DurableIteratorObjectHandlerOptions extends RPCHandlerOptions<DurableIteratorObjectRouterContext>, EventResumeStorageOptions {
|
|
177
|
+
/**
|
|
178
|
+
* The signing key to use verify the token.
|
|
179
|
+
*/
|
|
180
|
+
signingKey: string;
|
|
181
|
+
/**
|
|
182
|
+
* Called after a client successfully subscribes to the main iterator.
|
|
183
|
+
* You can start sending events to the client here.
|
|
184
|
+
*
|
|
185
|
+
* @param websocket Corresponding WebSocket connection.
|
|
186
|
+
* @param lastEventId Can be `undefined` if this is the first connection (not a resumed session).
|
|
187
|
+
*/
|
|
188
|
+
onSubscribed?: (websocket: DurableIteratorWebsocket, lastEventId: string | undefined) => void;
|
|
189
|
+
}
|
|
190
|
+
declare class DurableIteratorObjectHandler<T extends object, TProps> implements DurableIteratorObjectDef<T> {
|
|
191
|
+
private readonly object;
|
|
192
|
+
private readonly options;
|
|
193
|
+
'~eventPayloadType'?: {
|
|
194
|
+
type: T;
|
|
195
|
+
};
|
|
196
|
+
private readonly handler;
|
|
197
|
+
private readonly resumeStorage;
|
|
198
|
+
/**
|
|
199
|
+
* Proxied, ensure you don't accidentally change internal state, and auto close if expired websockets before .send is called
|
|
200
|
+
*/
|
|
201
|
+
ctx: DurableIteratorObjectState<TProps>;
|
|
202
|
+
constructor(ctx: DurableObjectState<TProps>, object: DurableObject<any, TProps>, options: DurableIteratorObjectHandlerOptions);
|
|
203
|
+
/**
|
|
204
|
+
* Publish an event to a set of clients.
|
|
205
|
+
*/
|
|
206
|
+
publishEvent(payload: T, options?: PublishEventOptions): void;
|
|
207
|
+
/**
|
|
208
|
+
* This method is called when a HTTP request is received for upgrading to a WebSocket connection.
|
|
209
|
+
* Should mapping with corresponding `fetch` inside durable object
|
|
210
|
+
*/
|
|
211
|
+
fetch(request: Request): Promise<Response>;
|
|
212
|
+
/**
|
|
213
|
+
* This method is called when a WebSocket message is received.
|
|
214
|
+
* Should mapping with corresponding `webSocketMessage` inside durable object
|
|
215
|
+
*/
|
|
216
|
+
webSocketMessage(websocket_: WebSocket, message: string | ArrayBuffer): Promise<void>;
|
|
217
|
+
/**
|
|
218
|
+
* This method is called when a WebSocket connection is closed.
|
|
219
|
+
* Should mapping with corresponding `webSocketClose` inside durable object
|
|
220
|
+
*/
|
|
221
|
+
webSocketClose(ws_: WebSocket, _code: number, _reason: string, _wasClean: boolean): void | Promise<void>;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
declare class DurableIteratorObject<T extends object, TEnv = Cloudflare.Env, TProps = {}> extends DurableObject<TEnv, TProps> implements DurableIteratorObject$1<T> {
|
|
225
|
+
'~orpc': DurableIteratorObjectHandler<T, TProps>;
|
|
226
|
+
/**
|
|
227
|
+
* Proxied, ensure you don't accidentally change internal state, and auto close if expired websockets before .send is called
|
|
228
|
+
*/
|
|
229
|
+
protected ctx: DurableIteratorObjectState<TProps>;
|
|
230
|
+
constructor(ctx: DurableObjectState<TProps>, env: TEnv, options: DurableIteratorObjectHandlerOptions);
|
|
231
|
+
/**
|
|
232
|
+
* Publish an event to clients
|
|
233
|
+
*/
|
|
234
|
+
publishEvent(payload: T, options?: PublishEventOptions): void;
|
|
235
|
+
/**
|
|
236
|
+
* Upgrades websocket connection
|
|
237
|
+
*
|
|
238
|
+
* @info You can safety intercept non-upgrade requests
|
|
239
|
+
* @warning No verification is done here, you should verify the token payload before calling this method.
|
|
240
|
+
*/
|
|
241
|
+
fetch(request: Request): Promise<Response>;
|
|
242
|
+
/**
|
|
243
|
+
* Handle WebSocket messages
|
|
244
|
+
*
|
|
245
|
+
* @warning Use `toDurableIteratorWebsocket` to proxy the WebSocket when interacting
|
|
246
|
+
* to avoid accidentally modifying internal state, and auto close if expired before .send is called
|
|
247
|
+
*/
|
|
248
|
+
webSocketMessage(websocket: WebSocket, message: string | ArrayBuffer): Promise<void>;
|
|
249
|
+
/**
|
|
250
|
+
* Handle WebSocket close event
|
|
251
|
+
*
|
|
252
|
+
* @warning Use `toDurableIteratorWebsocket` to proxy the WebSocket when interacting
|
|
253
|
+
* to avoid accidentally modifying internal state, and auto close if expired before .send is called
|
|
254
|
+
*/
|
|
255
|
+
webSocketClose(websocket: WebSocket, code: number, reason: string, wasClean: boolean): void | Promise<void>;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
interface UpgradeDurableIteratorRequestOptions {
|
|
259
|
+
/**
|
|
260
|
+
* The signing key used to verify the token
|
|
261
|
+
*/
|
|
262
|
+
signingKey: string;
|
|
263
|
+
/**
|
|
264
|
+
* The durable object namespace
|
|
265
|
+
*/
|
|
266
|
+
namespace: DurableObjectNamespace<any>;
|
|
267
|
+
/**
|
|
268
|
+
* The options to use when getting the durable object stub
|
|
269
|
+
*/
|
|
270
|
+
namespaceGetOptions?: DurableObjectNamespaceGetDurableObjectOptions;
|
|
271
|
+
/**
|
|
272
|
+
* intercept upgrade process
|
|
273
|
+
*/
|
|
274
|
+
interceptors?: Interceptor<{
|
|
275
|
+
payload: DurableIteratorTokenPayload;
|
|
276
|
+
}, Promise<Response>>[];
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Verifies and upgrades a durable iterator request.
|
|
280
|
+
*
|
|
281
|
+
* @info Verify token before forwarding to durable object to prevent DDoS attacks
|
|
282
|
+
*/
|
|
283
|
+
declare function upgradeDurableIteratorRequest(request: Request, options: UpgradeDurableIteratorRequestOptions): Promise<Response>;
|
|
284
|
+
|
|
285
|
+
export { DurableIteratorObject, DurableIteratorObjectHandler, EventResumeStorage, toDurableIteratorObjectState, toDurableIteratorWebsocket, upgradeDurableIteratorRequest };
|
|
286
|
+
export type { DurableIteratorObjectHandlerOptions, DurableIteratorObjectState, DurableIteratorObjectStateInternal, DurableIteratorWebsocket, DurableIteratorWebsocketInternal, EventResumeStorageOptions, PublishEventOptions, ResumeEventFilter, UpgradeDurableIteratorRequestOptions };
|