index.cjs 9.3 KB

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