index.cjs 8.8 KB

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