@thestarweb/star-framework-web-runtime 0.0.0-stage → 1.2.3
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/CHANGELOG.md +112 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/mime.js +6 -0
- package/dist/mime.js.map +1 -0
- package/dist/node.js +55 -0
- package/dist/node.js.map +1 -0
- package/dist/package-loader/base-package-loader.js +132 -0
- package/dist/package-loader/base-package-loader.js.map +1 -0
- package/dist/package-loader/default-loader/index.js +17 -0
- package/dist/package-loader/default-loader/index.js.map +1 -0
- package/dist/package-loader/index.js +14 -0
- package/dist/package-loader/index.js.map +1 -0
- package/dist/package-loader/worker-thread/index.js +82 -0
- package/dist/package-loader/worker-thread/index.js.map +1 -0
- package/dist/package-loader/worker-thread/main-thread-ctrl.js +102 -0
- package/dist/package-loader/worker-thread/main-thread-ctrl.js.map +1 -0
- package/dist/package-loader/worker-thread/types.js +14 -0
- package/dist/package-loader/worker-thread/types.js.map +1 -0
- package/dist/package-loader/worker-thread/worker.js +51 -0
- package/dist/package-loader/worker-thread/worker.js.map +1 -0
- package/dist/parse-instances-config.js +80 -0
- package/dist/parse-instances-config.js.map +1 -0
- package/dist/remote-call/const.js +21 -0
- package/dist/remote-call/const.js.map +1 -0
- package/dist/remote-call/remote-call-client.js +76 -0
- package/dist/remote-call/remote-call-client.js.map +1 -0
- package/dist/remote-call/utile/message-port-to-connection.js +150 -0
- package/dist/remote-call/utile/message-port-to-connection.js.map +1 -0
- package/dist/remote-call/utile/remote-message/build.js +30 -0
- package/dist/remote-call/utile/remote-message/build.js.map +1 -0
- package/dist/remote-call/utile/remote-message/json-handle.js +15 -0
- package/dist/remote-call/utile/remote-message/json-handle.js.map +1 -0
- package/dist/remote-call/utile/remote-message/message.js +45 -0
- package/dist/remote-call/utile/remote-message/message.js.map +1 -0
- package/dist/remote-call/utile/remote-message/reader.js +18 -0
- package/dist/remote-call/utile/remote-message/reader.js.map +1 -0
- package/dist/remote-call/utile/remote-message/uint8-array-handle.js +27 -0
- package/dist/remote-call/utile/remote-message/uint8-array-handle.js.map +1 -0
- package/dist/remote-call/utile/remote-message/uint8-handle.js +19 -0
- package/dist/remote-call/utile/remote-message/uint8-handle.js.map +1 -0
- package/dist/remote-call/utile/remote-message/value-handle.js +5 -0
- package/dist/remote-call/utile/remote-message/value-handle.js.map +1 -0
- package/dist/remote-call/utile/remote-message/var-char-handle.js +31 -0
- package/dist/remote-call/utile/remote-message/var-char-handle.js.map +1 -0
- package/dist/remote-call/utile/remote-message/var-uint-handle.js +36 -0
- package/dist/remote-call/utile/remote-message/var-uint-handle.js.map +1 -0
- package/dist/types/config.js +11 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/index.js +5 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/instance.js +7 -0
- package/dist/types/instance.js.map +1 -0
- package/dist/types/package-info.js +4 -0
- package/dist/types/package-info.js.map +1 -0
- package/dist/types/resolved.js +13 -0
- package/dist/types/resolved.js.map +1 -0
- package/package.json +38 -5
- package/src/index.ts +3 -0
- package/src/mime.ts +5 -0
- package/src/node.ts +49 -0
- package/src/package-loader/base-package-loader.ts +148 -0
- package/src/package-loader/default-loader/index.ts +15 -0
- package/src/package-loader/index.ts +23 -0
- package/src/package-loader/worker-thread/index.ts +81 -0
- package/src/package-loader/worker-thread/main-thread-ctrl.ts +100 -0
- package/src/package-loader/worker-thread/types.ts +16 -0
- package/src/package-loader/worker-thread/worker.ts +49 -0
- package/src/parse-instances-config.ts +83 -0
- package/src/remote-call/const.ts +17 -0
- package/src/remote-call/remote-call-client.ts +95 -0
- package/src/remote-call/utile/message-port-to-connection.ts +134 -0
- package/src/remote-call/utile/remote-message/build.ts +26 -0
- package/src/remote-call/utile/remote-message/json-handle.ts +14 -0
- package/src/remote-call/utile/remote-message/message.ts +57 -0
- package/src/remote-call/utile/remote-message/reader.ts +16 -0
- package/src/remote-call/utile/remote-message/uint8-array-handle.ts +23 -0
- package/src/remote-call/utile/remote-message/uint8-handle.ts +16 -0
- package/src/remote-call/utile/remote-message/value-handle.ts +10 -0
- package/src/remote-call/utile/remote-message/var-char-handle.ts +28 -0
- package/src/remote-call/utile/remote-message/var-uint-handle.ts +32 -0
- package/src/types/config.ts +13 -0
- package/src/types/index.ts +4 -0
- package/src/types/instance.ts +15 -0
- package/src/types/package-info.ts +14 -0
- package/src/types/resolved.ts +42 -0
- package/README.md +0 -3
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function __assignType(fn, args) {
|
|
2
|
+
fn.__type = args;
|
|
3
|
+
return fn;
|
|
4
|
+
}
|
|
5
|
+
import { Buffer } from "node:buffer";
|
|
6
|
+
import { VarUIntHandle } from "./var-uint-handle.js";
|
|
7
|
+
const encoder = new TextEncoder();
|
|
8
|
+
const decoder = new TextDecoder();
|
|
9
|
+
export const VarCharHandle = {
|
|
10
|
+
build(value) {
|
|
11
|
+
let strLength = Buffer.byteLength(value, "utf-8");
|
|
12
|
+
const lFlag = VarUIntHandle.build(strLength);
|
|
13
|
+
const length = lFlag.length + strLength;
|
|
14
|
+
return {
|
|
15
|
+
length,
|
|
16
|
+
fill: __assignType(function fill(builder) {
|
|
17
|
+
lFlag.fill(builder);
|
|
18
|
+
builder.buff.write(value, builder.offset, strLength, "utf-8");
|
|
19
|
+
builder.offset += strLength;
|
|
20
|
+
}, ['builder', 'fill', 'P"2!"/"']),
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
read: __assignType(function (reader) {
|
|
24
|
+
const length = VarUIntHandle.read(reader);
|
|
25
|
+
const end = reader.pos + length;
|
|
26
|
+
const text = decoder.decode(reader.arr.subarray(reader.pos, end));
|
|
27
|
+
reader.pos = end;
|
|
28
|
+
return text;
|
|
29
|
+
}, ['reader', '', 'P"2!"/"']),
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=var-char-handle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"var-char-handle.js","sourceRoot":"","sources":["../../../../src/remote-call/utile/remote-message/var-char-handle.ts"],"names":[],"mappings":";;;;AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAClC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAClC,MAAM,CAAC,MAAM,aAAa,GAAwB;IACjD,KAAK,CAAC,KAAK;QACV,IAAI,SAAS,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAClD,MAAM,KAAK,GAAG,aAAa,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAC7C,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,GAAG,SAAS,CAAC;QACxC,OAAO;YACN,MAAM;YACN,IAAI,wBAAJ,IAAI,CAAC,OAAO;gBACX,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBACpB,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;gBAC9D,OAAO,CAAC,MAAM,IAAI,SAAS,CAAC;YAC7B,CAAC;SACD,CAAC;IACH,CAAC;IACD,IAAI,eAAE,UAAU,MAAM;QACrB,MAAM,MAAM,GAAG,aAAa,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC1C,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,GAAG,MAAM,CAAC;QAChC,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;QAClE,MAAM,CAAC,GAAG,GAAG,GAAG,CAAC;QACjB,OAAO,IAAI,CAAC;IACb,CAAC,4BAAA;CACD,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
function __assignType(fn, args) {
|
|
2
|
+
fn.__type = args;
|
|
3
|
+
return fn;
|
|
4
|
+
}
|
|
5
|
+
const map = [0x80, 0x4000, 0x200000, 0x10000000, 0x800000000];
|
|
6
|
+
export const VarUIntHandle = {
|
|
7
|
+
build(value) {
|
|
8
|
+
let i = 0;
|
|
9
|
+
while (map[i] < value)
|
|
10
|
+
i++;
|
|
11
|
+
const length = i + 1;
|
|
12
|
+
return {
|
|
13
|
+
length,
|
|
14
|
+
fill: __assignType(function fill(builder) {
|
|
15
|
+
let v = value;
|
|
16
|
+
while (v > 0x7f) {
|
|
17
|
+
builder.write((v & 0x7f) | 0x80);
|
|
18
|
+
v >>= 7;
|
|
19
|
+
}
|
|
20
|
+
builder.write(v);
|
|
21
|
+
}, ['builder', 'fill', 'P"2!"/"']),
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
read: __assignType(function (reader) {
|
|
25
|
+
let value;
|
|
26
|
+
let res = 0;
|
|
27
|
+
let i = 0;
|
|
28
|
+
do {
|
|
29
|
+
value = reader.arr[reader.pos++];
|
|
30
|
+
res += (value & 0x7f) << i;
|
|
31
|
+
i += 7;
|
|
32
|
+
} while (value & 0x80);
|
|
33
|
+
return res;
|
|
34
|
+
}, ['reader', '', 'P"2!"/"']),
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=var-uint-handle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"var-uint-handle.js","sourceRoot":"","sources":["../../../../src/remote-call/utile/remote-message/var-uint-handle.ts"],"names":[],"mappings":";;;;AAEA,MAAM,GAAG,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,CAAC,CAAC;AAC9D,MAAM,CAAC,MAAM,aAAa,GAAwB;IACjD,KAAK,CAAC,KAAK;QACV,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,OAAO,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK;YAAE,CAAC,EAAE,CAAC;QAC3B,MAAM,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC;QACrB,OAAO;YACN,MAAM;YACN,IAAI,wBAAJ,IAAI,CAAC,OAAO;gBACX,IAAI,CAAC,GAAG,KAAK,CAAC;gBACd,OAAO,CAAC,GAAG,IAAI,EAAE,CAAC;oBACjB,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;oBACjC,CAAC,KAAK,CAAC,CAAC;gBACT,CAAC;gBACD,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAClB,CAAC;SACD,CAAC;IACH,CAAC;IACD,IAAI,eAAE,UAAU,MAAM;QACrB,IAAI,KAAa,CAAC;QAClB,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,GAAG,CAAC;YACH,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC;YACjC,GAAG,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;YAC3B,CAAC,IAAI,CAAC,CAAC;QACR,CAAC,QAAQ,KAAK,GAAG,IAAI,EAAE;QACvB,OAAO,GAAG,CAAC;IACZ,CAAC,4BAAA;CACD,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const __ΩParameters = ['T', 'args', '', 'Parameters', 'l>e"!R!RPde#!Ph"!@2""/#qk#\'QRb!Pde"!p)w$y'];
|
|
2
|
+
import { loadPackage } from "../package-loader/index.js";
|
|
3
|
+
/*@ts-ignore*/
|
|
4
|
+
import { __ΩResolvedServerInstance } from "./resolved.js";
|
|
5
|
+
const __ΩInlineServer = [() => __ΩResolvedServerInstance, 'InlineServer', 'n!w"y'];
|
|
6
|
+
export { __ΩInlineServer as __ΩInlineServer };
|
|
7
|
+
const __ΩServerInstanceConfig = [() => __ΩInlineServer, () => __ΩParameters, () => loadPackage, 0, 'ServerInstanceConfig', 'Pn!i#o"".$fJw%y'];
|
|
8
|
+
export { __ΩServerInstanceConfig as __ΩServerInstanceConfig };
|
|
9
|
+
const __ΩServerConfig = [() => __ΩServerInstanceConfig, 'instances', 'port', 'ServerConfig', 'Pn!F4"\'4#Mw$y'];
|
|
10
|
+
export { __ΩServerConfig as __ΩServerConfig };
|
|
11
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/types/config.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;;0CAClB,eAAe"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/*@ts-ignore*/
|
|
2
|
+
import { __ΩRequestContext, __ΩResponseData, __ΩRequestProp } from "./resolved.js";
|
|
3
|
+
const __ΩServerInstanceOld = ['method', 'url', 'header', 'body', () => __ΩRequestContext, 'context', () => __ΩResponseData, '', 'onRequest', 'ServerInstanceOld', 'PP&2!P&!J2""2#!2$n%2&Pn\'-J`/(4)Mw*y'];
|
|
4
|
+
export { __ΩServerInstanceOld as __ΩServerInstanceOld };
|
|
5
|
+
const __ΩServerInstance = [() => __ΩRequestProp, 'requestProp', () => __ΩResponseData, '', 'onRequest', 'ServerInstance', 'PPn!2"n#`/$4%Mw&y'];
|
|
6
|
+
export { __ΩServerInstance as __ΩServerInstance };
|
|
7
|
+
//# sourceMappingURL=instance.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"instance.js","sourceRoot":"","sources":["../../src/types/instance.ts"],"names":[],"mappings":";mEAC0D,eAAe"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
const __ΩPackageInfoV1 = ["1", 'sf-formate', 'name', 'version', 'buildAt', 'dir', 'main', 'server', 'browser', 'PackageInfoV1', 'P.!4"&4#&4$\'4%P&4&&4\'M4(P&4&M4)Mw*y'];
|
|
2
|
+
const __ΩPackageInfo = [() => __ΩPackageInfoV1, 'PackageInfo', 'n!w"y'];
|
|
3
|
+
export { __ΩPackageInfo as __ΩPackageInfo };
|
|
4
|
+
//# sourceMappingURL=package-info.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package-info.js","sourceRoot":"","sources":["../../src/types/package-info.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const __ΩServerRuntimeContext = ['root', 'static', 'indexHtml', () => __ΩRequestProp, 'prop', () => __ΩResponseData, '', 'innerCall', 'ServerRuntimeContext', 'P&4!&4"&4#Pn$2%n&`/\'4(Mw)y'];
|
|
2
|
+
export { __ΩServerRuntimeContext as __ΩServerRuntimeContext };
|
|
3
|
+
const __ΩRequestContext = ['prefix', 'RequestContext', 'P&4!8Mw"y'];
|
|
4
|
+
export { __ΩRequestContext as __ΩRequestContext };
|
|
5
|
+
const __ΩMethod = ["GET", "POST", "PUT", "DELETE", "WS", "OPTION", 'Method', 'P.!.".#.$.%.&Jw\'y'];
|
|
6
|
+
export { __ΩMethod as __ΩMethod };
|
|
7
|
+
const __ΩRequestProp = ['ip', 'port', () => __ΩMethod, 'method', 'url', 'header', 'body', () => __ΩRequestContext, 'context', 'signal', 'appName', 'RequestProp', 'P&4!\'4"n#4$!4%"4&!4\'n(4)!4*&4+8Mw,y'];
|
|
8
|
+
export { __ΩRequestProp as __ΩRequestProp };
|
|
9
|
+
const __ΩResponseData = ['code', 'header', 'data', 'ResponseData', 'P\'4!"4"P!,J4#Mw$y'];
|
|
10
|
+
export { __ΩResponseData as __ΩResponseData };
|
|
11
|
+
const __ΩResolvedServerInstance = ['host', 'prefix', 'ip', 'matchRequest', 'important', 'staticFilePath', 'name', () => __ΩRequestProp, 'prop', () => __ΩResponseData, '', 'onRequest', 'destroy', 'ResolvedServerInstance', 'PPA4!8A4"8A4#8M4$9\'4%89&4&89&4\'9Pn(2)n*`/+4,P$1-8Mw.y'];
|
|
12
|
+
export { __ΩResolvedServerInstance as __ΩResolvedServerInstance };
|
|
13
|
+
//# sourceMappingURL=resolved.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolved.js","sourceRoot":"","sources":["../../src/types/resolved.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@thestarweb/star-framework-web-runtime",
|
|
3
|
+
"version": "1.2.3",
|
|
4
|
+
"description": "Star Framework Web App",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "src/index.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"repository": {
|
|
9
|
+
"url": "https://github.com/star-ss-qaq/star-node-framework"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tspc -p tsconfig.build.json",
|
|
16
|
+
"dev": "tspc -p tsconfig.build.json --watch",
|
|
17
|
+
"test": "vitest"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@deepkit/type": "^1.0.19",
|
|
21
|
+
"quico": "^0.8.0",
|
|
22
|
+
"tslib": "^2.8.1"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@deepkit/type-compiler": "^1.0.0",
|
|
26
|
+
"@types/node": "^26.0.1",
|
|
27
|
+
"ts-patch": "^4.0.1",
|
|
28
|
+
"typescript": "^6.0.3",
|
|
29
|
+
"vitest": "^4.1.9"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"src",
|
|
34
|
+
"CHANGELOG.md",
|
|
35
|
+
"README.md"
|
|
36
|
+
],
|
|
37
|
+
"license": "ISC",
|
|
38
|
+
"gitHead": "adf3256b302c09f1ab9ef9139481959e08c22f11"
|
|
39
|
+
}
|
package/src/index.ts
ADDED
package/src/mime.ts
ADDED
package/src/node.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createServer } from "http";
|
|
2
|
+
import { ServerConfig } from "./types/index.js";
|
|
3
|
+
import { Readable } from "stream";
|
|
4
|
+
import { parseInstancesConfig } from "./parse-instances-config.js";
|
|
5
|
+
|
|
6
|
+
export async function createHttpServer(config: ServerConfig) {
|
|
7
|
+
const server = createServer();
|
|
8
|
+
let handle = await parseInstancesConfig(config.instances);
|
|
9
|
+
server.on("request", async (req, res) => {
|
|
10
|
+
const controller = new AbortController();
|
|
11
|
+
const { signal } = controller;
|
|
12
|
+
res.on("close", () => {
|
|
13
|
+
controller.abort();
|
|
14
|
+
});
|
|
15
|
+
const url = new URL(
|
|
16
|
+
req.url || "/",
|
|
17
|
+
`http://${req.headers.host || "127.0.0.1"}/`,
|
|
18
|
+
);
|
|
19
|
+
const ret = await handle({
|
|
20
|
+
method: (req.method?.toUpperCase() as any) || "GET",
|
|
21
|
+
url,
|
|
22
|
+
header: req.headers,
|
|
23
|
+
body: Readable.toWeb(req) as any,
|
|
24
|
+
context: { prefix: "/" },
|
|
25
|
+
signal,
|
|
26
|
+
ip: req.socket.remoteAddress || "127.0.0.1",
|
|
27
|
+
port: req.socket.remotePort || 0,
|
|
28
|
+
});
|
|
29
|
+
if (signal.aborted) return;
|
|
30
|
+
if (ret.code) {
|
|
31
|
+
res.statusCode = ret.code;
|
|
32
|
+
}
|
|
33
|
+
Object.entries(ret.header).forEach(([key, data]) => {
|
|
34
|
+
res.setHeader(key, data as any);
|
|
35
|
+
});
|
|
36
|
+
if (ret.data) {
|
|
37
|
+
if (ret.data instanceof Readable) {
|
|
38
|
+
ret.data.pipe(res);
|
|
39
|
+
} else {
|
|
40
|
+
Readable.fromWeb(ret.data as any).pipe(res);
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
res.end();
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
server.listen(config.port);
|
|
47
|
+
console.log(`server start at: http://127.0.0.1:${config.port}`);
|
|
48
|
+
return {};
|
|
49
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { Readable } from "node:stream";
|
|
2
|
+
import {
|
|
3
|
+
PackageInfo,
|
|
4
|
+
RequestProp,
|
|
5
|
+
ResolvedServerInstance,
|
|
6
|
+
ResponseData,
|
|
7
|
+
ServerRuntimeContext,
|
|
8
|
+
} from "../types/index.js";
|
|
9
|
+
import { isAbsolute, join } from "node:path";
|
|
10
|
+
import { readFile } from "node:fs/promises";
|
|
11
|
+
import { reflectOrUndefined, typeOf, validate } from "@deepkit/type";
|
|
12
|
+
import { FSWatcher, watch } from "node:fs";
|
|
13
|
+
import { handleRef } from "../parse-instances-config.js";
|
|
14
|
+
import { pathToFileURL } from "node:url";
|
|
15
|
+
|
|
16
|
+
export interface CommonPackageConfig {
|
|
17
|
+
package: string;
|
|
18
|
+
host?: string | RegExp | (string | RegExp)[];
|
|
19
|
+
prefix?: string | RegExp | (string | RegExp)[];
|
|
20
|
+
ip?: string | RegExp | (string | RegExp)[];
|
|
21
|
+
important?: number | undefined;
|
|
22
|
+
name?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function strToReg(str: string, matchEnd: boolean) {
|
|
26
|
+
return new RegExp(
|
|
27
|
+
`^${str.replace(/([\.\[\]\(\)\^\$\+\*\{\}\|\\])/g, "\\$1")}${matchEnd ? "$" : ""}`,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
function mergeToReg(
|
|
31
|
+
config: string | RegExp | (string | RegExp)[],
|
|
32
|
+
matchEnd = true,
|
|
33
|
+
) {
|
|
34
|
+
if (Array.isArray(config)) {
|
|
35
|
+
return new RegExp(
|
|
36
|
+
config
|
|
37
|
+
.map(
|
|
38
|
+
(i) => `(${i instanceof RegExp ? i.source : strToReg(i, matchEnd)})`,
|
|
39
|
+
)
|
|
40
|
+
.join("|"),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
if (config instanceof RegExp) return config;
|
|
44
|
+
return strToReg(config, matchEnd);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export abstract class BasePackageLoader implements ResolvedServerInstance {
|
|
48
|
+
private _running = true;
|
|
49
|
+
private _packagePath!: string;
|
|
50
|
+
private _packageInfo!: PackageInfo;
|
|
51
|
+
private _mainPath!: string;
|
|
52
|
+
private _mainFileURL!: string;
|
|
53
|
+
public get name() {
|
|
54
|
+
return this._config.name || this._packageInfo.name;
|
|
55
|
+
}
|
|
56
|
+
get mainPath() {
|
|
57
|
+
return this._mainPath;
|
|
58
|
+
}
|
|
59
|
+
get mainFileURL() {
|
|
60
|
+
return this._mainFileURL;
|
|
61
|
+
}
|
|
62
|
+
serverRuntimeContext!: ServerRuntimeContext;
|
|
63
|
+
staticFilePath?: string | undefined;
|
|
64
|
+
async loadPackageInfo() {
|
|
65
|
+
this._packagePath = isAbsolute(this._config.package)
|
|
66
|
+
? this._config.package
|
|
67
|
+
: join(process.cwd(), this._config.package);
|
|
68
|
+
const info = JSON.parse(
|
|
69
|
+
await readFile(join(this._packagePath, "package-info.json"), "utf8"),
|
|
70
|
+
) as PackageInfo;
|
|
71
|
+
if (!this._running) return;
|
|
72
|
+
const error = validate(info, typeOf<PackageInfo>());
|
|
73
|
+
if (error?.length) {
|
|
74
|
+
throw new Error(error.map((i) => i.message).join("、"));
|
|
75
|
+
}
|
|
76
|
+
this._packageInfo = info;
|
|
77
|
+
this._mainPath = join(this._packagePath, info.server.dir, info.server.main);
|
|
78
|
+
this._mainFileURL = pathToFileURL(this.mainPath).toString();
|
|
79
|
+
const browserDir = join(this._packagePath, info.browser.dir);
|
|
80
|
+
this.serverRuntimeContext = {
|
|
81
|
+
root: this._packagePath,
|
|
82
|
+
static: browserDir,
|
|
83
|
+
indexHtml: await readFile(join(browserDir, "index.html"), "utf-8"),
|
|
84
|
+
innerCall: this.innerCall,
|
|
85
|
+
};
|
|
86
|
+
if (!this._running) return;
|
|
87
|
+
this.staticFilePath = browserDir;
|
|
88
|
+
this.matchRequest.host = this.config.host
|
|
89
|
+
? mergeToReg(this.config.host)
|
|
90
|
+
: undefined;
|
|
91
|
+
this.matchRequest.prefix = this.config.prefix
|
|
92
|
+
? mergeToReg(this.config.prefix, false)
|
|
93
|
+
: undefined;
|
|
94
|
+
this.matchRequest.ip = this.config.ip
|
|
95
|
+
? mergeToReg(this.config.ip)
|
|
96
|
+
: undefined;
|
|
97
|
+
}
|
|
98
|
+
get important() {
|
|
99
|
+
return this._config.important;
|
|
100
|
+
}
|
|
101
|
+
abstract load(isHoot: boolean): void | Promise<void>;
|
|
102
|
+
private watchHandle!: FSWatcher;
|
|
103
|
+
currentVersion = Date.now();
|
|
104
|
+
reloadTimeout?: NodeJS.Timeout;
|
|
105
|
+
lazyCheck() {
|
|
106
|
+
if (this.currentVersion + 5000 > Date.now()) {
|
|
107
|
+
if (!this.reloadTimeout) {
|
|
108
|
+
this.reloadTimeout = setTimeout(() => {
|
|
109
|
+
this.reloadTimeout = undefined;
|
|
110
|
+
this.lazyCheck();
|
|
111
|
+
}, 5200);
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
this.loadPackageInfo().then(() => {
|
|
115
|
+
if (this._running) {
|
|
116
|
+
this.load(true);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
startLoading: Promise<void>;
|
|
122
|
+
public get config() {
|
|
123
|
+
return this._config;
|
|
124
|
+
}
|
|
125
|
+
innerCall: (prop: RequestProp) => Promise<ResponseData>;
|
|
126
|
+
constructor(protected _config: CommonPackageConfig) {
|
|
127
|
+
this.innerCall = handleRef.value;
|
|
128
|
+
this.startLoading = this.loadPackageInfo().then(() => {
|
|
129
|
+
if (!this._running) return;
|
|
130
|
+
this.watchHandle = watch(
|
|
131
|
+
this._packagePath,
|
|
132
|
+
{ recursive: true },
|
|
133
|
+
(type, file) => {
|
|
134
|
+
if (file?.endsWith(".js")) {
|
|
135
|
+
this.currentVersion = Date.now();
|
|
136
|
+
this.lazyCheck();
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
);
|
|
140
|
+
return this.load(false);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
matchRequest: ResolvedServerInstance["matchRequest"] = {};
|
|
144
|
+
destroy?(): void {
|
|
145
|
+
throw new Error("Method not implemented.");
|
|
146
|
+
}
|
|
147
|
+
abstract onRequest(prop: RequestProp): Promise<ResponseData>;
|
|
148
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ServerInstance } from "../../types/instance.js";
|
|
2
|
+
import { RequestProp, ResponseData } from "../../types/resolved.js";
|
|
3
|
+
import { BasePackageLoader } from "../base-package-loader.js";
|
|
4
|
+
|
|
5
|
+
export class PackageDefaultLoader extends BasePackageLoader {
|
|
6
|
+
instance!: ServerInstance;
|
|
7
|
+
async load(isHoot: boolean) {
|
|
8
|
+
const module = await import(this.mainFileURL);
|
|
9
|
+
this.instance = await module.default(this.serverRuntimeContext);
|
|
10
|
+
}
|
|
11
|
+
async onRequest(prop: RequestProp): Promise<ResponseData> {
|
|
12
|
+
return await this.instance.onRequest(prop);
|
|
13
|
+
}
|
|
14
|
+
//
|
|
15
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { PackageDefaultLoader } from "./default-loader/index.js";
|
|
2
|
+
import { PackageWorkerThreadLoader } from "./worker-thread/index.js";
|
|
3
|
+
|
|
4
|
+
const map = {
|
|
5
|
+
Default: PackageDefaultLoader,
|
|
6
|
+
WorkerThread: PackageWorkerThreadLoader,
|
|
7
|
+
};
|
|
8
|
+
export async function loadPackage<T extends keyof typeof map>(
|
|
9
|
+
config:
|
|
10
|
+
| ({
|
|
11
|
+
/**
|
|
12
|
+
* 指定应用加载方式
|
|
13
|
+
* - `Default` 默认标准加载方式,直接讲程序import到运行库中,简单高效,但是不支持热更新、崩溃重启,无隔离
|
|
14
|
+
* - `WorkerThread` 使用WorkerThreads加载,存在可控的性能损耗,但能支持热更新、独立环境变量等参数,同时支持多实例、示例崩溃自动重启等
|
|
15
|
+
*/
|
|
16
|
+
loader: T;
|
|
17
|
+
} & ConstructorParameters<(typeof map)[T]>[0])
|
|
18
|
+
| ConstructorParameters<typeof PackageWorkerThreadLoader>[0],
|
|
19
|
+
) {
|
|
20
|
+
const t = new (map as any)[(config as any).loader || "Default"](config);
|
|
21
|
+
await t.startLoading;
|
|
22
|
+
return t;
|
|
23
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { RequestProp } from "../../types/index.js";
|
|
2
|
+
import {
|
|
3
|
+
BasePackageLoader,
|
|
4
|
+
CommonPackageConfig,
|
|
5
|
+
} from "../base-package-loader.js";
|
|
6
|
+
import { WorkerMainThreadCtrl } from "./main-thread-ctrl.js";
|
|
7
|
+
|
|
8
|
+
export class PackageWorkerThreadLoader extends BasePackageLoader {
|
|
9
|
+
public get config() {
|
|
10
|
+
return this._config;
|
|
11
|
+
}
|
|
12
|
+
activeIns: WorkerMainThreadCtrl[] = [];
|
|
13
|
+
loadingIns: Set<WorkerMainThreadCtrl> = new Set();
|
|
14
|
+
get loading() {
|
|
15
|
+
if (this.loadingIns.size > 0) {
|
|
16
|
+
return Promise.all([...this.loadingIns].map((i) => i.loading));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
onInsOnline(ins: WorkerMainThreadCtrl) {
|
|
20
|
+
this.activeIns = [...new Set(this.activeIns).add(ins)];
|
|
21
|
+
this.loadingIns.delete(ins);
|
|
22
|
+
if (
|
|
23
|
+
this.activeIns.length + this.loadingIns.size <
|
|
24
|
+
this._config.expectationCount!
|
|
25
|
+
) {
|
|
26
|
+
this.load(true);
|
|
27
|
+
} else {
|
|
28
|
+
if (this.activeIns.length > this._config.expectationCount!) {
|
|
29
|
+
this.activeIns.shift()!.kill();
|
|
30
|
+
}
|
|
31
|
+
if (this.activeIns[0].version < this.currentVersion) {
|
|
32
|
+
// 更新下一个实例
|
|
33
|
+
this.load(true);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
onInsError(ins: WorkerMainThreadCtrl) {
|
|
38
|
+
this.activeIns = this.activeIns.filter((i) => i !== ins);
|
|
39
|
+
this.loadingIns.add(ins);
|
|
40
|
+
}
|
|
41
|
+
async load(isReload: boolean) {
|
|
42
|
+
if (isReload) {
|
|
43
|
+
this.loadingIns.add(new WorkerMainThreadCtrl(this));
|
|
44
|
+
} else {
|
|
45
|
+
for (var i = 0; i < this._config.expectationCount!; i++) {
|
|
46
|
+
this.loadingIns.add(new WorkerMainThreadCtrl(this));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
await this.loading;
|
|
50
|
+
}
|
|
51
|
+
async loadPackageInfo() {
|
|
52
|
+
await super.loadPackageInfo();
|
|
53
|
+
if (!this._config.expectationCount) {
|
|
54
|
+
this._config.expectationCount = 1;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
constructor(
|
|
58
|
+
protected _config: CommonPackageConfig & {
|
|
59
|
+
expectationCount?: number;
|
|
60
|
+
env?: Record<string, string>;
|
|
61
|
+
cwd?: string;
|
|
62
|
+
arg?: string[];
|
|
63
|
+
},
|
|
64
|
+
) {
|
|
65
|
+
super(_config);
|
|
66
|
+
}
|
|
67
|
+
async onRequest(prop: RequestProp) {
|
|
68
|
+
if (this.activeIns.length === 0) {
|
|
69
|
+
return { code: 500, header: {}, data: null };
|
|
70
|
+
}
|
|
71
|
+
// TODO 如果有多个存活实例,应该做负载均衡,同时可能需要考虑动态扩容
|
|
72
|
+
return this.activeIns[0].onRequest(prop);
|
|
73
|
+
}
|
|
74
|
+
kill() {
|
|
75
|
+
this._config.expectationCount = 0;
|
|
76
|
+
this.activeIns.forEach((i) => i.kill());
|
|
77
|
+
this.activeIns = [];
|
|
78
|
+
this.loadingIns.forEach((i) => i.kill());
|
|
79
|
+
this.loadingIns.clear();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Worker, MessagePort } from "worker_threads";
|
|
2
|
+
import { extname, join } from "path";
|
|
3
|
+
import { Readable } from "node:stream";
|
|
4
|
+
import {
|
|
5
|
+
RequestContext,
|
|
6
|
+
RequestProp,
|
|
7
|
+
ResponseData,
|
|
8
|
+
} from "../../types/index.js";
|
|
9
|
+
import { PackageWorkerThreadLoader } from "./index.js";
|
|
10
|
+
import { RemoteCallClient } from "../../remote-call/remote-call-client.js";
|
|
11
|
+
import { messagePortToConnection } from "../../remote-call/utile/message-port-to-connection.js";
|
|
12
|
+
import { InitMessage, Message, RequestMessage } from "./types.js";
|
|
13
|
+
|
|
14
|
+
const ext = extname(import.meta.filename);
|
|
15
|
+
|
|
16
|
+
export class WorkerMainThreadCtrl extends RemoteCallClient {
|
|
17
|
+
handleRequest(prop: RequestProp): Promise<ResponseData> {
|
|
18
|
+
return this.main.serverRuntimeContext.innerCall(prop);
|
|
19
|
+
}
|
|
20
|
+
createConnection() {
|
|
21
|
+
const { port1, port2 } = new MessageChannel();
|
|
22
|
+
this.worker.postMessage({ type: 2, port: port2 } as RequestMessage, [
|
|
23
|
+
port2 as any,
|
|
24
|
+
]);
|
|
25
|
+
return messagePortToConnection(port1, "main");
|
|
26
|
+
}
|
|
27
|
+
private shouldRun = true;
|
|
28
|
+
|
|
29
|
+
private worker!: Worker;
|
|
30
|
+
_loading?: Promise<void>;
|
|
31
|
+
public get loading() {
|
|
32
|
+
return this._loading;
|
|
33
|
+
}
|
|
34
|
+
private startErrorTime = 0;
|
|
35
|
+
_version = 0;
|
|
36
|
+
public get version() {
|
|
37
|
+
return this._version;
|
|
38
|
+
}
|
|
39
|
+
load() {
|
|
40
|
+
this._loading = new Promise((r, reject) => {
|
|
41
|
+
const worker = new Worker(join(import.meta.dirname, `worker${ext}`), {
|
|
42
|
+
env: {
|
|
43
|
+
...process.env,
|
|
44
|
+
...this.main.config.env,
|
|
45
|
+
},
|
|
46
|
+
argv: this.main.config.arg,
|
|
47
|
+
});
|
|
48
|
+
worker.on("online", () => {
|
|
49
|
+
this.worker.postMessage({
|
|
50
|
+
type: 1,
|
|
51
|
+
mainPath: this.main.mainPath,
|
|
52
|
+
mainFileURL: this.main.mainFileURL,
|
|
53
|
+
context: { ...this.main.serverRuntimeContext, innerCall: undefined },
|
|
54
|
+
} as InitMessage);
|
|
55
|
+
});
|
|
56
|
+
worker.on("message", (message: Message) => {
|
|
57
|
+
switch (message.type) {
|
|
58
|
+
case 3:
|
|
59
|
+
this._loading = undefined;
|
|
60
|
+
r();
|
|
61
|
+
this.main.onInsOnline(this);
|
|
62
|
+
break;
|
|
63
|
+
case 2:
|
|
64
|
+
this.onConnection(messagePortToConnection(message.port, "worker"));
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
worker.on("exit", () => {
|
|
69
|
+
if (this.shouldRun) {
|
|
70
|
+
this.main.onInsError(this);
|
|
71
|
+
if (this.loading) {
|
|
72
|
+
if (++this.startErrorTime > 5) {
|
|
73
|
+
console.error("can not start");
|
|
74
|
+
reject();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
console.log("worker exit, restart it now ");
|
|
79
|
+
this.load();
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
worker.on("error", (error) => {
|
|
83
|
+
console.error("worker has error", error);
|
|
84
|
+
});
|
|
85
|
+
this.worker = worker;
|
|
86
|
+
this._version = this.main.currentVersion;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
constructor(private main: PackageWorkerThreadLoader) {
|
|
90
|
+
super();
|
|
91
|
+
this.load();
|
|
92
|
+
}
|
|
93
|
+
// async sendMessage(message: Uint8Array) {
|
|
94
|
+
// this.worker.postMessage(message);
|
|
95
|
+
// }
|
|
96
|
+
kill() {
|
|
97
|
+
this.shouldRun = false;
|
|
98
|
+
this.worker.terminate();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ServerRuntimeContext } from "../../types/index.js";
|
|
2
|
+
|
|
3
|
+
export interface InitMessage {
|
|
4
|
+
type: 1;
|
|
5
|
+
mainPath: string;
|
|
6
|
+
mainFileURL: string;
|
|
7
|
+
context: Omit<ServerRuntimeContext, "innerCall">;
|
|
8
|
+
}
|
|
9
|
+
export interface InitdMessage {
|
|
10
|
+
type: 3;
|
|
11
|
+
}
|
|
12
|
+
export interface RequestMessage {
|
|
13
|
+
type: 2;
|
|
14
|
+
port: MessagePort;
|
|
15
|
+
}
|
|
16
|
+
export type Message = InitMessage | RequestMessage | InitdMessage;
|