1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const os = require("os");
|
4 | const path = require("path");
|
5 | const startsWith = require("lodash/startsWith");
|
6 | const structured_clone_1 = require("@hyurl/structured-clone");
|
7 | const isOwnKey_1 = require("@hyurl/utils/isOwnKey");
|
8 | const hash = require("string-hash");
|
9 | const WinPipe = "\\\\?\\pipe\\";
|
10 | exports.local = Symbol("local");
|
11 | exports.proxyRoot = Symbol("proxyRoot");
|
12 | exports.readyState = Symbol("readyState");
|
13 | function absPath(filename, withPipe) {
|
14 | if (!path.isAbsolute(filename)) {
|
15 | filename = path.resolve(process.cwd(), filename);
|
16 | }
|
17 | if (withPipe && os.platform() == "win32" && !startsWith(filename, WinPipe)) {
|
18 | filename = WinPipe + filename;
|
19 | }
|
20 | return filename;
|
21 | }
|
22 | exports.absPath = absPath;
|
23 | function set(target, prop, value, writable = false, enumerable = false) {
|
24 | Object.defineProperty(target, prop, {
|
25 | configurable: true,
|
26 | enumerable,
|
27 | writable,
|
28 | value
|
29 | });
|
30 | }
|
31 | exports.set = set;
|
32 | function getInstance(mod, forRemote = false) {
|
33 | let ins;
|
34 | let { ctor } = mod;
|
35 | if (ctor) {
|
36 | if (!forRemote) {
|
37 | if (typeof ctor.getInstance === "function") {
|
38 | ins = ctor.getInstance();
|
39 | }
|
40 | else {
|
41 | ins = mod.create();
|
42 | }
|
43 | }
|
44 | else {
|
45 | ins = Object.create(ctor.prototype);
|
46 | }
|
47 | }
|
48 | else {
|
49 | ins = mod.proto;
|
50 | }
|
51 | return ins;
|
52 | }
|
53 | exports.getInstance = getInstance;
|
54 | function mergeFnProperties(fn, origin) {
|
55 | set(fn, "proxified", true);
|
56 | set(fn, "name", origin.name);
|
57 | set(fn, "length", origin.length);
|
58 | set(fn, "toString", function toString() {
|
59 | return "[ModuleProxy] " + Function.prototype.toString.call(origin);
|
60 | }, true);
|
61 | return fn;
|
62 | }
|
63 | exports.mergeFnProperties = mergeFnProperties;
|
64 | function createRemoteInstance(mod, fnCreator) {
|
65 | return new Proxy(getInstance(mod, true), {
|
66 | get: (ins, prop) => {
|
67 | if (typeof prop === "symbol") {
|
68 | return ins[prop];
|
69 | }
|
70 | let type = typeof ins[prop];
|
71 | let isFn = type === "function";
|
72 | if (isFn && !ins[prop]["proxified"] && !isOwnKey_1.default(ins, prop)) {
|
73 | set(ins, prop, mergeFnProperties(fnCreator(prop), ins[prop]));
|
74 | }
|
75 | return isFn ? ins[prop] : (type === "undefined" ? undefined : null);
|
76 | },
|
77 | has: (ins, prop) => {
|
78 | return typeof prop === "symbol"
|
79 | ? (prop in ins)
|
80 | : typeof ins[prop] === "function";
|
81 | }
|
82 | });
|
83 | }
|
84 | exports.createRemoteInstance = createRemoteInstance;
|
85 | function humanizeDuration(duration) {
|
86 | let num;
|
87 | let unit;
|
88 | if (duration < 1000) {
|
89 | num = duration;
|
90 | unit = "millisecond";
|
91 | }
|
92 | else if (duration < 60000) {
|
93 | num = Math.round(duration / 1000);
|
94 | unit = "second";
|
95 | }
|
96 | else {
|
97 | num = Math.round(duration / 60000);
|
98 | unit = "minute";
|
99 | }
|
100 | if (num !== 1)
|
101 | unit += "s";
|
102 | return num + " " + unit;
|
103 | }
|
104 | exports.humanizeDuration = humanizeDuration;
|
105 | async function tryLifeCycleFunction(mod, fn, errorHandle = void 0) {
|
106 | let ins = mod();
|
107 | if (fn === "init") {
|
108 | if (typeof ins.init === "function") {
|
109 | ins[exports.readyState] = 1;
|
110 | if (errorHandle) {
|
111 | try {
|
112 | await ins.init();
|
113 | }
|
114 | catch (err) {
|
115 | errorHandle(err);
|
116 | }
|
117 | }
|
118 | else {
|
119 | await ins.init();
|
120 | }
|
121 | }
|
122 | ins[exports.readyState] = 2;
|
123 | }
|
124 | else if (fn === "destroy") {
|
125 | if (typeof ins.destroy === "function") {
|
126 | ins[exports.readyState] = 3;
|
127 | if (errorHandle) {
|
128 | try {
|
129 | await ins.destroy();
|
130 | }
|
131 | catch (err) {
|
132 | errorHandle(err);
|
133 | }
|
134 | }
|
135 | else {
|
136 | await ins.destroy();
|
137 | }
|
138 | }
|
139 | ins[exports.readyState] = 0;
|
140 | }
|
141 | }
|
142 | exports.tryLifeCycleFunction = tryLifeCycleFunction;
|
143 | function throwUnavailableError(name) {
|
144 | throw new ReferenceError(`Service ${name} is not available`);
|
145 | }
|
146 | exports.throwUnavailableError = throwUnavailableError;
|
147 | function getCodecOptions(codec) {
|
148 | switch (codec) {
|
149 | case "JSON":
|
150 | return {
|
151 | objectSerializer: JSON.stringify,
|
152 | objectDeserializer: JSON.parse
|
153 | };
|
154 | case "CLONE":
|
155 | return {
|
156 | objectSerializer: structured_clone_1.serialize,
|
157 | objectDeserializer: structured_clone_1.deserialize
|
158 | };
|
159 | case "FRON": {
|
160 | let FRON = require("fron");
|
161 | return {
|
162 | objectSerializer: FRON.stringify,
|
163 | objectDeserializer: FRON.parse
|
164 | };
|
165 | }
|
166 | case "BSON": {
|
167 | let BSON;
|
168 | try {
|
169 | let BSONExt = require("bson-ext");
|
170 | BSON = new BSONExt([
|
171 | BSONExt.Binary,
|
172 | BSONExt.Code,
|
173 | BSONExt.DBRef,
|
174 | BSONExt.Decimal128,
|
175 | BSONExt.Double,
|
176 | BSONExt.Int32,
|
177 | BSONExt.Long,
|
178 | BSONExt.Map,
|
179 | BSONExt.MaxKey,
|
180 | BSONExt.MinKey,
|
181 | BSONExt.ObjectId,
|
182 | BSONExt.BSONRegExp,
|
183 | BSONExt.Symbol,
|
184 | BSONExt.Timestamp
|
185 | ]);
|
186 | }
|
187 | catch (e) {
|
188 | try {
|
189 | BSON = require("bson");
|
190 | }
|
191 | catch (e) {
|
192 | throw new Error("Cannot find module 'bson' or 'bson-ext'");
|
193 | }
|
194 | }
|
195 | return {
|
196 | objectSerializer: BSON.serialize.bind(BSON),
|
197 | objectDeserializer: BSON.deserialize.bind(BSON),
|
198 | serializationStyle: "buffer"
|
199 | };
|
200 | }
|
201 | }
|
202 | }
|
203 | exports.getCodecOptions = getCodecOptions;
|
204 | function patchProperties(target, filename, loader, singletons) {
|
205 | set(target, "path", path.normalize(filename), false, true);
|
206 | set(target, "loader", loader);
|
207 | set(target, "singletons", singletons);
|
208 | set(target, "remoteSingletons", dict());
|
209 | set(target, "children", dict());
|
210 | }
|
211 | exports.patchProperties = patchProperties;
|
212 | function dict() {
|
213 | return Object.create(null);
|
214 | }
|
215 | exports.dict = dict;
|
216 | function evalRouteId(value) {
|
217 | let type = typeof value;
|
218 | switch (type) {
|
219 | case "number":
|
220 | case "boolean":
|
221 | return Number(value);
|
222 | case "string":
|
223 | case "symbol":
|
224 | case "bigint":
|
225 | return hash(String(value));
|
226 | case "function":
|
227 | return hash(String(value.name || value));
|
228 | case "object":
|
229 | case "undefined":
|
230 | if (value === null || value === undefined) {
|
231 | return 0;
|
232 | }
|
233 | else {
|
234 | return hash(formatObjectStructure(value));
|
235 | }
|
236 | }
|
237 | }
|
238 | exports.evalRouteId = evalRouteId;
|
239 | function formatObjectStructure(obj, memory = void 0) {
|
240 | memory || (memory = [obj]);
|
241 | let token = "{";
|
242 | Object.keys(obj).sort().forEach((key, i) => {
|
243 | if (i !== 0) {
|
244 | token += "," + key;
|
245 | }
|
246 | else {
|
247 | token += key;
|
248 | }
|
249 | try {
|
250 | if (obj[key] !== null && typeof obj[key] === "object") {
|
251 | if (!memory.includes(obj[key])) {
|
252 | memory.push(obj[key]);
|
253 | token += ":" + formatObjectStructure(obj[key], memory);
|
254 | }
|
255 | }
|
256 | }
|
257 | catch (e) { }
|
258 | });
|
259 | return token + "}";
|
260 | }
|
261 |
|
\ | No newline at end of file |