@hokan/core 0.1.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/LICENSE +21 -0
- package/lib/index.d.ts +51 -0
- package/lib/index.js +50 -0
- package/lib/trace.d.ts +45 -0
- package/lib/trace.js +59 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 huyongle
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { createTracer, loggerSink, type HostLogger, type TraceEntry, type TraceSink, type Tracer, type TracerOptions } from './trace.js';
|
|
2
|
+
export { createTracer, loggerSink };
|
|
3
|
+
export type { HostLogger, TraceEntry, TraceSink, Tracer, TracerOptions };
|
|
4
|
+
/**
|
|
5
|
+
* dsh-hokan 全部功能包共用的路由前缀(浏览器侧因运行时隔离需各自定义)。
|
|
6
|
+
*/
|
|
7
|
+
export declare const ROUTE_PREFIX = "/dsh-hokan";
|
|
8
|
+
/**
|
|
9
|
+
* 宿主 Web 路由注册的请求形状(Node HTTP 子集)。
|
|
10
|
+
*/
|
|
11
|
+
export interface HostRequest {
|
|
12
|
+
method?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* 宿主 Web 路由注册的响应形状(Node HTTP 子集)。
|
|
16
|
+
*/
|
|
17
|
+
export interface HostResponse {
|
|
18
|
+
writeHead(statusCode: number, headers: Record<string, string>): void;
|
|
19
|
+
end(body?: string): void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* exact 路由声明。
|
|
23
|
+
*/
|
|
24
|
+
export interface ExactRoute {
|
|
25
|
+
kind: 'exact';
|
|
26
|
+
path: string;
|
|
27
|
+
handler: (request: HostRequest, response: HostResponse) => void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 功能行宿主上下文的最小结构类型(DSH cordis 宿主注入)。
|
|
31
|
+
*/
|
|
32
|
+
export interface HostContext {
|
|
33
|
+
effect(setup: () => (() => void) | undefined, label?: string): void;
|
|
34
|
+
webServer: {
|
|
35
|
+
register(route: ExactRoute): () => void;
|
|
36
|
+
};
|
|
37
|
+
logger?: HostLogger;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 注册本功能行的 config 下发路由:GET /dsh-hokan/<featureId>/config.json。
|
|
41
|
+
* 浏览器半侧收不到行 config(DSH 客户端配置管线已知限制),经本路由补齐。
|
|
42
|
+
*
|
|
43
|
+
* @param context 功能行宿主上下文
|
|
44
|
+
* @param options 功能 id、回显的配置对象与可选 tracer
|
|
45
|
+
* @returns 注销函数
|
|
46
|
+
*/
|
|
47
|
+
export declare function serveRowConfig(context: HostContext, options: {
|
|
48
|
+
featureId: string;
|
|
49
|
+
config: unknown;
|
|
50
|
+
tracer?: Tracer;
|
|
51
|
+
}): () => void;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createTracer, loggerSink } from './trace.js';
|
|
2
|
+
export { createTracer, loggerSink };
|
|
3
|
+
/**
|
|
4
|
+
* dsh-hokan 全部功能包共用的路由前缀(浏览器侧因运行时隔离需各自定义)。
|
|
5
|
+
*/
|
|
6
|
+
export const ROUTE_PREFIX = '/dsh-hokan';
|
|
7
|
+
// JSON 响应头。
|
|
8
|
+
const JSON_HEADERS = {
|
|
9
|
+
'content-type': 'application/json; charset=utf-8',
|
|
10
|
+
'cache-control': 'no-store',
|
|
11
|
+
};
|
|
12
|
+
// 非 GET/HEAD 请求回 405;已处理返回 true。
|
|
13
|
+
function rejectUnsupportedMethod(request, response) {
|
|
14
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
15
|
+
response.writeHead(405, {});
|
|
16
|
+
response.end('');
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 注册本功能行的 config 下发路由:GET /dsh-hokan/<featureId>/config.json。
|
|
23
|
+
* 浏览器半侧收不到行 config(DSH 客户端配置管线已知限制),经本路由补齐。
|
|
24
|
+
*
|
|
25
|
+
* @param context 功能行宿主上下文
|
|
26
|
+
* @param options 功能 id、回显的配置对象与可选 tracer
|
|
27
|
+
* @returns 注销函数
|
|
28
|
+
*/
|
|
29
|
+
export function serveRowConfig(context, options) {
|
|
30
|
+
const disposeRoute = context.webServer.register({
|
|
31
|
+
kind: 'exact',
|
|
32
|
+
path: ROUTE_PREFIX + '/' + options.featureId + '/config.json',
|
|
33
|
+
handler: (request, response) => {
|
|
34
|
+
const startedAt = performance.now();
|
|
35
|
+
if (rejectUnsupportedMethod(request, response))
|
|
36
|
+
return;
|
|
37
|
+
response.writeHead(200, JSON_HEADERS);
|
|
38
|
+
response.end(JSON.stringify({ feature: options.featureId, config: options.config ?? {} }));
|
|
39
|
+
if (options.tracer?.enabled) {
|
|
40
|
+
options.tracer.event('config-route.serve', {
|
|
41
|
+
featureId: options.featureId,
|
|
42
|
+
durationMs: Math.round((performance.now() - startedAt) * 100) / 100,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
return () => {
|
|
48
|
+
disposeRoute();
|
|
49
|
+
};
|
|
50
|
+
}
|
package/lib/trace.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface TraceEntry {
|
|
2
|
+
timestamp: string;
|
|
3
|
+
scope: string;
|
|
4
|
+
event: string;
|
|
5
|
+
durationMs?: number;
|
|
6
|
+
slow?: boolean;
|
|
7
|
+
detail?: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
export interface TraceSink {
|
|
10
|
+
write(entry: TraceEntry): void;
|
|
11
|
+
}
|
|
12
|
+
export interface HostLogger {
|
|
13
|
+
debug?(message: string): void;
|
|
14
|
+
info?(message: string): void;
|
|
15
|
+
warn?(message: string): void;
|
|
16
|
+
error?(message: string): void;
|
|
17
|
+
}
|
|
18
|
+
export interface TracerOptions {
|
|
19
|
+
scope: string;
|
|
20
|
+
enabled: boolean;
|
|
21
|
+
sink?: TraceSink;
|
|
22
|
+
slowMs?: number;
|
|
23
|
+
}
|
|
24
|
+
export interface Tracer {
|
|
25
|
+
readonly scope: string;
|
|
26
|
+
readonly enabled: boolean;
|
|
27
|
+
child(childScope: string): Tracer;
|
|
28
|
+
event(name: string, detail?: Record<string, unknown>): void;
|
|
29
|
+
measure<T>(name: string, operation: () => Promise<T>): Promise<T>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 构造写入宿主 logger.debug 的 sink;logger 缺失时退回传入的 fallback(默认 console.log)。
|
|
33
|
+
*
|
|
34
|
+
* @param logger 宿主 logger(可选)
|
|
35
|
+
* @param fallback 无 logger 时的输出函数
|
|
36
|
+
* @returns TraceSink
|
|
37
|
+
*/
|
|
38
|
+
export declare function loggerSink(logger?: HostLogger, fallback?: (message: string) => void): TraceSink;
|
|
39
|
+
/**
|
|
40
|
+
* 创建一个 tracer。disabled 时 event/measure 零输出零开销(守卫在入口)。
|
|
41
|
+
*
|
|
42
|
+
* @param options 作用域、开关、sink 与慢阈值
|
|
43
|
+
* @returns Tracer
|
|
44
|
+
*/
|
|
45
|
+
export declare function createTracer(options: TracerOptions): Tracer;
|
package/lib/trace.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// 默认慢阈值(毫秒)。
|
|
2
|
+
const DEFAULT_SLOW_MS = 200;
|
|
3
|
+
// JSON 行前缀;grep 'hokan-trace' 即得全部轨迹。
|
|
4
|
+
const LINE_PREFIX = 'hokan-trace ';
|
|
5
|
+
/**
|
|
6
|
+
* 构造写入宿主 logger.debug 的 sink;logger 缺失时退回传入的 fallback(默认 console.log)。
|
|
7
|
+
*
|
|
8
|
+
* @param logger 宿主 logger(可选)
|
|
9
|
+
* @param fallback 无 logger 时的输出函数
|
|
10
|
+
* @returns TraceSink
|
|
11
|
+
*/
|
|
12
|
+
export function loggerSink(logger, fallback) {
|
|
13
|
+
return {
|
|
14
|
+
write(entry) {
|
|
15
|
+
const line = LINE_PREFIX + JSON.stringify(entry);
|
|
16
|
+
if (logger?.debug)
|
|
17
|
+
logger.debug(line);
|
|
18
|
+
else
|
|
19
|
+
(fallback ?? console.log)(line);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 创建一个 tracer。disabled 时 event/measure 零输出零开销(守卫在入口)。
|
|
25
|
+
*
|
|
26
|
+
* @param options 作用域、开关、sink 与慢阈值
|
|
27
|
+
* @returns Tracer
|
|
28
|
+
*/
|
|
29
|
+
export function createTracer(options) {
|
|
30
|
+
const sink = options.sink ?? loggerSink();
|
|
31
|
+
const slowMs = options.slowMs ?? DEFAULT_SLOW_MS;
|
|
32
|
+
const emit = (name, fields, detail) => {
|
|
33
|
+
sink.write({ timestamp: new Date().toISOString(), scope: options.scope, event: name, ...fields, detail });
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
scope: options.scope,
|
|
37
|
+
enabled: options.enabled,
|
|
38
|
+
child(childScope) {
|
|
39
|
+
return createTracer({ ...options, scope: options.scope + '.' + childScope });
|
|
40
|
+
},
|
|
41
|
+
event(name, detail) {
|
|
42
|
+
if (!options.enabled)
|
|
43
|
+
return;
|
|
44
|
+
emit(name, {}, detail);
|
|
45
|
+
},
|
|
46
|
+
async measure(name, operation) {
|
|
47
|
+
if (!options.enabled)
|
|
48
|
+
return operation();
|
|
49
|
+
const startedAt = performance.now();
|
|
50
|
+
try {
|
|
51
|
+
return await operation();
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
const durationMs = Math.round((performance.now() - startedAt) * 100) / 100;
|
|
55
|
+
emit(name, { durationMs, slow: durationMs >= slowMs });
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hokan/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "dsh-hokan 功能包共享宿主助手与类型(普通库,不注册 bundle 行)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"types": "lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"default": "./lib/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"lib"
|
|
17
|
+
],
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/YuluoY/dsh-hokan.git",
|
|
22
|
+
"directory": "packages/core"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc -p tsconfig.json",
|
|
29
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
30
|
+
"dev": "tsc -p tsconfig.json --watch"
|
|
31
|
+
}
|
|
32
|
+
}
|