@zero-server/webrtc 0.9.7

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 (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +37 -0
  3. package/index.d.ts +2 -0
  4. package/index.js +53 -0
  5. package/lib/auth/index.js +1 -0
  6. package/lib/debug.js +372 -0
  7. package/lib/errors.js +1 -0
  8. package/lib/middleware/index.js +1 -0
  9. package/lib/observe/index.js +1 -0
  10. package/lib/webrtc/bot.js +361 -0
  11. package/lib/webrtc/cli.js +182 -0
  12. package/lib/webrtc/cluster.js +350 -0
  13. package/lib/webrtc/e2ee.js +282 -0
  14. package/lib/webrtc/ice.js +370 -0
  15. package/lib/webrtc/index.js +132 -0
  16. package/lib/webrtc/joinToken.js +116 -0
  17. package/lib/webrtc/observe.js +229 -0
  18. package/lib/webrtc/peer.js +116 -0
  19. package/lib/webrtc/room.js +171 -0
  20. package/lib/webrtc/sdp.js +508 -0
  21. package/lib/webrtc/sfu/index.js +201 -0
  22. package/lib/webrtc/sfu/livekit.js +301 -0
  23. package/lib/webrtc/sfu/mediasoup.js +317 -0
  24. package/lib/webrtc/sfu/memory.js +204 -0
  25. package/lib/webrtc/signaling.js +546 -0
  26. package/lib/webrtc/stun.js +492 -0
  27. package/lib/webrtc/turn/codec.js +370 -0
  28. package/lib/webrtc/turn/credentials.js +141 -0
  29. package/lib/webrtc/turn/server.js +633 -0
  30. package/lib/ws/index.js +1 -0
  31. package/package.json +62 -0
  32. package/types/app.d.ts +223 -0
  33. package/types/auth.d.ts +520 -0
  34. package/types/body.d.ts +14 -0
  35. package/types/cli.d.ts +2 -0
  36. package/types/cluster.d.ts +75 -0
  37. package/types/env.d.ts +80 -0
  38. package/types/errors.d.ts +316 -0
  39. package/types/fetch.d.ts +43 -0
  40. package/types/grpc.d.ts +432 -0
  41. package/types/index.d.ts +396 -0
  42. package/types/lifecycle.d.ts +60 -0
  43. package/types/middleware.d.ts +320 -0
  44. package/types/observe.d.ts +304 -0
  45. package/types/orm.d.ts +1887 -0
  46. package/types/request.d.ts +109 -0
  47. package/types/response.d.ts +157 -0
  48. package/types/router.d.ts +78 -0
  49. package/types/sse.d.ts +78 -0
  50. package/types/webrtc.d.ts +501 -0
  51. package/types/websocket.d.ts +126 -0
@@ -0,0 +1,432 @@
1
+ // Type definitions for zero-server gRPC module
2
+
3
+ /// <reference types="node" />
4
+
5
+ import { EventEmitter } from 'events';
6
+ import { Http2Stream, ClientHttp2Session } from 'http2';
7
+
8
+ // --- Status Codes -------------------------------------------
9
+
10
+ export declare enum GrpcStatus {
11
+ OK = 0,
12
+ CANCELLED = 1,
13
+ UNKNOWN = 2,
14
+ INVALID_ARGUMENT = 3,
15
+ DEADLINE_EXCEEDED = 4,
16
+ NOT_FOUND = 5,
17
+ ALREADY_EXISTS = 6,
18
+ PERMISSION_DENIED = 7,
19
+ RESOURCE_EXHAUSTED = 8,
20
+ FAILED_PRECONDITION = 9,
21
+ ABORTED = 10,
22
+ OUT_OF_RANGE = 11,
23
+ UNIMPLEMENTED = 12,
24
+ INTERNAL = 13,
25
+ UNAVAILABLE = 14,
26
+ DATA_LOSS = 15,
27
+ UNAUTHENTICATED = 16,
28
+ }
29
+
30
+ export declare const STATUS_NAMES: Record<number, string>;
31
+ export declare function grpcToHttp(code: number): number;
32
+ export declare function statusName(code: number): string;
33
+
34
+ // --- Metadata -----------------------------------------------
35
+
36
+ export declare class Metadata {
37
+ constructor();
38
+ set(key: string, value: string | Buffer): void;
39
+ add(key: string, value: string | Buffer): void;
40
+ get(key: string): string | Buffer | undefined;
41
+ getAll(key: string): (string | Buffer)[];
42
+ has(key: string): boolean;
43
+ remove(key: string): void;
44
+ clear(): void;
45
+ merge(other: Metadata): void;
46
+ clone(): Metadata;
47
+ entries(): [string, (string | Buffer)[]][];
48
+ keys(): string[];
49
+ readonly size: number;
50
+ toHeaders(): Record<string, string>;
51
+ static fromHeaders(headers: Record<string, string | string[]>): Metadata;
52
+ }
53
+
54
+ // --- Protobuf Codec -----------------------------------------
55
+
56
+ export declare const WIRE_TYPE: {
57
+ VARINT: 0;
58
+ FIXED64: 1;
59
+ LENGTH_DELIMITED: 2;
60
+ START_GROUP: 3;
61
+ END_GROUP: 4;
62
+ FIXED32: 5;
63
+ };
64
+
65
+ export declare const TYPE_INFO: Record<string, { wireType: number; default: any }>;
66
+
67
+ export declare class Writer {
68
+ constructor();
69
+ writeVarint(value: number): void;
70
+ writeSVarint(value: number): void;
71
+ writeFixed32(value: number): void;
72
+ writeFixed64(lo: number, hi: number): void;
73
+ writeSFixed32(value: number): void;
74
+ writeSFixed64(lo: number, hi: number): void;
75
+ writeFloat(value: number): void;
76
+ writeDouble(value: number): void;
77
+ writeBool(value: boolean): void;
78
+ writeString(value: string): void;
79
+ writeBytes(value: Buffer): void;
80
+ writeTag(fieldNumber: number, wireType: number): void;
81
+ finish(): Buffer;
82
+ readonly length: number;
83
+ }
84
+
85
+ export declare class Reader {
86
+ constructor(buffer: Buffer);
87
+ readVarint(): number;
88
+ readSVarint(): number;
89
+ readFixed32(): number;
90
+ readFixed64(): [number, number];
91
+ readSFixed32(): number;
92
+ readSFixed64(): [number, number];
93
+ readFloat(): number;
94
+ readDouble(): number;
95
+ readBool(): boolean;
96
+ readString(): string;
97
+ readBytes(): Buffer;
98
+ readTag(): { fieldNumber: number; wireType: number };
99
+ skipField(wireType: number): void;
100
+ readSubReader(length: number): Reader;
101
+ readonly done: boolean;
102
+ readonly remaining: number;
103
+ }
104
+
105
+ export declare function encode(obj: Record<string, any>, messageDesc: MessageDescriptor, allMessages?: Record<string, MessageDescriptor>): Buffer;
106
+ export declare function decode(buffer: Buffer, messageDesc: MessageDescriptor, allMessages?: Record<string, MessageDescriptor>): Record<string, any>;
107
+
108
+ // --- Proto Parser -------------------------------------------
109
+
110
+ export interface FieldDescriptor {
111
+ name: string;
112
+ type: string;
113
+ number: number;
114
+ repeated?: boolean;
115
+ optional?: boolean;
116
+ map?: boolean;
117
+ mapKeyType?: string;
118
+ mapValueType?: string;
119
+ oneofName?: string;
120
+ options?: Record<string, any>;
121
+ enumDef?: EnumDescriptor;
122
+ }
123
+
124
+ export interface MessageDescriptor {
125
+ name: string;
126
+ fields: FieldDescriptor[];
127
+ nested?: Record<string, MessageDescriptor>;
128
+ enums?: Record<string, EnumDescriptor>;
129
+ }
130
+
131
+ export interface EnumDescriptor {
132
+ name: string;
133
+ values: Record<string, number>;
134
+ options?: Record<string, any>;
135
+ }
136
+
137
+ export interface MethodDescriptor {
138
+ name: string;
139
+ inputType: string;
140
+ outputType: string;
141
+ clientStreaming: boolean;
142
+ serverStreaming: boolean;
143
+ options?: Record<string, any>;
144
+ }
145
+
146
+ export interface ServiceDescriptor {
147
+ name: string;
148
+ methods: Record<string, MethodDescriptor>;
149
+ options?: Record<string, any>;
150
+ }
151
+
152
+ export interface ProtoSchema {
153
+ syntax: string;
154
+ package: string;
155
+ imports: string[];
156
+ options: Record<string, any>;
157
+ messages: Record<string, MessageDescriptor>;
158
+ enums: Record<string, EnumDescriptor>;
159
+ services: Record<string, ServiceDescriptor>;
160
+ }
161
+
162
+ export interface ParseProtoOptions {
163
+ resolveImports?: boolean;
164
+ basePath?: string;
165
+ }
166
+
167
+ export declare function parseProto(source: string, opts?: ParseProtoOptions): ProtoSchema;
168
+ export declare function parseProtoFile(filePath: string, opts?: ParseProtoOptions): ProtoSchema;
169
+ export declare function tokenize(source: string): { type: string; value: string; line: number }[];
170
+
171
+ // --- Frame --------------------------------------------------
172
+
173
+ export declare const FRAME_HEADER_SIZE: 5;
174
+ export declare const MAX_FRAME_SIZE: number;
175
+
176
+ export interface FrameEncodeOptions {
177
+ compress?: boolean;
178
+ }
179
+
180
+ export declare function frameEncode(message: Buffer, opts?: FrameEncodeOptions): Buffer | Promise<Buffer>;
181
+
182
+ export interface FrameParserOptions {
183
+ maxMessageSize?: number;
184
+ }
185
+
186
+ export declare class FrameParser {
187
+ constructor(opts?: FrameParserOptions);
188
+ onMessage: ((buf: Buffer) => void) | null;
189
+ onError: ((err: Error) => void) | null;
190
+ push(chunk: Buffer): void;
191
+ reset(): void;
192
+ destroy(): void;
193
+ }
194
+
195
+ // --- Call Objects --------------------------------------------
196
+
197
+ export interface CallOptions {
198
+ maxMessageSize?: number;
199
+ compress?: boolean;
200
+ }
201
+
202
+ export declare class BaseCall extends EventEmitter {
203
+ readonly stream: Http2Stream;
204
+ readonly method: MethodDescriptor;
205
+ readonly metadata: Metadata;
206
+ trailingMetadata: Metadata;
207
+ readonly peer: string;
208
+ readonly cancelled: boolean;
209
+
210
+ sendMetadata(md?: Metadata | Record<string, string>): void;
211
+ sendStatus(code: number, message?: string): void;
212
+ sendError(code: number, message: string): void;
213
+ write(message: Record<string, any>): boolean;
214
+ cancel(): void;
215
+ }
216
+
217
+ export declare class UnaryCall extends BaseCall {
218
+ request: Record<string, any> | null;
219
+ }
220
+
221
+ export declare class ServerStreamCall extends BaseCall {
222
+ request: Record<string, any> | null;
223
+ end(): void;
224
+ }
225
+
226
+ export declare class ClientStreamCall extends BaseCall implements AsyncIterable<Record<string, any>> {
227
+ [Symbol.asyncIterator](): AsyncIterator<Record<string, any>>;
228
+ }
229
+
230
+ export declare class BidiStreamCall extends BaseCall implements AsyncIterable<Record<string, any>> {
231
+ end(): void;
232
+ [Symbol.asyncIterator](): AsyncIterator<Record<string, any>>;
233
+ }
234
+
235
+ // --- Server -------------------------------------------------
236
+
237
+ export interface GrpcServiceOptions {
238
+ interceptors?: GrpcInterceptor[];
239
+ maxMessageSize?: number;
240
+ compress?: boolean;
241
+ }
242
+
243
+ export type GrpcInterceptor = (call: BaseCall, next: () => Promise<void>) => void | Promise<void>;
244
+ export type GrpcHandler = (call: BaseCall) => any | Promise<any>;
245
+
246
+ export declare class GrpcServiceRegistry {
247
+ constructor();
248
+ addService(schema: ProtoSchema, serviceName: string, handlers: Record<string, GrpcHandler>, opts?: GrpcServiceOptions): void;
249
+ addInterceptor(fn: GrpcInterceptor): void;
250
+ handleStream(stream: Http2Stream, headers: Record<string, string>): boolean;
251
+ drain(timeout?: number): Promise<void>;
252
+ routes(): { method: string; path: string; type: string; implemented: boolean }[];
253
+ }
254
+
255
+ // --- Client -------------------------------------------------
256
+
257
+ export interface GrpcClientOptions {
258
+ ca?: Buffer | string;
259
+ key?: Buffer | string;
260
+ cert?: Buffer | string;
261
+ metadata?: Record<string, string>;
262
+ maxMessageSize?: number;
263
+ compress?: boolean;
264
+ deadline?: number;
265
+ keepAlive?: boolean;
266
+ keepAliveInterval?: number;
267
+ rejectUnauthorized?: boolean;
268
+ }
269
+
270
+ export interface GrpcCallOptions {
271
+ metadata?: Metadata | Record<string, string>;
272
+ deadline?: number;
273
+ }
274
+
275
+ export interface ClientStreamHandle<TResponse = Record<string, any>> {
276
+ write(msg: Record<string, any>): void;
277
+ end(): void;
278
+ cancel(): void;
279
+ response: Promise<TResponse>;
280
+ }
281
+
282
+ export interface BidiStreamHandle<TResponse = Record<string, any>> extends AsyncIterable<TResponse> {
283
+ write(msg: Record<string, any>): void;
284
+ end(): void;
285
+ cancel(): void;
286
+ }
287
+
288
+ export interface ServerStreamHandle<TResponse = Record<string, any>> extends AsyncIterable<TResponse> {
289
+ cancel(): void;
290
+ }
291
+
292
+ export interface GrpcClientMultiAddressOptions {
293
+ addresses: string[];
294
+ address?: string;
295
+ loadBalancing?: 'pick-first' | 'round-robin';
296
+ healthCheck?: boolean;
297
+ ca?: Buffer | string;
298
+ key?: Buffer | string;
299
+ cert?: Buffer | string;
300
+ metadata?: Record<string, string>;
301
+ rejectUnauthorized?: boolean;
302
+ }
303
+
304
+ export declare class GrpcClient extends EventEmitter {
305
+ constructor(address: string | GrpcClientMultiAddressOptions, schema: ProtoSchema, serviceName: string, opts?: GrpcClientOptions);
306
+
307
+ defaultMetadata: Metadata;
308
+ readonly connected: boolean;
309
+
310
+ call(methodName: string, request: Record<string, any>, opts?: GrpcCallOptions): Promise<Record<string, any>>;
311
+ serverStream(methodName: string, request: Record<string, any>, opts?: GrpcCallOptions): ServerStreamHandle;
312
+ clientStream(methodName: string, opts?: GrpcCallOptions): ClientStreamHandle;
313
+ bidiStream(methodName: string, opts?: GrpcCallOptions): BidiStreamHandle;
314
+ close(): void;
315
+ }
316
+
317
+ // --- Health Check Service -----------------------------------
318
+
319
+ export declare enum ServingStatus {
320
+ UNKNOWN = 0,
321
+ SERVING = 1,
322
+ NOT_SERVING = 2,
323
+ SERVICE_UNKNOWN = 3,
324
+ }
325
+
326
+ export declare class HealthService {
327
+ constructor();
328
+ setStatus(serviceName: string, status: ServingStatus | string | number): void;
329
+ getStatus(serviceName: string): ServingStatus;
330
+ setAllNotServing(): void;
331
+ getSchema(): ProtoSchema;
332
+ getHandlers(): Record<string, GrpcHandler>;
333
+ }
334
+
335
+ // --- Server Reflection --------------------------------------
336
+
337
+ export declare class ReflectionService {
338
+ constructor(opts?: { production?: boolean });
339
+ addSchema(schema: ProtoSchema, filename?: string): void;
340
+ getSchema(): ProtoSchema;
341
+ getHandlers(): Record<string, GrpcHandler>;
342
+ }
343
+
344
+ export declare function buildFileDescriptorProto(schema: ProtoSchema, filename?: string): Buffer;
345
+
346
+ // --- Load Balancing -----------------------------------------
347
+
348
+ export declare enum SubchannelState {
349
+ IDLE = 0,
350
+ CONNECTING = 1,
351
+ READY = 2,
352
+ TRANSIENT_FAILURE = 3,
353
+ SHUTDOWN = 4,
354
+ }
355
+
356
+ export declare class Subchannel extends EventEmitter {
357
+ constructor(address: string, connectOpts?: Record<string, any>);
358
+ readonly state: SubchannelState;
359
+ readonly address: string;
360
+ readonly healthy: boolean;
361
+ connect(): void;
362
+ getSession(): ClientHttp2Session | null;
363
+ shutdown(): void;
364
+ }
365
+
366
+ export declare class LoadBalancer {
367
+ constructor(addresses: string[], opts?: { policy?: 'pick-first' | 'round-robin'; connectOpts?: Record<string, any> });
368
+ pick(): Subchannel | null;
369
+ getSession(): ClientHttp2Session | null;
370
+ shutdown(): void;
371
+ }
372
+
373
+ export declare class RoundRobinPicker {
374
+ constructor(subchannels: Subchannel[]);
375
+ pick(): Subchannel | null;
376
+ }
377
+
378
+ // --- Channel Credentials ------------------------------------
379
+
380
+ export declare enum CredentialType {
381
+ INSECURE = 'insecure',
382
+ SSL = 'ssl',
383
+ METADATA = 'metadata',
384
+ COMPOSITE = 'composite',
385
+ }
386
+
387
+ export declare class ChannelCredentials {
388
+ readonly type: string;
389
+
390
+ static createInsecure(): ChannelCredentials;
391
+ static createSsl(rootCerts?: Buffer | string | null, clientKey?: Buffer | string | null, clientCert?: Buffer | string | null, opts?: { rejectUnauthorized?: boolean }): ChannelCredentials;
392
+ static createSslFromFiles(caPath?: string | null, keyPath?: string | null, certPath?: string | null, opts?: { rejectUnauthorized?: boolean }): ChannelCredentials;
393
+ static createFromMetadata(metadataGenerator: (params?: { serviceUrl?: string; methodName?: string }) => Record<string, string> | Promise<Record<string, string>>): ChannelCredentials;
394
+ static combine(...credentials: ChannelCredentials[]): ChannelCredentials;
395
+
396
+ isSecure(): boolean;
397
+ getConnectionOptions(): Record<string, any> | null;
398
+ generateMetadata(params?: { serviceUrl?: string; methodName?: string }): Promise<Record<string, string>>;
399
+ }
400
+
401
+ export interface RotatingCredentialsOptions {
402
+ caPath: string;
403
+ keyPath?: string;
404
+ certPath?: string;
405
+ pollInterval?: number;
406
+ sslOpts?: { rejectUnauthorized?: boolean };
407
+ }
408
+
409
+ export declare function createRotatingCredentials(opts: RotatingCredentialsOptions): {
410
+ getCurrent(): ChannelCredentials;
411
+ stop(): void;
412
+ };
413
+
414
+ // --- Proto Hot-Reload ---------------------------------------
415
+
416
+ export interface WatchProtoOptions {
417
+ interceptors?: GrpcInterceptor[];
418
+ maxMessageSize?: number;
419
+ compress?: boolean;
420
+ debounce?: number;
421
+ production?: boolean;
422
+ onReload?: (schema: ProtoSchema) => void;
423
+ onError?: (err: Error) => void;
424
+ }
425
+
426
+ export declare function watchProto(
427
+ app: any,
428
+ protoPath: string,
429
+ serviceName: string,
430
+ handlers: Record<string, GrpcHandler>,
431
+ opts?: WatchProtoOptions,
432
+ ): { stop(): void; readonly schema: ProtoSchema };