node-ipc.cjs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __getProtoOf = Object.getPrototypeOf;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  8. var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
  9. var __commonJS = (cb, mod) => function __require() {
  10. return mod || (0, cb[Object.keys(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  11. };
  12. var __export = (target, all) => {
  13. __markAsModule(target);
  14. for (var name in all)
  15. __defProp(target, name, { get: all[name], enumerable: true });
  16. };
  17. var __reExport = (target, module2, desc) => {
  18. if (module2 && typeof module2 === "object" || typeof module2 === "function") {
  19. for (let key of __getOwnPropNames(module2))
  20. if (!__hasOwnProp.call(target, key) && key !== "default")
  21. __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
  22. }
  23. return target;
  24. };
  25. var __toModule = (module2) => {
  26. return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
  27. };
  28. var __publicField = (obj, key, value) => {
  29. __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  30. return value;
  31. };
  32. var __accessCheck = (obj, member, msg) => {
  33. if (!member.has(obj))
  34. throw TypeError("Cannot " + msg);
  35. };
  36. var __privateGet = (obj, member, getter) => {
  37. __accessCheck(obj, member, "read from private field");
  38. return getter ? getter.call(obj) : member.get(obj);
  39. };
  40. var __privateAdd = (obj, member, value) => {
  41. if (member.has(obj))
  42. throw TypeError("Cannot add the same private member more than once");
  43. member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
  44. };
  45. // node_modules/js-message/Message.js
  46. var require_Message = __commonJS({
  47. "node_modules/js-message/Message.js"(exports, module2) {
  48. function Message3() {
  49. Object.defineProperties(this, {
  50. data: {
  51. enumerable: true,
  52. get: getData,
  53. set: setData
  54. },
  55. type: {
  56. enumerable: true,
  57. get: getType,
  58. set: setType
  59. },
  60. load: {
  61. enumerable: true,
  62. writable: false,
  63. value: parse
  64. },
  65. JSON: {
  66. enumerable: true,
  67. get: getJSON
  68. }
  69. });
  70. var type = "";
  71. var data = {};
  72. function getType() {
  73. return type;
  74. }
  75. function getData() {
  76. return data;
  77. }
  78. function getJSON() {
  79. return JSON.stringify({
  80. type,
  81. data
  82. });
  83. }
  84. function setType(value) {
  85. type = value;
  86. }
  87. function setData(value) {
  88. data = value;
  89. }
  90. function parse(message) {
  91. try {
  92. var message = JSON.parse(message);
  93. type = message.type;
  94. data = message.data;
  95. } catch (err) {
  96. var badMessage = message;
  97. type = "error", data = {
  98. message: "Invalid JSON response format",
  99. err,
  100. response: badMessage
  101. };
  102. }
  103. }
  104. }
  105. module2.exports = Message3;
  106. }
  107. });
  108. // node_modules/@node-ipc/js-queue/queue.js
  109. var require_queue = __commonJS({
  110. "node_modules/@node-ipc/js-queue/queue.js"(exports, module2) {
  111. function Queue2(asStack) {
  112. Object.defineProperties(this, {
  113. add: {
  114. enumerable: true,
  115. writable: false,
  116. value: addToQueue
  117. },
  118. next: {
  119. enumerable: true,
  120. writable: false,
  121. value: run
  122. },
  123. clear: {
  124. enumerable: true,
  125. writable: false,
  126. value: clearQueue
  127. },
  128. contents: {
  129. enumerable: false,
  130. get: getQueue,
  131. set: setQueue
  132. },
  133. autoRun: {
  134. enumerable: true,
  135. writable: true,
  136. value: true
  137. },
  138. stop: {
  139. enumerable: true,
  140. writable: true,
  141. value: false
  142. }
  143. });
  144. var queue = [];
  145. var running = false;
  146. var stop = false;
  147. function clearQueue() {
  148. queue = [];
  149. return queue;
  150. }
  151. function getQueue() {
  152. return queue;
  153. }
  154. function setQueue(val) {
  155. queue = val;
  156. return queue;
  157. }
  158. function addToQueue() {
  159. for (var i in arguments) {
  160. queue.push(arguments[i]);
  161. }
  162. if (!running && !this.stop && this.autoRun) {
  163. this.next();
  164. }
  165. }
  166. function run() {
  167. running = true;
  168. if (queue.length < 1 || this.stop) {
  169. running = false;
  170. return;
  171. }
  172. queue.shift().bind(this)();
  173. }
  174. }
  175. module2.exports = Queue2;
  176. }
  177. });
  178. // node-ipc.js
  179. __export(exports, {
  180. IPCModule: () => IPCModule,
  181. default: () => singleton
  182. });
  183. // entities/Defaults.js
  184. var import_os = __toModule(require("os"));
  185. var Defaults = class {
  186. constructor() {
  187. __publicField(this, "appspace", "app.");
  188. __publicField(this, "socketRoot", "/tmp/");
  189. __publicField(this, "id", import_os.default.hostname());
  190. __publicField(this, "encoding", "utf8");
  191. __publicField(this, "rawBuffer", false);
  192. __publicField(this, "sync", false);
  193. __publicField(this, "unlink", true);
  194. __publicField(this, "delimiter", "\f");
  195. __publicField(this, "silent", false);
  196. __publicField(this, "logDepth", 5);
  197. __publicField(this, "logInColor", true);
  198. __publicField(this, "logger", console.log.bind(console));
  199. __publicField(this, "maxConnections", 100);
  200. __publicField(this, "retry", 500);
  201. __publicField(this, "maxRetries", Infinity);
  202. __publicField(this, "stopRetrying", false);
  203. __publicField(this, "IPType", getIPType());
  204. __publicField(this, "tls", false);
  205. __publicField(this, "networkHost", this.IPType == "IPv6" ? "::1" : "127.0.0.1");
  206. __publicField(this, "networkPort", 8e3);
  207. __publicField(this, "readableAll", false);
  208. __publicField(this, "writableAll", false);
  209. __publicField(this, "interface", {
  210. localAddress: false,
  211. localPort: false,
  212. family: false,
  213. hints: false,
  214. lookup: false
  215. });
  216. }
  217. };
  218. function getIPType() {
  219. const networkInterfaces = import_os.default.networkInterfaces();
  220. let IPType = "";
  221. if (networkInterfaces && Array.isArray(networkInterfaces) && networkInterfaces.length > 0) {
  222. IPType = networkInterfaces[Object.keys(networkInterfaces)[0]][0].family;
  223. }
  224. return IPType;
  225. }
  226. // dao/client.js
  227. var import_net = __toModule(require("net"));
  228. var import_tls = __toModule(require("tls"));
  229. // entities/EventParser.js
  230. var Parser = class {
  231. constructor(config) {
  232. if (!config) {
  233. config = new Defaults();
  234. }
  235. this.delimiter = config.delimiter;
  236. }
  237. format(message) {
  238. if (!message.data && message.data !== false && message.data !== 0) {
  239. message.data = {};
  240. }
  241. if (message.data["_maxListeners"]) {
  242. message.data = {};
  243. }
  244. message = message.JSON + this.delimiter;
  245. return message;
  246. }
  247. parse(data) {
  248. let events = data.split(this.delimiter);
  249. events.pop();
  250. return events;
  251. }
  252. };
  253. // dao/client.js
  254. var import_js_message = __toModule(require_Message());
  255. var import_fs = __toModule(require("fs"));
  256. var import_js_queue = __toModule(require_queue());
  257. // node_modules/strong-type/index.js
  258. var Fake = class {
  259. };
  260. var FakeCore = class {
  261. };
  262. var Is = class {
  263. constructor(strict = true) {
  264. this.strict = strict;
  265. }
  266. throw(valueType, expectedType) {
  267. let err = new TypeError();
  268. err.message = `expected type of ${valueType} to be ${expectedType}`;
  269. if (!this.strict) {
  270. return false;
  271. }
  272. throw err;
  273. }
  274. typeCheck(value, type) {
  275. if (typeof value === type) {
  276. return true;
  277. }
  278. return this.throw(typeof value, type);
  279. }
  280. instanceCheck(value = new Fake(), constructor = FakeCore) {
  281. if (value instanceof constructor) {
  282. return true;
  283. }
  284. return this.throw(typeof value, constructor.name);
  285. }
  286. symbolStringCheck(value, type) {
  287. if (Object.prototype.toString.call(value) == `[object ${type}]`) {
  288. return true;
  289. }
  290. return this.throw(Object.prototype.toString.call(value), `[object ${type}]`);
  291. }
  292. compare(value, targetValue, typeName) {
  293. if (value == targetValue) {
  294. return true;
  295. }
  296. if (!this.strict) {
  297. return false;
  298. }
  299. throw new Error(`expected ${value} == ${targetValue} but it is not.`);
  300. }
  301. defined(value) {
  302. const weakIs = new Is(false);
  303. if (weakIs.undefined(value)) {
  304. return this.throw("undefined", "defined");
  305. }
  306. return true;
  307. }
  308. any(value) {
  309. return this.defined(value);
  310. }
  311. exists(value) {
  312. return this.defined(value);
  313. }
  314. union(value, typesString) {
  315. const types = typesString.split("|");
  316. const weakIs = new Is(false);
  317. let pass = false;
  318. let type = "undefined";
  319. for (type of types) {
  320. try {
  321. if (weakIs[type](value)) {
  322. pass = true;
  323. break;
  324. }
  325. } catch (err) {
  326. return this.throw(type, "a method available on strong-type");
  327. }
  328. }
  329. if (pass) {
  330. return this[type](value);
  331. }
  332. return this.throw(typeof value, types.join("|"));
  333. }
  334. finite(value) {
  335. if (isFinite(value)) {
  336. return true;
  337. }
  338. return this.throw(typeof value, "finite");
  339. }
  340. NaN(value) {
  341. if (!this.number(value)) {
  342. return this.number(value);
  343. }
  344. if (isNaN(value)) {
  345. return true;
  346. }
  347. return this.throw(typeof value, "NaN");
  348. }
  349. null(value) {
  350. return this.compare(value, null, "null");
  351. }
  352. array(value) {
  353. return this.instanceCheck(value, Array);
  354. }
  355. boolean(value) {
  356. return this.typeCheck(value, "boolean");
  357. }
  358. bigInt(value) {
  359. return this.typeCheck(value, "bigint");
  360. }
  361. date(value) {
  362. return this.instanceCheck(value, Date);
  363. }
  364. generator(value) {
  365. return this.symbolStringCheck(value, "Generator");
  366. }
  367. asyncGenerator(value) {
  368. return this.symbolStringCheck(value, "AsyncGenerator");
  369. }
  370. globalThis(value) {
  371. return this.compare(value, globalThis, "explicitly globalThis, not window, global nor self");
  372. }
  373. infinity(value) {
  374. return this.compare(value, Infinity, "Infinity");
  375. }
  376. map(value) {
  377. return this.instanceCheck(value, Map);
  378. }
  379. weakMap(value) {
  380. return this.instanceCheck(value, WeakMap);
  381. }
  382. number(value) {
  383. return this.typeCheck(value, "number");
  384. }
  385. object(value) {
  386. return this.typeCheck(value, "object");
  387. }
  388. promise(value) {
  389. return this.instanceCheck(value, Promise);
  390. }
  391. regExp(value) {
  392. return this.instanceCheck(value, RegExp);
  393. }
  394. undefined(value) {
  395. return this.typeCheck(value, "undefined");
  396. }
  397. set(value) {
  398. return this.instanceCheck(value, Set);
  399. }
  400. weakSet(value) {
  401. return this.instanceCheck(value, WeakSet);
  402. }
  403. string(value) {
  404. return this.typeCheck(value, "string");
  405. }
  406. symbol(value) {
  407. return this.typeCheck(value, "symbol");
  408. }
  409. function(value) {
  410. return this.typeCheck(value, "function");
  411. }
  412. asyncFunction(value) {
  413. return this.symbolStringCheck(value, "AsyncFunction");
  414. }
  415. generatorFunction(value) {
  416. return this.symbolStringCheck(value, "GeneratorFunction");
  417. }
  418. asyncGeneratorFunction(value) {
  419. return this.symbolStringCheck(value, "AsyncGeneratorFunction");
  420. }
  421. error(value) {
  422. return this.instanceCheck(value, Error);
  423. }
  424. evalError(value) {
  425. return this.instanceCheck(value, EvalError);
  426. }
  427. rangeError(value) {
  428. return this.instanceCheck(value, RangeError);
  429. }
  430. referenceError(value) {
  431. return this.instanceCheck(value, ReferenceError);
  432. }
  433. syntaxError(value) {
  434. return this.instanceCheck(value, SyntaxError);
  435. }
  436. typeError(value) {
  437. return this.instanceCheck(value, TypeError);
  438. }
  439. URIError(value) {
  440. return this.instanceCheck(value, URIError);
  441. }
  442. bigInt64Array(value) {
  443. return this.instanceCheck(value, BigInt64Array);
  444. }
  445. bigUint64Array(value) {
  446. return this.instanceCheck(value, BigUint64Array);
  447. }
  448. float32Array(value) {
  449. return this.instanceCheck(value, Float32Array);
  450. }
  451. float64Array(value) {
  452. return this.instanceCheck(value, Float64Array);
  453. }
  454. int8Array(value) {
  455. return this.instanceCheck(value, Int8Array);
  456. }
  457. int16Array(value) {
  458. return this.instanceCheck(value, Int16Array);
  459. }
  460. int32Array(value) {
  461. return this.instanceCheck(value, Int32Array);
  462. }
  463. uint8Array(value) {
  464. return this.instanceCheck(value, Uint8Array);
  465. }
  466. uint8ClampedArray(value) {
  467. return this.instanceCheck(value, Uint8ClampedArray);
  468. }
  469. uint16Array(value) {
  470. return this.instanceCheck(value, Uint16Array);
  471. }
  472. uint32Array(value) {
  473. return this.instanceCheck(value, Uint32Array);
  474. }
  475. arrayBuffer(value) {
  476. return this.instanceCheck(value, ArrayBuffer);
  477. }
  478. dataView(value) {
  479. return this.instanceCheck(value, DataView);
  480. }
  481. sharedArrayBuffer(value) {
  482. return this.instanceCheck(value, function() {
  483. try {
  484. return SharedArrayBuffer;
  485. } catch (e) {
  486. return Fake;
  487. }
  488. }());
  489. }
  490. intlDateTimeFormat(value) {
  491. return this.instanceCheck(value, Intl.DateTimeFormat);
  492. }
  493. intlCollator(value) {
  494. return this.instanceCheck(value, Intl.Collator);
  495. }
  496. intlDisplayNames(value) {
  497. return this.instanceCheck(value, Intl.DisplayNames);
  498. }
  499. intlListFormat(value) {
  500. return this.instanceCheck(value, Intl.ListFormat);
  501. }
  502. intlLocale(value) {
  503. return this.instanceCheck(value, Intl.Locale);
  504. }
  505. intlNumberFormat(value) {
  506. return this.instanceCheck(value, Intl.NumberFormat);
  507. }
  508. intlPluralRules(value) {
  509. return this.instanceCheck(value, Intl.PluralRules);
  510. }
  511. intlRelativeTimeFormat(value) {
  512. return this.instanceCheck(value, Intl.RelativeTimeFormat);
  513. }
  514. intlRelativeTimeFormat(value) {
  515. return this.instanceCheck(value, Intl.RelativeTimeFormat);
  516. }
  517. finalizationRegistry(value) {
  518. return this.instanceCheck(value, FinalizationRegistry);
  519. }
  520. weakRef(value) {
  521. return this.instanceCheck(value, WeakRef);
  522. }
  523. };
  524. // node_modules/event-pubsub/index.js
  525. var is = new Is();
  526. var _handleOnce, _all, _once, _events;
  527. var EventPubSub = class {
  528. constructor() {
  529. __privateAdd(this, _handleOnce, (type, handlers, ...args) => {
  530. is.string(type);
  531. is.array(handlers);
  532. const deleteOnceHandled = [];
  533. for (let handler of handlers) {
  534. handler(...args);
  535. if (handler[__privateGet(this, _once)]) {
  536. deleteOnceHandled.push(handler);
  537. }
  538. }
  539. for (let handler of deleteOnceHandled) {
  540. this.off(type, handler);
  541. }
  542. });
  543. __privateAdd(this, _all, Symbol.for("event-pubsub-all"));
  544. __privateAdd(this, _once, Symbol.for("event-pubsub-once"));
  545. __privateAdd(this, _events, {});
  546. }
  547. on(type, handler, once = false) {
  548. is.string(type);
  549. is.function(handler);
  550. is.boolean(once);
  551. if (type == "*") {
  552. type = __privateGet(this, _all);
  553. }
  554. if (!__privateGet(this, _events)[type]) {
  555. __privateGet(this, _events)[type] = [];
  556. }
  557. handler[__privateGet(this, _once)] = once;
  558. __privateGet(this, _events)[type].push(handler);
  559. return this;
  560. }
  561. once(type, handler) {
  562. return this.on(type, handler, true);
  563. }
  564. off(type = "*", handler = "*") {
  565. is.string(type);
  566. if (type == __privateGet(this, _all).toString() || type == "*") {
  567. type = __privateGet(this, _all);
  568. }
  569. if (!__privateGet(this, _events)[type]) {
  570. return this;
  571. }
  572. if (handler == "*") {
  573. delete __privateGet(this, _events)[type];
  574. return this;
  575. }
  576. is.function(handler);
  577. const handlers = __privateGet(this, _events)[type];
  578. while (handlers.includes(handler)) {
  579. handlers.splice(handlers.indexOf(handler), 1);
  580. }
  581. if (handlers.length < 1) {
  582. delete __privateGet(this, _events)[type];
  583. }
  584. return this;
  585. }
  586. emit(type, ...args) {
  587. is.string(type);
  588. const globalHandlers = __privateGet(this, _events)[__privateGet(this, _all)] || [];
  589. __privateGet(this, _handleOnce).call(this, __privateGet(this, _all).toString(), globalHandlers, type, ...args);
  590. if (!__privateGet(this, _events)[type]) {
  591. return this;
  592. }
  593. const handlers = __privateGet(this, _events)[type];
  594. __privateGet(this, _handleOnce).call(this, type, handlers, ...args);
  595. return this;
  596. }
  597. reset() {
  598. this.off(__privateGet(this, _all).toString());
  599. for (let type in __privateGet(this, _events)) {
  600. this.off(type);
  601. }
  602. return this;
  603. }
  604. get list() {
  605. return Object.assign({}, __privateGet(this, _events));
  606. }
  607. };
  608. _handleOnce = new WeakMap();
  609. _all = new WeakMap();
  610. _once = new WeakMap();
  611. _events = new WeakMap();
  612. // dao/client.js
  613. var eventParser = new Parser();
  614. var Client = class extends EventPubSub {
  615. constructor(config, log2) {
  616. super();
  617. __publicField(this, "Client", Client);
  618. __publicField(this, "queue", new import_js_queue.default());
  619. __publicField(this, "socket", false);
  620. __publicField(this, "connect", connect);
  621. __publicField(this, "emit", emit);
  622. __publicField(this, "retriesRemaining", 0);
  623. __publicField(this, "explicitlyDisconnected", false);
  624. this.config = config;
  625. this.log = log2;
  626. this.publish = super.emit;
  627. config.maxRetries ? this.retriesRemaining = config.maxRetries : 0;
  628. eventParser = new Parser(this.config);
  629. }
  630. };
  631. function emit(type, data) {
  632. this.log("dispatching event to ", this.id, this.path, " : ", type, ",", data);
  633. let message = new import_js_message.default();
  634. message.type = type;
  635. message.data = data;
  636. if (this.config.rawBuffer) {
  637. message = Buffer.from(type, this.config.encoding);
  638. } else {
  639. message = eventParser.format(message);
  640. }
  641. if (!this.config.sync) {
  642. this.socket.write(message);
  643. return;
  644. }
  645. this.queue.add(syncEmit.bind(this, message));
  646. }
  647. function syncEmit(message) {
  648. this.log("dispatching event to ", this.id, this.path, " : ", message);
  649. this.socket.write(message);
  650. }
  651. function connect() {
  652. let client = this;
  653. client.log("requested connection to ", client.id, client.path);
  654. if (!this.path) {
  655. client.log("\n\n######\nerror: ", client.id, " client has not specified socket path it wishes to connect to.");
  656. return;
  657. }
  658. const options = {};
  659. if (!client.port) {
  660. client.log("Connecting client on Unix Socket :", client.path);
  661. options.path = client.path;
  662. if (process.platform === "win32" && !client.path.startsWith("\\\\.\\pipe\\")) {
  663. options.path = options.path.replace(/^\//, "");
  664. options.path = options.path.replace(/\//g, "-");
  665. options.path = `\\\\.\\pipe\\${options.path}`;
  666. }
  667. client.socket = import_net.default.connect(options);
  668. } else {
  669. options.host = client.path;
  670. options.port = client.port;
  671. if (client.config.interface.localAddress) {
  672. options.localAddress = client.config.interface.localAddress;
  673. }
  674. if (client.config.interface.localPort) {
  675. options.localPort = client.config.interface.localPort;
  676. }
  677. if (client.config.interface.family) {
  678. options.family = client.config.interface.family;
  679. }
  680. if (client.config.interface.hints) {
  681. options.hints = client.config.interface.hints;
  682. }
  683. if (client.config.interface.lookup) {
  684. options.lookup = client.config.interface.lookup;
  685. }
  686. if (!client.config.tls) {
  687. client.log("Connecting client via TCP to", options);
  688. client.socket = import_net.default.connect(options);
  689. } else {
  690. client.log("Connecting client via TLS to", client.path, client.port, client.config.tls);
  691. if (client.config.tls.private) {
  692. client.config.tls.key = import_fs.default.readFileSync(client.config.tls.private);
  693. }
  694. if (client.config.tls.public) {
  695. client.config.tls.cert = import_fs.default.readFileSync(client.config.tls.public);
  696. }
  697. if (client.config.tls.trustedConnections) {
  698. if (typeof client.config.tls.trustedConnections === "string") {
  699. client.config.tls.trustedConnections = [client.config.tls.trustedConnections];
  700. }
  701. client.config.tls.ca = [];
  702. for (let i = 0; i < client.config.tls.trustedConnections.length; i++) {
  703. client.config.tls.ca.push(import_fs.default.readFileSync(client.config.tls.trustedConnections[i]));
  704. }
  705. }
  706. Object.assign(client.config.tls, options);
  707. client.socket = import_tls.default.connect(client.config.tls);
  708. }
  709. }
  710. client.socket.setEncoding(this.config.encoding);
  711. client.socket.on("error", function(err) {
  712. client.log("\n\n######\nerror: ", err);
  713. client.publish("error", err);
  714. });
  715. client.socket.on("connect", function connectionMade() {
  716. client.publish("connect");
  717. client.retriesRemaining = client.config.maxRetries;
  718. client.log("retrying reset");
  719. });
  720. client.socket.on("close", function connectionClosed() {
  721. client.log("connection closed", client.id, client.path, client.retriesRemaining, "tries remaining of", client.config.maxRetries);
  722. if (client.config.stopRetrying || client.retriesRemaining < 1 || client.explicitlyDisconnected) {
  723. client.publish("disconnect");
  724. client.log(client.config.id, "exceeded connection rety amount of", " or stopRetrying flag set.");
  725. client.socket.destroy();
  726. client.publish("destroy");
  727. client = void 0;
  728. return;
  729. }
  730. setTimeout(function retryTimeout() {
  731. if (client.explicitlyDisconnected) {
  732. return;
  733. }
  734. client.retriesRemaining--;
  735. client.connect();
  736. }.bind(null, client), client.config.retry);
  737. client.publish("disconnect");
  738. });
  739. client.socket.on("data", function(data) {
  740. client.log("## received events ##");
  741. if (client.config.rawBuffer) {
  742. client.publish("data", Buffer.from(data, client.config.encoding));
  743. if (!client.config.sync) {
  744. return;
  745. }
  746. client.queue.next();
  747. return;
  748. }
  749. if (!this.ipcBuffer) {
  750. this.ipcBuffer = "";
  751. }
  752. data = this.ipcBuffer += data;
  753. if (data.slice(-1) != eventParser.delimiter || data.indexOf(eventParser.delimiter) == -1) {
  754. client.log("Messages are large, You may want to consider smaller messages.");
  755. return;
  756. }
  757. this.ipcBuffer = "";
  758. const events = eventParser.parse(data);
  759. const eCount = events.length;
  760. for (let i = 0; i < eCount; i++) {
  761. let message = new import_js_message.default();
  762. message.load(events[i]);
  763. client.log("detected event", message.type, message.data);
  764. client.publish(message.type, message.data);
  765. }
  766. if (!client.config.sync) {
  767. return;
  768. }
  769. client.queue.next();
  770. });
  771. }
  772. // dao/socketServer.js
  773. var import_net2 = __toModule(require("net"));
  774. var import_tls2 = __toModule(require("tls"));
  775. var import_fs2 = __toModule(require("fs"));
  776. var import_dgram = __toModule(require("dgram"));
  777. var import_js_message2 = __toModule(require_Message());
  778. var eventParser2 = new Parser();
  779. var Server = class extends EventPubSub {
  780. constructor(path, config, log2, port) {
  781. super();
  782. __publicField(this, "udp4", false);
  783. __publicField(this, "udp6", false);
  784. __publicField(this, "server", false);
  785. __publicField(this, "sockets", []);
  786. __publicField(this, "emit", emit2);
  787. __publicField(this, "broadcast", broadcast);
  788. this.config = config;
  789. this.path = path;
  790. this.port = port;
  791. this.log = log2;
  792. this.publish = super.emit;
  793. eventParser2 = new Parser(this.config);
  794. this.on("close", serverClosed.bind(this));
  795. }
  796. onStart(socket) {
  797. this.publish("start", socket);
  798. }
  799. stop() {
  800. this.server.close();
  801. }
  802. start() {
  803. if (!this.path) {
  804. this.log("Socket Server Path not specified, refusing to start");
  805. return;
  806. }
  807. if (this.config.unlink) {
  808. import_fs2.default.unlink(this.path, startServer.bind(this));
  809. } else {
  810. startServer.bind(this)();
  811. }
  812. }
  813. };
  814. function emit2(socket, type, data) {
  815. this.log("dispatching event to socket", " : ", type, data);
  816. let message = new import_js_message2.default();
  817. message.type = type;
  818. message.data = data;
  819. if (this.config.rawBuffer) {
  820. this.log(this.config.encoding);
  821. message = Buffer.from(type, this.config.encoding);
  822. } else {
  823. message = eventParser2.format(message);
  824. }
  825. if (this.udp4 || this.udp6) {
  826. if (!socket.address || !socket.port) {
  827. this.log("Attempting to emit to a single UDP socket without supplying socket address or port. Redispatching event as broadcast to all connected sockets");
  828. this.broadcast(type, data);
  829. return;
  830. }
  831. this.server.write(message, socket);
  832. return;
  833. }
  834. socket.write(message);
  835. }
  836. function broadcast(type, data) {
  837. this.log("broadcasting event to all known sockets listening to ", this.path, " : ", this.port ? this.port : "", type, data);
  838. let message = new import_js_message2.default();
  839. message.type = type;
  840. message.data = data;
  841. if (this.config.rawBuffer) {
  842. message = Buffer.from(type, this.config.encoding);
  843. } else {
  844. message = eventParser2.format(message);
  845. }
  846. if (this.udp4 || this.udp6) {
  847. for (let i = 1, count = this.sockets.length; i < count; i++) {
  848. this.server.write(message, this.sockets[i]);
  849. }
  850. } else {
  851. for (let i = 0, count = this.sockets.length; i < count; i++) {
  852. this.sockets[i].write(message);
  853. }
  854. }
  855. }
  856. function serverClosed() {
  857. for (let i = 0, count = this.sockets.length; i < count; i++) {
  858. let socket = this.sockets[i];
  859. let destroyedSocketId = false;
  860. if (socket) {
  861. if (socket.readable) {
  862. continue;
  863. }
  864. }
  865. if (socket.id) {
  866. destroyedSocketId = socket.id;
  867. }
  868. this.log("socket disconnected", destroyedSocketId.toString());
  869. if (socket && socket.destroy) {
  870. socket.destroy();
  871. }
  872. this.sockets.splice(i, 1);
  873. this.publish("socket.disconnected", socket, destroyedSocketId);
  874. return;
  875. }
  876. }
  877. function gotData(socket, data, UDPSocket) {
  878. let sock = this.udp4 || this.udp6 ? UDPSocket : socket;
  879. if (this.config.rawBuffer) {
  880. data = Buffer.from(data, this.config.encoding);
  881. this.publish("data", data, sock);
  882. return;
  883. }
  884. if (!sock.ipcBuffer) {
  885. sock.ipcBuffer = "";
  886. }
  887. data = sock.ipcBuffer += data;
  888. if (data.slice(-1) != eventParser2.delimiter || data.indexOf(eventParser2.delimiter) == -1) {
  889. this.log("Messages are large, You may want to consider smaller messages.");
  890. return;
  891. }
  892. sock.ipcBuffer = "";
  893. data = eventParser2.parse(data);
  894. while (data.length > 0) {
  895. let message = new import_js_message2.default();
  896. message.load(data.shift());
  897. if (message.data && message.data.id) {
  898. sock.id = message.data.id;
  899. }
  900. this.log("received event of : ", message.type, message.data);
  901. this.publish(message.type, message.data, sock);
  902. }
  903. }
  904. function socketClosed(socket) {
  905. this.publish("close", socket);
  906. }
  907. function serverCreated(socket) {
  908. this.sockets.push(socket);
  909. if (socket.setEncoding) {
  910. socket.setEncoding(this.config.encoding);
  911. }
  912. this.log("## socket connection to server detected ##");
  913. socket.on("close", socketClosed.bind(this));
  914. socket.on("error", function(err) {
  915. this.log("server socket error", err);
  916. this.publish("error", err);
  917. }.bind(this));
  918. socket.on("data", gotData.bind(this, socket));
  919. socket.on("message", function(msg, rinfo) {
  920. if (!rinfo) {
  921. return;
  922. }
  923. this.log("Received UDP message from ", rinfo.address, rinfo.port);
  924. let data;
  925. if (this.config.rawSocket) {
  926. data = Buffer.from(msg, this.config.encoding);
  927. } else {
  928. data = msg.toString();
  929. }
  930. socket.emit("data", data, rinfo);
  931. }.bind(this));
  932. this.publish("connect", socket);
  933. if (this.config.rawBuffer) {
  934. return;
  935. }
  936. }
  937. function startServer() {
  938. this.log("starting server on ", this.path, this.port ? `:${this.port}` : "");
  939. if (!this.udp4 && !this.udp6) {
  940. this.log("starting TLS server", this.config.tls);
  941. if (!this.config.tls) {
  942. this.server = import_net2.default.createServer(serverCreated.bind(this));
  943. } else {
  944. startTLSServer.bind(this)();
  945. }
  946. } else {
  947. this.server = import_dgram.default.createSocket(this.udp4 ? "udp4" : "udp6");
  948. this.server.write = UDPWrite.bind(this);
  949. this.server.on("listening", function UDPServerStarted() {
  950. serverCreated.bind(this)(this.server);
  951. }.bind(this));
  952. }
  953. this.server.on("error", function(err) {
  954. this.log("server error", err);
  955. this.publish("error", err);
  956. }.bind(this));
  957. this.server.maxConnections = this.config.maxConnections;
  958. if (!this.port) {
  959. this.log("starting server as", "Unix || Windows Socket");
  960. if (process.platform === "win32") {
  961. this.path = this.path.replace(/^\//, "");
  962. this.path = this.path.replace(/\//g, "-");
  963. this.path = `\\\\.\\pipe\\${this.path}`;
  964. }
  965. this.server.listen({
  966. path: this.path,
  967. readableAll: this.config.readableAll,
  968. writableAll: this.config.writableAll
  969. }, this.onStart.bind(this));
  970. return;
  971. }
  972. if (!this.udp4 && !this.udp6) {
  973. this.log("starting server as", this.config.tls ? "TLS" : "TCP");
  974. this.server.listen(this.port, this.path, this.onStart.bind(this));
  975. return;
  976. }
  977. this.log("starting server as", this.udp4 ? "udp4" : "udp6");
  978. this.server.bind(this.port, this.path);
  979. this.onStart({
  980. address: this.path,
  981. port: this.port
  982. });
  983. }
  984. function startTLSServer() {
  985. this.log("starting TLS server", this.config.tls);
  986. if (this.config.tls.private) {
  987. this.config.tls.key = import_fs2.default.readFileSync(this.config.tls.private);
  988. } else {
  989. this.config.tls.key = import_fs2.default.readFileSync(`${__dirname}/../local-node-ipc-certs/private/server.key`);
  990. }
  991. if (this.config.tls.public) {
  992. this.config.tls.cert = import_fs2.default.readFileSync(this.config.tls.public);
  993. } else {
  994. this.config.tls.cert = import_fs2.default.readFileSync(`${__dirname}/../local-node-ipc-certs/server.pub`);
  995. }
  996. if (this.config.tls.dhparam) {
  997. this.config.tls.dhparam = import_fs2.default.readFileSync(this.config.tls.dhparam);
  998. }
  999. if (this.config.tls.trustedConnections) {
  1000. if (typeof this.config.tls.trustedConnections === "string") {
  1001. this.config.tls.trustedConnections = [this.config.tls.trustedConnections];
  1002. }
  1003. this.config.tls.ca = [];
  1004. for (let i = 0; i < this.config.tls.trustedConnections.length; i++) {
  1005. this.config.tls.ca.push(import_fs2.default.readFileSync(this.config.tls.trustedConnections[i]));
  1006. }
  1007. }
  1008. this.server = import_tls2.default.createServer(this.config.tls, serverCreated.bind(this));
  1009. }
  1010. function UDPWrite(message, socket) {
  1011. let data = Buffer.from(message, this.config.encoding);
  1012. this.server.send(data, 0, data.length, socket.port, socket.address, function(err, bytes) {
  1013. if (err) {
  1014. this.log("error writing data to socket", err);
  1015. this.publish("error", function(err2) {
  1016. this.publish("error", err2);
  1017. });
  1018. }
  1019. });
  1020. }
  1021. // services/IPC.js
  1022. var import_util = __toModule(require("util"));
  1023. var IPC = class {
  1024. constructor() {
  1025. __publicField(this, "config", new Defaults());
  1026. __publicField(this, "of", {});
  1027. __publicField(this, "server", false);
  1028. }
  1029. get connectTo() {
  1030. return connect2;
  1031. }
  1032. get connectToNet() {
  1033. return connectNet;
  1034. }
  1035. get disconnect() {
  1036. return disconnect;
  1037. }
  1038. get serve() {
  1039. return serve;
  1040. }
  1041. get serveNet() {
  1042. return serveNet;
  1043. }
  1044. get log() {
  1045. return log;
  1046. }
  1047. set connectTo(value) {
  1048. return connect2;
  1049. }
  1050. set connectToNet(value) {
  1051. return connectNet;
  1052. }
  1053. set disconnect(value) {
  1054. return disconnect;
  1055. }
  1056. set serve(value) {
  1057. return serve;
  1058. }
  1059. set serveNet(value) {
  1060. return serveNet;
  1061. }
  1062. set log(value) {
  1063. return log;
  1064. }
  1065. };
  1066. function log(...args) {
  1067. if (this.config.silent) {
  1068. return;
  1069. }
  1070. for (let i = 0, count = args.length; i < count; i++) {
  1071. if (typeof args[i] != "object") {
  1072. continue;
  1073. }
  1074. args[i] = import_util.default.inspect(args[i], {
  1075. depth: this.config.logDepth,
  1076. colors: this.config.logInColor
  1077. });
  1078. }
  1079. this.config.logger(args.join(" "));
  1080. }
  1081. function disconnect(id) {
  1082. if (!this.of[id]) {
  1083. return;
  1084. }
  1085. this.of[id].explicitlyDisconnected = true;
  1086. this.of[id].off("*", "*");
  1087. if (this.of[id].socket) {
  1088. if (this.of[id].socket.destroy) {
  1089. this.of[id].socket.destroy();
  1090. }
  1091. }
  1092. delete this.of[id];
  1093. }
  1094. function serve(path, callback) {
  1095. if (typeof path == "function") {
  1096. callback = path;
  1097. path = false;
  1098. }
  1099. if (!path) {
  1100. this.log("Server path not specified, so defaulting to", "ipc.config.socketRoot + ipc.config.appspace + ipc.config.id", this.config.socketRoot + this.config.appspace + this.config.id);
  1101. path = this.config.socketRoot + this.config.appspace + this.config.id;
  1102. }
  1103. if (!callback) {
  1104. callback = emptyCallback;
  1105. }
  1106. this.server = new Server(path, this.config, log);
  1107. this.server.on("start", callback);
  1108. }
  1109. function emptyCallback() {
  1110. }
  1111. function serveNet(host, port, UDPType2, callback) {
  1112. if (typeof host == "number") {
  1113. callback = UDPType2;
  1114. UDPType2 = port;
  1115. port = host;
  1116. host = false;
  1117. }
  1118. if (typeof host == "function") {
  1119. callback = host;
  1120. UDPType2 = false;
  1121. host = false;
  1122. port = false;
  1123. }
  1124. if (!host) {
  1125. this.log("Server host not specified, so defaulting to", "ipc.config.networkHost", this.config.networkHost);
  1126. host = this.config.networkHost;
  1127. }
  1128. if (host.toLowerCase() == "udp4" || host.toLowerCase() == "udp6") {
  1129. callback = port;
  1130. UDPType2 = host.toLowerCase();
  1131. port = false;
  1132. host = this.config.networkHost;
  1133. }
  1134. if (typeof port == "string") {
  1135. callback = UDPType2;
  1136. UDPType2 = port;
  1137. port = false;
  1138. }
  1139. if (typeof port == "function") {
  1140. callback = port;
  1141. UDPType2 = false;
  1142. port = false;
  1143. }
  1144. if (!port) {
  1145. this.log("Server port not specified, so defaulting to", "ipc.config.networkPort", this.config.networkPort);
  1146. port = this.config.networkPort;
  1147. }
  1148. if (typeof UDPType2 == "function") {
  1149. callback = UDPType2;
  1150. UDPType2 = false;
  1151. }
  1152. if (!callback) {
  1153. callback = emptyCallback;
  1154. }
  1155. this.server = new Server(host, this.config, log, port);
  1156. if (UDPType2) {
  1157. this.server[UDPType2] = true;
  1158. if (UDPType2 === "udp4" && host === "::1") {
  1159. this.server.path = "127.0.0.1";
  1160. }
  1161. }
  1162. this.server.on("start", callback);
  1163. }
  1164. function connect2(id, path, callback) {
  1165. if (typeof path == "function") {
  1166. callback = path;
  1167. path = false;
  1168. }
  1169. if (!callback) {
  1170. callback = emptyCallback;
  1171. }
  1172. if (!id) {
  1173. this.log("Service id required", "Requested service connection without specifying service id. Aborting connection attempt");
  1174. return;
  1175. }
  1176. if (!path) {
  1177. this.log("Service path not specified, so defaulting to", "ipc.config.socketRoot + ipc.config.appspace + id", (this.config.socketRoot + this.config.appspace + id).data);
  1178. path = this.config.socketRoot + this.config.appspace + id;
  1179. }
  1180. if (this.of[id]) {
  1181. if (!this.of[id].socket.destroyed) {
  1182. this.log("Already Connected to", id, "- So executing success without connection");
  1183. callback();
  1184. return;
  1185. }
  1186. this.of[id].socket.destroy();
  1187. }
  1188. this.of[id] = new Client(this.config, this.log);
  1189. this.of[id].id = id;
  1190. this.of[id].socket ? this.of[id].socket.id = id : null;
  1191. this.of[id].path = path;
  1192. this.of[id].connect();
  1193. callback(this);
  1194. }
  1195. function connectNet(id, host, port, callback) {
  1196. if (!id) {
  1197. this.log("Service id required", "Requested service connection without specifying service id. Aborting connection attempt");
  1198. return;
  1199. }
  1200. if (typeof host == "number") {
  1201. callback = port;
  1202. port = host;
  1203. host = false;
  1204. }
  1205. if (typeof host == "function") {
  1206. callback = host;
  1207. host = false;
  1208. port = false;
  1209. }
  1210. if (!host) {
  1211. this.log("Server host not specified, so defaulting to", "ipc.config.networkHost", this.config.networkHost);
  1212. host = this.config.networkHost;
  1213. }
  1214. if (typeof port == "function") {
  1215. callback = port;
  1216. port = false;
  1217. }
  1218. if (!port) {
  1219. this.log("Server port not specified, so defaulting to", "ipc.config.networkPort", this.config.networkPort);
  1220. port = this.config.networkPort;
  1221. }
  1222. if (typeof callback == "string") {
  1223. UDPType = callback;
  1224. callback = false;
  1225. }
  1226. if (!callback) {
  1227. callback = emptyCallback;
  1228. }
  1229. if (this.of[id]) {
  1230. if (!this.of[id].socket.destroyed) {
  1231. this.log("Already Connected to", id, "- So executing success without connection");
  1232. callback();
  1233. return;
  1234. }
  1235. this.of[id].socket.destroy();
  1236. }
  1237. this.of[id] = new Client(this.config, this.log);
  1238. this.of[id].id = id;
  1239. this.of[id].socket ? this.of[id].socket.id = id : null;
  1240. this.of[id].path = host;
  1241. this.of[id].port = port;
  1242. this.of[id].connect();
  1243. callback(this);
  1244. }
  1245. // node-ipc.js
  1246. var IPCModule = class extends IPC {
  1247. constructor() {
  1248. super();
  1249. __publicField(this, "IPC", IPC);
  1250. }
  1251. };
  1252. var singleton = new IPCModule();
  1253. // Annotate the CommonJS export names for ESM import in node:
  1254. 0 && (module.exports = {
  1255. IPCModule
  1256. });