@gjsify/dgram 0.3.12 → 0.3.14

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 (2) hide show
  1. package/lib/esm/index.js +444 -434
  2. package/package.json +8 -8
package/lib/esm/index.js CHANGED
@@ -3,440 +3,450 @@ import GLib from "@girs/glib-2.0";
3
3
  import { EventEmitter } from "node:events";
4
4
  import { Buffer } from "node:buffer";
5
5
  import { deferEmit, ensureMainLoop } from "@gjsify/utils";
6
- const _activeSockets = /* @__PURE__ */ new Set();
7
- class Socket extends EventEmitter {
8
- type;
9
- _socket = null;
10
- _bound = false;
11
- _closed = false;
12
- _receiving = false;
13
- _address = { address: "0.0.0.0", family: "IPv4", port: 0 };
14
- _cancellable = new Gio.Cancellable();
15
- // Strong JS ref to the GSocketSource so SM GC cannot finalize the
16
- // BoxedInstance while GLib's main context still holds it.
17
- _readSource = null;
18
- _reuseAddr;
19
- _connected = false;
20
- _remoteAddress = null;
21
- constructor(options) {
22
- super();
23
- if (typeof options === "string") {
24
- this.type = options;
25
- this._reuseAddr = false;
26
- } else {
27
- this.type = options.type;
28
- this._reuseAddr = options.reuseAddr ?? false;
29
- }
30
- const family = this.type === "udp6" ? Gio.SocketFamily.IPV6 : Gio.SocketFamily.IPV4;
31
- try {
32
- this._socket = Gio.Socket.new(family, Gio.SocketType.DATAGRAM, Gio.SocketProtocol.UDP);
33
- this._socket.set_blocking(false);
34
- } catch (err) {
35
- this._socket = null;
36
- deferEmit(this, "error", err);
37
- }
38
- }
39
- /**
40
- * Bind the socket to a port and optional address.
41
- */
42
- bind(port, address, callback) {
43
- if (this._closed || !this._socket) return this;
44
- let bindPort = 0;
45
- let bindAddress = this.type === "udp6" ? "::" : "0.0.0.0";
46
- if (typeof port === "object") {
47
- const opts = port;
48
- bindPort = opts.port || 0;
49
- bindAddress = opts.address || bindAddress;
50
- if (typeof address === "function") callback = address;
51
- } else if (typeof port === "number") {
52
- bindPort = port;
53
- if (typeof address === "string") bindAddress = address;
54
- else if (typeof address === "function") callback = address;
55
- } else if (typeof port === "function") {
56
- callback = port;
57
- }
58
- if (callback) this.once("listening", callback);
59
- try {
60
- const family = this.type === "udp6" ? Gio.SocketFamily.IPV6 : Gio.SocketFamily.IPV4;
61
- const inetAddr = Gio.InetAddress.new_from_string(bindAddress) || (family === Gio.SocketFamily.IPV6 ? Gio.InetAddress.new_any(Gio.SocketFamily.IPV6) : Gio.InetAddress.new_any(Gio.SocketFamily.IPV4));
62
- const sockAddr = new Gio.InetSocketAddress({ address: inetAddr, port: bindPort });
63
- this._socket.bind(sockAddr, this._reuseAddr);
64
- this._bound = true;
65
- _activeSockets.add(this);
66
- ensureMainLoop();
67
- const localAddr = this._socket.get_local_address();
68
- if (localAddr) {
69
- this._address = {
70
- address: localAddr.get_address().to_string(),
71
- family: this.type === "udp6" ? "IPv6" : "IPv4",
72
- port: localAddr.get_port()
73
- };
74
- }
75
- setTimeout(() => {
76
- this.emit("listening");
77
- this._startReceiving();
78
- }, 0);
79
- } catch (err) {
80
- deferEmit(this, "error", err);
81
- }
82
- return this;
83
- }
84
- /**
85
- * Send a message.
86
- */
87
- send(msg, offset, length, port, address, callback) {
88
- if (this._closed || !this._socket) return;
89
- let buf;
90
- let destPort;
91
- let destAddress;
92
- let cb;
93
- if (typeof offset === "function") {
94
- cb = offset;
95
- buf = this._toBuffer(msg);
96
- destPort = this._address.port;
97
- destAddress = this._address.address;
98
- } else if (typeof offset === "number" && typeof length === "string") {
99
- destPort = offset;
100
- destAddress = length;
101
- cb = port;
102
- buf = this._toBuffer(msg);
103
- } else if (typeof offset === "number" && typeof length === "number" && typeof address === "function") {
104
- cb = address;
105
- destPort = port;
106
- destAddress = this.type === "udp6" ? "::1" : "127.0.0.1";
107
- buf = this._toBufferSlice(msg, offset, length);
108
- } else if (typeof offset === "number" && typeof length === "number") {
109
- destPort = port;
110
- destAddress = address || (this.type === "udp6" ? "::1" : "127.0.0.1");
111
- cb = callback;
112
- buf = this._toBufferSlice(msg, offset, length);
113
- } else {
114
- destPort = Number(offset) || 0;
115
- destAddress = this.type === "udp6" ? "::1" : "127.0.0.1";
116
- buf = this._toBuffer(msg);
117
- }
118
- this._resolveAndSend(destAddress, destPort, buf, cb);
119
- }
120
- _autoBind() {
121
- if (this._bound || !this._socket) return;
122
- const anyAddr = this.type === "udp6" ? Gio.InetAddress.new_any(Gio.SocketFamily.IPV6) : Gio.InetAddress.new_any(Gio.SocketFamily.IPV4);
123
- const anySockAddr = new Gio.InetSocketAddress({ address: anyAddr, port: 0 });
124
- this._socket.bind(anySockAddr, false);
125
- this._bound = true;
126
- _activeSockets.add(this);
127
- ensureMainLoop();
128
- }
129
- /** Socket family that matches `this.type` (udp4 IPv4, udp6 IPv6). */
130
- _socketFamily() {
131
- return this.type === "udp6" ? Gio.SocketFamily.IPV6 : Gio.SocketFamily.IPV4;
132
- }
133
- /** True when `addr` can be sent to from a socket bound with `this._socketFamily()`. */
134
- _isAddressCompatible(addr) {
135
- return addr.get_family() === this._socketFamily();
136
- }
137
- /** Build an EINVAL error for a destination whose family doesn't match the socket.
138
- * Matches Node.js dgram behavior (node reports EINVAL from sendto(2) when the
139
- * destination address family is incompatible with the bound socket). */
140
- _mismatchError(destAddress) {
141
- const want = this.type === "udp6" ? "IPv6" : "IPv4";
142
- const err = new Error(
143
- `EINVAL: cannot send to ${destAddress} from a ${want} socket (address-family mismatch)`
144
- );
145
- err.code = "EINVAL";
146
- err.syscall = "send";
147
- return err;
148
- }
149
- _resolveAndSend(destAddress, destPort, buf, cb) {
150
- if (this._closed || !this._socket) {
151
- if (cb) cb(new Error("Socket is closed"), 0);
152
- return;
153
- }
154
- let inetAddr = Gio.InetAddress.new_from_string(destAddress);
155
- const doSend = (addr) => {
156
- if (!this._isAddressCompatible(addr)) {
157
- const err = this._mismatchError(destAddress);
158
- if (cb) cb(err, 0);
159
- else this.emit("error", err);
160
- return;
161
- }
162
- try {
163
- this._autoBind();
164
- const sockAddr = new Gio.InetSocketAddress({ address: addr, port: destPort });
165
- const bytesSent = this._socket.send_to(sockAddr, buf, this._cancellable);
166
- if (cb) cb(null, bytesSent);
167
- } catch (err) {
168
- if (cb) cb(err instanceof Error ? err : new Error(String(err)), 0);
169
- else this.emit("error", err);
170
- }
171
- };
172
- if (inetAddr) {
173
- doSend(inetAddr);
174
- return;
175
- }
176
- const resolver = Gio.Resolver.get_default();
177
- resolver.lookup_by_name_async(destAddress, this._cancellable, (_obj, res) => {
178
- try {
179
- const addresses = resolver.lookup_by_name_finish(res);
180
- if (!addresses || addresses.length === 0) {
181
- const err = new Error(`ENOTFOUND ${destAddress}`);
182
- err.code = "ENOTFOUND";
183
- if (cb) cb(err, 0);
184
- else this.emit("error", err);
185
- return;
186
- }
187
- const compatible = addresses.find((a) => this._isAddressCompatible(a));
188
- if (!compatible) {
189
- const err = this._mismatchError(destAddress);
190
- if (cb) cb(err, 0);
191
- else this.emit("error", err);
192
- return;
193
- }
194
- doSend(compatible);
195
- } catch (err) {
196
- if (cb) cb(err instanceof Error ? err : new Error(String(err)), 0);
197
- else this.emit("error", err);
198
- }
199
- });
200
- }
201
- _toBuffer(msg) {
202
- if (Array.isArray(msg)) {
203
- return Buffer.concat(msg.map((m) => typeof m === "string" ? Buffer.from(m) : Buffer.from(m)));
204
- }
205
- return typeof msg === "string" ? Buffer.from(msg) : Buffer.from(msg);
206
- }
207
- _toBufferSlice(msg, offset, length) {
208
- const buf = this._toBuffer(msg);
209
- return Buffer.from(buf.buffer, buf.byteOffset + offset, length);
210
- }
211
- /**
212
- * Close the socket.
213
- */
214
- close(callback) {
215
- if (this._closed) {
216
- throw new Error("Not running");
217
- }
218
- this._closed = true;
219
- _activeSockets.delete(this);
220
- if (callback) this.once("close", callback);
221
- if (this._readSource) {
222
- try {
223
- this._readSource.destroy();
224
- } catch (_e) {
225
- }
226
- this._readSource = null;
227
- }
228
- this._cancellable.cancel();
229
- if (this._socket) {
230
- try {
231
- this._socket.close();
232
- } catch (_e) {
233
- }
234
- this._socket = null;
235
- }
236
- deferEmit(this, "close");
237
- return this;
238
- }
239
- /**
240
- * Associate the socket with a remote address/port (connected UDP).
241
- * After connect(), send() can omit address and port.
242
- */
243
- connect(port, address, callback) {
244
- if (this._connected) {
245
- const err = new Error("Already connected");
246
- err.code = "ERR_SOCKET_DGRAM_IS_CONNECTED";
247
- throw err;
248
- }
249
- if (!port || port <= 0 || port >= 65536) {
250
- const err = new RangeError(`Port should be > 0 and < 65536. Received ${port}.`);
251
- err.code = "ERR_SOCKET_BAD_PORT";
252
- throw err;
253
- }
254
- let resolvedAddr;
255
- let cb;
256
- if (typeof address === "function") {
257
- cb = address;
258
- resolvedAddr = this.type === "udp6" ? "::1" : "127.0.0.1";
259
- } else {
260
- resolvedAddr = address || (this.type === "udp6" ? "::1" : "127.0.0.1");
261
- cb = callback;
262
- }
263
- this._connected = true;
264
- this._remoteAddress = {
265
- address: resolvedAddr,
266
- family: this.type === "udp6" ? "IPv6" : "IPv4",
267
- port
268
- };
269
- if (cb) {
270
- Promise.resolve().then(() => {
271
- this.emit("connect");
272
- cb();
273
- });
274
- } else {
275
- Promise.resolve().then(() => this.emit("connect"));
276
- }
277
- }
278
- /**
279
- * Dissociate a connected socket from its remote address.
280
- */
281
- disconnect() {
282
- if (!this._connected) {
283
- const err = new Error("Not connected");
284
- err.code = "ERR_SOCKET_DGRAM_NOT_CONNECTED";
285
- throw err;
286
- }
287
- this._connected = false;
288
- this._remoteAddress = null;
289
- }
290
- /**
291
- * Returns the remote address of a connected socket.
292
- * Throws ERR_SOCKET_DGRAM_NOT_CONNECTED if not connected.
293
- */
294
- remoteAddress() {
295
- if (!this._connected || !this._remoteAddress) {
296
- const err = new Error("Not connected");
297
- err.code = "ERR_SOCKET_DGRAM_NOT_CONNECTED";
298
- throw err;
299
- }
300
- return { ...this._remoteAddress };
301
- }
302
- /**
303
- * Get the bound address info.
304
- */
305
- address() {
306
- return { ...this._address };
307
- }
308
- /**
309
- * Set the broadcast flag.
310
- */
311
- setBroadcast(flag) {
312
- if (this._socket) {
313
- this._socket.set_broadcast(flag);
314
- }
315
- }
316
- /**
317
- * Set the TTL.
318
- */
319
- setTTL(ttl) {
320
- if (this._socket) {
321
- this._socket.set_ttl(ttl);
322
- }
323
- return ttl;
324
- }
325
- /**
326
- * Set multicast TTL.
327
- */
328
- setMulticastTTL(ttl) {
329
- if (this._socket) {
330
- this._socket.set_multicast_ttl(ttl);
331
- }
332
- return ttl;
333
- }
334
- /**
335
- * Set multicast loopback.
336
- */
337
- setMulticastLoopback(flag) {
338
- if (this._socket) {
339
- this._socket.set_multicast_loopback(flag);
340
- }
341
- return flag;
342
- }
343
- /**
344
- * Add multicast group membership.
345
- */
346
- addMembership(multicastAddress, multicastInterface) {
347
- if (!this._socket) return;
348
- try {
349
- const mcastAddr = Gio.InetAddress.new_from_string(multicastAddress);
350
- this._socket.join_multicast_group(mcastAddr, false, multicastInterface || null);
351
- } catch (err) {
352
- this.emit("error", err);
353
- }
354
- }
355
- /**
356
- * Drop multicast group membership.
357
- */
358
- dropMembership(multicastAddress, multicastInterface) {
359
- if (!this._socket) return;
360
- try {
361
- const mcastAddr = Gio.InetAddress.new_from_string(multicastAddress);
362
- this._socket.leave_multicast_group(mcastAddr, false, multicastInterface || null);
363
- } catch (err) {
364
- this.emit("error", err);
365
- }
366
- }
367
- /**
368
- * Set multicast interface.
369
- */
370
- setMulticastInterface(_interfaceAddress) {
371
- }
372
- /** Ref the socket (keep event loop alive). */
373
- ref() {
374
- return this;
375
- }
376
- /** Unref the socket (allow event loop to exit). */
377
- unref() {
378
- return this;
379
- }
380
- /** Get/Set receive buffer size. */
381
- getRecvBufferSize() {
382
- return 65536;
383
- }
384
- setRecvBufferSize(_size) {
385
- }
386
- /** Get/Set send buffer size. */
387
- getSendBufferSize() {
388
- return 65536;
389
- }
390
- setSendBufferSize(_size) {
391
- }
392
- /**
393
- * Start receiving messages in background.
394
- */
395
- _startReceiving() {
396
- if (this._receiving || this._closed || !this._socket) return;
397
- this._receiving = true;
398
- this._receiveLoop();
399
- }
400
- _receiveLoop() {
401
- if (this._closed || !this._socket || this._readSource) return;
402
- const source = this._socket.create_source(GLib.IOCondition.IN, this._cancellable);
403
- this._readSource = source;
404
- source.set_callback(() => {
405
- if (this._closed || !this._socket) return GLib.SOURCE_REMOVE;
406
- try {
407
- const [bytes, srcAddr] = this._socket.receive_bytes_from(65536, 0, this._cancellable);
408
- if (bytes && srcAddr) {
409
- const data = Buffer.from(bytes.get_data());
410
- const inetSockAddr = srcAddr;
411
- const rinfo = {
412
- address: inetSockAddr.get_address().to_string(),
413
- family: this.type === "udp6" ? "IPv6" : "IPv4",
414
- port: inetSockAddr.get_port()
415
- };
416
- if (data.length > 0) this.emit("message", data, rinfo);
417
- }
418
- } catch (err) {
419
- const errObj = err;
420
- if (!this._closed && errObj.code !== Gio.IOErrorEnum.CANCELLED) {
421
- this.emit("error", err);
422
- }
423
- }
424
- return GLib.SOURCE_CONTINUE;
425
- });
426
- source.attach(null);
427
- }
428
- }
6
+
7
+ //#region src/index.ts
8
+ const _activeSockets = new Set();
9
+ var Socket = class extends EventEmitter {
10
+ type;
11
+ _socket = null;
12
+ _bound = false;
13
+ _closed = false;
14
+ _receiving = false;
15
+ _address = {
16
+ address: "0.0.0.0",
17
+ family: "IPv4",
18
+ port: 0
19
+ };
20
+ _cancellable = new Gio.Cancellable();
21
+ _readSource = null;
22
+ _reuseAddr;
23
+ _connected = false;
24
+ _remoteAddress = null;
25
+ constructor(options) {
26
+ super();
27
+ if (typeof options === "string") {
28
+ this.type = options;
29
+ this._reuseAddr = false;
30
+ } else {
31
+ this.type = options.type;
32
+ this._reuseAddr = options.reuseAddr ?? false;
33
+ }
34
+ const family = this.type === "udp6" ? Gio.SocketFamily.IPV6 : Gio.SocketFamily.IPV4;
35
+ try {
36
+ this._socket = Gio.Socket.new(family, Gio.SocketType.DATAGRAM, Gio.SocketProtocol.UDP);
37
+ this._socket.set_blocking(false);
38
+ } catch (err) {
39
+ this._socket = null;
40
+ deferEmit(this, "error", err);
41
+ }
42
+ }
43
+ /**
44
+ * Bind the socket to a port and optional address.
45
+ */
46
+ bind(port, address, callback) {
47
+ if (this._closed || !this._socket) return this;
48
+ let bindPort = 0;
49
+ let bindAddress = this.type === "udp6" ? "::" : "0.0.0.0";
50
+ if (typeof port === "object") {
51
+ const opts = port;
52
+ bindPort = opts.port || 0;
53
+ bindAddress = opts.address || bindAddress;
54
+ if (typeof address === "function") callback = address;
55
+ } else if (typeof port === "number") {
56
+ bindPort = port;
57
+ if (typeof address === "string") bindAddress = address;
58
+ else if (typeof address === "function") callback = address;
59
+ } else if (typeof port === "function") {
60
+ callback = port;
61
+ }
62
+ if (callback) this.once("listening", callback);
63
+ try {
64
+ const family = this.type === "udp6" ? Gio.SocketFamily.IPV6 : Gio.SocketFamily.IPV4;
65
+ const inetAddr = Gio.InetAddress.new_from_string(bindAddress) || (family === Gio.SocketFamily.IPV6 ? Gio.InetAddress.new_any(Gio.SocketFamily.IPV6) : Gio.InetAddress.new_any(Gio.SocketFamily.IPV4));
66
+ const sockAddr = new Gio.InetSocketAddress({
67
+ address: inetAddr,
68
+ port: bindPort
69
+ });
70
+ this._socket.bind(sockAddr, this._reuseAddr);
71
+ this._bound = true;
72
+ _activeSockets.add(this);
73
+ ensureMainLoop();
74
+ const localAddr = this._socket.get_local_address();
75
+ if (localAddr) {
76
+ this._address = {
77
+ address: localAddr.get_address().to_string(),
78
+ family: this.type === "udp6" ? "IPv6" : "IPv4",
79
+ port: localAddr.get_port()
80
+ };
81
+ }
82
+ setTimeout(() => {
83
+ this.emit("listening");
84
+ this._startReceiving();
85
+ }, 0);
86
+ } catch (err) {
87
+ deferEmit(this, "error", err);
88
+ }
89
+ return this;
90
+ }
91
+ /**
92
+ * Send a message.
93
+ */
94
+ send(msg, offset, length, port, address, callback) {
95
+ if (this._closed || !this._socket) return;
96
+ let buf;
97
+ let destPort;
98
+ let destAddress;
99
+ let cb;
100
+ if (typeof offset === "function") {
101
+ cb = offset;
102
+ buf = this._toBuffer(msg);
103
+ destPort = this._address.port;
104
+ destAddress = this._address.address;
105
+ } else if (typeof offset === "number" && typeof length === "string") {
106
+ destPort = offset;
107
+ destAddress = length;
108
+ cb = port;
109
+ buf = this._toBuffer(msg);
110
+ } else if (typeof offset === "number" && typeof length === "number" && typeof address === "function") {
111
+ cb = address;
112
+ destPort = port;
113
+ destAddress = this.type === "udp6" ? "::1" : "127.0.0.1";
114
+ buf = this._toBufferSlice(msg, offset, length);
115
+ } else if (typeof offset === "number" && typeof length === "number") {
116
+ destPort = port;
117
+ destAddress = address || (this.type === "udp6" ? "::1" : "127.0.0.1");
118
+ cb = callback;
119
+ buf = this._toBufferSlice(msg, offset, length);
120
+ } else {
121
+ destPort = Number(offset) || 0;
122
+ destAddress = this.type === "udp6" ? "::1" : "127.0.0.1";
123
+ buf = this._toBuffer(msg);
124
+ }
125
+ this._resolveAndSend(destAddress, destPort, buf, cb);
126
+ }
127
+ _autoBind() {
128
+ if (this._bound || !this._socket) return;
129
+ const anyAddr = this.type === "udp6" ? Gio.InetAddress.new_any(Gio.SocketFamily.IPV6) : Gio.InetAddress.new_any(Gio.SocketFamily.IPV4);
130
+ const anySockAddr = new Gio.InetSocketAddress({
131
+ address: anyAddr,
132
+ port: 0
133
+ });
134
+ this._socket.bind(anySockAddr, false);
135
+ this._bound = true;
136
+ _activeSockets.add(this);
137
+ ensureMainLoop();
138
+ }
139
+ /** Socket family that matches `this.type` (udp4 IPv4, udp6 → IPv6). */
140
+ _socketFamily() {
141
+ return this.type === "udp6" ? Gio.SocketFamily.IPV6 : Gio.SocketFamily.IPV4;
142
+ }
143
+ /** True when `addr` can be sent to from a socket bound with `this._socketFamily()`. */
144
+ _isAddressCompatible(addr) {
145
+ return addr.get_family() === this._socketFamily();
146
+ }
147
+ /** Build an EINVAL error for a destination whose family doesn't match the socket.
148
+ * Matches Node.js dgram behavior (node reports EINVAL from sendto(2) when the
149
+ * destination address family is incompatible with the bound socket). */
150
+ _mismatchError(destAddress) {
151
+ const want = this.type === "udp6" ? "IPv6" : "IPv4";
152
+ const err = new Error(`EINVAL: cannot send to ${destAddress} from a ${want} socket (address-family mismatch)`);
153
+ err.code = "EINVAL";
154
+ err.syscall = "send";
155
+ return err;
156
+ }
157
+ _resolveAndSend(destAddress, destPort, buf, cb) {
158
+ if (this._closed || !this._socket) {
159
+ if (cb) cb(new Error("Socket is closed"), 0);
160
+ return;
161
+ }
162
+ let inetAddr = Gio.InetAddress.new_from_string(destAddress);
163
+ const doSend = (addr) => {
164
+ if (!this._isAddressCompatible(addr)) {
165
+ const err = this._mismatchError(destAddress);
166
+ if (cb) cb(err, 0);
167
+ else this.emit("error", err);
168
+ return;
169
+ }
170
+ try {
171
+ this._autoBind();
172
+ const sockAddr = new Gio.InetSocketAddress({
173
+ address: addr,
174
+ port: destPort
175
+ });
176
+ const bytesSent = this._socket.send_to(sockAddr, buf, this._cancellable);
177
+ if (cb) cb(null, bytesSent);
178
+ } catch (err) {
179
+ if (cb) cb(err instanceof Error ? err : new Error(String(err)), 0);
180
+ else this.emit("error", err);
181
+ }
182
+ };
183
+ if (inetAddr) {
184
+ doSend(inetAddr);
185
+ return;
186
+ }
187
+ const resolver = Gio.Resolver.get_default();
188
+ resolver.lookup_by_name_async(destAddress, this._cancellable, (_obj, res) => {
189
+ try {
190
+ const addresses = resolver.lookup_by_name_finish(res);
191
+ if (!addresses || addresses.length === 0) {
192
+ const err = new Error(`ENOTFOUND ${destAddress}`);
193
+ err.code = "ENOTFOUND";
194
+ if (cb) cb(err, 0);
195
+ else this.emit("error", err);
196
+ return;
197
+ }
198
+ const compatible = addresses.find((a) => this._isAddressCompatible(a));
199
+ if (!compatible) {
200
+ const err = this._mismatchError(destAddress);
201
+ if (cb) cb(err, 0);
202
+ else this.emit("error", err);
203
+ return;
204
+ }
205
+ doSend(compatible);
206
+ } catch (err) {
207
+ if (cb) cb(err instanceof Error ? err : new Error(String(err)), 0);
208
+ else this.emit("error", err);
209
+ }
210
+ });
211
+ }
212
+ _toBuffer(msg) {
213
+ if (Array.isArray(msg)) {
214
+ return Buffer.concat(msg.map((m) => typeof m === "string" ? Buffer.from(m) : Buffer.from(m)));
215
+ }
216
+ return typeof msg === "string" ? Buffer.from(msg) : Buffer.from(msg);
217
+ }
218
+ _toBufferSlice(msg, offset, length) {
219
+ const buf = this._toBuffer(msg);
220
+ return Buffer.from(buf.buffer, buf.byteOffset + offset, length);
221
+ }
222
+ /**
223
+ * Close the socket.
224
+ */
225
+ close(callback) {
226
+ if (this._closed) {
227
+ throw new Error("Not running");
228
+ }
229
+ this._closed = true;
230
+ _activeSockets.delete(this);
231
+ if (callback) this.once("close", callback);
232
+ if (this._readSource) {
233
+ try {
234
+ this._readSource.destroy();
235
+ } catch (_e) {}
236
+ this._readSource = null;
237
+ }
238
+ this._cancellable.cancel();
239
+ if (this._socket) {
240
+ try {
241
+ this._socket.close();
242
+ } catch (_e) {}
243
+ this._socket = null;
244
+ }
245
+ deferEmit(this, "close");
246
+ return this;
247
+ }
248
+ /**
249
+ * Associate the socket with a remote address/port (connected UDP).
250
+ * After connect(), send() can omit address and port.
251
+ */
252
+ connect(port, address, callback) {
253
+ if (this._connected) {
254
+ const err = new Error("Already connected");
255
+ err.code = "ERR_SOCKET_DGRAM_IS_CONNECTED";
256
+ throw err;
257
+ }
258
+ if (!port || port <= 0 || port >= 65536) {
259
+ const err = new RangeError(`Port should be > 0 and < 65536. Received ${port}.`);
260
+ err.code = "ERR_SOCKET_BAD_PORT";
261
+ throw err;
262
+ }
263
+ let resolvedAddr;
264
+ let cb;
265
+ if (typeof address === "function") {
266
+ cb = address;
267
+ resolvedAddr = this.type === "udp6" ? "::1" : "127.0.0.1";
268
+ } else {
269
+ resolvedAddr = address || (this.type === "udp6" ? "::1" : "127.0.0.1");
270
+ cb = callback;
271
+ }
272
+ this._connected = true;
273
+ this._remoteAddress = {
274
+ address: resolvedAddr,
275
+ family: this.type === "udp6" ? "IPv6" : "IPv4",
276
+ port
277
+ };
278
+ if (cb) {
279
+ Promise.resolve().then(() => {
280
+ this.emit("connect");
281
+ cb();
282
+ });
283
+ } else {
284
+ Promise.resolve().then(() => this.emit("connect"));
285
+ }
286
+ }
287
+ /**
288
+ * Dissociate a connected socket from its remote address.
289
+ */
290
+ disconnect() {
291
+ if (!this._connected) {
292
+ const err = new Error("Not connected");
293
+ err.code = "ERR_SOCKET_DGRAM_NOT_CONNECTED";
294
+ throw err;
295
+ }
296
+ this._connected = false;
297
+ this._remoteAddress = null;
298
+ }
299
+ /**
300
+ * Returns the remote address of a connected socket.
301
+ * Throws ERR_SOCKET_DGRAM_NOT_CONNECTED if not connected.
302
+ */
303
+ remoteAddress() {
304
+ if (!this._connected || !this._remoteAddress) {
305
+ const err = new Error("Not connected");
306
+ err.code = "ERR_SOCKET_DGRAM_NOT_CONNECTED";
307
+ throw err;
308
+ }
309
+ return { ...this._remoteAddress };
310
+ }
311
+ /**
312
+ * Get the bound address info.
313
+ */
314
+ address() {
315
+ return { ...this._address };
316
+ }
317
+ /**
318
+ * Set the broadcast flag.
319
+ */
320
+ setBroadcast(flag) {
321
+ if (this._socket) {
322
+ this._socket.set_broadcast(flag);
323
+ }
324
+ }
325
+ /**
326
+ * Set the TTL.
327
+ */
328
+ setTTL(ttl) {
329
+ if (this._socket) {
330
+ this._socket.set_ttl(ttl);
331
+ }
332
+ return ttl;
333
+ }
334
+ /**
335
+ * Set multicast TTL.
336
+ */
337
+ setMulticastTTL(ttl) {
338
+ if (this._socket) {
339
+ this._socket.set_multicast_ttl(ttl);
340
+ }
341
+ return ttl;
342
+ }
343
+ /**
344
+ * Set multicast loopback.
345
+ */
346
+ setMulticastLoopback(flag) {
347
+ if (this._socket) {
348
+ this._socket.set_multicast_loopback(flag);
349
+ }
350
+ return flag;
351
+ }
352
+ /**
353
+ * Add multicast group membership.
354
+ */
355
+ addMembership(multicastAddress, multicastInterface) {
356
+ if (!this._socket) return;
357
+ try {
358
+ const mcastAddr = Gio.InetAddress.new_from_string(multicastAddress);
359
+ this._socket.join_multicast_group(mcastAddr, false, multicastInterface || null);
360
+ } catch (err) {
361
+ this.emit("error", err);
362
+ }
363
+ }
364
+ /**
365
+ * Drop multicast group membership.
366
+ */
367
+ dropMembership(multicastAddress, multicastInterface) {
368
+ if (!this._socket) return;
369
+ try {
370
+ const mcastAddr = Gio.InetAddress.new_from_string(multicastAddress);
371
+ this._socket.leave_multicast_group(mcastAddr, false, multicastInterface || null);
372
+ } catch (err) {
373
+ this.emit("error", err);
374
+ }
375
+ }
376
+ /**
377
+ * Set multicast interface.
378
+ */
379
+ setMulticastInterface(_interfaceAddress) {}
380
+ /** Ref the socket (keep event loop alive). */
381
+ ref() {
382
+ return this;
383
+ }
384
+ /** Unref the socket (allow event loop to exit). */
385
+ unref() {
386
+ return this;
387
+ }
388
+ /** Get/Set receive buffer size. */
389
+ getRecvBufferSize() {
390
+ return 65536;
391
+ }
392
+ setRecvBufferSize(_size) {}
393
+ /** Get/Set send buffer size. */
394
+ getSendBufferSize() {
395
+ return 65536;
396
+ }
397
+ setSendBufferSize(_size) {}
398
+ /**
399
+ * Start receiving messages in background.
400
+ */
401
+ _startReceiving() {
402
+ if (this._receiving || this._closed || !this._socket) return;
403
+ this._receiving = true;
404
+ this._receiveLoop();
405
+ }
406
+ _receiveLoop() {
407
+ if (this._closed || !this._socket || this._readSource) return;
408
+ const source = this._socket.create_source(GLib.IOCondition.IN, this._cancellable);
409
+ this._readSource = source;
410
+ source.set_callback(() => {
411
+ if (this._closed || !this._socket) return GLib.SOURCE_REMOVE;
412
+ try {
413
+ const [bytes, srcAddr] = this._socket.receive_bytes_from(65536, 0, this._cancellable);
414
+ if (bytes && srcAddr) {
415
+ const data = Buffer.from(bytes.get_data());
416
+ const inetSockAddr = srcAddr;
417
+ const rinfo = {
418
+ address: inetSockAddr.get_address().to_string(),
419
+ family: this.type === "udp6" ? "IPv6" : "IPv4",
420
+ port: inetSockAddr.get_port()
421
+ };
422
+ if (data.length > 0) this.emit("message", data, rinfo);
423
+ }
424
+ } catch (err) {
425
+ const errObj = err;
426
+ if (!this._closed && errObj.code !== Gio.IOErrorEnum.CANCELLED) {
427
+ this.emit("error", err);
428
+ }
429
+ }
430
+ return GLib.SOURCE_CONTINUE;
431
+ });
432
+ source.attach(null);
433
+ }
434
+ };
435
+ /**
436
+ * Create a UDP socket.
437
+ */
429
438
  function createSocket(type, callback) {
430
- const opts = typeof type === "string" ? { type } : type;
431
- const socket = new Socket(opts);
432
- if (callback) {
433
- socket.on("message", callback);
434
- }
435
- return socket;
439
+ const opts = typeof type === "string" ? { type } : type;
440
+ const socket = new Socket(opts);
441
+ if (callback) {
442
+ socket.on("message", callback);
443
+ }
444
+ return socket;
436
445
  }
437
- var index_default = { Socket, createSocket };
438
- export {
439
- Socket,
440
- createSocket,
441
- index_default as default
446
+ var src_default = {
447
+ Socket,
448
+ createSocket
442
449
  };
450
+
451
+ //#endregion
452
+ export { Socket, createSocket, src_default as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/dgram",
3
- "version": "0.3.12",
3
+ "version": "0.3.14",
4
4
  "description": "Node.js dgram module for Gjs",
5
5
  "type": "module",
6
6
  "module": "lib/esm/index.js",
@@ -30,15 +30,15 @@
30
30
  "dgram"
31
31
  ],
32
32
  "dependencies": {
33
- "@girs/gio-2.0": "^2.88.0-4.0.0-rc.9",
34
- "@girs/glib-2.0": "^2.88.0-4.0.0-rc.9",
35
- "@gjsify/buffer": "^0.3.12",
36
- "@gjsify/events": "^0.3.12",
37
- "@gjsify/utils": "^0.3.12"
33
+ "@girs/gio-2.0": "2.88.0-4.0.0-rc.9",
34
+ "@girs/glib-2.0": "2.88.0-4.0.0-rc.9",
35
+ "@gjsify/buffer": "^0.3.14",
36
+ "@gjsify/events": "^0.3.14",
37
+ "@gjsify/utils": "^0.3.14"
38
38
  },
39
39
  "devDependencies": {
40
- "@gjsify/cli": "^0.3.12",
41
- "@gjsify/unit": "^0.3.12",
40
+ "@gjsify/cli": "^0.3.14",
41
+ "@gjsify/unit": "^0.3.14",
42
42
  "@types/node": "^25.6.0",
43
43
  "typescript": "^6.0.3"
44
44
  }