deno.land / std@0.157.0 / node / dgram.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Copyright Joyent, Inc. and other Node contributors.//// Permission is hereby granted, free of charge, to any person obtaining a// copy of this software and associated documentation files (the// "Software"), to deal in the Software without restriction, including// without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit// persons to whom the Software is furnished to do so, subject to the// following conditions://// The above copyright notice and this permission notice shall be included// in all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.
import { Buffer } from "./buffer.ts";import { EventEmitter } from "./events.ts";import { lookup as defaultLookup } from "./dns.ts";import type { ErrnoException, NodeSystemErrorCtx } from "./internal/errors.ts";import { ERR_BUFFER_OUT_OF_BOUNDS, ERR_INVALID_ARG_TYPE, ERR_INVALID_FD_TYPE, ERR_MISSING_ARGS, ERR_SOCKET_ALREADY_BOUND, ERR_SOCKET_BAD_BUFFER_SIZE, ERR_SOCKET_BUFFER_SIZE, ERR_SOCKET_DGRAM_IS_CONNECTED, ERR_SOCKET_DGRAM_NOT_CONNECTED, ERR_SOCKET_DGRAM_NOT_RUNNING, errnoException, exceptionWithHostPort,} from "./internal/errors.ts";import type { Abortable } from "./_events.d.ts";import { kStateSymbol, newHandle } from "./internal/dgram.ts";import type { SocketType } from "./internal/dgram.ts";import { asyncIdSymbol, defaultTriggerAsyncIdScope, ownerSymbol,} from "./internal/async_hooks.ts";import { SendWrap, UDP } from "./internal_binding/udp_wrap.ts";import { isInt32, validateAbortSignal, validateNumber, validatePort, validateString,} from "./internal/validators.mjs";import { guessHandleType } from "./internal_binding/util.ts";import { os } from "./internal_binding/constants.ts";import { nextTick } from "./process.ts";import { isArrayBufferView } from "./internal/util/types.ts";
const { UV_UDP_REUSEADDR, UV_UDP_IPV6ONLY } = os;
const BIND_STATE_UNBOUND = 0;const BIND_STATE_BINDING = 1;const BIND_STATE_BOUND = 2;
const CONNECT_STATE_DISCONNECTED = 0;const CONNECT_STATE_CONNECTING = 1;const CONNECT_STATE_CONNECTED = 2;
const RECV_BUFFER = true;const SEND_BUFFER = false;
export interface AddressInfo { address: string; family: number; port: number;}
export type MessageType = string | Uint8Array | Buffer | DataView;
export type RemoteInfo = { address: string; family: "IPv4" | "IPv6"; port: number; size?: number;};
export interface BindOptions { port?: number; address?: string; exclusive?: boolean; fd?: number;}
export interface SocketOptions extends Abortable { type: SocketType; reuseAddr?: boolean; /** * @default false */ ipv6Only?: boolean; recvBufferSize?: number; sendBufferSize?: number; lookup?: typeof defaultLookup;}
interface SocketInternalState { handle: UDP | null; receiving: boolean; bindState: | typeof BIND_STATE_UNBOUND | typeof BIND_STATE_BINDING | typeof BIND_STATE_BOUND; connectState: | typeof CONNECT_STATE_DISCONNECTED | typeof CONNECT_STATE_CONNECTING | typeof CONNECT_STATE_CONNECTED; queue?: Array<() => void>; reuseAddr?: boolean; ipv6Only?: boolean; recvBufferSize?: number; sendBufferSize?: number;}
const isSocketOptions = ( socketOption: unknown,): socketOption is SocketOptions => socketOption !== null && typeof socketOption === "object";
const isUdpHandle = (handle: unknown): handle is UDP => handle !== null && typeof handle === "object" && typeof (handle as UDP).recvStart === "function";
const isBindOptions = (options: unknown): options is BindOptions => options !== null && typeof options === "object";
/** * Encapsulates the datagram functionality. * * New instances of `dgram.Socket` are created using `createSocket`. * The `new` keyword is not to be used to create `dgram.Socket` instances. */export class Socket extends EventEmitter { [asyncIdSymbol]!: number; [kStateSymbol]!: SocketInternalState;
type!: SocketType;
constructor( type: SocketType | SocketOptions, listener?: (msg: Buffer, rinfo: RemoteInfo) => void, ) { super();
let lookup; let recvBufferSize; let sendBufferSize;
let options: SocketOptions | undefined;
if (isSocketOptions(type)) { options = type; type = options.type; lookup = options.lookup; recvBufferSize = options.recvBufferSize; sendBufferSize = options.sendBufferSize; }
const handle = newHandle(type, lookup); handle[ownerSymbol] = this;
this[asyncIdSymbol] = handle.getAsyncId(); this.type = type;
if (typeof listener === "function") { this.on("message", listener); }
this[kStateSymbol] = { handle, receiving: false, bindState: BIND_STATE_UNBOUND, connectState: CONNECT_STATE_DISCONNECTED, queue: undefined, reuseAddr: options && options.reuseAddr, // Use UV_UDP_REUSEADDR if true. ipv6Only: options && options.ipv6Only, recvBufferSize, sendBufferSize, };
if (options?.signal !== undefined) { const { signal } = options;
validateAbortSignal(signal, "options.signal");
const onAborted = () => { this.close(); };
if (signal.aborted) { onAborted(); } else { signal.addEventListener("abort", onAborted);
this.once( "close", () => signal.removeEventListener("abort", onAborted), ); } } }
/** * Tells the kernel to join a multicast group at the given `multicastAddress` * and `multicastInterface` using the `IP_ADD_MEMBERSHIP` socket option. If * the`multicastInterface` argument is not specified, the operating system * will choose one interface and will add membership to it. To add membership * to every available interface, call `addMembership` multiple times, once * per interface. * * When called on an unbound socket, this method will implicitly bind to a * random port, listening on all interfaces. * * When sharing a UDP socket across multiple `cluster` workers, the * `socket.addMembership()` function must be called only once or an * `EADDRINUSE` error will occur: * * ```js * import cluster from 'cluster'; * import dgram from 'dgram'; * * if (cluster.isPrimary) { * cluster.fork(); // Works ok. * cluster.fork(); // Fails with EADDRINUSE. * } else { * const s = dgram.createSocket('udp4'); * s.bind(1234, () => { * s.addMembership('224.0.0.114'); * }); * } * ``` */ addMembership(multicastAddress: string, interfaceAddress?: string) { healthCheck(this);
if (!multicastAddress) { throw new ERR_MISSING_ARGS("multicastAddress"); }
const { handle } = this[kStateSymbol]; const err = handle!.addMembership(multicastAddress, interfaceAddress);
if (err) { throw errnoException(err, "addMembership"); } }
/** * Tells the kernel to join a source-specific multicast channel at the given * `sourceAddress` and `groupAddress`, using the `multicastInterface` with * the `IP_ADD_SOURCE_MEMBERSHIP` socket option. If the `multicastInterface` * argument is not specified, the operating system will choose one interface * and will add membership to it. To add membership to every available * interface, call `socket.addSourceSpecificMembership()` multiple times, * once per interface. * * When called on an unbound socket, this method will implicitly bind to a * random port, listening on all interfaces. */ addSourceSpecificMembership( sourceAddress: string, groupAddress: string, interfaceAddress?: string, ) { healthCheck(this);
validateString(sourceAddress, "sourceAddress"); validateString(groupAddress, "groupAddress");
const err = this[kStateSymbol].handle!.addSourceSpecificMembership( sourceAddress, groupAddress, interfaceAddress, );
if (err) { throw errnoException(err, "addSourceSpecificMembership"); } }
/** * Returns an object containing the address information for a socket. * For UDP sockets, this object will contain `address`, `family` and `port`properties. * * This method throws `EBADF` if called on an unbound socket. */ address(): AddressInfo { healthCheck(this);
const out = {}; const err = this[kStateSymbol].handle!.getsockname(out);
if (err) { throw errnoException(err, "getsockname"); }
return out as AddressInfo; }
/** * For UDP sockets, causes the `dgram.Socket` to listen for datagram * messages on a named `port` and optional `address`. If `port` is not * specified or is `0`, the operating system will attempt to bind to a * random port. If `address` is not specified, the operating system will * attempt to listen on all addresses. Once binding is complete, a * `'listening'` event is emitted and the optional `callback` function is * called. * * Specifying both a `'listening'` event listener and passing a `callback` to * the `socket.bind()` method is not harmful but not very useful. * * A bound datagram socket keeps the Node.js process running to receive * datagram messages. * * If binding fails, an `'error'` event is generated. In rare case (e.g. * attempting to bind with a closed socket), an `Error` may be thrown. * * Example of a UDP server listening on port 41234: * * ```js * import dgram from 'dgram'; * * const server = dgram.createSocket('udp4'); * * server.on('error', (err) => { * console.log(`server error:\n${err.stack}`); * server.close(); * }); * * server.on('message', (msg, rinfo) => { * console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`); * }); * * server.on('listening', () => { * const address = server.address(); * console.log(`server listening ${address.address}:${address.port}`); * }); * * server.bind(41234); * // Prints: server listening 0.0.0.0:41234 * ``` * * @param callback with no parameters. Called when binding is complete. */ bind(port?: number, address?: string, callback?: () => void): this; bind(port: number, callback?: () => void): this; bind(callback: () => void): this; bind(options: BindOptions, callback?: () => void): this; bind(port_?: unknown, address_?: unknown /* callback */): this { let port = typeof port_ === "function" ? null : port_;
healthCheck(this);
const state = this[kStateSymbol];
if (state.bindState !== BIND_STATE_UNBOUND) { throw new ERR_SOCKET_ALREADY_BOUND(); }
state.bindState = BIND_STATE_BINDING;
const cb = arguments.length && arguments[arguments.length - 1];
if (typeof cb === "function") { // deno-lint-ignore no-inner-declarations function removeListeners(this: Socket) { this.removeListener("error", removeListeners); this.removeListener("listening", onListening); }
// deno-lint-ignore no-inner-declarations function onListening(this: Socket) { removeListeners.call(this); cb.call(this); }
this.on("error", removeListeners); this.on("listening", onListening); }
if (isUdpHandle(port)) { replaceHandle(this, port); startListening(this);
return this; }
// Open an existing fd instead of creating a new one. if (isBindOptions(port) && isInt32(port.fd!) && port.fd! > 0) { const fd = port.fd!; const state = this[kStateSymbol];
// TODO(cmorten): here we deviate somewhat from the Node implementation which // makes use of the https://nodejs.org/api/cluster.html module to run servers // across a "cluster" of Node processes to take advantage of multi-core // systems. // // Though Deno has has a Worker capability from which we could simulate this, // for now we assert that we are _always_ on the primary process.
const type = guessHandleType(fd);
if (type !== "UDP") { throw new ERR_INVALID_FD_TYPE(type); }
const err = state.handle!.open(fd);
if (err) { throw errnoException(err, "open"); }
startListening(this);
return this; }
let address: string;
if (isBindOptions(port)) { address = port.address || ""; port = port.port; } else { address = typeof address_ === "function" ? "" : (address_ as string); }
// Defaulting address for bind to all interfaces if (!address) { if (this.type === "udp4") { address = "0.0.0.0"; } else { address = "::"; } }
// Resolve address first state.handle!.lookup(address, (lookupError, ip) => { if (lookupError) { state.bindState = BIND_STATE_UNBOUND; this.emit("error", lookupError);
return; }
let flags = 0;
if (state.reuseAddr) { flags |= UV_UDP_REUSEADDR; } if (state.ipv6Only) { flags |= UV_UDP_IPV6ONLY; }
// TODO(cmorten): here we deviate somewhat from the Node implementation which // makes use of the https://nodejs.org/api/cluster.html module to run servers // across a "cluster" of Node processes to take advantage of multi-core // systems. // // Though Deno has has a Worker capability from which we could simulate this, // for now we assert that we are _always_ on the primary process.
if (!state.handle) { return; // Handle has been closed in the mean time }
const err = state.handle.bind(ip, port as number || 0, flags);
if (err) { const ex = exceptionWithHostPort(err, "bind", ip, port as number); state.bindState = BIND_STATE_UNBOUND; this.emit("error", ex);
// Todo: close? return; }
startListening(this); });
return this; }
/** * Close the underlying socket and stop listening for data on it. If a * callback is provided, it is added as a listener for the `'close'` event. * * @param callback Called when the socket has been closed. */ close(callback?: () => void): this { const state = this[kStateSymbol]; const queue = state.queue;
if (typeof callback === "function") { this.on("close", callback); }
if (queue !== undefined) { queue.push(this.close.bind(this));
return this; }
healthCheck(this); stopReceiving(this);
state.handle!.close(); state.handle = null;
defaultTriggerAsyncIdScope( this[asyncIdSymbol], nextTick, socketCloseNT, this, );
return this; }
/** * Associates the `dgram.Socket` to a remote address and port. Every * message sent by this handle is automatically sent to that destination. * Also, the socket will only receive messages from that remote peer. * Trying to call `connect()` on an already connected socket will result * in an `ERR_SOCKET_DGRAM_IS_CONNECTED` exception. If `address` is not * provided, `'127.0.0.1'` (for `udp4` sockets) or `'::1'` (for `udp6` sockets) * will be used by default. Once the connection is complete, a `'connect'` event * is emitted and the optional `callback` function is called. In case of failure, * the `callback` is called or, failing this, an `'error'` event is emitted. * * @param callback Called when the connection is completed or on error. */ connect( port: number, address?: string, callback?: (err?: ErrnoException) => void, ): void; connect(port: number, callback: (err?: ErrnoException) => void): void; connect(port: number, address?: unknown, callback?: unknown) { port = validatePort(port, "Port", false);
if (typeof address === "function") { callback = address; address = ""; } else if (address === undefined) { address = ""; }
validateString(address, "address");
const state = this[kStateSymbol];
if (state.connectState !== CONNECT_STATE_DISCONNECTED) { throw new ERR_SOCKET_DGRAM_IS_CONNECTED(); }
state.connectState = CONNECT_STATE_CONNECTING;
if (state.bindState === BIND_STATE_UNBOUND) { this.bind({ port: 0, exclusive: true }); }
if (state.bindState !== BIND_STATE_BOUND) { enqueue( this, _connect.bind( this, port, address as string, callback as (err?: ErrnoException) => void, ), );
return; }
Reflect.apply(_connect, this, [port, address, callback]); }
/** * A synchronous function that disassociates a connected `dgram.Socket` from * its remote address. Trying to call `disconnect()` on an unbound or already * disconnected socket will result in an `ERR_SOCKET_DGRAM_NOT_CONNECTED` * exception. */ disconnect() { const state = this[kStateSymbol];
if (state.connectState !== CONNECT_STATE_CONNECTED) { throw new ERR_SOCKET_DGRAM_NOT_CONNECTED(); }
const err = state.handle!.disconnect();
if (err) { throw errnoException(err, "connect"); } else { state.connectState = CONNECT_STATE_DISCONNECTED; } }
/** * Instructs the kernel to leave a multicast group at `multicastAddress` * using the `IP_DROP_MEMBERSHIP` socket option. This method is automatically * called by the kernel when the socket is closed or the process terminates, * so most apps will never have reason to call this. * * If `multicastInterface` is not specified, the operating system will * attempt to drop membership on all valid interfaces. */ dropMembership(multicastAddress: string, interfaceAddress?: string) { healthCheck(this);
if (!multicastAddress) { throw new ERR_MISSING_ARGS("multicastAddress"); }
const err = this[kStateSymbol].handle!.dropMembership( multicastAddress, interfaceAddress, );
if (err) { throw errnoException(err, "dropMembership"); } }
/** * Instructs the kernel to leave a source-specific multicast channel at the * given `sourceAddress` and `groupAddress` using the * `IP_DROP_SOURCE_MEMBERSHIP` socket option. This method is automatically * called by the kernel when the socket is closed or the process terminates, * so most apps will never have reason to call this. * * If `multicastInterface` is not specified, the operating system will * attempt to drop membership on all valid interfaces. */ dropSourceSpecificMembership( sourceAddress: string, groupAddress: string, interfaceAddress?: string, ) { healthCheck(this);
validateString(sourceAddress, "sourceAddress"); validateString(groupAddress, "groupAddress");
const err = this[kStateSymbol].handle!.dropSourceSpecificMembership( sourceAddress, groupAddress, interfaceAddress, );
if (err) { throw errnoException(err, "dropSourceSpecificMembership"); } }
/** * This method throws `ERR_SOCKET_BUFFER_SIZE` if called on an unbound * socket. * * @return the `SO_RCVBUF` socket receive buffer size in bytes. */ getRecvBufferSize(): number { return bufferSize(this, 0, RECV_BUFFER); }
/** * This method throws `ERR_SOCKET_BUFFER_SIZE` if called on an unbound * socket. * * @return the `SO_SNDBUF` socket send buffer size in bytes. */ getSendBufferSize(): number { return bufferSize(this, 0, SEND_BUFFER); }
/** * By default, binding a socket will cause it to block the Node.js process * from exiting as long as the socket is open. The `socket.unref()` method * can be used to exclude the socket from the reference counting that keeps * the Node.js process active. The `socket.ref()` method adds the socket back * to the reference counting and restores the default behavior. * * Calling `socket.ref()` multiples times will have no additional effect. * * The `socket.ref()` method returns a reference to the socket so calls can * be chained. */ ref(): this { const handle = this[kStateSymbol].handle;
if (handle) { handle.ref(); }
return this; }
/** * Returns an object containing the `address`, `family`, and `port` of the * remote endpoint. This method throws an `ERR_SOCKET_DGRAM_NOT_CONNECTED` * exception if the socket is not connected. */ remoteAddress(): AddressInfo { healthCheck(this);
const state = this[kStateSymbol];
if (state.connectState !== CONNECT_STATE_CONNECTED) { throw new ERR_SOCKET_DGRAM_NOT_CONNECTED(); }
const out = {}; const err = state.handle!.getpeername(out);
if (err) { throw errnoException(err, "getpeername"); }
return out as AddressInfo; }
/** * Broadcasts a datagram on the socket. * For connectionless sockets, the destination `port` and `address` must be * specified. Connected sockets, on the other hand, will use their associated * remote endpoint, so the `port` and `address` arguments must not be set. * * The `msg` argument contains the message to be sent. * Depending on its type, different behavior can apply. If `msg` is a * `Buffer`, any `TypedArray` or a `DataView`, * the `offset` and `length` specify the offset within the `Buffer` where the * message begins and the number of bytes in the message, respectively. * If `msg` is a `String`, then it is automatically converted to a `Buffer` * with `'utf8'` encoding. With messages that contain multi-byte characters, * `offset` and `length` will be calculated with respect to `byte length` and * not the character position. If `msg` is an array, `offset` and `length` * must not be specified. * * The `address` argument is a string. If the value of `address` is a host * name, DNS will be used to resolve the address of the host. If `address` * is not provided or otherwise nullish, `'127.0.0.1'` (for `udp4` sockets) * or `'::1'`(for `udp6` sockets) will be used by default. * * If the socket has not been previously bound with a call to `bind`, the * socket is assigned a random port number and is bound to the "all * interfaces" address (`'0.0.0.0'` for `udp4` sockets, `'::0'` for `udp6` * sockets.) * * An optional `callback` function may be specified to as a way of * reporting DNS errors or for determining when it is safe to reuse the `buf` * object. DNS lookups delay the time to send for at least one tick of the * Node.js event loop. * * The only way to know for sure that the datagram has been sent is by using * a `callback`. If an error occurs and a `callback` is given, the error will * be passed as the first argument to the `callback`. If a `callback` is not * given, the error is emitted as an `'error'` event on the `socket` object. * * Offset and length are optional but both _must_ be set if either are used. * They are supported only when the first argument is a `Buffer`, a * `TypedArray`, or a `DataView`. * * This method throws `ERR_SOCKET_BAD_PORT` if called on an unbound socket. * * Example of sending a UDP packet to a port on `localhost`; * * ```js * import dgram from 'dgram'; * import { Buffer } from 'buffer'; * * const message = Buffer.from('Some bytes'); * const client = dgram.createSocket('udp4'); * client.send(message, 41234, 'localhost', (err) => { * client.close(); * }); * ``` * * Example of sending a UDP packet composed of multiple buffers to a port on * `127.0.0.1`; * * ```js * import dgram from 'dgram'; * import { Buffer } from 'buffer'; * * const buf1 = Buffer.from('Some '); * const buf2 = Buffer.from('bytes'); * const client = dgram.createSocket('udp4'); * client.send([buf1, buf2], 41234, (err) => { * client.close(); * }); * ``` * * Sending multiple buffers might be faster or slower depending on the * application and operating system. Run benchmarks to * determine the optimal strategy on a case-by-case basis. Generally * speaking, however, sending multiple buffers is faster. * * Example of sending a UDP packet using a socket connected to a port on * `localhost`: * * ```js * import dgram from 'dgram'; * import { Buffer } from 'buffer'; * * const message = Buffer.from('Some bytes'); * const client = dgram.createSocket('udp4'); * client.connect(41234, 'localhost', (err) => { * client.send(message, (err) => { * client.close(); * }); * }); * ``` * * @param msg Message to be sent. * @param offset Offset in the buffer where the message starts. * @param length Number of bytes in the message. * @param port Destination port. * @param address Destination host name or IP address. * @param callback Called when the message has been sent. */ send( msg: MessageType | ReadonlyArray<MessageType>, port?: number, address?: string, callback?: (error: ErrnoException | null, bytes?: number) => void, ): void; send( msg: MessageType | ReadonlyArray<MessageType>, port?: number, callback?: (error: ErrnoException | null, bytes?: number) => void, ): void; send( msg: MessageType | ReadonlyArray<MessageType>, callback?: (error: ErrnoException | null, bytes?: number) => void, ): void; send( msg: MessageType, offset: number, length: number, port?: number, address?: string, callback?: (error: ErrnoException | null, bytes?: number) => void, ): void; send( msg: MessageType, offset: number, length: number, port?: number, callback?: (error: ErrnoException | null, bytes?: number) => void, ): void; send( msg: MessageType, offset: number, length: number, callback?: (error: ErrnoException | null, bytes?: number) => void, ): void; send( buffer: unknown, offset?: unknown, length?: unknown, port?: unknown, address?: unknown, callback?: unknown, ) { let list: MessageType[] | null;
const state = this[kStateSymbol]; const connected = state.connectState === CONNECT_STATE_CONNECTED;
if (!connected) { if (address || (port && typeof port !== "function")) { buffer = sliceBuffer( buffer as MessageType, offset as number, length as number, ); } else { callback = port; port = offset; address = length; } } else { if (typeof length === "number") { buffer = sliceBuffer(buffer as MessageType, offset as number, length);
if (typeof port === "function") { callback = port; port = null; } } else { callback = offset; }
if (port || address) { throw new ERR_SOCKET_DGRAM_IS_CONNECTED(); } }
if (!Array.isArray(buffer)) { if (typeof buffer === "string") { list = [Buffer.from(buffer)]; } else if (!isArrayBufferView(buffer)) { throw new ERR_INVALID_ARG_TYPE( "buffer", ["Buffer", "TypedArray", "DataView", "string"], buffer, ); } else { list = [buffer as MessageType]; } } else if (!(list = fixBufferList(buffer))) { throw new ERR_INVALID_ARG_TYPE( "buffer list arguments", ["Buffer", "TypedArray", "DataView", "string"], buffer, ); }
if (!connected) { port = validatePort(port, "Port", false); }
// Normalize callback so it's either a function or undefined but not anything // else. if (typeof callback !== "function") { callback = undefined; }
if (typeof address === "function") { callback = address; address = undefined; } else if (address && typeof address !== "string") { throw new ERR_INVALID_ARG_TYPE("address", ["string", "falsy"], address); }
healthCheck(this);
if (state.bindState === BIND_STATE_UNBOUND) { this.bind({ port: 0, exclusive: true }); }
if (list.length === 0) { list.push(Buffer.alloc(0)); }
// If the socket hasn't been bound yet, push the outbound packet onto the // send queue and send after binding is complete. if (state.bindState !== BIND_STATE_BOUND) { // @ts-ignore mapping unknowns back onto themselves doesn't type nicely enqueue(this, this.send.bind(this, list, port, address, callback));
return; }
const afterDns = (ex: ErrnoException | null, ip: string) => { defaultTriggerAsyncIdScope( this[asyncIdSymbol], doSend, ex, this, ip, list, address, port, callback, ); };
if (!connected) { state.handle!.lookup(address as string, afterDns); } else { afterDns(null, ""); } }
/** * Sets or clears the `SO_BROADCAST` socket option. When set to `true`, UDP * packets may be sent to a local interface's broadcast address. * * This method throws `EBADF` if called on an unbound socket. */ setBroadcast(arg: boolean) { const err = this[kStateSymbol].handle!.setBroadcast(arg ? 1 : 0);
if (err) { throw errnoException(err, "setBroadcast"); } }
/** * _All references to scope in this section are referring to [IPv6 Zone Indices](https://en.wikipedia.org/wiki/IPv6_address#Scoped_literal_IPv6_addresses), which are defined by [RFC * 4007](https://tools.ietf.org/html/rfc4007). In string form, an IP_ * _with a scope index is written as `'IP%scope'` where scope is an interface name_ * _or interface number._ * * Sets the default outgoing multicast interface of the socket to a chosen * interface or back to system interface selection. The `multicastInterface` must * be a valid string representation of an IP from the socket's family. * * For IPv4 sockets, this should be the IP configured for the desired physical * interface. All packets sent to multicast on the socket will be sent on the * interface determined by the most recent successful use of this call. * * For IPv6 sockets, `multicastInterface` should include a scope to indicate the * interface as in the examples that follow. In IPv6, individual `send` calls can * also use explicit scope in addresses, so only packets sent to a multicast * address without specifying an explicit scope are affected by the most recent * successful use of this call. * * This method throws `EBADF` if called on an unbound socket. * * #### Example: IPv6 outgoing multicast interface * * On most systems, where scope format uses the interface name: * * ```js * const socket = dgram.createSocket('udp6'); * * socket.bind(1234, () => { * socket.setMulticastInterface('::%eth1'); * }); * ``` * * On Windows, where scope format uses an interface number: * * ```js * const socket = dgram.createSocket('udp6'); * * socket.bind(1234, () => { * socket.setMulticastInterface('::%2'); * }); * ``` * * #### Example: IPv4 outgoing multicast interface * * All systems use an IP of the host on the desired physical interface: * * ```js * const socket = dgram.createSocket('udp4'); * * socket.bind(1234, () => { * socket.setMulticastInterface('10.0.0.2'); * }); * ``` */ setMulticastInterface(interfaceAddress: string) { healthCheck(this); validateString(interfaceAddress, "interfaceAddress");
const err = this[kStateSymbol].handle!.setMulticastInterface( interfaceAddress, );
if (err) { throw errnoException(err, "setMulticastInterface"); } }
/** * Sets or clears the `IP_MULTICAST_LOOP` socket option. When set to `true`, * multicast packets will also be received on the local interface. * * This method throws `EBADF` if called on an unbound socket. */ setMulticastLoopback(arg: boolean): typeof arg { const err = this[kStateSymbol].handle!.setMulticastLoopback(arg ? 1 : 0);
if (err) { throw errnoException(err, "setMulticastLoopback"); }
return arg; // 0.4 compatibility }
/** * Sets the `IP_MULTICAST_TTL` socket option. While TTL generally stands for * "Time to Live", in this context it specifies the number of IP hops that a * packet is allowed to travel through, specifically for multicast traffic. Each * router or gateway that forwards a packet decrements the TTL. If the TTL is * decremented to 0 by a router, it will not be forwarded. * * The `ttl` argument may be between 0 and 255\. The default on most systems is `1`. * * This method throws `EBADF` if called on an unbound socket. */ setMulticastTTL(ttl: number): typeof ttl { validateNumber(ttl, "ttl");
const err = this[kStateSymbol].handle!.setMulticastTTL(ttl);
if (err) { throw errnoException(err, "setMulticastTTL"); }
return ttl; }
/** * Sets the `SO_RCVBUF` socket option. Sets the maximum socket receive buffer * in bytes. * * This method throws `ERR_SOCKET_BUFFER_SIZE` if called on an unbound socket. */ setRecvBufferSize(size: number) { bufferSize(this, size, RECV_BUFFER); }
/** * Sets the `SO_SNDBUF` socket option. Sets the maximum socket send buffer * in bytes. * * This method throws `ERR_SOCKET_BUFFER_SIZE` if called on an unbound socket. */ setSendBufferSize(size: number) { bufferSize(this, size, SEND_BUFFER); }
/** * Sets the `IP_TTL` socket option. While TTL generally stands for "Time to Live", * in this context it specifies the number of IP hops that a packet is allowed to * travel through. Each router or gateway that forwards a packet decrements the * TTL. If the TTL is decremented to 0 by a router, it will not be forwarded. * Changing TTL values is typically done for network probes or when multicasting. * * The `ttl` argument may be between between 1 and 255\. The default on most systems * is 64. * * This method throws `EBADF` if called on an unbound socket. */ setTTL(ttl: number): typeof ttl { validateNumber(ttl, "ttl");
const err = this[kStateSymbol].handle!.setTTL(ttl);
if (err) { throw errnoException(err, "setTTL"); }
return ttl; }
/** * By default, binding a socket will cause it to block the Node.js process from * exiting as long as the socket is open. The `socket.unref()` method can be used * to exclude the socket from the reference counting that keeps the Node.js * process active, allowing the process to exit even if the socket is still * listening. * * Calling `socket.unref()` multiple times will have no addition effect. * * The `socket.unref()` method returns a reference to the socket so calls can be * chained. */ unref(): this { const handle = this[kStateSymbol].handle;
if (handle) { handle.unref(); }
return this; }}
/** * Creates a `dgram.Socket` object. Once the socket is created, calling * `socket.bind()` will instruct the socket to begin listening for datagram * messages. When `address` and `port` are not passed to `socket.bind()` the * method will bind the socket to the "all interfaces" address on a random port * (it does the right thing for both `udp4` and `udp6` sockets). The bound * address and port can be retrieved using `socket.address().address` and * `socket.address().port`. * * If the `signal` option is enabled, calling `.abort()` on the corresponding * `AbortController` is similar to calling `.close()` on the socket: * * ```js * const controller = new AbortController(); * const { signal } = controller; * const server = dgram.createSocket({ type: 'udp4', signal }); * server.on('message', (msg, rinfo) => { * console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`); * }); * // Later, when you want to close the server. * controller.abort(); * ``` * * @param options * @param callback Attached as a listener for `'message'` events. Optional. */export function createSocket( type: SocketType, listener?: (msg: Buffer, rinfo: RemoteInfo) => void,): Socket;export function createSocket( type: SocketOptions, listener?: (msg: Buffer, rinfo: RemoteInfo) => void,): Socket;export function createSocket( type: SocketType | SocketOptions, listener?: (msg: Buffer, rinfo: RemoteInfo) => void,): Socket { return new Socket(type, listener);}
function startListening(socket: Socket) { const state = socket[kStateSymbol];
state.handle!.onmessage = onMessage; // Todo: handle errors state.handle!.recvStart(); state.receiving = true; state.bindState = BIND_STATE_BOUND;
if (state.recvBufferSize) { bufferSize(socket, state.recvBufferSize, RECV_BUFFER); }
if (state.sendBufferSize) { bufferSize(socket, state.sendBufferSize, SEND_BUFFER); }
socket.emit("listening");}
function replaceHandle(self: Socket, newHandle: UDP) { const state = self[kStateSymbol]; const oldHandle = state.handle!;
// Set up the handle that we got from primary. newHandle.lookup = oldHandle.lookup; newHandle.bind = oldHandle.bind; newHandle.send = oldHandle.send; newHandle[ownerSymbol] = self;
// Replace the existing handle by the handle we got from primary. oldHandle.close(); state.handle = newHandle;}
function bufferSize(self: Socket, size: number, buffer: boolean): number { if (size >>> 0 !== size) { throw new ERR_SOCKET_BAD_BUFFER_SIZE(); }
const ctx = {}; const ret = self[kStateSymbol].handle!.bufferSize(size, buffer, ctx);
if (ret === undefined) { throw new ERR_SOCKET_BUFFER_SIZE(ctx as NodeSystemErrorCtx); }
return ret;}
function socketCloseNT(self: Socket) { self.emit("close");}
function healthCheck(socket: Socket) { if (!socket[kStateSymbol].handle) { // Error message from dgram_legacy.js. throw new ERR_SOCKET_DGRAM_NOT_RUNNING(); }}
function stopReceiving(socket: Socket) { const state = socket[kStateSymbol];
if (!state.receiving) { return; }
state.handle!.recvStop(); state.receiving = false;}
function onMessage( nread: number, handle: UDP, buf?: Buffer, rinfo?: RemoteInfo,) { const self = handle[ownerSymbol] as Socket;
if (nread < 0) { self.emit("error", errnoException(nread, "recvmsg"));
return; }
rinfo!.size = buf!.length; // compatibility
self.emit("message", buf, rinfo);}
function sliceBuffer(buffer: MessageType, offset: number, length: number) { if (typeof buffer === "string") { buffer = Buffer.from(buffer); } else if (!isArrayBufferView(buffer)) { throw new ERR_INVALID_ARG_TYPE( "buffer", ["Buffer", "TypedArray", "DataView", "string"], buffer, ); }
offset = offset >>> 0; length = length >>> 0;
if (offset > buffer.byteLength) { throw new ERR_BUFFER_OUT_OF_BOUNDS("offset"); }
if (offset + length > buffer.byteLength) { throw new ERR_BUFFER_OUT_OF_BOUNDS("length"); }
return Buffer.from(buffer.buffer, buffer.byteOffset + offset, length);}
function fixBufferList( list: ReadonlyArray<MessageType>,): Array<MessageType> | null { const newList = new Array(list.length);
for (let i = 0, l = list.length; i < l; i++) { const buf = list[i];
if (typeof buf === "string") { newList[i] = Buffer.from(buf); } else if (!isArrayBufferView(buf)) { return null; } else { newList[i] = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength); } }
return newList;}
function enqueue(self: Socket, toEnqueue: () => void) { const state = self[kStateSymbol];
// If the send queue hasn't been initialized yet, do it, and install an // event handler that flushes the send queue after binding is done. if (state.queue === undefined) { state.queue = [];
self.once(EventEmitter.errorMonitor, onListenError); self.once("listening", onListenSuccess); }
state.queue.push(toEnqueue);}
function onListenSuccess(this: Socket) { this.removeListener(EventEmitter.errorMonitor, onListenError); clearQueue.call(this);}
function onListenError(this: Socket) { this.removeListener("listening", onListenSuccess); this[kStateSymbol].queue = undefined;}
function clearQueue(this: Socket) { const state = this[kStateSymbol]; const queue = state.queue; state.queue = undefined;
// Flush the send queue. for (const queueEntry of queue!) { queueEntry(); }}
function _connect( this: Socket, port: number, address: string, callback: (err?: ErrnoException) => void,) { const state = this[kStateSymbol];
if (callback) { this.once("connect", callback); }
const afterDns = (ex: ErrnoException | null, ip: string) => { defaultTriggerAsyncIdScope( this[asyncIdSymbol], doConnect, ex, this, ip, address, port, callback, ); };
state.handle!.lookup(address, afterDns);}
function doConnect( ex: ErrnoException | null, self: Socket, ip: string, address: string, port: number, callback: (err?: ErrnoException) => void,) { const state = self[kStateSymbol];
if (!state.handle) { return; }
if (!ex) { const err = state.handle.connect(ip, port);
if (err) { ex = exceptionWithHostPort(err, "connect", address, port); } }
if (ex) { state.connectState = CONNECT_STATE_DISCONNECTED;
return nextTick(() => { if (callback) { self.removeListener("connect", callback);
callback(ex!); } else { self.emit("error", ex); } }); }
state.connectState = CONNECT_STATE_CONNECTED;
nextTick(() => self.emit("connect"));}
function doSend( ex: ErrnoException | null, self: Socket, ip: string, list: MessageType[], address: string, port: number, callback?: (error: ErrnoException | null, bytes?: number) => void,) { const state = self[kStateSymbol];
if (ex) { if (typeof callback === "function") { nextTick(callback, ex);
return; }
nextTick(() => self.emit("error", ex));
return; } else if (!state.handle) { return; }
const req = new SendWrap(); req.list = list; // Keep reference alive. req.address = address; req.port = port;
if (callback) { req.callback = callback; req.oncomplete = afterSend; }
let err;
if (port) { err = state.handle.send(req, list, list.length, port, ip, !!callback); } else { err = state.handle.send(req, list, list.length, !!callback); }
if (err >= 1) { // Synchronous finish. The return code is msg_length + 1 so that we can // distinguish between synchronous success and asynchronous success. if (callback) { nextTick(callback, null, err - 1); }
return; }
if (err && callback) { // Don't emit as error, dgram_legacy.js compatibility const ex = exceptionWithHostPort(err, "send", address, port);
nextTick(callback, ex); }}
function afterSend(this: SendWrap, err: number | null, sent?: number) { let ex: ErrnoException | null;
if (err) { ex = exceptionWithHostPort(err, "send", this.address, this.port); } else { ex = null; }
this.callback(ex, sent);}
export type { SocketType };
export default { createSocket, Socket,};
std

Version Info

Tagged at
a year ago