@grest-ts/websocket 0.0.6 → 0.0.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grest-ts/websocket",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "WebSocket server and client library for Node.js and browser",
@@ -51,24 +51,24 @@
51
51
  "ws"
52
52
  ],
53
53
  "engines": {
54
- "node": ">=22"
54
+ "node": ">=25"
55
55
  },
56
56
  "dependencies": {
57
- "@grest-ts/common": "0.0.6",
58
- "@grest-ts/context": "0.0.6",
59
- "@grest-ts/http": "0.0.6",
60
- "@grest-ts/logger": "0.0.6",
61
- "@grest-ts/schema": "0.0.6",
62
- "@grest-ts/trace": "0.0.6"
57
+ "@grest-ts/common": "0.0.8",
58
+ "@grest-ts/context": "0.0.8",
59
+ "@grest-ts/http": "0.0.8",
60
+ "@grest-ts/logger": "0.0.8",
61
+ "@grest-ts/schema": "0.0.8",
62
+ "@grest-ts/trace": "0.0.8"
63
63
  },
64
64
  "peerDependencies": {
65
- "@grest-ts/discovery": "0.0.6",
66
- "@grest-ts/locator": "0.0.6",
67
- "@grest-ts/metrics": "0.0.6",
65
+ "@grest-ts/discovery": "0.0.8",
66
+ "@grest-ts/locator": "0.0.8",
67
+ "@grest-ts/metrics": "0.0.8",
68
68
  "ws": "^8.19.0"
69
69
  },
70
70
  "devDependencies": {
71
- "@grest-ts/testkit": "0.0.6",
72
- "@grest-ts/x-packager": "0.0.6"
71
+ "@grest-ts/testkit": "0.0.8",
72
+ "@grest-ts/x-packager": "0.0.8"
73
73
  }
74
74
  }
@@ -1,63 +1,63 @@
1
- import {SocketAdapter} from "../socket/SocketAdapter";
2
-
3
- export class BrowserSocketAdapter implements SocketAdapter {
4
- private ws: globalThis.WebSocket;
5
- private messageWrappers = new WeakMap<(data: string) => void, (event: MessageEvent) => void>();
6
- private errorWrappers = new WeakMap<(error: Error) => void, (event: Event) => void>();
7
-
8
- constructor(url: string) {
9
- this.ws = new WebSocket(url);
10
- }
11
-
12
- send(message: string): void {
13
- this.ws.send(message);
14
- }
15
-
16
- close(): void {
17
- this.ws.close();
18
- }
19
-
20
- onOpen(handler: () => void): void {
21
- this.ws.addEventListener('open', handler);
22
- }
23
-
24
- onMessage(handler: (data: string) => void): void {
25
- const wrapper = (event: MessageEvent) => handler(event.data);
26
- this.messageWrappers.set(handler, wrapper);
27
- this.ws.addEventListener('message', wrapper);
28
- }
29
-
30
- onClose(handler: () => void): void {
31
- this.ws.addEventListener('close', handler);
32
- }
33
-
34
- onError(handler: (error: Error) => void): void {
35
- const wrapper = (event: Event) => handler(new Error('WebSocket error: ' + event.type));
36
- this.errorWrappers.set(handler, wrapper);
37
- this.ws.addEventListener('error', wrapper);
38
- }
39
-
40
- offOpen(handler: () => void): void {
41
- this.ws.removeEventListener('open', handler);
42
- }
43
-
44
- offMessage(handler: (data: string) => void): void {
45
- const wrapper = this.messageWrappers.get(handler);
46
- if (wrapper) {
47
- this.ws.removeEventListener('message', wrapper);
48
- this.messageWrappers.delete(handler);
49
- }
50
- }
51
-
52
- offClose(handler: () => void): void {
53
- this.ws.removeEventListener('close', handler);
54
- }
55
-
56
- offError(handler: (error: Error) => void): void {
57
- const wrapper = this.errorWrappers.get(handler);
58
- if (wrapper) {
59
- this.ws.removeEventListener('error', wrapper);
60
- this.errorWrappers.delete(handler);
61
- }
62
- }
63
- }
1
+ import {SocketAdapter} from "../socket/SocketAdapter";
2
+
3
+ export class BrowserSocketAdapter implements SocketAdapter {
4
+ private ws: globalThis.WebSocket;
5
+ private messageWrappers = new WeakMap<(data: string) => void, (event: MessageEvent) => void>();
6
+ private errorWrappers = new WeakMap<(error: Error) => void, (event: Event) => void>();
7
+
8
+ constructor(url: string) {
9
+ this.ws = new WebSocket(url);
10
+ }
11
+
12
+ send(message: string): void {
13
+ this.ws.send(message);
14
+ }
15
+
16
+ close(): void {
17
+ this.ws.close();
18
+ }
19
+
20
+ onOpen(handler: () => void): void {
21
+ this.ws.addEventListener('open', handler);
22
+ }
23
+
24
+ onMessage(handler: (data: string) => void): void {
25
+ const wrapper = (event: MessageEvent) => handler(event.data);
26
+ this.messageWrappers.set(handler, wrapper);
27
+ this.ws.addEventListener('message', wrapper);
28
+ }
29
+
30
+ onClose(handler: () => void): void {
31
+ this.ws.addEventListener('close', handler);
32
+ }
33
+
34
+ onError(handler: (error: Error) => void): void {
35
+ const wrapper = (event: Event) => handler(new Error('WebSocket error: ' + event.type));
36
+ this.errorWrappers.set(handler, wrapper);
37
+ this.ws.addEventListener('error', wrapper);
38
+ }
39
+
40
+ offOpen(handler: () => void): void {
41
+ this.ws.removeEventListener('open', handler);
42
+ }
43
+
44
+ offMessage(handler: (data: string) => void): void {
45
+ const wrapper = this.messageWrappers.get(handler);
46
+ if (wrapper) {
47
+ this.ws.removeEventListener('message', wrapper);
48
+ this.messageWrappers.delete(handler);
49
+ }
50
+ }
51
+
52
+ offClose(handler: () => void): void {
53
+ this.ws.removeEventListener('close', handler);
54
+ }
55
+
56
+ offError(handler: (error: Error) => void): void {
57
+ const wrapper = this.errorWrappers.get(handler);
58
+ if (wrapper) {
59
+ this.ws.removeEventListener('error', wrapper);
60
+ this.errorWrappers.delete(handler);
61
+ }
62
+ }
63
+ }
@@ -1,67 +1,67 @@
1
- import WebSocket from "ws";
2
- import {SocketAdapter} from "../socket/SocketAdapter";
3
-
4
- export interface NodeSocketAdapterOptions {
5
- headers?: Record<string, string>;
6
- }
7
-
8
- export class NodeSocketAdapter implements SocketAdapter {
9
- private ws: WebSocket;
10
- private messageWrappers = new WeakMap<(data: string) => void, (rawMessage: Buffer) => void>();
11
-
12
- constructor(urlOrSocket: string | WebSocket, options?: NodeSocketAdapterOptions) {
13
- if (typeof urlOrSocket === 'string') {
14
- this.ws = new WebSocket(urlOrSocket, {
15
- headers: options?.headers
16
- });
17
- } else {
18
- this.ws = urlOrSocket;
19
- }
20
- }
21
-
22
- send(message: string): void {
23
- this.ws.send(message);
24
- }
25
-
26
- close(): void {
27
- this.ws.close();
28
- }
29
-
30
- onOpen(handler: () => void): void {
31
- this.ws.on('open', handler);
32
- }
33
-
34
- onMessage(handler: (data: string) => void): void {
35
- const wrapper = (rawMessage: Buffer) => handler(String(rawMessage))
36
- this.messageWrappers.set(handler, wrapper);
37
- this.ws.on('message', wrapper);
38
- }
39
-
40
- onClose(handler: () => void): void {
41
- this.ws.on('close', handler);
42
- }
43
-
44
- onError(handler: (error: Error) => void): void {
45
- this.ws.on('error', handler);
46
- }
47
-
48
- offOpen(handler: () => void): void {
49
- this.ws.off('open', handler);
50
- }
51
-
52
- offMessage(handler: (data: string) => void): void {
53
- const wrapper = this.messageWrappers.get(handler);
54
- if (wrapper) {
55
- this.ws.off('message', wrapper);
56
- this.messageWrappers.delete(handler);
57
- }
58
- }
59
-
60
- offClose(handler: () => void): void {
61
- this.ws.off('close', handler);
62
- }
63
-
64
- offError(handler: (error: Error) => void): void {
65
- this.ws.off('error', handler);
66
- }
67
- }
1
+ import WebSocket from "ws";
2
+ import {SocketAdapter} from "../socket/SocketAdapter";
3
+
4
+ export interface NodeSocketAdapterOptions {
5
+ headers?: Record<string, string>;
6
+ }
7
+
8
+ export class NodeSocketAdapter implements SocketAdapter {
9
+ private ws: WebSocket;
10
+ private messageWrappers = new WeakMap<(data: string) => void, (rawMessage: Buffer) => void>();
11
+
12
+ constructor(urlOrSocket: string | WebSocket, options?: NodeSocketAdapterOptions) {
13
+ if (typeof urlOrSocket === 'string') {
14
+ this.ws = new WebSocket(urlOrSocket, {
15
+ headers: options?.headers
16
+ });
17
+ } else {
18
+ this.ws = urlOrSocket;
19
+ }
20
+ }
21
+
22
+ send(message: string): void {
23
+ this.ws.send(message);
24
+ }
25
+
26
+ close(): void {
27
+ this.ws.close();
28
+ }
29
+
30
+ onOpen(handler: () => void): void {
31
+ this.ws.on('open', handler);
32
+ }
33
+
34
+ onMessage(handler: (data: string) => void): void {
35
+ const wrapper = (rawMessage: Buffer) => handler(String(rawMessage))
36
+ this.messageWrappers.set(handler, wrapper);
37
+ this.ws.on('message', wrapper);
38
+ }
39
+
40
+ onClose(handler: () => void): void {
41
+ this.ws.on('close', handler);
42
+ }
43
+
44
+ onError(handler: (error: Error) => void): void {
45
+ this.ws.on('error', handler);
46
+ }
47
+
48
+ offOpen(handler: () => void): void {
49
+ this.ws.off('open', handler);
50
+ }
51
+
52
+ offMessage(handler: (data: string) => void): void {
53
+ const wrapper = this.messageWrappers.get(handler);
54
+ if (wrapper) {
55
+ this.ws.off('message', wrapper);
56
+ this.messageWrappers.delete(handler);
57
+ }
58
+ }
59
+
60
+ offClose(handler: () => void): void {
61
+ this.ws.off('close', handler);
62
+ }
63
+
64
+ offError(handler: (error: Error) => void): void {
65
+ this.ws.off('error', handler);
66
+ }
67
+ }
@@ -1,14 +1,14 @@
1
- import {isNode} from "@grest-ts/common";
2
-
3
- /**
4
- * Get default adapter - lazy loaded to avoid top-level await
5
- */
6
- export async function getDefaultAdapter(): Promise<any> {
7
- if (isNode()) {
8
- const {NodeSocketAdapter} = await import('./NodeSocketAdapter');
9
- return NodeSocketAdapter;
10
- } else {
11
- const {BrowserSocketAdapter} = await import('./BrowserSocketAdapter');
12
- return BrowserSocketAdapter;
13
- }
1
+ import {isNode} from "@grest-ts/common";
2
+
3
+ /**
4
+ * Get default adapter - lazy loaded to avoid top-level await
5
+ */
6
+ export async function getDefaultAdapter(): Promise<any> {
7
+ if (isNode()) {
8
+ const {NodeSocketAdapter} = await import('./NodeSocketAdapter');
9
+ return NodeSocketAdapter;
10
+ } else {
11
+ const {BrowserSocketAdapter} = await import('./BrowserSocketAdapter');
12
+ return BrowserSocketAdapter;
13
+ }
14
14
  }
@@ -1,25 +1,25 @@
1
- import {GGSocket} from "../socket/GGSocket";
2
- import {GGContractMethod} from "@grest-ts/schema";
3
-
4
- export abstract class GGSocketClient {
5
-
6
- public readonly socket: GGSocket
7
-
8
- constructor(socket: GGSocket) {
9
- this.socket = socket;
10
- }
11
-
12
- public onClose(onClose: () => void) {
13
- this.socket.onClose(onClose)
14
- return this
15
- }
16
-
17
- public close(): void {
18
- this.socket.close()
19
- }
20
-
21
- public __defineApi<T extends Record<string, GGContractMethod<any, any, any>>>(api: T): T {
22
- return api;
23
- }
24
-
25
- }
1
+ import {GGSocket} from "../socket/GGSocket";
2
+ import {GGContractMethod} from "@grest-ts/schema";
3
+
4
+ export abstract class GGSocketClient {
5
+
6
+ public readonly socket: GGSocket
7
+
8
+ constructor(socket: GGSocket) {
9
+ this.socket = socket;
10
+ }
11
+
12
+ public onClose(onClose: () => void) {
13
+ this.socket.onClose(onClose)
14
+ return this
15
+ }
16
+
17
+ public close(): void {
18
+ this.socket.close()
19
+ }
20
+
21
+ public __defineApi<T extends Record<string, GGContractMethod<any, any, any>>>(api: T): T {
22
+ return api;
23
+ }
24
+
25
+ }