@gjsify/dgram 0.1.0

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/src/index.ts ADDED
@@ -0,0 +1,489 @@
1
+ // Node.js dgram module for GJS — UDP sockets via Gio.Socket
2
+ // Reference: Node.js lib/dgram.js
3
+
4
+ import Gio from '@girs/gio-2.0';
5
+ import GLib from '@girs/glib-2.0';
6
+ import { EventEmitter } from 'node:events';
7
+ import { Buffer } from 'node:buffer';
8
+ import { deferEmit, ensureMainLoop } from '@gjsify/utils';
9
+
10
+ export interface SocketOptions {
11
+ type: 'udp4' | 'udp6';
12
+ reuseAddr?: boolean;
13
+ reusePort?: boolean;
14
+ ipv6Only?: boolean;
15
+ recvBufferSize?: number;
16
+ sendBufferSize?: number;
17
+ signal?: AbortSignal;
18
+ }
19
+
20
+ export interface AddressInfo {
21
+ address: string;
22
+ family: string;
23
+ port: number;
24
+ }
25
+
26
+ /**
27
+ * dgram.Socket — UDP socket wrapping Gio.Socket.
28
+ */
29
+ interface RemoteAddressInfo {
30
+ address: string;
31
+ family: string;
32
+ port: number;
33
+ }
34
+
35
+ // GC guard — GJS garbage-collects objects with no JS references.
36
+ // Keep strong references to bound UDP sockets to prevent their
37
+ // Gio.Socket from being collected while receiving data.
38
+ const _activeSockets = new Set<Socket>();
39
+
40
+ export class Socket extends EventEmitter {
41
+ readonly type: 'udp4' | 'udp6';
42
+
43
+ private _socket: Gio.Socket | null = null;
44
+ private _bound = false;
45
+ private _closed = false;
46
+ private _receiving = false;
47
+ private _address: AddressInfo = { address: '0.0.0.0', family: 'IPv4', port: 0 };
48
+ private _cancellable: Gio.Cancellable = new Gio.Cancellable();
49
+ private _reuseAddr: boolean;
50
+ private _connected = false;
51
+ private _remoteAddress: RemoteAddressInfo | null = null;
52
+
53
+ constructor(options: SocketOptions | string) {
54
+ super();
55
+
56
+ if (typeof options === 'string') {
57
+ this.type = options as 'udp4' | 'udp6';
58
+ this._reuseAddr = false;
59
+ } else {
60
+ this.type = options.type;
61
+ this._reuseAddr = options.reuseAddr ?? false;
62
+ }
63
+
64
+ const family = this.type === 'udp6' ? Gio.SocketFamily.IPV6 : Gio.SocketFamily.IPV4;
65
+
66
+ try {
67
+ this._socket = Gio.Socket.new(family, Gio.SocketType.DATAGRAM, Gio.SocketProtocol.UDP);
68
+ this._socket.set_blocking(false);
69
+ } catch (err) {
70
+ this._socket = null;
71
+ // Defer error emission
72
+ deferEmit(this, 'error', err);
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Bind the socket to a port and optional address.
78
+ */
79
+ bind(port?: number | { port?: number; address?: string; exclusive?: boolean }, address?: string | (() => void), callback?: () => void): this {
80
+ if (this._closed || !this._socket) return this;
81
+
82
+ let bindPort = 0;
83
+ let bindAddress = this.type === 'udp6' ? '::' : '0.0.0.0';
84
+
85
+ if (typeof port === 'object') {
86
+ const opts = port;
87
+ bindPort = opts.port || 0;
88
+ bindAddress = opts.address || bindAddress;
89
+ if (typeof address === 'function') callback = address;
90
+ } else if (typeof port === 'number') {
91
+ bindPort = port;
92
+ if (typeof address === 'string') bindAddress = address;
93
+ else if (typeof address === 'function') callback = address;
94
+ } else if (typeof port === 'function') {
95
+ callback = port;
96
+ }
97
+
98
+ if (callback) this.once('listening', callback);
99
+
100
+ try {
101
+ const family = this.type === 'udp6' ? Gio.SocketFamily.IPV6 : Gio.SocketFamily.IPV4;
102
+ const inetAddr = Gio.InetAddress.new_from_string(bindAddress) ||
103
+ (family === Gio.SocketFamily.IPV6 ? Gio.InetAddress.new_any(Gio.SocketFamily.IPV6) : Gio.InetAddress.new_any(Gio.SocketFamily.IPV4));
104
+ const sockAddr = new Gio.InetSocketAddress({ address: inetAddr, port: bindPort });
105
+
106
+ this._socket.bind(sockAddr, this._reuseAddr);
107
+ this._bound = true;
108
+ _activeSockets.add(this);
109
+ ensureMainLoop();
110
+
111
+ // Get actual bound address
112
+ const localAddr = this._socket.get_local_address() as Gio.InetSocketAddress;
113
+ if (localAddr) {
114
+ this._address = {
115
+ address: localAddr.get_address().to_string(),
116
+ family: this.type === 'udp6' ? 'IPv6' : 'IPv4',
117
+ port: localAddr.get_port(),
118
+ };
119
+ }
120
+
121
+ setTimeout(() => {
122
+ this.emit('listening');
123
+ this._startReceiving();
124
+ }, 0);
125
+ } catch (err) {
126
+ deferEmit(this, 'error', err);
127
+ }
128
+
129
+ return this;
130
+ }
131
+
132
+ /**
133
+ * Send a message.
134
+ */
135
+ send(
136
+ msg: Buffer | string | Uint8Array | (Buffer | string | Uint8Array)[],
137
+ offset?: number | ((err: Error | null, bytes: number) => void),
138
+ length?: number,
139
+ port?: number,
140
+ address?: string | ((err: Error | null, bytes: number) => void),
141
+ callback?: (err: Error | null, bytes: number) => void,
142
+ ): void {
143
+ if (this._closed || !this._socket) return;
144
+
145
+ // Handle overloaded signatures:
146
+ // send(msg, port, address, callback)
147
+ // send(msg, offset, length, port, address, callback)
148
+ let buf: Buffer;
149
+ let destPort: number;
150
+ let destAddress: string;
151
+ let cb: ((err: Error | null, bytes: number) => void) | undefined;
152
+
153
+ if (typeof offset === 'function') {
154
+ // send(msg, callback)
155
+ cb = offset;
156
+ buf = this._toBuffer(msg);
157
+ destPort = this._address.port;
158
+ destAddress = this._address.address;
159
+ } else if (typeof offset === 'number' && typeof length === 'string') {
160
+ // send(msg, port, address, callback) — offset=port, length=address, port=callback
161
+ destPort = offset;
162
+ destAddress = length;
163
+ cb = port as unknown as ((err: Error | null, bytes: number) => void) | undefined;
164
+ buf = this._toBuffer(msg);
165
+ } else if (typeof offset === 'number' && typeof length === 'number' && typeof address === 'function') {
166
+ // send(msg, offset, length, port, callback)
167
+ cb = address;
168
+ destPort = port!;
169
+ destAddress = this.type === 'udp6' ? '::1' : '127.0.0.1';
170
+ buf = this._toBufferSlice(msg, offset, length);
171
+ } else if (typeof offset === 'number' && typeof length === 'number') {
172
+ // send(msg, offset, length, port, address, callback)
173
+ destPort = port!;
174
+ destAddress = (address as string) || (this.type === 'udp6' ? '::1' : '127.0.0.1');
175
+ cb = callback;
176
+ buf = this._toBufferSlice(msg, offset, length);
177
+ } else {
178
+ // send(msg, port) or similar — best effort
179
+ destPort = Number(offset) || 0;
180
+ destAddress = this.type === 'udp6' ? '::1' : '127.0.0.1';
181
+ buf = this._toBuffer(msg);
182
+ }
183
+
184
+ try {
185
+ const inetAddr = Gio.InetAddress.new_from_string(destAddress);
186
+ const sockAddr = new Gio.InetSocketAddress({ address: inetAddr, port: destPort });
187
+
188
+ // Auto-bind if not yet bound
189
+ if (!this._bound) {
190
+ const anyAddr = this.type === 'udp6'
191
+ ? Gio.InetAddress.new_any(Gio.SocketFamily.IPV6)
192
+ : Gio.InetAddress.new_any(Gio.SocketFamily.IPV4);
193
+ const anySockAddr = new Gio.InetSocketAddress({ address: anyAddr, port: 0 });
194
+ this._socket.bind(anySockAddr, false);
195
+ this._bound = true;
196
+ }
197
+
198
+ const bytesSent = this._socket.send_to(sockAddr, buf, this._cancellable);
199
+ if (cb) cb(null, bytesSent);
200
+ } catch (err) {
201
+ if (cb) cb(err instanceof Error ? err : new Error(String(err)), 0);
202
+ else this.emit('error', err);
203
+ }
204
+ }
205
+
206
+ private _toBuffer(msg: Buffer | string | Uint8Array | (Buffer | string | Uint8Array)[]): Buffer {
207
+ if (Array.isArray(msg)) {
208
+ return Buffer.concat(msg.map(m => typeof m === 'string' ? Buffer.from(m) : Buffer.from(m)));
209
+ }
210
+ return typeof msg === 'string' ? Buffer.from(msg) : Buffer.from(msg);
211
+ }
212
+
213
+ private _toBufferSlice(msg: Buffer | string | Uint8Array | (Buffer | string | Uint8Array)[], offset: number, length: number): Buffer {
214
+ const buf = this._toBuffer(msg);
215
+ return Buffer.from(buf.buffer, buf.byteOffset + offset, length);
216
+ }
217
+
218
+ /**
219
+ * Close the socket.
220
+ */
221
+ close(callback?: () => void): this {
222
+ if (this._closed) {
223
+ throw new Error('Not running');
224
+ }
225
+ this._closed = true;
226
+ _activeSockets.delete(this);
227
+
228
+ if (callback) this.once('close', callback);
229
+
230
+ this._cancellable.cancel();
231
+
232
+ if (this._socket) {
233
+ try {
234
+ this._socket.close();
235
+ } catch (_e) {
236
+ // Ignore close errors
237
+ }
238
+ this._socket = null;
239
+ }
240
+
241
+ deferEmit(this, 'close');
242
+ return this;
243
+ }
244
+
245
+ /**
246
+ * Associate the socket with a remote address/port (connected UDP).
247
+ * After connect(), send() can omit address and port.
248
+ */
249
+ connect(port: number, address?: string | (() => void), callback?: () => void): void {
250
+ if (this._connected) {
251
+ const err = new Error('Already connected') as NodeJS.ErrnoException;
252
+ err.code = 'ERR_SOCKET_DGRAM_IS_CONNECTED';
253
+ throw err;
254
+ }
255
+ if (!port || port <= 0 || port >= 65536) {
256
+ const err = new RangeError(`Port should be > 0 and < 65536. Received ${port}.`) as NodeJS.ErrnoException;
257
+ err.code = 'ERR_SOCKET_BAD_PORT';
258
+ throw err;
259
+ }
260
+ let resolvedAddr: string;
261
+ let cb: (() => void) | undefined;
262
+ if (typeof address === 'function') {
263
+ cb = address;
264
+ resolvedAddr = this.type === 'udp6' ? '::1' : '127.0.0.1';
265
+ } else {
266
+ resolvedAddr = address || (this.type === 'udp6' ? '::1' : '127.0.0.1');
267
+ cb = callback;
268
+ }
269
+
270
+ this._connected = true;
271
+ this._remoteAddress = {
272
+ address: resolvedAddr,
273
+ family: this.type === 'udp6' ? 'IPv6' : 'IPv4',
274
+ port,
275
+ };
276
+
277
+ if (cb) {
278
+ // Emit connect asynchronously (matches Node.js behaviour)
279
+ Promise.resolve().then(() => {
280
+ this.emit('connect');
281
+ cb!();
282
+ });
283
+ } else {
284
+ Promise.resolve().then(() => this.emit('connect'));
285
+ }
286
+ }
287
+
288
+ /**
289
+ * Dissociate a connected socket from its remote address.
290
+ */
291
+ disconnect(): void {
292
+ if (!this._connected) {
293
+ const err = new Error('Not connected') as NodeJS.ErrnoException;
294
+ err.code = 'ERR_SOCKET_DGRAM_NOT_CONNECTED';
295
+ throw err;
296
+ }
297
+ this._connected = false;
298
+ this._remoteAddress = null;
299
+ }
300
+
301
+ /**
302
+ * Returns the remote address of a connected socket.
303
+ * Throws ERR_SOCKET_DGRAM_NOT_CONNECTED if not connected.
304
+ */
305
+ remoteAddress(): RemoteAddressInfo {
306
+ if (!this._connected || !this._remoteAddress) {
307
+ const err = new Error('Not connected') as NodeJS.ErrnoException;
308
+ err.code = 'ERR_SOCKET_DGRAM_NOT_CONNECTED';
309
+ throw err;
310
+ }
311
+ return { ...this._remoteAddress };
312
+ }
313
+
314
+ /**
315
+ * Get the bound address info.
316
+ */
317
+ address(): AddressInfo {
318
+ return { ...this._address };
319
+ }
320
+
321
+ /**
322
+ * Set the broadcast flag.
323
+ */
324
+ setBroadcast(flag: boolean): void {
325
+ if (this._socket) {
326
+ this._socket.set_broadcast(flag);
327
+ }
328
+ }
329
+
330
+ /**
331
+ * Set the TTL.
332
+ */
333
+ setTTL(ttl: number): number {
334
+ if (this._socket) {
335
+ this._socket.set_ttl(ttl);
336
+ }
337
+ return ttl;
338
+ }
339
+
340
+ /**
341
+ * Set multicast TTL.
342
+ */
343
+ setMulticastTTL(ttl: number): number {
344
+ if (this._socket) {
345
+ this._socket.set_multicast_ttl(ttl);
346
+ }
347
+ return ttl;
348
+ }
349
+
350
+ /**
351
+ * Set multicast loopback.
352
+ */
353
+ setMulticastLoopback(flag: boolean): boolean {
354
+ if (this._socket) {
355
+ this._socket.set_multicast_loopback(flag);
356
+ }
357
+ return flag;
358
+ }
359
+
360
+ /**
361
+ * Add multicast group membership.
362
+ */
363
+ addMembership(multicastAddress: string, multicastInterface?: string): void {
364
+ if (!this._socket) return;
365
+ try {
366
+ const mcastAddr = Gio.InetAddress.new_from_string(multicastAddress);
367
+ this._socket.join_multicast_group(mcastAddr, false, multicastInterface || null);
368
+ } catch (err) {
369
+ this.emit('error', err);
370
+ }
371
+ }
372
+
373
+ /**
374
+ * Drop multicast group membership.
375
+ */
376
+ dropMembership(multicastAddress: string, multicastInterface?: string): void {
377
+ if (!this._socket) return;
378
+ try {
379
+ const mcastAddr = Gio.InetAddress.new_from_string(multicastAddress);
380
+ this._socket.leave_multicast_group(mcastAddr, false, multicastInterface || null);
381
+ } catch (err) {
382
+ this.emit('error', err);
383
+ }
384
+ }
385
+
386
+ /**
387
+ * Set multicast interface.
388
+ */
389
+ setMulticastInterface(_interfaceAddress: string): void {
390
+ // GLib handles this via join_multicast_group interface parameter
391
+ }
392
+
393
+ /** Ref the socket (keep event loop alive). */
394
+ ref(): this {
395
+ return this;
396
+ }
397
+
398
+ /** Unref the socket (allow event loop to exit). */
399
+ unref(): this {
400
+ return this;
401
+ }
402
+
403
+ /** Get/Set receive buffer size. */
404
+ getRecvBufferSize(): number {
405
+ return 65536; // Default
406
+ }
407
+
408
+ setRecvBufferSize(_size: number): void {
409
+ // Gio.Socket doesn't expose SO_RCVBUF directly
410
+ }
411
+
412
+ /** Get/Set send buffer size. */
413
+ getSendBufferSize(): number {
414
+ return 65536; // Default
415
+ }
416
+
417
+ setSendBufferSize(_size: number): void {
418
+ // Gio.Socket doesn't expose SO_SNDBUF directly
419
+ }
420
+
421
+ /**
422
+ * Start receiving messages in background.
423
+ */
424
+ private _startReceiving(): void {
425
+ if (this._receiving || this._closed || !this._socket) return;
426
+ this._receiving = true;
427
+ this._receiveLoop();
428
+ }
429
+
430
+ private _receiveLoop(): void {
431
+ if (this._closed || !this._socket) return;
432
+
433
+ // Use condition_timed_wait with a short timeout to poll for data
434
+ // Then read with receive_from
435
+ try {
436
+ if (!this._socket.condition_check(GLib.IOCondition.IN)) {
437
+ // No data yet, schedule retry
438
+ setTimeout(() => this._receiveLoop(), 50);
439
+ return;
440
+ }
441
+
442
+ const buf = new Uint8Array(65536);
443
+ const result = (this._socket as unknown as { receive_from(buf: Uint8Array, cancellable: Gio.Cancellable): [number, Gio.SocketAddress | null] }).receive_from(buf, this._cancellable);
444
+ const bytesRead = Array.isArray(result) ? result[0] : result;
445
+ const srcAddr = Array.isArray(result) ? result[1] : null;
446
+
447
+ if (bytesRead > 0 && srcAddr) {
448
+ const data = Buffer.from(buf.subarray(0, bytesRead as number));
449
+ const inetSockAddr = srcAddr as Gio.InetSocketAddress;
450
+ const rinfo: AddressInfo = {
451
+ address: inetSockAddr.get_address().to_string(),
452
+ family: this.type === 'udp6' ? 'IPv6' : 'IPv4',
453
+ port: inetSockAddr.get_port(),
454
+ };
455
+
456
+ this.emit('message', data, rinfo);
457
+ }
458
+
459
+ // Continue receiving
460
+ if (!this._closed) {
461
+ setTimeout(() => this._receiveLoop(), 0);
462
+ }
463
+ } catch (err: unknown) {
464
+ if (!this._closed) {
465
+ // IOErrorEnum CANCELLED is expected when socket is closed
466
+ const errObj = err as { code?: number };
467
+ if (errObj.code !== Gio.IOErrorEnum.CANCELLED) {
468
+ this.emit('error', err);
469
+ }
470
+ }
471
+ }
472
+ }
473
+ }
474
+
475
+ /**
476
+ * Create a UDP socket.
477
+ */
478
+ export function createSocket(type: 'udp4' | 'udp6' | SocketOptions, callback?: (msg: Buffer, rinfo: AddressInfo) => void): Socket {
479
+ const opts = typeof type === 'string' ? { type } : type;
480
+ const socket = new Socket(opts);
481
+
482
+ if (callback) {
483
+ socket.on('message', callback);
484
+ }
485
+
486
+ return socket;
487
+ }
488
+
489
+ export default { Socket, createSocket };
package/src/test.mts ADDED
@@ -0,0 +1,3 @@
1
+ import { run } from '@gjsify/unit';
2
+ import testSuite from './index.spec.js';
3
+ run({ testSuite });
package/tsconfig.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "ESNext",
4
+ "target": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "types": [
7
+ "node"
8
+ ],
9
+ "experimentalDecorators": true,
10
+ "emitDeclarationOnly": true,
11
+ "declaration": true,
12
+ "allowImportingTsExtensions": true,
13
+ "outDir": "lib",
14
+ "rootDir": "src",
15
+ "declarationDir": "lib/types",
16
+ "composite": true,
17
+ "skipLibCheck": true,
18
+ "allowJs": true,
19
+ "checkJs": false,
20
+ "strict": false
21
+ },
22
+ "include": [
23
+ "src/**/*.ts"
24
+ ],
25
+ "exclude": [
26
+ "src/test.ts",
27
+ "src/test.mts",
28
+ "src/**/*.spec.ts",
29
+ "src/**/*.spec.mts"
30
+ ]
31
+ }