imean-service-engine 1.2.3 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mod.cjs +53 -23
- package/dist/mod.d.cts +22 -3
- package/dist/mod.d.ts +22 -3
- package/dist/mod.js +50 -23
- package/package.json +5 -2
package/dist/mod.cjs
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
3
|
var zod = require('zod');
|
4
|
+
var ejson4 = require('ejson');
|
5
|
+
var lruCache = require('lru-cache');
|
4
6
|
var nodeServer = require('@hono/node-server');
|
5
|
-
var ejson3 = require('ejson');
|
6
7
|
var etcd3 = require('etcd3');
|
7
8
|
var fs = require('fs-extra');
|
8
9
|
var hono = require('hono');
|
9
|
-
var lruCache = require('lru-cache');
|
10
10
|
var api = require('@opentelemetry/api');
|
11
11
|
var winston = require('winston');
|
12
12
|
var prettier = require('prettier');
|
@@ -17,7 +17,7 @@ var dayjs = require('dayjs');
|
|
17
17
|
|
18
18
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
19
19
|
|
20
|
-
var
|
20
|
+
var ejson4__default = /*#__PURE__*/_interopDefault(ejson4);
|
21
21
|
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
22
22
|
var winston__default = /*#__PURE__*/_interopDefault(winston);
|
23
23
|
var prettier__default = /*#__PURE__*/_interopDefault(prettier);
|
@@ -25,6 +25,38 @@ var crypto2__default = /*#__PURE__*/_interopDefault(crypto2);
|
|
25
25
|
var dayjs__default = /*#__PURE__*/_interopDefault(dayjs);
|
26
26
|
|
27
27
|
// mod.ts
|
28
|
+
var CacheAdapter = class {
|
29
|
+
};
|
30
|
+
var RedisCacheAdapter = class extends CacheAdapter {
|
31
|
+
constructor(redis) {
|
32
|
+
super();
|
33
|
+
this.redis = redis;
|
34
|
+
}
|
35
|
+
async get(key) {
|
36
|
+
const value = await this.redis.get(key);
|
37
|
+
return value ? ejson4__default.default.parse(value) : null;
|
38
|
+
}
|
39
|
+
async set(key, value, ttl) {
|
40
|
+
return this.redis.set(key, ejson4__default.default.stringify(value), "EX", ttl / 1e3);
|
41
|
+
}
|
42
|
+
};
|
43
|
+
var MemoryCacheAdapter = class extends CacheAdapter {
|
44
|
+
cache;
|
45
|
+
constructor(options) {
|
46
|
+
super();
|
47
|
+
this.cache = new lruCache.LRUCache({
|
48
|
+
max: 1e3,
|
49
|
+
ttl: 1e3 * 60 * 10,
|
50
|
+
...options
|
51
|
+
});
|
52
|
+
}
|
53
|
+
async get(key) {
|
54
|
+
return this.cache.get(key);
|
55
|
+
}
|
56
|
+
async set(key, value, ttl) {
|
57
|
+
this.cache.set(key, value, { ttl });
|
58
|
+
}
|
59
|
+
};
|
28
60
|
var ACTION_METADATA = Symbol("action:metadata");
|
29
61
|
function Action(options) {
|
30
62
|
options.params = options.params || [];
|
@@ -297,9 +329,6 @@ function getZodTypeString(schema, defaultOptional = false) {
|
|
297
329
|
}).join("; ");
|
298
330
|
return `{ ${props} }`;
|
299
331
|
}
|
300
|
-
case "ZodEnum": {
|
301
|
-
return def.values.map((opt) => `"${opt}"`).join(" | ");
|
302
|
-
}
|
303
332
|
case "ZodUnion": {
|
304
333
|
return def.options.map((opt) => processType(opt)).join(" | ");
|
305
334
|
}
|
@@ -328,8 +357,8 @@ function getZodTypeString(schema, defaultOptional = false) {
|
|
328
357
|
case "ZodUnknown": {
|
329
358
|
return "unknown";
|
330
359
|
}
|
331
|
-
case "ZodEnum
|
332
|
-
return def.values.map((opt) => `"${opt}"`).join(" | ");
|
360
|
+
case "ZodEnum": {
|
361
|
+
return "(" + def.values.map((opt) => `"${opt}"`).join(" | ") + ")";
|
333
362
|
}
|
334
363
|
case "ZodDefault": {
|
335
364
|
return processType(def.innerType);
|
@@ -455,7 +484,7 @@ var ActionHandler = class {
|
|
455
484
|
let args;
|
456
485
|
if (typeof req === "string") {
|
457
486
|
try {
|
458
|
-
args = Object.values(
|
487
|
+
args = Object.values(ejson4__default.default.parse(req));
|
459
488
|
} catch (error) {
|
460
489
|
throw new Error(`Invalid request body: ${error.message}`);
|
461
490
|
}
|
@@ -473,7 +502,7 @@ var ActionHandler = class {
|
|
473
502
|
}
|
474
503
|
});
|
475
504
|
}
|
476
|
-
const requestHash = hashText(
|
505
|
+
const requestHash = hashText(ejson4__default.default.stringify(args));
|
477
506
|
span.setAttribute("requestHash", requestHash);
|
478
507
|
return { args, requestHash };
|
479
508
|
} finally {
|
@@ -484,7 +513,7 @@ var ActionHandler = class {
|
|
484
513
|
const span = api.trace.getActiveSpan();
|
485
514
|
const { args, requestHash } = await this._validate(req);
|
486
515
|
const cacheHash = typeof this.metadata.cache === "function" ? this.metadata.cache(...args) : requestHash;
|
487
|
-
const cacheKey = `${this.moduleName}
|
516
|
+
const cacheKey = `${this.microservice.options.name}:cache:${this.moduleName}:${this.actionName}:${hashText(ejson4__default.default.stringify(cacheHash))}`;
|
488
517
|
span?.setAttribute("args", JSON.stringify(args));
|
489
518
|
if (!this.metadata.stream && this.metadata.cache && !this.microservice.options.disableCache) {
|
490
519
|
const cached = await this.microservice.cache.get(cacheKey);
|
@@ -539,7 +568,7 @@ var ActionHandler = class {
|
|
539
568
|
data: parsedResult,
|
540
569
|
expireAt: this.metadata.ttl ? now + this.metadata.ttl : void 0
|
541
570
|
},
|
542
|
-
|
571
|
+
this.metadata.ttl ?? 1e3
|
543
572
|
);
|
544
573
|
}
|
545
574
|
return parsedResult;
|
@@ -568,7 +597,7 @@ var WebSocketHandler = class {
|
|
568
597
|
this.sockets.add(ws);
|
569
598
|
}
|
570
599
|
async encodeMessage(message) {
|
571
|
-
const jsonStr =
|
600
|
+
const jsonStr = ejson4__default.default.stringify(message);
|
572
601
|
const data = new TextEncoder().encode(jsonStr);
|
573
602
|
const compressed = await brotli.compress(data, 6);
|
574
603
|
return compressed;
|
@@ -577,7 +606,7 @@ var WebSocketHandler = class {
|
|
577
606
|
try {
|
578
607
|
const decompressed = await brotli.decompress(new Uint8Array(data));
|
579
608
|
const jsonStr = new TextDecoder().decode(decompressed);
|
580
|
-
return
|
609
|
+
return ejson4__default.default.parse(jsonStr);
|
581
610
|
} catch (error) {
|
582
611
|
console.error("Error decoding message", error);
|
583
612
|
throw error;
|
@@ -638,7 +667,7 @@ var WebSocketHandler = class {
|
|
638
667
|
false
|
639
668
|
);
|
640
669
|
ws.send(
|
641
|
-
|
670
|
+
ejson4__default.default.stringify({
|
642
671
|
id: message.id,
|
643
672
|
success: false,
|
644
673
|
error: "Request timeout"
|
@@ -754,17 +783,15 @@ var Microservice = class {
|
|
754
783
|
this.options = {
|
755
784
|
prefix: "/api",
|
756
785
|
name: "microservice",
|
786
|
+
cacheAdapter: new MemoryCacheAdapter(),
|
757
787
|
...options
|
758
788
|
};
|
789
|
+
this.cache = this.options.cacheAdapter;
|
759
790
|
this.fetch = this.app.request;
|
760
791
|
this.waitingInitialization = this.initialize();
|
761
792
|
ServiceContext.service = this;
|
762
793
|
}
|
763
794
|
async initialize() {
|
764
|
-
this.cache = new lruCache.LRUCache({
|
765
|
-
max: 1e3,
|
766
|
-
ttl: 1e3 * 60 * 10
|
767
|
-
});
|
768
795
|
if (this.options.etcd) {
|
769
796
|
this.initEtcd(this.options.etcd);
|
770
797
|
}
|
@@ -1128,12 +1155,12 @@ Received SIGINT signal`);
|
|
1128
1155
|
try {
|
1129
1156
|
for await (const value of result) {
|
1130
1157
|
const response = { value, done: false };
|
1131
|
-
const chunk = encoder.encode(
|
1158
|
+
const chunk = encoder.encode(ejson4__default.default.stringify(response) + "\n");
|
1132
1159
|
controller.enqueue(chunk);
|
1133
1160
|
}
|
1134
1161
|
controller.enqueue(
|
1135
1162
|
encoder.encode(
|
1136
|
-
|
1163
|
+
ejson4__default.default.stringify({ done: true }) + "\n"
|
1137
1164
|
)
|
1138
1165
|
);
|
1139
1166
|
controller.close();
|
@@ -1143,7 +1170,7 @@ Received SIGINT signal`);
|
|
1143
1170
|
done: true
|
1144
1171
|
};
|
1145
1172
|
controller.enqueue(
|
1146
|
-
encoder.encode(
|
1173
|
+
encoder.encode(ejson4__default.default.stringify(response) + "\n")
|
1147
1174
|
);
|
1148
1175
|
controller.close();
|
1149
1176
|
}
|
@@ -1157,7 +1184,7 @@ Received SIGINT signal`);
|
|
1157
1184
|
}
|
1158
1185
|
});
|
1159
1186
|
}
|
1160
|
-
return ctx.text(
|
1187
|
+
return ctx.text(ejson4__default.default.stringify({ success: true, data: result }));
|
1161
1188
|
} catch (error) {
|
1162
1189
|
return ctx.json({ success: false, error: error.message });
|
1163
1190
|
}
|
@@ -1218,8 +1245,11 @@ Object.defineProperty(exports, "dayjs", {
|
|
1218
1245
|
get: function () { return dayjs__default.default; }
|
1219
1246
|
});
|
1220
1247
|
exports.Action = Action;
|
1248
|
+
exports.CacheAdapter = CacheAdapter;
|
1249
|
+
exports.MemoryCacheAdapter = MemoryCacheAdapter;
|
1221
1250
|
exports.Microservice = Microservice;
|
1222
1251
|
exports.Module = Module;
|
1252
|
+
exports.RedisCacheAdapter = RedisCacheAdapter;
|
1223
1253
|
exports.Schedule = Schedule;
|
1224
1254
|
exports.ScheduleMode = ScheduleMode;
|
1225
1255
|
exports.ServiceContext = ServiceContext;
|
package/dist/mod.d.cts
CHANGED
@@ -1,11 +1,29 @@
|
|
1
1
|
import { z } from 'zod';
|
2
2
|
export * from 'zod';
|
3
|
+
import { Redis, Cluster } from 'ioredis';
|
4
|
+
import { LRUCache } from 'lru-cache';
|
3
5
|
import { Lease, Etcd3 } from 'etcd3';
|
4
6
|
import { Hono } from 'hono';
|
5
|
-
import { LRUCache } from 'lru-cache';
|
6
7
|
export { default as dayjs } from 'dayjs';
|
7
8
|
import winston from 'winston';
|
8
9
|
|
10
|
+
declare abstract class CacheAdapter {
|
11
|
+
abstract get(key: string): Promise<any>;
|
12
|
+
abstract set(key: string, value: any, ttl: number): Promise<any>;
|
13
|
+
}
|
14
|
+
declare class RedisCacheAdapter extends CacheAdapter {
|
15
|
+
private readonly redis;
|
16
|
+
constructor(redis: Redis | Cluster);
|
17
|
+
get(key: string): Promise<any>;
|
18
|
+
set(key: string, value: string, ttl: number): Promise<"OK">;
|
19
|
+
}
|
20
|
+
declare class MemoryCacheAdapter extends CacheAdapter {
|
21
|
+
private cache;
|
22
|
+
constructor(options?: LRUCache.Options<string, any, any>);
|
23
|
+
get(key: string): Promise<any>;
|
24
|
+
set(key: string, value: any, ttl: number): Promise<void>;
|
25
|
+
}
|
26
|
+
|
9
27
|
type CacheFn = (...args: any[]) => any;
|
10
28
|
interface ActionOptions {
|
11
29
|
params?: z.ZodType<any>[];
|
@@ -131,6 +149,7 @@ interface MicroserviceOptions {
|
|
131
149
|
enabled?: boolean;
|
132
150
|
timeout?: number;
|
133
151
|
};
|
152
|
+
cacheAdapter?: CacheAdapter;
|
134
153
|
}
|
135
154
|
interface ModuleInfo extends ModuleOptions {
|
136
155
|
actions: Record<string, ActionMetadata>;
|
@@ -177,7 +196,7 @@ declare class Microservice {
|
|
177
196
|
modules: Map<string, ModuleInfo>;
|
178
197
|
readonly fetch: typeof fetch;
|
179
198
|
options: MicroserviceOptions;
|
180
|
-
cache:
|
199
|
+
cache: CacheAdapter;
|
181
200
|
serviceId: string;
|
182
201
|
constructor(options: MicroserviceOptions);
|
183
202
|
private initialize;
|
@@ -259,4 +278,4 @@ declare function Schedule(options: ScheduleOptions): Function;
|
|
259
278
|
|
260
279
|
declare const logger: winston.Logger;
|
261
280
|
|
262
|
-
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, type CacheFn, type EtcdConfig, type EventServiceInfo, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
|
281
|
+
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, CacheAdapter, type CacheFn, type EtcdConfig, type EventServiceInfo, MemoryCacheAdapter, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, RedisCacheAdapter, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
|
package/dist/mod.d.ts
CHANGED
@@ -1,11 +1,29 @@
|
|
1
1
|
import { z } from 'zod';
|
2
2
|
export * from 'zod';
|
3
|
+
import { Redis, Cluster } from 'ioredis';
|
4
|
+
import { LRUCache } from 'lru-cache';
|
3
5
|
import { Lease, Etcd3 } from 'etcd3';
|
4
6
|
import { Hono } from 'hono';
|
5
|
-
import { LRUCache } from 'lru-cache';
|
6
7
|
export { default as dayjs } from 'dayjs';
|
7
8
|
import winston from 'winston';
|
8
9
|
|
10
|
+
declare abstract class CacheAdapter {
|
11
|
+
abstract get(key: string): Promise<any>;
|
12
|
+
abstract set(key: string, value: any, ttl: number): Promise<any>;
|
13
|
+
}
|
14
|
+
declare class RedisCacheAdapter extends CacheAdapter {
|
15
|
+
private readonly redis;
|
16
|
+
constructor(redis: Redis | Cluster);
|
17
|
+
get(key: string): Promise<any>;
|
18
|
+
set(key: string, value: string, ttl: number): Promise<"OK">;
|
19
|
+
}
|
20
|
+
declare class MemoryCacheAdapter extends CacheAdapter {
|
21
|
+
private cache;
|
22
|
+
constructor(options?: LRUCache.Options<string, any, any>);
|
23
|
+
get(key: string): Promise<any>;
|
24
|
+
set(key: string, value: any, ttl: number): Promise<void>;
|
25
|
+
}
|
26
|
+
|
9
27
|
type CacheFn = (...args: any[]) => any;
|
10
28
|
interface ActionOptions {
|
11
29
|
params?: z.ZodType<any>[];
|
@@ -131,6 +149,7 @@ interface MicroserviceOptions {
|
|
131
149
|
enabled?: boolean;
|
132
150
|
timeout?: number;
|
133
151
|
};
|
152
|
+
cacheAdapter?: CacheAdapter;
|
134
153
|
}
|
135
154
|
interface ModuleInfo extends ModuleOptions {
|
136
155
|
actions: Record<string, ActionMetadata>;
|
@@ -177,7 +196,7 @@ declare class Microservice {
|
|
177
196
|
modules: Map<string, ModuleInfo>;
|
178
197
|
readonly fetch: typeof fetch;
|
179
198
|
options: MicroserviceOptions;
|
180
|
-
cache:
|
199
|
+
cache: CacheAdapter;
|
181
200
|
serviceId: string;
|
182
201
|
constructor(options: MicroserviceOptions);
|
183
202
|
private initialize;
|
@@ -259,4 +278,4 @@ declare function Schedule(options: ScheduleOptions): Function;
|
|
259
278
|
|
260
279
|
declare const logger: winston.Logger;
|
261
280
|
|
262
|
-
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, type CacheFn, type EtcdConfig, type EventServiceInfo, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
|
281
|
+
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, CacheAdapter, type CacheFn, type EtcdConfig, type EventServiceInfo, MemoryCacheAdapter, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, RedisCacheAdapter, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
|
package/dist/mod.js
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
import { z } from 'zod';
|
2
2
|
export * from 'zod';
|
3
|
+
import ejson4 from 'ejson';
|
4
|
+
import { LRUCache } from 'lru-cache';
|
3
5
|
import { serve } from '@hono/node-server';
|
4
|
-
import ejson3 from 'ejson';
|
5
6
|
import { Etcd3 } from 'etcd3';
|
6
7
|
import fs from 'fs-extra';
|
7
8
|
import { Hono } from 'hono';
|
8
|
-
import { LRUCache } from 'lru-cache';
|
9
9
|
import { trace, SpanStatusCode } from '@opentelemetry/api';
|
10
10
|
import winston, { format } from 'winston';
|
11
11
|
import prettier from 'prettier';
|
@@ -16,6 +16,38 @@ import { createNodeWebSocket } from '@hono/node-ws';
|
|
16
16
|
export { default as dayjs } from 'dayjs';
|
17
17
|
|
18
18
|
// mod.ts
|
19
|
+
var CacheAdapter = class {
|
20
|
+
};
|
21
|
+
var RedisCacheAdapter = class extends CacheAdapter {
|
22
|
+
constructor(redis) {
|
23
|
+
super();
|
24
|
+
this.redis = redis;
|
25
|
+
}
|
26
|
+
async get(key) {
|
27
|
+
const value = await this.redis.get(key);
|
28
|
+
return value ? ejson4.parse(value) : null;
|
29
|
+
}
|
30
|
+
async set(key, value, ttl) {
|
31
|
+
return this.redis.set(key, ejson4.stringify(value), "EX", ttl / 1e3);
|
32
|
+
}
|
33
|
+
};
|
34
|
+
var MemoryCacheAdapter = class extends CacheAdapter {
|
35
|
+
cache;
|
36
|
+
constructor(options) {
|
37
|
+
super();
|
38
|
+
this.cache = new LRUCache({
|
39
|
+
max: 1e3,
|
40
|
+
ttl: 1e3 * 60 * 10,
|
41
|
+
...options
|
42
|
+
});
|
43
|
+
}
|
44
|
+
async get(key) {
|
45
|
+
return this.cache.get(key);
|
46
|
+
}
|
47
|
+
async set(key, value, ttl) {
|
48
|
+
this.cache.set(key, value, { ttl });
|
49
|
+
}
|
50
|
+
};
|
19
51
|
var ACTION_METADATA = Symbol("action:metadata");
|
20
52
|
function Action(options) {
|
21
53
|
options.params = options.params || [];
|
@@ -288,9 +320,6 @@ function getZodTypeString(schema, defaultOptional = false) {
|
|
288
320
|
}).join("; ");
|
289
321
|
return `{ ${props} }`;
|
290
322
|
}
|
291
|
-
case "ZodEnum": {
|
292
|
-
return def.values.map((opt) => `"${opt}"`).join(" | ");
|
293
|
-
}
|
294
323
|
case "ZodUnion": {
|
295
324
|
return def.options.map((opt) => processType(opt)).join(" | ");
|
296
325
|
}
|
@@ -319,8 +348,8 @@ function getZodTypeString(schema, defaultOptional = false) {
|
|
319
348
|
case "ZodUnknown": {
|
320
349
|
return "unknown";
|
321
350
|
}
|
322
|
-
case "ZodEnum
|
323
|
-
return def.values.map((opt) => `"${opt}"`).join(" | ");
|
351
|
+
case "ZodEnum": {
|
352
|
+
return "(" + def.values.map((opt) => `"${opt}"`).join(" | ") + ")";
|
324
353
|
}
|
325
354
|
case "ZodDefault": {
|
326
355
|
return processType(def.innerType);
|
@@ -446,7 +475,7 @@ var ActionHandler = class {
|
|
446
475
|
let args;
|
447
476
|
if (typeof req === "string") {
|
448
477
|
try {
|
449
|
-
args = Object.values(
|
478
|
+
args = Object.values(ejson4.parse(req));
|
450
479
|
} catch (error) {
|
451
480
|
throw new Error(`Invalid request body: ${error.message}`);
|
452
481
|
}
|
@@ -464,7 +493,7 @@ var ActionHandler = class {
|
|
464
493
|
}
|
465
494
|
});
|
466
495
|
}
|
467
|
-
const requestHash = hashText(
|
496
|
+
const requestHash = hashText(ejson4.stringify(args));
|
468
497
|
span.setAttribute("requestHash", requestHash);
|
469
498
|
return { args, requestHash };
|
470
499
|
} finally {
|
@@ -475,7 +504,7 @@ var ActionHandler = class {
|
|
475
504
|
const span = trace.getActiveSpan();
|
476
505
|
const { args, requestHash } = await this._validate(req);
|
477
506
|
const cacheHash = typeof this.metadata.cache === "function" ? this.metadata.cache(...args) : requestHash;
|
478
|
-
const cacheKey = `${this.moduleName}
|
507
|
+
const cacheKey = `${this.microservice.options.name}:cache:${this.moduleName}:${this.actionName}:${hashText(ejson4.stringify(cacheHash))}`;
|
479
508
|
span?.setAttribute("args", JSON.stringify(args));
|
480
509
|
if (!this.metadata.stream && this.metadata.cache && !this.microservice.options.disableCache) {
|
481
510
|
const cached = await this.microservice.cache.get(cacheKey);
|
@@ -530,7 +559,7 @@ var ActionHandler = class {
|
|
530
559
|
data: parsedResult,
|
531
560
|
expireAt: this.metadata.ttl ? now + this.metadata.ttl : void 0
|
532
561
|
},
|
533
|
-
|
562
|
+
this.metadata.ttl ?? 1e3
|
534
563
|
);
|
535
564
|
}
|
536
565
|
return parsedResult;
|
@@ -559,7 +588,7 @@ var WebSocketHandler = class {
|
|
559
588
|
this.sockets.add(ws);
|
560
589
|
}
|
561
590
|
async encodeMessage(message) {
|
562
|
-
const jsonStr =
|
591
|
+
const jsonStr = ejson4.stringify(message);
|
563
592
|
const data = new TextEncoder().encode(jsonStr);
|
564
593
|
const compressed = await brotli.compress(data, 6);
|
565
594
|
return compressed;
|
@@ -568,7 +597,7 @@ var WebSocketHandler = class {
|
|
568
597
|
try {
|
569
598
|
const decompressed = await brotli.decompress(new Uint8Array(data));
|
570
599
|
const jsonStr = new TextDecoder().decode(decompressed);
|
571
|
-
return
|
600
|
+
return ejson4.parse(jsonStr);
|
572
601
|
} catch (error) {
|
573
602
|
console.error("Error decoding message", error);
|
574
603
|
throw error;
|
@@ -629,7 +658,7 @@ var WebSocketHandler = class {
|
|
629
658
|
false
|
630
659
|
);
|
631
660
|
ws.send(
|
632
|
-
|
661
|
+
ejson4.stringify({
|
633
662
|
id: message.id,
|
634
663
|
success: false,
|
635
664
|
error: "Request timeout"
|
@@ -745,17 +774,15 @@ var Microservice = class {
|
|
745
774
|
this.options = {
|
746
775
|
prefix: "/api",
|
747
776
|
name: "microservice",
|
777
|
+
cacheAdapter: new MemoryCacheAdapter(),
|
748
778
|
...options
|
749
779
|
};
|
780
|
+
this.cache = this.options.cacheAdapter;
|
750
781
|
this.fetch = this.app.request;
|
751
782
|
this.waitingInitialization = this.initialize();
|
752
783
|
ServiceContext.service = this;
|
753
784
|
}
|
754
785
|
async initialize() {
|
755
|
-
this.cache = new LRUCache({
|
756
|
-
max: 1e3,
|
757
|
-
ttl: 1e3 * 60 * 10
|
758
|
-
});
|
759
786
|
if (this.options.etcd) {
|
760
787
|
this.initEtcd(this.options.etcd);
|
761
788
|
}
|
@@ -1119,12 +1146,12 @@ Received SIGINT signal`);
|
|
1119
1146
|
try {
|
1120
1147
|
for await (const value of result) {
|
1121
1148
|
const response = { value, done: false };
|
1122
|
-
const chunk = encoder.encode(
|
1149
|
+
const chunk = encoder.encode(ejson4.stringify(response) + "\n");
|
1123
1150
|
controller.enqueue(chunk);
|
1124
1151
|
}
|
1125
1152
|
controller.enqueue(
|
1126
1153
|
encoder.encode(
|
1127
|
-
|
1154
|
+
ejson4.stringify({ done: true }) + "\n"
|
1128
1155
|
)
|
1129
1156
|
);
|
1130
1157
|
controller.close();
|
@@ -1134,7 +1161,7 @@ Received SIGINT signal`);
|
|
1134
1161
|
done: true
|
1135
1162
|
};
|
1136
1163
|
controller.enqueue(
|
1137
|
-
encoder.encode(
|
1164
|
+
encoder.encode(ejson4.stringify(response) + "\n")
|
1138
1165
|
);
|
1139
1166
|
controller.close();
|
1140
1167
|
}
|
@@ -1148,7 +1175,7 @@ Received SIGINT signal`);
|
|
1148
1175
|
}
|
1149
1176
|
});
|
1150
1177
|
}
|
1151
|
-
return ctx.text(
|
1178
|
+
return ctx.text(ejson4.stringify({ success: true, data: result }));
|
1152
1179
|
} catch (error) {
|
1153
1180
|
return ctx.json({ success: false, error: error.message });
|
1154
1181
|
}
|
@@ -1204,4 +1231,4 @@ async function startCheck(checkers, pass) {
|
|
1204
1231
|
if (pass) await pass();
|
1205
1232
|
}
|
1206
1233
|
|
1207
|
-
export { Action, Microservice, Module, Schedule, ScheduleMode, ServiceContext, logger_default as logger, startCheck };
|
1234
|
+
export { Action, CacheAdapter, MemoryCacheAdapter, Microservice, Module, RedisCacheAdapter, Schedule, ScheduleMode, ServiceContext, logger_default as logger, startCheck };
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "imean-service-engine",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.3.0",
|
4
4
|
"description": "microservice engine",
|
5
5
|
"keywords": [
|
6
6
|
"microservice",
|
@@ -50,7 +50,8 @@
|
|
50
50
|
"zod": "^3.24.1"
|
51
51
|
},
|
52
52
|
"peerDependencies": {
|
53
|
-
"@opentelemetry/api": "^1.x"
|
53
|
+
"@opentelemetry/api": "^1.x",
|
54
|
+
"ioredis": "^5.6.0"
|
54
55
|
},
|
55
56
|
"devDependencies": {
|
56
57
|
"@opentelemetry/auto-instrumentations-node": "^0.55.3",
|
@@ -65,9 +66,11 @@
|
|
65
66
|
"@opentelemetry/winston-transport": "^0.10.0",
|
66
67
|
"@types/ejson": "^2.2.2",
|
67
68
|
"@types/fs-extra": "^11.0.4",
|
69
|
+
"@types/ioredis-mock": "^8.2.5",
|
68
70
|
"@types/node": "^20.0.0",
|
69
71
|
"@vitest/coverage-v8": "^3.0.4",
|
70
72
|
"imean-service-client": "^1.5.0",
|
73
|
+
"ioredis-mock": "^8.9.0",
|
71
74
|
"opentelemetry-instrumentation-fetch-node": "^1.2.3",
|
72
75
|
"tslib": "^2.8.1",
|
73
76
|
"tsup": "^8.0.1",
|