imean-service-engine 1.2.2 → 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 +55 -24
- package/dist/mod.d.cts +25 -5
- package/dist/mod.d.ts +25 -5
- package/dist/mod.js +52 -24
- 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 {
|
@@ -483,8 +512,10 @@ var ActionHandler = class {
|
|
483
512
|
async _handle(req) {
|
484
513
|
const span = api.trace.getActiveSpan();
|
485
514
|
const { args, requestHash } = await this._validate(req);
|
515
|
+
const cacheHash = typeof this.metadata.cache === "function" ? this.metadata.cache(...args) : requestHash;
|
516
|
+
const cacheKey = `${this.microservice.options.name}:cache:${this.moduleName}:${this.actionName}:${hashText(ejson4__default.default.stringify(cacheHash))}`;
|
517
|
+
span?.setAttribute("args", JSON.stringify(args));
|
486
518
|
if (!this.metadata.stream && this.metadata.cache && !this.microservice.options.disableCache) {
|
487
|
-
const cacheKey = `${this.moduleName}.${this.actionName}.${requestHash}`;
|
488
519
|
const cached = await this.microservice.cache.get(cacheKey);
|
489
520
|
const now = Date.now();
|
490
521
|
if (cached !== null && (!this.metadata.ttl || cached?.expireAt > now)) {
|
@@ -530,7 +561,6 @@ var ActionHandler = class {
|
|
530
561
|
}
|
531
562
|
}
|
532
563
|
if (this.metadata.cache && !this.microservice.options.disableCache) {
|
533
|
-
const cacheKey = `${this.moduleName}.${this.actionName}.${requestHash}`;
|
534
564
|
const now = Date.now();
|
535
565
|
this.microservice.cache.set(
|
536
566
|
cacheKey,
|
@@ -538,7 +568,7 @@ var ActionHandler = class {
|
|
538
568
|
data: parsedResult,
|
539
569
|
expireAt: this.metadata.ttl ? now + this.metadata.ttl : void 0
|
540
570
|
},
|
541
|
-
|
571
|
+
this.metadata.ttl ?? 1e3
|
542
572
|
);
|
543
573
|
}
|
544
574
|
return parsedResult;
|
@@ -567,7 +597,7 @@ var WebSocketHandler = class {
|
|
567
597
|
this.sockets.add(ws);
|
568
598
|
}
|
569
599
|
async encodeMessage(message) {
|
570
|
-
const jsonStr =
|
600
|
+
const jsonStr = ejson4__default.default.stringify(message);
|
571
601
|
const data = new TextEncoder().encode(jsonStr);
|
572
602
|
const compressed = await brotli.compress(data, 6);
|
573
603
|
return compressed;
|
@@ -576,7 +606,7 @@ var WebSocketHandler = class {
|
|
576
606
|
try {
|
577
607
|
const decompressed = await brotli.decompress(new Uint8Array(data));
|
578
608
|
const jsonStr = new TextDecoder().decode(decompressed);
|
579
|
-
return
|
609
|
+
return ejson4__default.default.parse(jsonStr);
|
580
610
|
} catch (error) {
|
581
611
|
console.error("Error decoding message", error);
|
582
612
|
throw error;
|
@@ -637,7 +667,7 @@ var WebSocketHandler = class {
|
|
637
667
|
false
|
638
668
|
);
|
639
669
|
ws.send(
|
640
|
-
|
670
|
+
ejson4__default.default.stringify({
|
641
671
|
id: message.id,
|
642
672
|
success: false,
|
643
673
|
error: "Request timeout"
|
@@ -753,17 +783,15 @@ var Microservice = class {
|
|
753
783
|
this.options = {
|
754
784
|
prefix: "/api",
|
755
785
|
name: "microservice",
|
786
|
+
cacheAdapter: new MemoryCacheAdapter(),
|
756
787
|
...options
|
757
788
|
};
|
789
|
+
this.cache = this.options.cacheAdapter;
|
758
790
|
this.fetch = this.app.request;
|
759
791
|
this.waitingInitialization = this.initialize();
|
760
792
|
ServiceContext.service = this;
|
761
793
|
}
|
762
794
|
async initialize() {
|
763
|
-
this.cache = new lruCache.LRUCache({
|
764
|
-
max: 1e3,
|
765
|
-
ttl: 1e3 * 60 * 10
|
766
|
-
});
|
767
795
|
if (this.options.etcd) {
|
768
796
|
this.initEtcd(this.options.etcd);
|
769
797
|
}
|
@@ -1127,12 +1155,12 @@ Received SIGINT signal`);
|
|
1127
1155
|
try {
|
1128
1156
|
for await (const value of result) {
|
1129
1157
|
const response = { value, done: false };
|
1130
|
-
const chunk = encoder.encode(
|
1158
|
+
const chunk = encoder.encode(ejson4__default.default.stringify(response) + "\n");
|
1131
1159
|
controller.enqueue(chunk);
|
1132
1160
|
}
|
1133
1161
|
controller.enqueue(
|
1134
1162
|
encoder.encode(
|
1135
|
-
|
1163
|
+
ejson4__default.default.stringify({ done: true }) + "\n"
|
1136
1164
|
)
|
1137
1165
|
);
|
1138
1166
|
controller.close();
|
@@ -1142,7 +1170,7 @@ Received SIGINT signal`);
|
|
1142
1170
|
done: true
|
1143
1171
|
};
|
1144
1172
|
controller.enqueue(
|
1145
|
-
encoder.encode(
|
1173
|
+
encoder.encode(ejson4__default.default.stringify(response) + "\n")
|
1146
1174
|
);
|
1147
1175
|
controller.close();
|
1148
1176
|
}
|
@@ -1156,7 +1184,7 @@ Received SIGINT signal`);
|
|
1156
1184
|
}
|
1157
1185
|
});
|
1158
1186
|
}
|
1159
|
-
return ctx.text(
|
1187
|
+
return ctx.text(ejson4__default.default.stringify({ success: true, data: result }));
|
1160
1188
|
} catch (error) {
|
1161
1189
|
return ctx.json({ success: false, error: error.message });
|
1162
1190
|
}
|
@@ -1217,8 +1245,11 @@ Object.defineProperty(exports, "dayjs", {
|
|
1217
1245
|
get: function () { return dayjs__default.default; }
|
1218
1246
|
});
|
1219
1247
|
exports.Action = Action;
|
1248
|
+
exports.CacheAdapter = CacheAdapter;
|
1249
|
+
exports.MemoryCacheAdapter = MemoryCacheAdapter;
|
1220
1250
|
exports.Microservice = Microservice;
|
1221
1251
|
exports.Module = Module;
|
1252
|
+
exports.RedisCacheAdapter = RedisCacheAdapter;
|
1222
1253
|
exports.Schedule = Schedule;
|
1223
1254
|
exports.ScheduleMode = ScheduleMode;
|
1224
1255
|
exports.ServiceContext = ServiceContext;
|
package/dist/mod.d.cts
CHANGED
@@ -1,25 +1,44 @@
|
|
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
|
+
|
27
|
+
type CacheFn = (...args: any[]) => any;
|
9
28
|
interface ActionOptions {
|
10
29
|
params?: z.ZodType<any>[];
|
11
30
|
returns?: z.ZodType<any>;
|
12
31
|
idempotence?: boolean;
|
13
32
|
description?: string;
|
14
33
|
printError?: boolean;
|
15
|
-
cache?: boolean;
|
34
|
+
cache?: boolean | CacheFn;
|
16
35
|
ttl?: number;
|
17
36
|
stream?: boolean;
|
18
37
|
}
|
19
38
|
interface ActionMetadata extends ActionOptions {
|
20
39
|
name: string;
|
21
40
|
idempotence: boolean;
|
22
|
-
cache?: boolean;
|
41
|
+
cache?: boolean | CacheFn;
|
23
42
|
ttl?: number;
|
24
43
|
stream?: boolean;
|
25
44
|
}
|
@@ -130,6 +149,7 @@ interface MicroserviceOptions {
|
|
130
149
|
enabled?: boolean;
|
131
150
|
timeout?: number;
|
132
151
|
};
|
152
|
+
cacheAdapter?: CacheAdapter;
|
133
153
|
}
|
134
154
|
interface ModuleInfo extends ModuleOptions {
|
135
155
|
actions: Record<string, ActionMetadata>;
|
@@ -176,7 +196,7 @@ declare class Microservice {
|
|
176
196
|
modules: Map<string, ModuleInfo>;
|
177
197
|
readonly fetch: typeof fetch;
|
178
198
|
options: MicroserviceOptions;
|
179
|
-
cache:
|
199
|
+
cache: CacheAdapter;
|
180
200
|
serviceId: string;
|
181
201
|
constructor(options: MicroserviceOptions);
|
182
202
|
private initialize;
|
@@ -258,4 +278,4 @@ declare function Schedule(options: ScheduleOptions): Function;
|
|
258
278
|
|
259
279
|
declare const logger: winston.Logger;
|
260
280
|
|
261
|
-
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, 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,25 +1,44 @@
|
|
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
|
+
|
27
|
+
type CacheFn = (...args: any[]) => any;
|
9
28
|
interface ActionOptions {
|
10
29
|
params?: z.ZodType<any>[];
|
11
30
|
returns?: z.ZodType<any>;
|
12
31
|
idempotence?: boolean;
|
13
32
|
description?: string;
|
14
33
|
printError?: boolean;
|
15
|
-
cache?: boolean;
|
34
|
+
cache?: boolean | CacheFn;
|
16
35
|
ttl?: number;
|
17
36
|
stream?: boolean;
|
18
37
|
}
|
19
38
|
interface ActionMetadata extends ActionOptions {
|
20
39
|
name: string;
|
21
40
|
idempotence: boolean;
|
22
|
-
cache?: boolean;
|
41
|
+
cache?: boolean | CacheFn;
|
23
42
|
ttl?: number;
|
24
43
|
stream?: boolean;
|
25
44
|
}
|
@@ -130,6 +149,7 @@ interface MicroserviceOptions {
|
|
130
149
|
enabled?: boolean;
|
131
150
|
timeout?: number;
|
132
151
|
};
|
152
|
+
cacheAdapter?: CacheAdapter;
|
133
153
|
}
|
134
154
|
interface ModuleInfo extends ModuleOptions {
|
135
155
|
actions: Record<string, ActionMetadata>;
|
@@ -176,7 +196,7 @@ declare class Microservice {
|
|
176
196
|
modules: Map<string, ModuleInfo>;
|
177
197
|
readonly fetch: typeof fetch;
|
178
198
|
options: MicroserviceOptions;
|
179
|
-
cache:
|
199
|
+
cache: CacheAdapter;
|
180
200
|
serviceId: string;
|
181
201
|
constructor(options: MicroserviceOptions);
|
182
202
|
private initialize;
|
@@ -258,4 +278,4 @@ declare function Schedule(options: ScheduleOptions): Function;
|
|
258
278
|
|
259
279
|
declare const logger: winston.Logger;
|
260
280
|
|
261
|
-
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, 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 {
|
@@ -474,8 +503,10 @@ var ActionHandler = class {
|
|
474
503
|
async _handle(req) {
|
475
504
|
const span = trace.getActiveSpan();
|
476
505
|
const { args, requestHash } = await this._validate(req);
|
506
|
+
const cacheHash = typeof this.metadata.cache === "function" ? this.metadata.cache(...args) : requestHash;
|
507
|
+
const cacheKey = `${this.microservice.options.name}:cache:${this.moduleName}:${this.actionName}:${hashText(ejson4.stringify(cacheHash))}`;
|
508
|
+
span?.setAttribute("args", JSON.stringify(args));
|
477
509
|
if (!this.metadata.stream && this.metadata.cache && !this.microservice.options.disableCache) {
|
478
|
-
const cacheKey = `${this.moduleName}.${this.actionName}.${requestHash}`;
|
479
510
|
const cached = await this.microservice.cache.get(cacheKey);
|
480
511
|
const now = Date.now();
|
481
512
|
if (cached !== null && (!this.metadata.ttl || cached?.expireAt > now)) {
|
@@ -521,7 +552,6 @@ var ActionHandler = class {
|
|
521
552
|
}
|
522
553
|
}
|
523
554
|
if (this.metadata.cache && !this.microservice.options.disableCache) {
|
524
|
-
const cacheKey = `${this.moduleName}.${this.actionName}.${requestHash}`;
|
525
555
|
const now = Date.now();
|
526
556
|
this.microservice.cache.set(
|
527
557
|
cacheKey,
|
@@ -529,7 +559,7 @@ var ActionHandler = class {
|
|
529
559
|
data: parsedResult,
|
530
560
|
expireAt: this.metadata.ttl ? now + this.metadata.ttl : void 0
|
531
561
|
},
|
532
|
-
|
562
|
+
this.metadata.ttl ?? 1e3
|
533
563
|
);
|
534
564
|
}
|
535
565
|
return parsedResult;
|
@@ -558,7 +588,7 @@ var WebSocketHandler = class {
|
|
558
588
|
this.sockets.add(ws);
|
559
589
|
}
|
560
590
|
async encodeMessage(message) {
|
561
|
-
const jsonStr =
|
591
|
+
const jsonStr = ejson4.stringify(message);
|
562
592
|
const data = new TextEncoder().encode(jsonStr);
|
563
593
|
const compressed = await brotli.compress(data, 6);
|
564
594
|
return compressed;
|
@@ -567,7 +597,7 @@ var WebSocketHandler = class {
|
|
567
597
|
try {
|
568
598
|
const decompressed = await brotli.decompress(new Uint8Array(data));
|
569
599
|
const jsonStr = new TextDecoder().decode(decompressed);
|
570
|
-
return
|
600
|
+
return ejson4.parse(jsonStr);
|
571
601
|
} catch (error) {
|
572
602
|
console.error("Error decoding message", error);
|
573
603
|
throw error;
|
@@ -628,7 +658,7 @@ var WebSocketHandler = class {
|
|
628
658
|
false
|
629
659
|
);
|
630
660
|
ws.send(
|
631
|
-
|
661
|
+
ejson4.stringify({
|
632
662
|
id: message.id,
|
633
663
|
success: false,
|
634
664
|
error: "Request timeout"
|
@@ -744,17 +774,15 @@ var Microservice = class {
|
|
744
774
|
this.options = {
|
745
775
|
prefix: "/api",
|
746
776
|
name: "microservice",
|
777
|
+
cacheAdapter: new MemoryCacheAdapter(),
|
747
778
|
...options
|
748
779
|
};
|
780
|
+
this.cache = this.options.cacheAdapter;
|
749
781
|
this.fetch = this.app.request;
|
750
782
|
this.waitingInitialization = this.initialize();
|
751
783
|
ServiceContext.service = this;
|
752
784
|
}
|
753
785
|
async initialize() {
|
754
|
-
this.cache = new LRUCache({
|
755
|
-
max: 1e3,
|
756
|
-
ttl: 1e3 * 60 * 10
|
757
|
-
});
|
758
786
|
if (this.options.etcd) {
|
759
787
|
this.initEtcd(this.options.etcd);
|
760
788
|
}
|
@@ -1118,12 +1146,12 @@ Received SIGINT signal`);
|
|
1118
1146
|
try {
|
1119
1147
|
for await (const value of result) {
|
1120
1148
|
const response = { value, done: false };
|
1121
|
-
const chunk = encoder.encode(
|
1149
|
+
const chunk = encoder.encode(ejson4.stringify(response) + "\n");
|
1122
1150
|
controller.enqueue(chunk);
|
1123
1151
|
}
|
1124
1152
|
controller.enqueue(
|
1125
1153
|
encoder.encode(
|
1126
|
-
|
1154
|
+
ejson4.stringify({ done: true }) + "\n"
|
1127
1155
|
)
|
1128
1156
|
);
|
1129
1157
|
controller.close();
|
@@ -1133,7 +1161,7 @@ Received SIGINT signal`);
|
|
1133
1161
|
done: true
|
1134
1162
|
};
|
1135
1163
|
controller.enqueue(
|
1136
|
-
encoder.encode(
|
1164
|
+
encoder.encode(ejson4.stringify(response) + "\n")
|
1137
1165
|
);
|
1138
1166
|
controller.close();
|
1139
1167
|
}
|
@@ -1147,7 +1175,7 @@ Received SIGINT signal`);
|
|
1147
1175
|
}
|
1148
1176
|
});
|
1149
1177
|
}
|
1150
|
-
return ctx.text(
|
1178
|
+
return ctx.text(ejson4.stringify({ success: true, data: result }));
|
1151
1179
|
} catch (error) {
|
1152
1180
|
return ctx.json({ success: false, error: error.message });
|
1153
1181
|
}
|
@@ -1203,4 +1231,4 @@ async function startCheck(checkers, pass) {
|
|
1203
1231
|
if (pass) await pass();
|
1204
1232
|
}
|
1205
1233
|
|
1206
|
-
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",
|