lupine.api 1.1.57 → 1.1.59
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/README.md +3 -3
- package/admin/admin-about.tsx +12 -16
- package/admin/admin-config.tsx +47 -44
- package/admin/admin-css.tsx +3 -3
- package/admin/admin-db.tsx +75 -75
- package/admin/admin-frame-helper.tsx +364 -364
- package/admin/admin-frame.tsx +164 -164
- package/admin/admin-index.tsx +65 -65
- package/admin/admin-login.tsx +111 -111
- package/admin/admin-menu-edit.tsx +637 -637
- package/admin/admin-menu-list.tsx +87 -87
- package/admin/admin-page-edit.tsx +564 -564
- package/admin/admin-page-list.tsx +83 -83
- package/admin/admin-performance.tsx +28 -28
- package/admin/admin-release.tsx +427 -404
- package/admin/admin-resources.tsx +382 -382
- package/admin/admin-shell.tsx +89 -89
- package/admin/admin-table-data.tsx +146 -146
- package/admin/admin-table-list.tsx +230 -230
- package/admin/admin-test-animations.tsx +395 -395
- package/admin/admin-test-component.tsx +823 -808
- package/admin/admin-test-edit.tsx +319 -319
- package/admin/admin-test-themes.tsx +56 -56
- package/admin/admin-tokens.tsx +338 -338
- package/admin/design/admin-design.tsx +174 -174
- package/admin/design/block-grid.tsx +36 -36
- package/admin/design/block-grid1.tsx +21 -21
- package/admin/design/block-paragraph.tsx +19 -19
- package/admin/design/block-title.tsx +19 -19
- package/admin/design/design-block-box.tsx +140 -140
- package/admin/design/drag-data.tsx +24 -24
- package/admin/index.ts +9 -9
- package/admin/package.json +15 -15
- package/admin/tsconfig.json +127 -127
- package/dev/copy-folder.js +32 -32
- package/dev/cp-index-html.js +69 -69
- package/dev/file-utils.js +12 -12
- package/dev/index.js +18 -19
- package/dev/package.json +12 -12
- package/dev/plugin-ifelse.js +168 -168
- package/dev/plugin-ifelse.test.js +37 -37
- package/dev/run-cmd.js +14 -14
- package/dev/send-request.js +12 -12
- package/package.json +55 -55
- package/src/admin-api/admin-api-helper.ts +210 -205
- package/src/admin-api/admin-api.ts +65 -65
- package/src/admin-api/admin-auth.ts +152 -146
- package/src/admin-api/admin-config.ts +94 -84
- package/src/admin-api/admin-csv.ts +94 -94
- package/src/admin-api/admin-db.ts +269 -269
- package/src/admin-api/admin-menu.ts +135 -135
- package/src/admin-api/admin-page.ts +135 -135
- package/src/admin-api/admin-performance.ts +128 -128
- package/src/admin-api/admin-release.ts +703 -700
- package/src/admin-api/admin-resources.ts +318 -318
- package/src/admin-api/admin-token-helper.ts +82 -79
- package/src/admin-api/admin-tokens.ts +90 -90
- package/src/admin-api/index.ts +2 -2
- package/src/admin-api/web-config-api.ts +19 -19
- package/src/api/api-cache.ts +103 -103
- package/src/api/api-helper.ts +44 -44
- package/src/api/api-module.ts +67 -60
- package/src/api/api-router.ts +177 -177
- package/src/api/api-shared-storage.ts +64 -64
- package/src/api/async-storage.ts +5 -5
- package/src/api/debug-service.ts +56 -56
- package/src/api/encode-html.ts +27 -27
- package/src/api/handle-status.ts +75 -75
- package/src/api/index.ts +15 -16
- package/src/api/mini-web-socket.ts +270 -270
- package/src/api/server-content-type.ts +82 -82
- package/src/api/server-render.ts +235 -215
- package/src/api/shell-service.ts +74 -74
- package/src/api/simple-storage.ts +80 -80
- package/src/api/static-server.ts +128 -125
- package/src/api/to-client-delivery.ts +26 -26
- package/src/app/app-cache.ts +55 -55
- package/src/app/app-helper.ts +62 -62
- package/src/app/app-message.ts +109 -109
- package/src/app/app-shared-storage.ts +363 -363
- package/src/app/app-start.ts +136 -136
- package/src/app/cleanup-exit.ts +16 -16
- package/src/app/host-to-path.ts +38 -38
- package/src/app/index.ts +11 -11
- package/src/app/process-dev-requests.ts +130 -130
- package/src/app/web-listener.ts +294 -294
- package/src/app/web-processor.ts +47 -42
- package/src/app/web-server.ts +100 -100
- package/src/common-js/web-env.js +104 -104
- package/src/index.ts +7 -7
- package/src/lang/api-lang-en.ts +26 -26
- package/src/lang/api-lang-zh-cn.ts +27 -27
- package/src/lang/index.ts +2 -2
- package/src/lang/lang-helper.ts +76 -76
- package/src/lang/lang-props.ts +6 -6
- package/src/lib/db/db-helper.ts +23 -23
- package/src/lib/db/db-mysql.ts +249 -250
- package/src/lib/db/db-sqlite.ts +101 -101
- package/src/lib/db/db.spec.ts +28 -28
- package/src/lib/db/db.ts +325 -325
- package/src/lib/db/index.ts +5 -5
- package/src/lib/index.ts +3 -3
- package/src/lib/logger.spec.ts +214 -214
- package/src/lib/logger.ts +281 -281
- package/src/lib/runtime-require.ts +37 -37
- package/src/lib/utils/cookie-util.ts +34 -34
- package/src/lib/utils/crypto.ts +58 -58
- package/src/lib/utils/date-utils.ts +317 -317
- package/src/lib/utils/deep-merge.ts +37 -37
- package/src/lib/utils/delay.ts +12 -12
- package/src/lib/utils/file-setting.ts +55 -55
- package/src/lib/utils/format-bytes.ts +11 -11
- package/src/lib/utils/fs-utils.ts +158 -158
- package/src/lib/utils/get-env.ts +27 -27
- package/src/lib/utils/index.ts +12 -12
- package/src/lib/utils/is-type.ts +48 -48
- package/src/lib/utils/load-env.ts +14 -14
- package/src/lib/utils/pad.ts +6 -6
- package/src/models/api-base.ts +5 -5
- package/src/models/api-module-props.ts +10 -11
- package/src/models/api-router-props.ts +26 -26
- package/src/models/app-cache-props.ts +33 -33
- package/src/models/app-data-props.ts +10 -10
- package/src/models/app-helper-props.ts +6 -6
- package/src/models/app-shared-storage-props.ts +38 -38
- package/src/models/app-start-props.ts +18 -18
- package/src/models/async-storage-props.ts +13 -13
- package/src/models/db-config.ts +30 -30
- package/src/models/host-to-path-props.ts +12 -12
- package/src/models/index.ts +16 -16
- package/src/models/json-object.ts +8 -8
- package/src/models/locals-props.ts +36 -36
- package/src/models/logger-props.ts +84 -84
- package/src/models/simple-storage-props.ts +13 -14
- package/src/models/to-client-delivery-props.ts +6 -6
- package/tsconfig.json +115 -115
- package/dev/plugin-gen-versions.js +0 -20
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A persistent storage for the Api (stored in primary process)
|
|
3
|
-
*/
|
|
4
|
-
import { IAppSharedStorage } from '../models';
|
|
5
|
-
import { SimpleStorageDataProps } from '../models';
|
|
6
|
-
import { apiCache } from './api-cache';
|
|
7
|
-
|
|
8
|
-
// ApiSharedStorage is used in api module to store variables under appName scope, but is stored in one place - primary process
|
|
9
|
-
export class ApiSharedStorage {
|
|
10
|
-
private storage: IAppSharedStorage | undefined;
|
|
11
|
-
|
|
12
|
-
public setAppSharedStorage(storage: IAppSharedStorage): void {
|
|
13
|
-
this.storage = storage;
|
|
14
|
-
}
|
|
15
|
-
public getAppSharedStorage(): IAppSharedStorage {
|
|
16
|
-
if (!this.storage) {
|
|
17
|
-
throw new Error('AppSharedStorage not initialized');
|
|
18
|
-
}
|
|
19
|
-
return this.storage;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// called from primary before exit, or from api to save changes
|
|
23
|
-
async save() {
|
|
24
|
-
const appName = apiCache.getAppName();
|
|
25
|
-
await this.getAppSharedStorage().save(appName);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
get(key: string): Promise<string> {
|
|
29
|
-
const appName = apiCache.getAppName();
|
|
30
|
-
return this.getAppSharedStorage().get(appName, key);
|
|
31
|
-
}
|
|
32
|
-
getWeb(key: string): Promise<string> {
|
|
33
|
-
const appName = apiCache.getAppName();
|
|
34
|
-
return this.getAppSharedStorage().getWeb(appName, key);
|
|
35
|
-
}
|
|
36
|
-
getApi(key: string): Promise<string> {
|
|
37
|
-
const appName = apiCache.getAppName();
|
|
38
|
-
return this.getAppSharedStorage().getApi(appName, key);
|
|
39
|
-
}
|
|
40
|
-
getWebAll(): Promise<SimpleStorageDataProps> {
|
|
41
|
-
const appName = apiCache.getAppName();
|
|
42
|
-
return this.getAppSharedStorage().getWebAll(appName);
|
|
43
|
-
}
|
|
44
|
-
getWithPrefix(prefixKey: string): Promise<SimpleStorageDataProps> {
|
|
45
|
-
const appName = apiCache.getAppName();
|
|
46
|
-
return this.getAppSharedStorage().getWithPrefix(appName, prefixKey);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
set(key: string, value: any) {
|
|
50
|
-
const appName = apiCache.getAppName();
|
|
51
|
-
return this.getAppSharedStorage().set(appName, key, value);
|
|
52
|
-
}
|
|
53
|
-
setWeb(key: string, value: any) {
|
|
54
|
-
const appName = apiCache.getAppName();
|
|
55
|
-
return this.getAppSharedStorage().setWeb(appName, key, value);
|
|
56
|
-
}
|
|
57
|
-
setApi(key: string, value: any) {
|
|
58
|
-
const appName = apiCache.getAppName();
|
|
59
|
-
return this.getAppSharedStorage().setApi(appName, key, value);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// this can be used in app, but in api, it should use getAppStorage()
|
|
64
|
-
export const apiStorage = /* @__PURE__ */ new ApiSharedStorage();
|
|
1
|
+
/**
|
|
2
|
+
* A persistent storage for the Api (stored in primary process)
|
|
3
|
+
*/
|
|
4
|
+
import { IAppSharedStorage } from '../models';
|
|
5
|
+
import { SimpleStorageDataProps } from '../models';
|
|
6
|
+
import { apiCache } from './api-cache';
|
|
7
|
+
|
|
8
|
+
// ApiSharedStorage is used in api module to store variables under appName scope, but is stored in one place - primary process
|
|
9
|
+
export class ApiSharedStorage {
|
|
10
|
+
private storage: IAppSharedStorage | undefined;
|
|
11
|
+
|
|
12
|
+
public setAppSharedStorage(storage: IAppSharedStorage): void {
|
|
13
|
+
this.storage = storage;
|
|
14
|
+
}
|
|
15
|
+
public getAppSharedStorage(): IAppSharedStorage {
|
|
16
|
+
if (!this.storage) {
|
|
17
|
+
throw new Error('AppSharedStorage not initialized');
|
|
18
|
+
}
|
|
19
|
+
return this.storage;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// called from primary before exit, or from api to save changes
|
|
23
|
+
async save() {
|
|
24
|
+
const appName = apiCache.getAppName();
|
|
25
|
+
await this.getAppSharedStorage().save(appName);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get(key: string): Promise<string> {
|
|
29
|
+
const appName = apiCache.getAppName();
|
|
30
|
+
return this.getAppSharedStorage().get(appName, key);
|
|
31
|
+
}
|
|
32
|
+
getWeb(key: string): Promise<string> {
|
|
33
|
+
const appName = apiCache.getAppName();
|
|
34
|
+
return this.getAppSharedStorage().getWeb(appName, key);
|
|
35
|
+
}
|
|
36
|
+
getApi(key: string): Promise<string> {
|
|
37
|
+
const appName = apiCache.getAppName();
|
|
38
|
+
return this.getAppSharedStorage().getApi(appName, key);
|
|
39
|
+
}
|
|
40
|
+
getWebAll(): Promise<SimpleStorageDataProps> {
|
|
41
|
+
const appName = apiCache.getAppName();
|
|
42
|
+
return this.getAppSharedStorage().getWebAll(appName);
|
|
43
|
+
}
|
|
44
|
+
getWithPrefix(prefixKey: string): Promise<SimpleStorageDataProps> {
|
|
45
|
+
const appName = apiCache.getAppName();
|
|
46
|
+
return this.getAppSharedStorage().getWithPrefix(appName, prefixKey);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
set(key: string, value: any) {
|
|
50
|
+
const appName = apiCache.getAppName();
|
|
51
|
+
return this.getAppSharedStorage().set(appName, key, value);
|
|
52
|
+
}
|
|
53
|
+
setWeb(key: string, value: any) {
|
|
54
|
+
const appName = apiCache.getAppName();
|
|
55
|
+
return this.getAppSharedStorage().setWeb(appName, key, value);
|
|
56
|
+
}
|
|
57
|
+
setApi(key: string, value: any) {
|
|
58
|
+
const appName = apiCache.getAppName();
|
|
59
|
+
return this.getAppSharedStorage().setApi(appName, key, value);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// this can be used in app, but in api, it should use getAppStorage()
|
|
64
|
+
export const apiStorage = /* @__PURE__ */ new ApiSharedStorage();
|
package/src/api/async-storage.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// It is similar to a thread-local storage in other languages.
|
|
2
|
-
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
-
import { AsyncStorageProps } from '../models/async-storage-props';
|
|
4
|
-
|
|
5
|
-
export const asyncLocalStorage = new AsyncLocalStorage<AsyncStorageProps>();
|
|
1
|
+
// It is similar to a thread-local storage in other languages.
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
+
import { AsyncStorageProps } from '../models/async-storage-props';
|
|
4
|
+
|
|
5
|
+
export const asyncLocalStorage = new AsyncLocalStorage<AsyncStorageProps>();
|
package/src/api/debug-service.ts
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import { IncomingMessage } from 'http';
|
|
2
|
-
import { Duplex } from 'stream';
|
|
3
|
-
import { MiniWebSocket } from './mini-web-socket';
|
|
4
|
-
import { ShellService } from './shell-service';
|
|
5
|
-
|
|
6
|
-
// This is only used in debug mode (no clusters)
|
|
7
|
-
export class DebugService {
|
|
8
|
-
static clientRefreshFlag = Date.now();
|
|
9
|
-
static miniWebSocket = new MiniWebSocket(this.onMessage.bind(this));
|
|
10
|
-
static shellMap = new Map<Duplex, ShellService>();
|
|
11
|
-
|
|
12
|
-
public static onMessage(msg: string, socket: Duplex) {
|
|
13
|
-
try {
|
|
14
|
-
const json = JSON.parse(msg);
|
|
15
|
-
if (json.message === 'get-flag') {
|
|
16
|
-
this.miniWebSocket.sendMessage(
|
|
17
|
-
socket,
|
|
18
|
-
JSON.stringify({
|
|
19
|
-
message: 'flag',
|
|
20
|
-
flag: DebugService.clientRefreshFlag,
|
|
21
|
-
})
|
|
22
|
-
);
|
|
23
|
-
} else if (json.message === 'shell') {
|
|
24
|
-
console.log(json);
|
|
25
|
-
let shell: ShellService;
|
|
26
|
-
if (this.shellMap.has(socket)) {
|
|
27
|
-
shell = this.shellMap.get(socket)!;
|
|
28
|
-
} else {
|
|
29
|
-
shell = new ShellService(socket, this.miniWebSocket);
|
|
30
|
-
this.shellMap.set(socket, shell);
|
|
31
|
-
socket.on('close', () => {
|
|
32
|
-
shell.stop();
|
|
33
|
-
this.shellMap.delete(socket);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
shell.cmd(json.cmd);
|
|
37
|
-
}
|
|
38
|
-
} catch (error) {
|
|
39
|
-
console.error(error);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public static handleUpgrade(req: IncomingMessage, socket: Duplex, head: Buffer) {
|
|
44
|
-
this.miniWebSocket.handleUpgrade(req, socket, head);
|
|
45
|
-
|
|
46
|
-
// socket.write(JSON.stringify({ message: 'flag', flag: DebugService.clientRefreshFlag }));
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// broadcast to all frontend clients
|
|
50
|
-
public static broadcastRefresh() {
|
|
51
|
-
console.log(`${process.pid} - broadcast refresh request to clients.`);
|
|
52
|
-
this.clientRefreshFlag = Date.now();
|
|
53
|
-
const msg = { message: 'Refresh', flag: this.clientRefreshFlag };
|
|
54
|
-
this.miniWebSocket.broadcast(JSON.stringify(msg));
|
|
55
|
-
}
|
|
56
|
-
}
|
|
1
|
+
import { IncomingMessage } from 'http';
|
|
2
|
+
import { Duplex } from 'stream';
|
|
3
|
+
import { MiniWebSocket } from './mini-web-socket';
|
|
4
|
+
import { ShellService } from './shell-service';
|
|
5
|
+
|
|
6
|
+
// This is only used in debug mode (no clusters)
|
|
7
|
+
export class DebugService {
|
|
8
|
+
static clientRefreshFlag = Date.now();
|
|
9
|
+
static miniWebSocket = new MiniWebSocket(this.onMessage.bind(this));
|
|
10
|
+
static shellMap = new Map<Duplex, ShellService>();
|
|
11
|
+
|
|
12
|
+
public static onMessage(msg: string, socket: Duplex) {
|
|
13
|
+
try {
|
|
14
|
+
const json = JSON.parse(msg);
|
|
15
|
+
if (json.message === 'get-flag') {
|
|
16
|
+
this.miniWebSocket.sendMessage(
|
|
17
|
+
socket,
|
|
18
|
+
JSON.stringify({
|
|
19
|
+
message: 'flag',
|
|
20
|
+
flag: DebugService.clientRefreshFlag,
|
|
21
|
+
})
|
|
22
|
+
);
|
|
23
|
+
} else if (json.message === 'shell') {
|
|
24
|
+
console.log(json);
|
|
25
|
+
let shell: ShellService;
|
|
26
|
+
if (this.shellMap.has(socket)) {
|
|
27
|
+
shell = this.shellMap.get(socket)!;
|
|
28
|
+
} else {
|
|
29
|
+
shell = new ShellService(socket, this.miniWebSocket);
|
|
30
|
+
this.shellMap.set(socket, shell);
|
|
31
|
+
socket.on('close', () => {
|
|
32
|
+
shell.stop();
|
|
33
|
+
this.shellMap.delete(socket);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
shell.cmd(json.cmd);
|
|
37
|
+
}
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error(error);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public static handleUpgrade(req: IncomingMessage, socket: Duplex, head: Buffer) {
|
|
44
|
+
this.miniWebSocket.handleUpgrade(req, socket, head);
|
|
45
|
+
|
|
46
|
+
// socket.write(JSON.stringify({ message: 'flag', flag: DebugService.clientRefreshFlag }));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// broadcast to all frontend clients
|
|
50
|
+
public static broadcastRefresh() {
|
|
51
|
+
console.log(`${process.pid} - broadcast refresh request to clients.`);
|
|
52
|
+
this.clientRefreshFlag = Date.now();
|
|
53
|
+
const msg = { message: 'Refresh', flag: this.clientRefreshFlag };
|
|
54
|
+
this.miniWebSocket.broadcast(JSON.stringify(msg));
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/api/encode-html.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
export const encodeHtml = (str: string): string => {
|
|
2
|
-
return str.replace(
|
|
3
|
-
/[&<>'"]/g,
|
|
4
|
-
(tag) =>
|
|
5
|
-
({
|
|
6
|
-
'&': '&',
|
|
7
|
-
'<': '<',
|
|
8
|
-
'>': '>',
|
|
9
|
-
"'": ''',
|
|
10
|
-
'"': '"',
|
|
11
|
-
}[tag] || '')
|
|
12
|
-
);
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export const decodeHtml = (str: string): string => {
|
|
16
|
-
return str.replace(
|
|
17
|
-
/&(\D+);/gi,
|
|
18
|
-
(tag) =>
|
|
19
|
-
({
|
|
20
|
-
'&': '&',
|
|
21
|
-
'<': '<',
|
|
22
|
-
'>': '>',
|
|
23
|
-
''': "'",
|
|
24
|
-
'"': '"',
|
|
25
|
-
}[tag] || '')
|
|
26
|
-
);
|
|
27
|
-
};
|
|
1
|
+
export const encodeHtml = (str: string): string => {
|
|
2
|
+
return str.replace(
|
|
3
|
+
/[&<>'"]/g,
|
|
4
|
+
(tag) =>
|
|
5
|
+
({
|
|
6
|
+
'&': '&',
|
|
7
|
+
'<': '<',
|
|
8
|
+
'>': '>',
|
|
9
|
+
"'": ''',
|
|
10
|
+
'"': '"',
|
|
11
|
+
}[tag] || '')
|
|
12
|
+
);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const decodeHtml = (str: string): string => {
|
|
16
|
+
return str.replace(
|
|
17
|
+
/&(\D+);/gi,
|
|
18
|
+
(tag) =>
|
|
19
|
+
({
|
|
20
|
+
'&': '&',
|
|
21
|
+
'<': '<',
|
|
22
|
+
'>': '>',
|
|
23
|
+
''': "'",
|
|
24
|
+
'"': '"',
|
|
25
|
+
}[tag] || '')
|
|
26
|
+
);
|
|
27
|
+
};
|
package/src/api/handle-status.ts
CHANGED
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
import { ServerResponse } from 'http';
|
|
2
|
-
import { encodeHtml } from './encode-html';
|
|
3
|
-
import { IsType } from '../lib';
|
|
4
|
-
|
|
5
|
-
export const handler200 = (res: ServerResponse, msg: string | object, title = '', contentType?: string) => {
|
|
6
|
-
handlerResponse(res, 200, title, msg, contentType);
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export const handler304 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
10
|
-
handlerResponse(res, 304, '304 Not Modified', msg, contentType);
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export const handler400 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
14
|
-
handlerResponse(res, 400, '400 Bad Request', msg, contentType);
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export const handler403 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
18
|
-
handlerResponse(res, 403, '403 Forbidden', msg, contentType);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const handler405 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
22
|
-
handlerResponse(res, 405, '405 Method Not Allowed', msg, contentType);
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export const handler404 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
26
|
-
handlerResponse(res, 404, '404 Not Found', msg, contentType);
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export const handler416 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
30
|
-
handlerResponse(res, 416, '416 Range Not Satisfiable', msg, contentType);
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export const handler500 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
34
|
-
handlerResponse(res, 500, '500 Internal Server Error', msg, contentType);
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export const handler503 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
38
|
-
handlerResponse(res, 503, '503 Service Unavailable', msg, contentType);
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export const handlerResponse = (
|
|
42
|
-
res: ServerResponse,
|
|
43
|
-
statusCode: number,
|
|
44
|
-
title: string,
|
|
45
|
-
msg?: string | object,
|
|
46
|
-
contentType?: string
|
|
47
|
-
) => {
|
|
48
|
-
res.statusCode = statusCode;
|
|
49
|
-
if (res.writable) {
|
|
50
|
-
try {
|
|
51
|
-
res.setHeader('content-type', contentType || 'text/html');
|
|
52
|
-
} catch (e) {
|
|
53
|
-
// errors may have been triggered when headers being sent already
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const text = IsType.isObject(msg) ? JSON.stringify(msg) : (msg as string);
|
|
58
|
-
const html = contentType === 'application/json' ? msg : generateHtml(title, text || title);
|
|
59
|
-
res.end(html);
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export const generateHtml = (title: string, message: string) => {
|
|
63
|
-
return `${[
|
|
64
|
-
'<!doctype html>',
|
|
65
|
-
'<html>',
|
|
66
|
-
' <head>',
|
|
67
|
-
' <meta charset="utf-8">',
|
|
68
|
-
` <title>${title}</title>`,
|
|
69
|
-
' </head>',
|
|
70
|
-
' <body>',
|
|
71
|
-
` <p>${encodeHtml(message)}</p>`,
|
|
72
|
-
' </body>',
|
|
73
|
-
'</html>',
|
|
74
|
-
].join('\n')}\n`;
|
|
75
|
-
};
|
|
1
|
+
import { ServerResponse } from 'http';
|
|
2
|
+
import { encodeHtml } from './encode-html';
|
|
3
|
+
import { IsType } from '../lib';
|
|
4
|
+
|
|
5
|
+
export const handler200 = (res: ServerResponse, msg: string | object, title = '', contentType?: string) => {
|
|
6
|
+
handlerResponse(res, 200, title, msg, contentType);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const handler304 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
10
|
+
handlerResponse(res, 304, '304 Not Modified', msg, contentType);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const handler400 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
14
|
+
handlerResponse(res, 400, '400 Bad Request', msg, contentType);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const handler403 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
18
|
+
handlerResponse(res, 403, '403 Forbidden', msg, contentType);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const handler405 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
22
|
+
handlerResponse(res, 405, '405 Method Not Allowed', msg, contentType);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const handler404 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
26
|
+
handlerResponse(res, 404, '404 Not Found', msg, contentType);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const handler416 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
30
|
+
handlerResponse(res, 416, '416 Range Not Satisfiable', msg, contentType);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const handler500 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
34
|
+
handlerResponse(res, 500, '500 Internal Server Error', msg, contentType);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const handler503 = (res: ServerResponse, msg?: string | object, contentType?: string) => {
|
|
38
|
+
handlerResponse(res, 503, '503 Service Unavailable', msg, contentType);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const handlerResponse = (
|
|
42
|
+
res: ServerResponse,
|
|
43
|
+
statusCode: number,
|
|
44
|
+
title: string,
|
|
45
|
+
msg?: string | object,
|
|
46
|
+
contentType?: string
|
|
47
|
+
) => {
|
|
48
|
+
res.statusCode = statusCode;
|
|
49
|
+
if (res.writable) {
|
|
50
|
+
try {
|
|
51
|
+
res.setHeader('content-type', contentType || 'text/html');
|
|
52
|
+
} catch (e) {
|
|
53
|
+
// errors may have been triggered when headers being sent already
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const text = IsType.isObject(msg) ? JSON.stringify(msg) : (msg as string);
|
|
58
|
+
const html = contentType === 'application/json' ? msg : generateHtml(title, text || title);
|
|
59
|
+
res.end(html);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const generateHtml = (title: string, message: string) => {
|
|
63
|
+
return `${[
|
|
64
|
+
'<!doctype html>',
|
|
65
|
+
'<html>',
|
|
66
|
+
' <head>',
|
|
67
|
+
' <meta charset="utf-8">',
|
|
68
|
+
` <title>${title}</title>`,
|
|
69
|
+
' </head>',
|
|
70
|
+
' <body>',
|
|
71
|
+
` <p>${encodeHtml(message)}</p>`,
|
|
72
|
+
' </body>',
|
|
73
|
+
'</html>',
|
|
74
|
+
].join('\n')}\n`;
|
|
75
|
+
};
|
package/src/api/index.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
export * from './api-cache';
|
|
2
|
-
export * from './api-helper';
|
|
3
|
-
export * from './api-module';
|
|
4
|
-
export * from './api-router';
|
|
5
|
-
export * from './api-shared-storage';
|
|
6
|
-
export * from './async-storage';
|
|
7
|
-
export * from './debug-service';
|
|
8
|
-
export * from './encode-html';
|
|
9
|
-
export * from './handle-status';
|
|
10
|
-
export * from './mini-web-socket';
|
|
11
|
-
export * from './server-content-type';
|
|
12
|
-
export * from './server-render';
|
|
13
|
-
export * from './simple-storage';
|
|
14
|
-
export * from './static-server';
|
|
15
|
-
export * from './to-client-delivery';
|
|
16
|
-
|
|
1
|
+
export * from './api-cache';
|
|
2
|
+
export * from './api-helper';
|
|
3
|
+
export * from './api-module';
|
|
4
|
+
export * from './api-router';
|
|
5
|
+
export * from './api-shared-storage';
|
|
6
|
+
export * from './async-storage';
|
|
7
|
+
export * from './debug-service';
|
|
8
|
+
export * from './encode-html';
|
|
9
|
+
export * from './handle-status';
|
|
10
|
+
export * from './mini-web-socket';
|
|
11
|
+
export * from './server-content-type';
|
|
12
|
+
export * from './server-render';
|
|
13
|
+
export * from './simple-storage';
|
|
14
|
+
export * from './static-server';
|
|
15
|
+
export * from './to-client-delivery';
|