index.cjs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  6. var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  7. var __export = (target, all) => {
  8. for (var name in all)
  9. __defProp(target, name, { get: all[name], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  20. var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  21. // src/index.js
  22. var src_exports = {};
  23. __export(src_exports, {
  24. Service: () => Service,
  25. ServiceContainer: () => ServiceContainer
  26. });
  27. module.exports = __toCommonJS(src_exports);
  28. // node_modules/@e22m4u/js-format/src/utils/is-class.js
  29. function isClass(value) {
  30. if (!value) return false;
  31. return typeof value === "function" && /^class\s/.test(Function.prototype.toString.call(value));
  32. }
  33. __name(isClass, "isClass");
  34. // node_modules/@e22m4u/js-format/src/value-to-string.js
  35. var BASE_CTOR_NAMES = [
  36. "String",
  37. "Number",
  38. "Boolean",
  39. "Object",
  40. "Array",
  41. "Function",
  42. "Symbol",
  43. "Map",
  44. "Set",
  45. "Date"
  46. ];
  47. function valueToString(input) {
  48. if (input == null) return String(input);
  49. if (typeof input === "string") return `"${input}"`;
  50. if (typeof input === "number" || typeof input === "boolean")
  51. return String(input);
  52. if (isClass(input)) return input.name ? input.name : "Class";
  53. if (input.constructor && input.constructor.name)
  54. return BASE_CTOR_NAMES.includes(input.constructor.name) ? input.constructor.name : `${input.constructor.name} (instance)`;
  55. if (typeof input === "object" && input.constructor == null) return "Object";
  56. return String(input);
  57. }
  58. __name(valueToString, "valueToString");
  59. // node_modules/@e22m4u/js-format/src/array-to-list.js
  60. var SEPARATOR = ", ";
  61. function arrayToList(input) {
  62. if (Array.isArray(input) && input.length)
  63. return input.map(valueToString).join(SEPARATOR);
  64. return valueToString(input);
  65. }
  66. __name(arrayToList, "arrayToList");
  67. // node_modules/@e22m4u/js-format/src/format.js
  68. function format(pattern) {
  69. if (pattern instanceof Date) {
  70. pattern = pattern.toISOString();
  71. } else if (typeof pattern !== "string") {
  72. pattern = String(pattern);
  73. }
  74. const re = /(%?)(%([sdjvl]))/g;
  75. const args = Array.prototype.slice.call(arguments, 1);
  76. if (args.length) {
  77. pattern = pattern.replace(re, function(match, escaped, ptn, flag) {
  78. let arg = args.shift();
  79. switch (flag) {
  80. case "s":
  81. arg = String(arg);
  82. break;
  83. case "d":
  84. arg = Number(arg);
  85. break;
  86. case "j":
  87. arg = JSON.stringify(arg);
  88. break;
  89. case "v":
  90. arg = valueToString(arg);
  91. break;
  92. case "l":
  93. arg = arrayToList(arg);
  94. break;
  95. }
  96. if (!escaped) return arg;
  97. args.unshift(arg);
  98. return match;
  99. });
  100. }
  101. if (args.length) pattern += " " + args.join(" ");
  102. pattern = pattern.replace(/%{2}/g, "%");
  103. return "" + pattern;
  104. }
  105. __name(format, "format");
  106. // node_modules/@e22m4u/js-format/src/errorf.js
  107. var _Errorf = class _Errorf extends Error {
  108. /**
  109. * Constructor.
  110. *
  111. * @param {string|undefined} pattern
  112. * @param {any} args
  113. */
  114. constructor(pattern = void 0, ...args) {
  115. const message = pattern != null ? format(pattern, ...args) : void 0;
  116. super(message);
  117. }
  118. };
  119. __name(_Errorf, "Errorf");
  120. var Errorf = _Errorf;
  121. // src/errors/invalid-argument-error.js
  122. var _InvalidArgumentError = class _InvalidArgumentError extends Errorf {
  123. };
  124. __name(_InvalidArgumentError, "InvalidArgumentError");
  125. var InvalidArgumentError = _InvalidArgumentError;
  126. // src/service-container.js
  127. var _ServiceContainer = class _ServiceContainer {
  128. /**
  129. * Services map.
  130. *
  131. * @type {Map<any, any>}
  132. * @private
  133. */
  134. _services = /* @__PURE__ */ new Map();
  135. /**
  136. * Parent container.
  137. *
  138. * @type {ServiceContainer}
  139. * @private
  140. */
  141. _parent;
  142. /**
  143. * Constructor.
  144. *
  145. * @param {ServiceContainer|undefined} parent
  146. */
  147. constructor(parent = void 0) {
  148. if (parent != null) {
  149. if (!(parent instanceof _ServiceContainer))
  150. throw new InvalidArgumentError(
  151. 'The provided parameter "parent" of ServicesContainer.constructor must be an instance ServiceContainer, but %v given.',
  152. parent
  153. );
  154. this._parent = parent;
  155. }
  156. }
  157. /**
  158. * Получить существующий или новый экземпляр.
  159. *
  160. * @param {*} ctor
  161. * @param {*} args
  162. * @return {*}
  163. */
  164. get(ctor, ...args) {
  165. if (!ctor || typeof ctor !== "function")
  166. throw new InvalidArgumentError(
  167. "The first argument of ServicesContainer.get must be a class constructor, but %v given.",
  168. ctor
  169. );
  170. if (!this._services.has(ctor) && this._parent && this._parent.has(ctor)) {
  171. return this._parent.get(ctor);
  172. }
  173. let service = this._services.get(ctor);
  174. if (!service || args.length) {
  175. service = ctor.kind === "Service" ? new ctor(this, ...args) : new ctor(...args);
  176. this._services.set(ctor, service);
  177. } else if (typeof service === "function") {
  178. service = service();
  179. this._services.set(ctor, service);
  180. }
  181. return service;
  182. }
  183. /**
  184. * Проверка существования конструктора в контейнере.
  185. *
  186. * @param {*} ctor
  187. * @return {boolean}
  188. */
  189. has(ctor) {
  190. if (this._services.has(ctor)) return true;
  191. if (this._parent) return this._parent.has(ctor);
  192. return false;
  193. }
  194. /**
  195. * Добавить конструктор в контейнер.
  196. *
  197. * @param {*} ctor
  198. * @param {*} args
  199. * @return {this}
  200. */
  201. add(ctor, ...args) {
  202. if (!ctor || typeof ctor !== "function")
  203. throw new InvalidArgumentError(
  204. "The first argument of ServicesContainer.add must be a class constructor, but %v given.",
  205. ctor
  206. );
  207. const factory = /* @__PURE__ */ __name(() => ctor.kind === "Service" ? new ctor(this, ...args) : new ctor(...args), "factory");
  208. this._services.set(ctor, factory);
  209. return this;
  210. }
  211. /**
  212. * Добавить конструктор и создать экземпляр.
  213. *
  214. * @param {*} ctor
  215. * @param {*} args
  216. * @return {this}
  217. */
  218. use(ctor, ...args) {
  219. if (!ctor || typeof ctor !== "function")
  220. throw new InvalidArgumentError(
  221. "The first argument of ServicesContainer.use must be a class constructor, but %v given.",
  222. ctor
  223. );
  224. const service = ctor.kind === "Service" ? new ctor(this, ...args) : new ctor(...args);
  225. this._services.set(ctor, service);
  226. return this;
  227. }
  228. /**
  229. * Добавить конструктор и связанный экземпляр.
  230. *
  231. * @param {*} ctor
  232. * @param {*} service
  233. * @return {this}
  234. */
  235. set(ctor, service) {
  236. if (!ctor || typeof ctor !== "function")
  237. throw new InvalidArgumentError(
  238. "The first argument of ServicesContainer.set must be a class constructor, but %v given.",
  239. ctor
  240. );
  241. if (!service || typeof service !== "object" || Array.isArray(service))
  242. throw new InvalidArgumentError(
  243. "The second argument of ServicesContainer.set must be an Object, but %v given.",
  244. service
  245. );
  246. this._services.set(ctor, service);
  247. return this;
  248. }
  249. };
  250. __name(_ServiceContainer, "ServiceContainer");
  251. var ServiceContainer = _ServiceContainer;
  252. // src/service.js
  253. var _Service = class _Service {
  254. /**
  255. * Container.
  256. *
  257. * @type {ServiceContainer}
  258. */
  259. container;
  260. /**
  261. * Constructor.
  262. *
  263. * @param {ServiceContainer|undefined} container
  264. */
  265. constructor(container = void 0) {
  266. this.container = container instanceof ServiceContainer ? container : new ServiceContainer();
  267. }
  268. /**
  269. * Получить существующий или новый экземпляр.
  270. *
  271. * @param {*} ctor
  272. * @param {*} args
  273. * @return {*}
  274. */
  275. getService(ctor, ...args) {
  276. return this.container.get(ctor, ...args);
  277. }
  278. /**
  279. * Проверка существования конструктора в контейнере.
  280. *
  281. * @param {*} ctor
  282. * @return {boolean}
  283. */
  284. hasService(ctor) {
  285. return this.container.has(ctor);
  286. }
  287. /**
  288. * Добавить конструктор в контейнер.
  289. *
  290. * @param {*} ctor
  291. * @param {*} args
  292. * @return {this}
  293. */
  294. addService(ctor, ...args) {
  295. this.container.add(ctor, ...args);
  296. return this;
  297. }
  298. /**
  299. * Добавить конструктор и создать экземпляр.
  300. *
  301. * @param {*} ctor
  302. * @param {*} args
  303. * @return {this}
  304. */
  305. useService(ctor, ...args) {
  306. this.container.use(ctor, ...args);
  307. return this;
  308. }
  309. /**
  310. * Добавить конструктор и связанный экземпляр.
  311. *
  312. * @param {*} ctor
  313. * @param {*} service
  314. * @return {this}
  315. */
  316. setService(ctor, service) {
  317. this.container.set(ctor, service);
  318. return this;
  319. }
  320. };
  321. __name(_Service, "Service");
  322. /**
  323. * Kind.
  324. *
  325. * @type {string}
  326. */
  327. __publicField(_Service, "kind", "Service");
  328. var Service = _Service;
  329. // Annotate the CommonJS export names for ESM import in node:
  330. 0 && (module.exports = {
  331. Service,
  332. ServiceContainer
  333. });