forked from cloudflare/dog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
180 lines (154 loc) · 5.1 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// TODO: tbd
declare namespace JSON {
type Value = Date | RegExp | string | number | boolean | null | JSON.Object;
type Object = JSON.Value[] | { [key: string]: JSON.Value };
}
// Socket Messages
// @todo support arraybuffer types
export type Message = JSON.Object | string;
export type RequestID = string;
export type GroupID = string;
export type ReplicaID = string;
export namespace Gossip {
type Message = {
[key: string]: JSON.Value;
};
type Payload = JSON.Object | JSON.Value;
}
export interface State {
group: string;
socket: Set<WebSocket>;
}
export interface Socket {
/**
* The request identifier.
* @see {Group.identify}
*/
uid: string;
/**
* Send the WebSocket client a string-serializable message.
*/
send: WebSocket['send'];
/**
* Close the WebSocket connection.
*/
close: WebSocket['close'];
/**
* Send a message to other WebSockets owned by the Replica.
* @param {boolean} [self] Send the message to the sender?
*/
emit(msg: Message, self?: boolean): void;
/**
* Send a message to ALL WebSockets within the CLUSTER.
* @param {boolean} [self] Send the message to the sender?
*/
broadcast(msg: Message, self?: boolean): Promise<void>;
/**
* Send a message to a specific WebSocket target.
*/
whisper(target: string, msg: Message): Promise<void>;
}
// @see https://github.com/cloudflare/workers-types/pull/102
export type Bindings = Record<string, KVNamespace | DurableObjectNamespace | CryptoKey | string>;
export abstract class Replica<T extends Bindings> {
readonly uid: string;
constructor(state: DurableObjectState, env: T);
/**
* Specify which `Group` class is the target.
* @NOTE User-supplied logic/function.
*/
abstract link(bindings: T): {
parent: DurableObjectNamespace & Group<T>;
self: DurableObjectNamespace & Replica<T>;
};
/**
* Receive the HTTP request.
* @NOTE User must call `this.connect` for WS connection.
* @NOTE User-supplied logic/function.
*/
abstract receive(req: Request): Promise<Response> | Response;
/** The WebSocket client connection was established. */
onopen?(socket: Socket): Promise<void> | void;
/** The WebSocket client was closed. */
onclose?(socket: Socket): Promise<void> | void;
/** The WebSocket client was closed due to an error. */
onerror?(socket: Socket): Promise<void> | void;
/** The WebSocket client sent the Replica a message. */
onmessage?(socket: Socket, data: string): Promise<void> | void;
/**
* Handle the WS connection upgrade.
*/
connect(req: Request): Promise<Response>;
/**
* Send a message (via HTTP) to WebSockets owned by the Replica
* @NOTE This is the HTTP-accessible version of `Socket.emit`
*/
emit(msg: Message): void;
/**
* Send a message (via HTTP) to ALL WebSockets within the CLUSTER.
* @NOTE This is the HTTP-accessible version of `Socket.broadcast`
*/
broadcast(msg: Message): Promise<void>;
/**
* Send a message (via HTTP) to a specific WebSocket target.
* @NOTE This is the HTTP-accessible version of `Socket.whisper`
*/
whisper(target: string, msg: Message): Promise<void>;
/**
* Respond to another Replica's gossip.
* @NOTE Must return a JSON-serializable value.
*/
ongossip?(msg: Gossip.Message): Promise<Gossip.Payload> | Gossip.Payload;
/**
* Send a message directly to other Replicas.
* A `Gossip.Message` must be a JSON object.
* Returns a list of `Gossip.Payload`s, one from each Replica sibling.
* @NOTE Peer-to-peer communication; does not involve client connections.
*/
gossip<M extends Gossip.Message>(msg: M): Promise<Gossip.Payload[]>;
/**
* Receives a request from a Group object.
* @IMPORTANT Do NOT define your own `fetch` method!
*/
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
}
export abstract class Group<T extends ModuleWorker.Bindings> {
abstract limit: number;
readonly uid: string;
constructor(state: DurableObjectState, env: T);
/**
* Specify which `Replica` class extension is the target.
* @NOTE User-supplied logic/function.
*/
abstract link(bindings: T): {
child: DurableObjectNamespace & Replica<T>;
self: DurableObjectNamespace & Group<T>;
};
/**
* Generate a `DurableObjectId` for the Replica cluster.
* @default target.newUniqueId()
*/
clusterize(req: Request, target: DurableObjectNamespace): Promise<DurableObjectId> | DurableObjectId;
/**
* Receive the HTTP request if not an internal route.
* @NOTE Unlike `Replica.receive`, this is optionally defined.
* Useful for supply custom routing/handler logic if the
* incoming `Request` was not significant to the DOG.
* @default utils.abort(404)
*/
receive(req: Request): Promise<Response> | Response;
/**
* Receives the initial request & figures out where to send it.
* @NOTE User should NOT redeclare/override this method.
*/
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
}
export interface Family<T extends ModuleWorker.Bindings> {
parent: DurableObjectNamespace & Group<T>;
child: DurableObjectNamespace & Replica<T>;
}
export function identify<T extends ModuleWorker.Bindings>(
groupid: DurableObjectId,
requestid: RequestID,
family: Family<T>,
): Promise<DurableObjectStub>;