@ricsam/isolate-fetch 0.1.13 → 0.1.15

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.
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/isolate-fetch",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "type": "module"
5
5
  }
@@ -1,9 +1,16 @@
1
1
  import ivm from "isolated-vm";
2
2
  import { clearAllInstanceState } from "@ricsam/isolate-core";
3
3
  export { clearAllInstanceState };
4
+ export interface FetchRequestInit {
5
+ method: string;
6
+ headers: [string, string][];
7
+ rawBody: Uint8Array | null;
8
+ body: BodyInit | null;
9
+ signal: AbortSignal;
10
+ }
4
11
  export interface FetchOptions {
5
12
  /** Handler for fetch requests from the isolate */
6
- onFetch?: (request: Request) => Promise<Response>;
13
+ onFetch?: (url: string, init: FetchRequestInit) => Promise<Response>;
7
14
  }
8
15
  export interface UpgradeRequest {
9
16
  requested: true;
@@ -16,6 +23,15 @@ export interface WebSocketCommand {
16
23
  code?: number;
17
24
  reason?: string;
18
25
  }
26
+ export interface ClientWebSocketCommand {
27
+ type: "connect" | "send" | "close";
28
+ socketId: string;
29
+ url?: string;
30
+ protocols?: string[];
31
+ data?: string | ArrayBuffer;
32
+ code?: number;
33
+ reason?: string;
34
+ }
19
35
  export interface DispatchRequestOptions {
20
36
  }
21
37
  export interface FetchHandle {
@@ -38,6 +54,20 @@ export interface FetchHandle {
38
54
  hasServeHandler(): boolean;
39
55
  /** Check if there are active WebSocket connections */
40
56
  hasActiveConnections(): boolean;
57
+ /** Dispatch open event to a client WebSocket in the isolate */
58
+ dispatchClientWebSocketOpen(socketId: string, protocol: string, extensions: string): void;
59
+ /** Dispatch message event to a client WebSocket in the isolate */
60
+ dispatchClientWebSocketMessage(socketId: string, data: string | ArrayBuffer): void;
61
+ /** Dispatch close event to a client WebSocket in the isolate */
62
+ dispatchClientWebSocketClose(socketId: string, code: number, reason: string, wasClean: boolean): void;
63
+ /** Dispatch error event to a client WebSocket in the isolate */
64
+ dispatchClientWebSocketError(socketId: string): void;
65
+ /** Register callback for client WebSocket commands from isolate */
66
+ onClientWebSocketCommand(callback: (cmd: ClientWebSocketCommand) => void): () => void;
67
+ /** Register callback for events emitted from isolate code */
68
+ onEvent(callback: (event: string, payload: unknown) => void): () => void;
69
+ /** Dispatch an event into the isolate (calls __on listeners) */
70
+ dispatchEvent(event: string, payload: unknown): void;
41
71
  }
42
72
  /**
43
73
  * Setup Fetch API in an isolated-vm context
@@ -47,9 +77,9 @@ export interface FetchHandle {
47
77
  *
48
78
  * @example
49
79
  * const handle = await setupFetch(context, {
50
- * onFetch: async (request) => {
80
+ * onFetch: async (url, init) => {
51
81
  * // Proxy fetch requests to the host
52
- * return fetch(request);
82
+ * return fetch(url, init);
53
83
  * }
54
84
  * });
55
85
  *
@@ -264,4 +264,237 @@ declare global {
264
264
  * });
265
265
  */
266
266
  function serve<T = unknown>(options: ServeOptions<T>): void;
267
+
268
+ // ============================================
269
+ // Client WebSocket API (outbound connections)
270
+ // ============================================
271
+
272
+ /**
273
+ * The type for WebSocket binary data handling.
274
+ */
275
+ type BinaryType = "blob" | "arraybuffer";
276
+
277
+ /**
278
+ * Event fired when a WebSocket connection is closed.
279
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
280
+ */
281
+ interface CloseEvent extends Event {
282
+ /**
283
+ * The close code sent by the server.
284
+ */
285
+ readonly code: number;
286
+
287
+ /**
288
+ * The close reason sent by the server.
289
+ */
290
+ readonly reason: string;
291
+
292
+ /**
293
+ * Whether the connection was closed cleanly.
294
+ */
295
+ readonly wasClean: boolean;
296
+ }
297
+
298
+ /**
299
+ * Event fired when a WebSocket receives a message.
300
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
301
+ */
302
+ interface MessageEvent<T = any> extends Event {
303
+ /**
304
+ * The data sent by the message emitter.
305
+ */
306
+ readonly data: T;
307
+
308
+ /**
309
+ * The origin of the message emitter.
310
+ */
311
+ readonly origin: string;
312
+
313
+ /**
314
+ * The last event ID (for Server-Sent Events).
315
+ */
316
+ readonly lastEventId: string;
317
+
318
+ /**
319
+ * The MessagePort array sent with the message (if any).
320
+ */
321
+ readonly ports: ReadonlyArray<MessagePort>;
322
+
323
+ /**
324
+ * The source of the message (if applicable).
325
+ */
326
+ readonly source: MessageEventSource | null;
327
+ }
328
+
329
+ /**
330
+ * WHATWG WebSocket client for making outbound WebSocket connections.
331
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
332
+ *
333
+ * @example
334
+ * const ws = new WebSocket("wss://echo.websocket.org");
335
+ *
336
+ * ws.onopen = () => {
337
+ * console.log("Connected!");
338
+ * ws.send("Hello, server!");
339
+ * };
340
+ *
341
+ * ws.onmessage = (event) => {
342
+ * console.log("Received:", event.data);
343
+ * };
344
+ *
345
+ * ws.onclose = (event) => {
346
+ * console.log("Closed:", event.code, event.reason);
347
+ * };
348
+ *
349
+ * ws.onerror = () => {
350
+ * console.log("Error occurred");
351
+ * };
352
+ */
353
+ interface WebSocket extends EventTarget {
354
+ /**
355
+ * The URL of the WebSocket connection.
356
+ */
357
+ readonly url: string;
358
+
359
+ /**
360
+ * The current state of the connection.
361
+ * - 0: CONNECTING
362
+ * - 1: OPEN
363
+ * - 2: CLOSING
364
+ * - 3: CLOSED
365
+ */
366
+ readonly readyState: number;
367
+
368
+ /**
369
+ * The number of bytes of data that have been queued but not yet transmitted.
370
+ */
371
+ readonly bufferedAmount: number;
372
+
373
+ /**
374
+ * The extensions selected by the server.
375
+ */
376
+ readonly extensions: string;
377
+
378
+ /**
379
+ * The subprotocol selected by the server.
380
+ */
381
+ readonly protocol: string;
382
+
383
+ /**
384
+ * The type of binary data being transmitted.
385
+ * Can be "blob" or "arraybuffer".
386
+ */
387
+ binaryType: BinaryType;
388
+
389
+ /**
390
+ * Send data through the WebSocket connection.
391
+ *
392
+ * @param data - The data to send
393
+ * @throws InvalidStateError if the connection is not open
394
+ *
395
+ * @example
396
+ * ws.send("Hello!");
397
+ * ws.send(new Uint8Array([1, 2, 3]));
398
+ */
399
+ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
400
+
401
+ /**
402
+ * Close the WebSocket connection.
403
+ *
404
+ * @param code - The close code (default: 1000)
405
+ * @param reason - The close reason (max 123 bytes UTF-8)
406
+ *
407
+ * @example
408
+ * ws.close();
409
+ * ws.close(1000, "Normal closure");
410
+ */
411
+ close(code?: number, reason?: string): void;
412
+
413
+ /**
414
+ * Event handler for when the connection is established.
415
+ */
416
+ onopen: ((this: WebSocket, ev: Event) => any) | null;
417
+
418
+ /**
419
+ * Event handler for when a message is received.
420
+ */
421
+ onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
422
+
423
+ /**
424
+ * Event handler for when an error occurs.
425
+ */
426
+ onerror: ((this: WebSocket, ev: Event) => any) | null;
427
+
428
+ /**
429
+ * Event handler for when the connection is closed.
430
+ */
431
+ onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
432
+
433
+ /**
434
+ * Connection is being established.
435
+ */
436
+ readonly CONNECTING: 0;
437
+
438
+ /**
439
+ * Connection is open and ready to communicate.
440
+ */
441
+ readonly OPEN: 1;
442
+
443
+ /**
444
+ * Connection is in the process of closing.
445
+ */
446
+ readonly CLOSING: 2;
447
+
448
+ /**
449
+ * Connection is closed or couldn't be opened.
450
+ */
451
+ readonly CLOSED: 3;
452
+ }
453
+
454
+ /**
455
+ * WebSocket constructor.
456
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket
457
+ */
458
+ interface WebSocketConstructor {
459
+ /**
460
+ * Create a new WebSocket connection.
461
+ *
462
+ * @param url - The URL to connect to (must be ws:// or wss://)
463
+ * @param protocols - Optional subprotocol(s) to request
464
+ * @throws SyntaxError if the URL is invalid
465
+ *
466
+ * @example
467
+ * const ws = new WebSocket("wss://example.com/socket");
468
+ * const ws = new WebSocket("wss://example.com/socket", "graphql-ws");
469
+ * const ws = new WebSocket("wss://example.com/socket", ["protocol1", "protocol2"]);
470
+ */
471
+ new (url: string | URL, protocols?: string | string[]): WebSocket;
472
+
473
+ readonly prototype: WebSocket;
474
+
475
+ /**
476
+ * Connection is being established.
477
+ */
478
+ readonly CONNECTING: 0;
479
+
480
+ /**
481
+ * Connection is open and ready to communicate.
482
+ */
483
+ readonly OPEN: 1;
484
+
485
+ /**
486
+ * Connection is in the process of closing.
487
+ */
488
+ readonly CLOSING: 2;
489
+
490
+ /**
491
+ * Connection is closed or couldn't be opened.
492
+ */
493
+ readonly CLOSED: 3;
494
+ }
495
+
496
+ /**
497
+ * WHATWG WebSocket client for making outbound WebSocket connections.
498
+ */
499
+ const WebSocket: WebSocketConstructor;
267
500
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ricsam/isolate-fetch",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "main": "./dist/cjs/index.cjs",
5
5
  "types": "./dist/types/index.d.ts",
6
6
  "exports": {