1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.sendInParallel = sendInParallel;
|
7 | exports.sendMessage = sendMessage;
|
8 | var _dgram = _interopRequireDefault(require("dgram"));
|
9 | var _net = _interopRequireDefault(require("net"));
|
10 | var _nodeUrl = _interopRequireDefault(require("node:url"));
|
11 | var _abortError = _interopRequireDefault(require("./errors/abort-error"));
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 | async function sendInParallel(addresses, port, request, signal) {
|
14 | if (signal.aborted) {
|
15 | throw new _abortError.default();
|
16 | }
|
17 | return await new Promise((resolve, reject) => {
|
18 | const sockets = [];
|
19 | let errorCount = 0;
|
20 | const onError = err => {
|
21 | errorCount++;
|
22 | if (errorCount === addresses.length) {
|
23 | signal.removeEventListener('abort', onAbort);
|
24 | clearSockets();
|
25 | reject(err);
|
26 | }
|
27 | };
|
28 | const onMessage = message => {
|
29 | signal.removeEventListener('abort', onAbort);
|
30 | clearSockets();
|
31 | resolve(message);
|
32 | };
|
33 | const onAbort = () => {
|
34 | clearSockets();
|
35 | reject(new _abortError.default());
|
36 | };
|
37 | const clearSockets = () => {
|
38 | for (const socket of sockets) {
|
39 | socket.removeListener('error', onError);
|
40 | socket.removeListener('message', onMessage);
|
41 | socket.close();
|
42 | }
|
43 | };
|
44 | signal.addEventListener('abort', onAbort, {
|
45 | once: true
|
46 | });
|
47 | for (let j = 0; j < addresses.length; j++) {
|
48 | const udpType = addresses[j].family === 6 ? 'udp6' : 'udp4';
|
49 | const socket = _dgram.default.createSocket(udpType);
|
50 | sockets.push(socket);
|
51 | socket.on('error', onError);
|
52 | socket.on('message', onMessage);
|
53 | socket.send(request, 0, request.length, port, addresses[j].address);
|
54 | }
|
55 | });
|
56 | }
|
57 | async function sendMessage(host, port, lookup, signal, request) {
|
58 | if (signal.aborted) {
|
59 | throw new _abortError.default();
|
60 | }
|
61 | let addresses;
|
62 | if (_net.default.isIP(host)) {
|
63 | addresses = [{
|
64 | address: host,
|
65 | family: _net.default.isIPv6(host) ? 6 : 4
|
66 | }];
|
67 | } else {
|
68 | addresses = await new Promise((resolve, reject) => {
|
69 | const onAbort = () => {
|
70 | reject(new _abortError.default());
|
71 | };
|
72 | signal.addEventListener('abort', onAbort);
|
73 | lookup(_nodeUrl.default.domainToASCII(host), {
|
74 | all: true
|
75 | }, (err, addresses) => {
|
76 | signal.removeEventListener('abort', onAbort);
|
77 | err ? reject(err) : resolve(addresses);
|
78 | });
|
79 | });
|
80 | }
|
81 | return await sendInParallel(addresses, port, request, signal);
|
82 | }
|
83 | //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfZGdyYW0iLCJfaW50ZXJvcFJlcXVpcmVEZWZhdWx0IiwicmVxdWlyZSIsIl9uZXQiLCJfbm9kZVVybCIsIl9hYm9ydEVycm9yIiwib2JqIiwiX19lc01vZHVsZSIsImRlZmF1bHQiLCJzZW5kSW5QYXJhbGxlbCIsImFkZHJlc3NlcyIsInBvcnQiLCJyZXF1ZXN0Iiwic2lnbmFsIiwiYWJvcnRlZCIsIkFib3J0RXJyb3IiLCJQcm9taXNlIiwicmVzb2x2ZSIsInJlamVjdCIsInNvY2tldHMiLCJlcnJvckNvdW50Iiwib25FcnJvciIsImVyciIsImxlbmd0aCIsInJlbW92ZUV2ZW50TGlzdGVuZXIiLCJvbkFib3J0IiwiY2xlYXJTb2NrZXRzIiwib25NZXNzYWdlIiwibWVzc2FnZSIsInNvY2tldCIsInJlbW92ZUxpc3RlbmVyIiwiY2xvc2UiLCJhZGRFdmVudExpc3RlbmVyIiwib25jZSIsImoiLCJ1ZHBUeXBlIiwiZmFtaWx5IiwiZGdyYW0iLCJjcmVhdGVTb2NrZXQiLCJwdXNoIiwib24iLCJzZW5kIiwiYWRkcmVzcyIsInNlbmRNZXNzYWdlIiwiaG9zdCIsImxvb2t1cCIsIm5ldCIsImlzSVAiLCJp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