UNPKG

4.55 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.SerializedGraph = void 0;
4const application_config_1 = require("../application-config");
5const external_context_creator_1 = require("../helpers/external-context-creator");
6const http_adapter_host_1 = require("../helpers/http-adapter-host");
7const inquirer_constants_1 = require("../injector/inquirer/inquirer-constants");
8const lazy_module_loader_1 = require("../injector/lazy-module-loader/lazy-module-loader");
9const module_ref_1 = require("../injector/module-ref");
10const modules_container_1 = require("../injector/modules-container");
11const request_constants_1 = require("../router/request/request-constants");
12const reflector_service_1 = require("../services/reflector.service");
13const deterministic_uuid_registry_1 = require("./deterministic-uuid-registry");
14class SerializedGraph {
15 constructor() {
16 this.nodes = new Map();
17 this.edges = new Map();
18 this.entrypoints = new Map();
19 this.extras = {
20 orphanedEnhancers: [],
21 attachedEnhancers: [],
22 };
23 this._status = 'complete';
24 }
25 set status(status) {
26 this._status = status;
27 }
28 set metadata(metadata) {
29 this._metadata = metadata;
30 }
31 insertNode(nodeDefinition) {
32 if (nodeDefinition.metadata.type === 'provider' &&
33 SerializedGraph.INTERNAL_PROVIDERS.includes(nodeDefinition.metadata.token)) {
34 nodeDefinition.metadata = {
35 ...nodeDefinition.metadata,
36 internal: true,
37 };
38 }
39 if (this.nodes.has(nodeDefinition.id)) {
40 return this.nodes.get(nodeDefinition.id);
41 }
42 this.nodes.set(nodeDefinition.id, nodeDefinition);
43 return nodeDefinition;
44 }
45 insertEdge(edgeDefinition) {
46 if (edgeDefinition.metadata.type === 'class-to-class' &&
47 (SerializedGraph.INTERNAL_PROVIDERS.includes(edgeDefinition.metadata.sourceClassToken) ||
48 SerializedGraph.INTERNAL_PROVIDERS.includes(edgeDefinition.metadata.targetClassToken))) {
49 edgeDefinition.metadata = {
50 ...edgeDefinition.metadata,
51 internal: true,
52 };
53 }
54 const id = edgeDefinition.id ?? this.generateUuidByEdgeDefinition(edgeDefinition);
55 const edge = {
56 ...edgeDefinition,
57 id,
58 };
59 this.edges.set(id, edge);
60 return edge;
61 }
62 insertEntrypoint(definition, parentId) {
63 if (this.entrypoints.has(parentId)) {
64 const existingCollection = this.entrypoints.get(parentId);
65 existingCollection.push(definition);
66 }
67 else {
68 this.entrypoints.set(parentId, [definition]);
69 }
70 }
71 insertOrphanedEnhancer(entry) {
72 this.extras.orphanedEnhancers.push(entry);
73 }
74 insertAttachedEnhancer(nodeId) {
75 this.extras.attachedEnhancers.push({
76 nodeId,
77 });
78 }
79 getNodeById(id) {
80 return this.nodes.get(id);
81 }
82 toJSON() {
83 const json = {
84 nodes: Object.fromEntries(this.nodes),
85 edges: Object.fromEntries(this.edges),
86 entrypoints: Object.fromEntries(this.entrypoints),
87 extras: this.extras,
88 };
89 if (this._status) {
90 json['status'] = this._status;
91 }
92 if (this._metadata) {
93 json['metadata'] = this._metadata;
94 }
95 return json;
96 }
97 toString() {
98 const replacer = (key, value) => {
99 if (typeof value === 'symbol') {
100 return value.toString();
101 }
102 return typeof value === 'function' ? value.name ?? 'Function' : value;
103 };
104 return JSON.stringify(this.toJSON(), replacer, 2);
105 }
106 generateUuidByEdgeDefinition(edgeDefinition) {
107 return deterministic_uuid_registry_1.DeterministicUuidRegistry.get(JSON.stringify(edgeDefinition));
108 }
109}
110exports.SerializedGraph = SerializedGraph;
111SerializedGraph.INTERNAL_PROVIDERS = [
112 application_config_1.ApplicationConfig,
113 module_ref_1.ModuleRef,
114 http_adapter_host_1.HttpAdapterHost,
115 lazy_module_loader_1.LazyModuleLoader,
116 external_context_creator_1.ExternalContextCreator,
117 modules_container_1.ModulesContainer,
118 reflector_service_1.Reflector,
119 SerializedGraph,
120 http_adapter_host_1.HttpAdapterHost.name,
121 reflector_service_1.Reflector.name,
122 request_constants_1.REQUEST,
123 inquirer_constants_1.INQUIRER,
124];