@dooboostore/simple-boot-http-server 1.0.23 → 1.0.24
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 +27 -1
- package/dist/cjs/SimpleBootHttpServer.js +79 -39
- package/dist/cjs/SimpleBootHttpServer.js.map +3 -3
- package/dist/cjs/codes/HttpHeaders.js +14 -3
- package/dist/cjs/codes/HttpHeaders.js.map +2 -2
- package/dist/cjs/codes/Mimes.js +2 -0
- package/dist/cjs/codes/Mimes.js.map +2 -2
- package/dist/cjs/decorators/MethodMapping.js +7 -6
- package/dist/cjs/decorators/MethodMapping.js.map +3 -3
- package/dist/cjs/endpoints/WebSocketEndPoint.js +16 -0
- package/dist/cjs/endpoints/WebSocketEndPoint.js.map +7 -0
- package/dist/cjs/filters/CacheFilter.js +75 -35
- package/dist/cjs/filters/CacheFilter.js.map +3 -3
- package/dist/cjs/filters/IntentSchemeFilter.js +98 -0
- package/dist/cjs/filters/IntentSchemeFilter.js.map +7 -0
- package/dist/cjs/filters/ResourceFilter.js +4 -1
- package/dist/cjs/filters/ResourceFilter.js.map +2 -2
- package/dist/cjs/filters/TopicProtocolFilter.js +343 -0
- package/dist/cjs/filters/TopicProtocolFilter.js.map +7 -0
- package/dist/cjs/filters/index.js +2 -0
- package/dist/cjs/filters/index.js.map +2 -2
- package/dist/cjs/index.js +28 -0
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/models/RequestResponse.js +51 -89
- package/dist/cjs/models/RequestResponse.js.map +3 -3
- package/dist/cjs/models/datas/body/ReqPathData.js +29 -0
- package/dist/cjs/models/datas/body/ReqPathData.js.map +7 -0
- package/dist/cjs/option/HttpServerOption.js +25 -5
- package/dist/cjs/option/HttpServerOption.js.map +2 -2
- package/dist/cjs/proxy/SymbolIntentApiServiceProxy.js +141 -0
- package/dist/cjs/proxy/SymbolIntentApiServiceProxy.js.map +7 -0
- package/dist/cjs/proxy/SymbolIntentApiServiceProxyBase.js +35 -0
- package/dist/cjs/proxy/SymbolIntentApiServiceProxyBase.js.map +7 -0
- package/dist/cjs/proxy/TopicProtocolApiServiceProxy.js +324 -0
- package/dist/cjs/proxy/TopicProtocolApiServiceProxy.js.map +7 -0
- package/dist/cjs/proxy/index.js +26 -0
- package/dist/cjs/proxy/index.js.map +7 -0
- package/dist/cjs/resolvers/ResourceResolver.js +2 -2
- package/dist/cjs/resolvers/ResourceResolver.js.map +2 -2
- package/dist/cjs/session/SessionManager.js +2 -2
- package/dist/cjs/session/SessionManager.js.map +2 -2
- package/dist/cjs/websocket/WebSocketClient.js +554 -0
- package/dist/cjs/websocket/WebSocketClient.js.map +7 -0
- package/dist/cjs/websocket/WebSocketManager.js +652 -0
- package/dist/cjs/websocket/WebSocketManager.js.map +7 -0
- package/dist/cjs/websocket/index.js +24 -0
- package/dist/cjs/websocket/index.js.map +7 -0
- package/dist/esm/SimpleBootHttpServer.js +59 -19
- package/dist/esm/SimpleBootHttpServer.js.map +2 -2
- package/dist/esm/codes/HttpHeaders.js +12 -2
- package/dist/esm/codes/HttpHeaders.js.map +2 -2
- package/dist/esm/codes/Mimes.js +2 -0
- package/dist/esm/codes/Mimes.js.map +2 -2
- package/dist/esm/decorators/MethodMapping.js +2 -1
- package/dist/esm/decorators/MethodMapping.js.map +2 -2
- package/dist/esm/endpoints/WebSocketEndPoint.js +1 -0
- package/dist/esm/endpoints/WebSocketEndPoint.js.map +7 -0
- package/dist/esm/filters/CacheFilter.js +67 -27
- package/dist/esm/filters/CacheFilter.js.map +2 -2
- package/dist/esm/filters/IntentSchemeFilter.js +75 -0
- package/dist/esm/filters/IntentSchemeFilter.js.map +7 -0
- package/dist/esm/filters/ResourceFilter.js +4 -1
- package/dist/esm/filters/ResourceFilter.js.map +2 -2
- package/dist/esm/filters/TopicProtocolFilter.js +310 -0
- package/dist/esm/filters/TopicProtocolFilter.js.map +7 -0
- package/dist/esm/filters/index.js +1 -0
- package/dist/esm/filters/index.js.map +2 -2
- package/dist/esm/index.js +12 -0
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/models/RequestResponse.js +46 -84
- package/dist/esm/models/RequestResponse.js.map +2 -2
- package/dist/esm/models/datas/body/ReqPathData.js +6 -0
- package/dist/esm/models/datas/body/ReqPathData.js.map +7 -0
- package/dist/esm/option/HttpServerOption.js +24 -4
- package/dist/esm/option/HttpServerOption.js.map +2 -2
- package/dist/esm/proxy/SymbolIntentApiServiceProxy.js +118 -0
- package/dist/esm/proxy/SymbolIntentApiServiceProxy.js.map +7 -0
- package/dist/esm/proxy/SymbolIntentApiServiceProxyBase.js +12 -0
- package/dist/esm/proxy/SymbolIntentApiServiceProxyBase.js.map +7 -0
- package/dist/esm/proxy/TopicProtocolApiServiceProxy.js +301 -0
- package/dist/esm/proxy/TopicProtocolApiServiceProxy.js.map +7 -0
- package/dist/esm/proxy/index.js +4 -0
- package/dist/esm/proxy/index.js.map +7 -0
- package/dist/esm/resolvers/ResourceResolver.js +1 -1
- package/dist/esm/resolvers/ResourceResolver.js.map +1 -1
- package/dist/esm/session/SessionManager.js +1 -1
- package/dist/esm/session/SessionManager.js.map +1 -1
- package/dist/esm/websocket/WebSocketClient.js +529 -0
- package/dist/esm/websocket/WebSocketClient.js.map +7 -0
- package/dist/esm/websocket/WebSocketManager.js +622 -0
- package/dist/esm/websocket/WebSocketManager.js.map +7 -0
- package/dist/esm/websocket/index.js +3 -0
- package/dist/esm/websocket/index.js.map +7 -0
- package/dist/esm-bundle/dooboostore-simple-boot-http-server.esm.js +13190 -4886
- package/dist/esm-bundle/dooboostore-simple-boot-http-server.esm.js.map +4 -4
- package/dist/types/SimpleBootHttpServer.d.ts +5 -3
- package/dist/types/SimpleBootHttpServer.d.ts.map +1 -1
- package/dist/types/codes/HttpHeaders.d.ts +3 -0
- package/dist/types/codes/HttpHeaders.d.ts.map +1 -1
- package/dist/types/codes/Mimes.d.ts +3 -1
- package/dist/types/codes/Mimes.d.ts.map +1 -1
- package/dist/types/decorators/MethodMapping.d.ts +3 -2
- package/dist/types/decorators/MethodMapping.d.ts.map +1 -1
- package/dist/types/endpoints/WebSocketEndPoint.d.ts +17 -0
- package/dist/types/endpoints/WebSocketEndPoint.d.ts.map +1 -0
- package/dist/types/filters/CacheFilter.d.ts +6 -0
- package/dist/types/filters/CacheFilter.d.ts.map +1 -1
- package/dist/types/filters/IntentSchemeFilter.d.ts +20 -0
- package/dist/types/filters/IntentSchemeFilter.d.ts.map +1 -0
- package/dist/types/filters/ResourceFilter.d.ts.map +1 -1
- package/dist/types/filters/TopicProtocolFilter.d.ts +34 -0
- package/dist/types/filters/TopicProtocolFilter.d.ts.map +1 -0
- package/dist/types/filters/index.d.ts +1 -0
- package/dist/types/filters/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +12 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/models/RequestResponse.d.ts +9 -6
- package/dist/types/models/RequestResponse.d.ts.map +1 -1
- package/dist/types/models/datas/body/ReqPathData.d.ts +4 -0
- package/dist/types/models/datas/body/ReqPathData.d.ts.map +1 -0
- package/dist/types/option/HttpServerOption.d.ts +30 -20
- package/dist/types/option/HttpServerOption.d.ts.map +1 -1
- package/dist/types/proxy/SymbolIntentApiServiceProxy.d.ts +18 -0
- package/dist/types/proxy/SymbolIntentApiServiceProxy.d.ts.map +1 -0
- package/dist/types/proxy/SymbolIntentApiServiceProxyBase.d.ts +5 -0
- package/dist/types/proxy/SymbolIntentApiServiceProxyBase.d.ts.map +1 -0
- package/dist/types/proxy/TopicProtocolApiServiceProxy.d.ts +28 -0
- package/dist/types/proxy/TopicProtocolApiServiceProxy.d.ts.map +1 -0
- package/dist/types/proxy/index.d.ts +4 -0
- package/dist/types/proxy/index.d.ts.map +1 -0
- package/dist/types/session/SessionManager.d.ts +2 -17
- package/dist/types/session/SessionManager.d.ts.map +1 -1
- package/dist/types/websocket/WebSocketClient.d.ts +88 -0
- package/dist/types/websocket/WebSocketClient.d.ts.map +1 -0
- package/dist/types/websocket/WebSocketManager.d.ts +163 -0
- package/dist/types/websocket/WebSocketManager.d.ts.map +1 -0
- package/dist/types/websocket/index.d.ts +3 -0
- package/dist/types/websocket/index.d.ts.map +1 -0
- package/dist/umd-bundle/dooboostore-simple-boot-http-server.umd.js +13262 -4958
- package/dist/umd-bundle/dooboostore-simple-boot-http-server.umd.js.map +4 -4
- package/dist/umd-bundle/websocket-client.umd.js +11194 -0
- package/dist/umd-bundle/websocket-client.umd.js.map +7 -0
- package/package.json +24 -15
- package/src/SimpleBootHttpServer.ts +366 -312
- package/src/codes/HttpHeaders.ts +13 -2
- package/src/codes/Mimes.ts +27 -25
- package/src/decorators/MethodMapping.ts +4 -3
- package/src/endpoints/WebSocketEndPoint.ts +12 -0
- package/src/filters/CacheFilter.ts +112 -67
- package/src/filters/IntentSchemeFilter.ts +69 -0
- package/src/filters/ResourceFilter.ts +6 -1
- package/src/filters/TopicProtocolFilter.ts +349 -0
- package/src/filters/index.ts +1 -0
- package/src/index.ts +12 -12
- package/src/models/RequestResponse.ts +108 -240
- package/src/models/datas/body/ReqPathData.ts +3 -0
- package/src/option/HttpServerOption.ts +73 -23
- package/src/proxy/SymbolIntentApiServiceProxy.ts +120 -0
- package/src/proxy/SymbolIntentApiServiceProxyBase.ts +16 -0
- package/src/proxy/TopicProtocolApiServiceProxy.ts +328 -0
- package/src/proxy/index.ts +3 -0
- package/src/resolvers/ResourceResolver.ts +1 -1
- package/src/session/SessionManager.ts +3 -3
- package/src/websocket/WebSocketClient.ts +637 -0
- package/src/websocket/WebSocketManager.ts +713 -0
- package/src/websocket/index.ts +2 -0
- package/tsconfig.json +3 -1
package/src/codes/HttpHeaders.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Mimes } from './Mimes';
|
|
2
|
+
|
|
1
3
|
export const HttpHeaders = {
|
|
2
4
|
ContentLength : 'Content-Length',
|
|
3
5
|
ContentType : 'Content-Type',
|
|
@@ -38,6 +40,15 @@ export const HttpHeaders = {
|
|
|
38
40
|
IfNoneMatch : 'If-None-Match',
|
|
39
41
|
IfRange : 'If-Range',
|
|
40
42
|
Allow : 'Allow',
|
|
41
|
-
Server : 'Server'
|
|
43
|
+
Server : 'Server',
|
|
44
|
+
XSimpleBootSsrIntentScheme: 'x-simple-boot-ssr-intent-scheme',
|
|
45
|
+
XSimpleBootHttpTopicProtocol: 'x-simple-boot-http-topic-protocol'
|
|
42
46
|
}
|
|
43
|
-
export type HttpHeadersType = keyof typeof HttpHeaders;
|
|
47
|
+
export type HttpHeadersType = keyof typeof HttpHeaders;
|
|
48
|
+
|
|
49
|
+
export const makeIntentHeaderBySymbolFor = (symbol: Symbol): HeadersInit => {
|
|
50
|
+
return {
|
|
51
|
+
[HttpHeaders.Accept]: Mimes.ApplicationJsonPostSimpleBootSsrIntentScheme,
|
|
52
|
+
[HttpHeaders.XSimpleBootSsrIntentScheme]: `Symbol.for(${symbol.description})`
|
|
53
|
+
}
|
|
54
|
+
};
|
package/src/codes/Mimes.ts
CHANGED
|
@@ -1,27 +1,29 @@
|
|
|
1
1
|
export enum Mimes {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
2
|
+
ApplicationJson = 'application/json',
|
|
3
|
+
ApplicationOctetStream = 'application/octet-stream',
|
|
4
|
+
ApplicationXml = 'application/xml',
|
|
5
|
+
ApplicationJsonPatch = 'application/json-patch+json',
|
|
6
|
+
ApplicationXWwwFormUrlencoded = 'application/x-www-form-urlencoded',
|
|
7
|
+
TextPlain = 'text/plain',
|
|
8
|
+
TextHtml = 'text/html',
|
|
9
|
+
All = '*/*',
|
|
10
|
+
TextCss = 'text/css',
|
|
11
|
+
TextJavascript = 'text/javascript',
|
|
12
|
+
TextXml = 'text/xml',
|
|
13
|
+
TextXslt = 'text/xslt',
|
|
14
|
+
TextXmlDtd = 'text/xml-dtd',
|
|
15
|
+
TextPlainUtf8 = 'text/plain; charset=utf-8',
|
|
16
|
+
ImagePng = 'image/png',
|
|
17
|
+
ImageGif = 'image/gif',
|
|
18
|
+
ImageJpeg = 'image/jpeg',
|
|
19
|
+
ImageSvg = 'image/svg+xml',
|
|
20
|
+
ImageBmp = 'image/bmp',
|
|
21
|
+
ImageTiff = 'image/tiff',
|
|
22
|
+
ImagePdf = 'application/pdf',
|
|
23
|
+
ImageXBmp = 'image/x-bmp',
|
|
24
|
+
ImageXpixmap = 'image/x-xpixmap',
|
|
25
|
+
ImageXxbm = 'image/x-xbm',
|
|
26
|
+
MultipartFormData = 'multipart/form-data',
|
|
27
|
+
ApplicationJsonPostSimpleBootSsrIntentScheme = 'application/json-post+simple-boot-ssr-intent-scheme',
|
|
28
|
+
Intent = 'intent'
|
|
27
29
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { ConstructorType } from '@dooboostore/core
|
|
2
|
-
import { ReflectMethod } from '@dooboostore/simple-boot
|
|
3
|
-
import { ReflectUtils } from '@dooboostore/core
|
|
1
|
+
import { ConstructorType } from '@dooboostore/core';
|
|
2
|
+
import { ReflectMethod } from '@dooboostore/simple-boot';
|
|
3
|
+
import { ReflectUtils } from '@dooboostore/core';
|
|
4
4
|
import { Resolver } from '../resolvers/Resolver';
|
|
5
5
|
import { Mimes } from '../codes/Mimes';
|
|
6
6
|
import { HttpMethod } from '../codes/HttpMethod';
|
|
7
7
|
|
|
8
8
|
export enum UrlMappingSituationType {
|
|
9
9
|
REQ_JSON_BODY = 'SIMPLE_BOOT_HTTP_SERVER://URLMAPPING/REQ_JSON_BODY',
|
|
10
|
+
REQ_PATH_DATA = 'SIMPLE_BOOT_HTTP_SERVER://URLMAPPING/REQ_PATH_DATA',
|
|
10
11
|
// REQ_URL_SEARCH_PARAMS = 'SIMPLE_BOOT_HTTP_SERVER://URLMAPPING/REQ_URL_SEARCH_PARAMS',
|
|
11
12
|
REQ_URL_SEARCH_PARAMS_OBJ = 'SIMPLE_BOOT_HTTP_SERVER://URLMAPPING/REQ_URL_SEARCH_PARAMS_OBJ',
|
|
12
13
|
REQ_FORM_URL_BODY = 'SIMPLE_BOOT_HTTP_SERVER://URLMAPPING/REQ_FORM_URL_BODY'
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RawData, WebSocket } from 'ws';
|
|
2
|
+
import { IncomingMessage } from 'http';
|
|
3
|
+
|
|
4
|
+
export type WebSocketSet ={ socket: WebSocket, request: IncomingMessage };
|
|
5
|
+
export interface WebSocketEndPoint {
|
|
6
|
+
|
|
7
|
+
connect?(wsSet: WebSocketSet): void;
|
|
8
|
+
ping?(wsSet: WebSocketSet): void;
|
|
9
|
+
close?(wsSet: WebSocketSet): void;
|
|
10
|
+
message(wsSet: WebSocketSet, data: {data: RawData, isBinary: boolean}): void;
|
|
11
|
+
error?(wsSet: WebSocketSet, error: Error): void;
|
|
12
|
+
}
|
|
@@ -4,85 +4,63 @@ import { RequestResponse } from '../models/RequestResponse';
|
|
|
4
4
|
import { SimpleBootHttpServer } from '../SimpleBootHttpServer';
|
|
5
5
|
import * as fs from 'fs';
|
|
6
6
|
import * as path from 'path';
|
|
7
|
-
import {
|
|
8
|
-
import { ConvertUtils } from '@dooboostore/core-node
|
|
9
|
-
import { RandomUtils } from '@dooboostore/core
|
|
7
|
+
import { FileUtils } from '@dooboostore/core-node';
|
|
8
|
+
import { ConvertUtils } from '@dooboostore/core-node';
|
|
9
|
+
import { RandomUtils } from '@dooboostore/core';
|
|
10
10
|
|
|
11
|
-
export type CacheFilterSessionType = { key: string
|
|
11
|
+
export type CacheFilterSessionType = { key: string; lifeTime: number; isUpdate: boolean; responseHeaders?: string[] };
|
|
12
12
|
|
|
13
|
-
type CacheConfigCacheItemType = { path: string
|
|
13
|
+
type CacheConfigCacheItemType = { path: string; createTime: number; lifeTime: number; responseHeader: { [key: string]: string | number | string[] } };
|
|
14
14
|
|
|
15
|
-
type KeyAndConfig = { key: string
|
|
15
|
+
type KeyAndConfig = { key: string; lifeTime?: number; responseHeaders?: string[] };
|
|
16
16
|
|
|
17
17
|
export class CacheFilter implements Filter {
|
|
18
|
+
public static readonly SYMBOL = Symbol.for('CacheFilter');
|
|
18
19
|
public static readonly CARRIER_KEY_NAME = '_CARRIER_CACHE_FILTER_KEY_NAME';
|
|
19
20
|
private storage = new Map<string, string | Buffer>();
|
|
20
21
|
private cacheConfig: {
|
|
21
22
|
createDate: string;
|
|
22
23
|
updateDate: string;
|
|
23
|
-
caches: { [key: string]: CacheConfigCacheItemType }
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
constructor(
|
|
27
|
-
config: {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
24
|
+
caches: { [key: string]: CacheConfigCacheItemType };
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
constructor(
|
|
28
|
+
private config: {
|
|
29
|
+
config: {
|
|
30
|
+
cacheDir?: string;
|
|
31
|
+
lifeTime: number;
|
|
32
|
+
startUpClean?: boolean;
|
|
33
|
+
};
|
|
34
|
+
filter?: (rr: RequestResponse, app: SimpleBootHttpServer) => boolean;
|
|
35
|
+
key: (rr: RequestResponse, app: SimpleBootHttpServer) => KeyAndConfig;
|
|
36
|
+
}
|
|
37
|
+
) {
|
|
36
38
|
if (config.config.cacheDir && config.config.startUpClean) {
|
|
37
|
-
FileUtils.deleteSync(config.config.cacheDir, {options: {recursive: true}});
|
|
39
|
+
FileUtils.deleteSync(config.config.cacheDir, { options: { recursive: true } });
|
|
38
40
|
}
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
async onInit(app: SimpleBootHttpServer) {
|
|
42
|
-
}
|
|
43
|
+
async onInit(app: SimpleBootHttpServer) {}
|
|
43
44
|
|
|
44
|
-
async proceedBefore({rr, app, carrier}: {rr: RequestResponse
|
|
45
|
+
async proceedBefore({ rr, app, carrier }: { rr: RequestResponse; app: SimpleBootHttpServer; carrier: Map<string, any> }) {
|
|
45
46
|
// this.key = undefined;
|
|
46
47
|
// this.lifeTime = undefined;
|
|
47
48
|
// this.isUpdate = false;
|
|
48
|
-
const filter = this.config.filter?.(rr,app);
|
|
49
|
+
const filter = this.config.filter?.(rr, app);
|
|
49
50
|
if (!filter) {
|
|
50
51
|
return true;
|
|
51
52
|
}
|
|
52
|
-
|
|
53
|
-
FileUtils.mkdirSync(this.config.config.cacheDir, {recursive: true});
|
|
54
|
-
}
|
|
55
|
-
if (!this.cacheConfig && this.config.config.cacheDir) {
|
|
56
|
-
try {
|
|
57
|
-
this.cacheConfig = JSON.parse(ConvertUtils.toString(FileUtils.readSync([this.config.config.cacheDir, 'cacheConfig.json'], { option: {encoding: 'utf-8'}})));
|
|
58
|
-
} catch (e) {
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
const date = new Date().toISOString();
|
|
62
|
-
this.cacheConfig ??= {
|
|
63
|
-
createDate: date,
|
|
64
|
-
updateDate: date,
|
|
65
|
-
// accessDate: date,
|
|
66
|
-
caches: {}
|
|
67
|
-
};
|
|
68
|
-
this.cacheConfig.createDate ??= date;
|
|
69
|
-
this.cacheConfig.updateDate ??= date;
|
|
70
|
-
// this.cacheConfig.accessDate ??= date;
|
|
71
|
-
this.cacheConfig.caches ??= {};
|
|
72
|
-
|
|
73
|
-
|
|
53
|
+
this.initCacheConfig();
|
|
74
54
|
const rkey = this.config.key(rr, app);
|
|
75
55
|
// console.log('--path filter', rr.reqMethod(), rr.reqUrlPathName, filter, rkey)
|
|
76
|
-
const sessionData: CacheFilterSessionType = {key: rkey.key, lifeTime: rkey.lifeTime ?? this.config.config.lifeTime, isUpdate: false, responseHeaders: rkey.responseHeaders};
|
|
77
|
-
|
|
56
|
+
const sessionData: CacheFilterSessionType = { key: rkey.key, lifeTime: rkey.lifeTime ?? this.config.config.lifeTime, isUpdate: false, responseHeaders: rkey.responseHeaders };
|
|
78
57
|
|
|
79
58
|
const cache = this.cacheConfig.caches[sessionData.key];
|
|
80
59
|
if (cache) {
|
|
81
60
|
const existes = this.config.config.cacheDir ? FileUtils.existsSync(cache.path) : this.storage.has(cache.path);
|
|
82
61
|
const diffTime = Date.now() - cache.createTime;
|
|
83
62
|
if (!existes || diffTime > cache.lifeTime) {
|
|
84
|
-
|
|
85
|
-
this.deleteChunk(cache.path);
|
|
63
|
+
this.clear(sessionData.key);
|
|
86
64
|
sessionData.isUpdate = true;
|
|
87
65
|
} else {
|
|
88
66
|
rr.resSetHeaders({
|
|
@@ -92,10 +70,10 @@ export class CacheFilter implements Filter {
|
|
|
92
70
|
// console.log('chunk--->', cache.path);
|
|
93
71
|
try {
|
|
94
72
|
const chunk = this.getChunk(cache.path);
|
|
95
|
-
|
|
96
|
-
|
|
73
|
+
// console.log('chunk--->', chunk);
|
|
74
|
+
await rr.resEnd(chunk);
|
|
97
75
|
} catch (e) {
|
|
98
|
-
console.log('--------e', e)
|
|
76
|
+
console.log('--------e', e);
|
|
99
77
|
}
|
|
100
78
|
}
|
|
101
79
|
} else {
|
|
@@ -105,6 +83,73 @@ export class CacheFilter implements Filter {
|
|
|
105
83
|
return sessionData.isUpdate;
|
|
106
84
|
}
|
|
107
85
|
|
|
86
|
+
public async refresh(path: string, app: SimpleBootHttpServer) {
|
|
87
|
+
// 1. 기존 캐시 파일 삭제
|
|
88
|
+
this.clear(path);
|
|
89
|
+
|
|
90
|
+
// 2. HTTP Fetch를 통한 외부 호출 (GET)
|
|
91
|
+
// 아키텍트님의 지시에 따라 네트워크 레이어를 통해 정석대로 호출합니다.
|
|
92
|
+
const url = `${app.option.protocol}://${app.option.hostname}:${app.option.port}${path}`;
|
|
93
|
+
try {
|
|
94
|
+
await fetch(url, {
|
|
95
|
+
method: 'GET',
|
|
96
|
+
headers: {
|
|
97
|
+
'x-cache-force-refresh': 'true',
|
|
98
|
+
'User-Agent': 'SimpleBootHttpServer-Internal-Refresh'
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
} catch (e) {
|
|
102
|
+
console.error('CacheFilter.refresh fetch error:', e);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public clear(key: string) {
|
|
107
|
+
this.initCacheConfig();
|
|
108
|
+
const cache = this.cacheConfig.caches[key];
|
|
109
|
+
if (cache) {
|
|
110
|
+
this.deleteChunk(cache.path);
|
|
111
|
+
delete this.cacheConfig.caches[key];
|
|
112
|
+
this.saveCacheConfig();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public clearAll() {
|
|
117
|
+
this.initCacheConfig();
|
|
118
|
+
Object.keys(this.cacheConfig.caches).forEach(key => {
|
|
119
|
+
this.deleteChunk(this.cacheConfig.caches[key].path);
|
|
120
|
+
});
|
|
121
|
+
this.cacheConfig.caches = {};
|
|
122
|
+
this.saveCacheConfig();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private initCacheConfig() {
|
|
126
|
+
if (this.config.config.cacheDir) {
|
|
127
|
+
FileUtils.mkdirSync(this.config.config.cacheDir, { recursive: true });
|
|
128
|
+
}
|
|
129
|
+
if (!this.cacheConfig && this.config.config.cacheDir) {
|
|
130
|
+
try {
|
|
131
|
+
this.cacheConfig = JSON.parse(ConvertUtils.toString(FileUtils.readSync([this.config.config.cacheDir, 'cacheConfig.json'], { option: { encoding: 'utf-8' } })));
|
|
132
|
+
} catch (e) {}
|
|
133
|
+
}
|
|
134
|
+
const date = new Date().toISOString();
|
|
135
|
+
this.cacheConfig ??= {
|
|
136
|
+
createDate: date,
|
|
137
|
+
updateDate: date,
|
|
138
|
+
// accessDate: date,
|
|
139
|
+
caches: {}
|
|
140
|
+
};
|
|
141
|
+
this.cacheConfig.createDate ??= date;
|
|
142
|
+
this.cacheConfig.updateDate ??= date;
|
|
143
|
+
// this.cacheConfig.accessDate ??= date;
|
|
144
|
+
this.cacheConfig.caches ??= {};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private saveCacheConfig() {
|
|
148
|
+
if (this.config.config.cacheDir && this.cacheConfig) {
|
|
149
|
+
FileUtils.write(JSON.stringify(this.cacheConfig), { path: [this.config.config.cacheDir, 'cacheConfig.json'] });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
108
153
|
private deleteChunk(path: string) {
|
|
109
154
|
if (this.config.config.cacheDir && FileUtils.existsSync(path)) {
|
|
110
155
|
FileUtils.deleteFileSync(path);
|
|
@@ -117,29 +162,31 @@ export class CacheFilter implements Filter {
|
|
|
117
162
|
return this.config.config.cacheDir ? FileUtils.readSync(path) : this.storage.get(path);
|
|
118
163
|
}
|
|
119
164
|
|
|
120
|
-
async proceedAfter({rr, app, carrier}: {rr: RequestResponse
|
|
165
|
+
async proceedAfter({ rr, app, carrier }: { rr: RequestResponse; app: SimpleBootHttpServer; carrier: Map<string, any> }) {
|
|
121
166
|
// const data = await rr.resBodyStringData()
|
|
122
|
-
const sessionData = carrier.get(CacheFilter.CARRIER_KEY_NAME) as CacheFilterSessionType
|
|
167
|
+
const sessionData = carrier.get(CacheFilter.CARRIER_KEY_NAME) as CacheFilterSessionType;
|
|
123
168
|
if (sessionData && sessionData.isUpdate) {
|
|
124
169
|
try {
|
|
125
170
|
if (this.config.config.cacheDir) {
|
|
126
|
-
FileUtils.mkdirSync([this.config.config.cacheDir, 'caches'], {recursive: true});
|
|
171
|
+
FileUtils.mkdirSync([this.config.config.cacheDir, 'caches'], { recursive: true });
|
|
127
172
|
}
|
|
128
173
|
const resBody = rr.resBodyData();
|
|
129
174
|
if (!resBody) {
|
|
130
175
|
return true;
|
|
131
176
|
}
|
|
132
|
-
let
|
|
177
|
+
let p: string | undefined = undefined;
|
|
133
178
|
const destFileUUID = RandomUtils.uuid4();
|
|
134
179
|
p = this.writeChunk(destFileUUID, resBody);
|
|
135
180
|
const updateDateNow = Date.now();
|
|
136
181
|
const updateDate = new Date(updateDateNow).toISOString();
|
|
137
182
|
this.cacheConfig.createDate ??= updateDate;
|
|
138
183
|
this.cacheConfig.updateDate = updateDate;
|
|
139
|
-
const responseHeaders: {[key: string]: string | number | string[]} = {}
|
|
140
|
-
sessionData.responseHeaders
|
|
141
|
-
|
|
142
|
-
|
|
184
|
+
const responseHeaders: { [key: string]: string | number | string[] } = {};
|
|
185
|
+
sessionData.responseHeaders
|
|
186
|
+
?.map(it => it.toLowerCase())
|
|
187
|
+
.forEach(it => {
|
|
188
|
+
responseHeaders[it] = rr.resHeader(it);
|
|
189
|
+
});
|
|
143
190
|
this.cacheConfig.caches[sessionData.key] = {
|
|
144
191
|
path: p,
|
|
145
192
|
createTime: updateDateNow,
|
|
@@ -147,20 +194,18 @@ export class CacheFilter implements Filter {
|
|
|
147
194
|
responseHeader: responseHeaders
|
|
148
195
|
};
|
|
149
196
|
|
|
150
|
-
|
|
151
|
-
FileUtils.write(JSON.stringify(this.cacheConfig), {path: [this.config.config.cacheDir, 'cacheConfig.json']});
|
|
152
|
-
}
|
|
197
|
+
this.saveCacheConfig();
|
|
153
198
|
} catch (e) {
|
|
154
|
-
console.log('---', e)
|
|
199
|
+
console.log('---', e);
|
|
155
200
|
}
|
|
156
201
|
}
|
|
157
202
|
|
|
158
203
|
return true;
|
|
159
204
|
}
|
|
160
205
|
|
|
161
|
-
private writeChunk(destFileUUID: string, resBody: string | Buffer
|
|
206
|
+
private writeChunk(destFileUUID: string, resBody: string | Buffer) {
|
|
162
207
|
if (this.config.config.cacheDir) {
|
|
163
|
-
return FileUtils.write(resBody, {path: [this.config.config.cacheDir, 'caches', destFileUUID]});
|
|
208
|
+
return FileUtils.write(resBody, { path: [this.config.config.cacheDir, 'caches', destFileUUID] });
|
|
164
209
|
} else {
|
|
165
210
|
this.storage.set(destFileUUID, resBody);
|
|
166
211
|
return destFileUUID;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Filter } from './Filter';
|
|
2
|
+
import { HttpHeaders } from '../codes/HttpHeaders';
|
|
3
|
+
import { Intent } from '@dooboostore/simple-boot';
|
|
4
|
+
import { IntentManager } from '@dooboostore/simple-boot';
|
|
5
|
+
import { Sim } from '@dooboostore/simple-boot';
|
|
6
|
+
import { HttpStatus } from '../codes/HttpStatus';
|
|
7
|
+
import { RequestResponse } from '../models/RequestResponse';
|
|
8
|
+
import { SimpleBootHttpServer } from '../SimpleBootHttpServer';
|
|
9
|
+
import { Mimes } from '../codes/Mimes';
|
|
10
|
+
import { NotFoundError } from '../errors/NotFoundError';
|
|
11
|
+
|
|
12
|
+
@Sim
|
|
13
|
+
export class IntentSchemeFilter implements Filter {
|
|
14
|
+
constructor(private intentManager: IntentManager) {}
|
|
15
|
+
|
|
16
|
+
async onInit(app: SimpleBootHttpServer) {}
|
|
17
|
+
|
|
18
|
+
async proceedBefore({ rr, app }: { rr: RequestResponse; app: SimpleBootHttpServer; carrier: Map<string, any> }) {
|
|
19
|
+
// const rr = rrr;
|
|
20
|
+
const url = rr.reqUrl;
|
|
21
|
+
const contentLength = Number(rr.reqHeader(HttpHeaders.ContentLength.toLowerCase() ?? '0'));
|
|
22
|
+
const acceptType = rr.reqHeader(HttpHeaders.Accept);
|
|
23
|
+
const contentType = rr.reqHeader(HttpHeaders.ContentType);
|
|
24
|
+
const intentScheme = rr.reqHeader(HttpHeaders.XSimpleBootSsrIntentScheme);
|
|
25
|
+
if (acceptType === Mimes.ApplicationJsonPostSimpleBootSsrIntentScheme && intentScheme) {
|
|
26
|
+
let intent = new Intent(`${intentScheme}:/${url}`);
|
|
27
|
+
// console.log('---------1--intent request', intentScheme, url, contentLength, contentType );
|
|
28
|
+
// const responseHeader = {} as any;
|
|
29
|
+
// responseHeader[HttpHeaders.ContentType] = Mimes.ApplicationJson;
|
|
30
|
+
if (contentLength > 0) {
|
|
31
|
+
if (contentType.includes(Mimes.ApplicationJson)) {
|
|
32
|
+
intent.data = [await rr.reqBodyJsonData(), rr];
|
|
33
|
+
} else if (contentType.includes(Mimes.MultipartFormData)) {
|
|
34
|
+
intent.data = [await rr.reqBodyMultipartFormDataObject(), rr];
|
|
35
|
+
}
|
|
36
|
+
// console.log('---------2--intent request',intent);
|
|
37
|
+
const { return: rdatas, target } = await this.intentManager.publishMeta(intent);
|
|
38
|
+
if (target.length <= 0) {
|
|
39
|
+
throw new NotFoundError({ message: 'Not found intent target' });
|
|
40
|
+
}
|
|
41
|
+
const rdata = rdatas[0];
|
|
42
|
+
const wdata = rdata instanceof Promise ? await rdata : rdata;
|
|
43
|
+
rr.resStatusCode(HttpStatus.Ok);
|
|
44
|
+
rr.resSetHeader(HttpHeaders.ContentType, [Mimes.ApplicationJson]);
|
|
45
|
+
await rr.resEnd(wdata ? JSON.stringify(wdata) : undefined);
|
|
46
|
+
} else {
|
|
47
|
+
intent.data = rr.reqUrlSearchParamTuples.length > 0 ? [rr.reqUrlSearchParamsObj, rr] : [rr];
|
|
48
|
+
const { return: rdatas, target } = await this.intentManager.publishMeta(intent);
|
|
49
|
+
if (target.length <= 0) {
|
|
50
|
+
throw new NotFoundError({ message: 'Not found intent target' });
|
|
51
|
+
}
|
|
52
|
+
const rdata = rdatas[0];
|
|
53
|
+
const wdata = rdata instanceof Promise ? await rdata : rdata;
|
|
54
|
+
// const wdata =undefined;
|
|
55
|
+
rr.resStatusCode(HttpStatus.Ok);
|
|
56
|
+
rr.resSetHeader(HttpHeaders.ContentType, [Mimes.ApplicationJson]);
|
|
57
|
+
await rr.resEnd(wdata ? JSON.stringify(wdata) : undefined);
|
|
58
|
+
}
|
|
59
|
+
// console.log('---------3--intent request', rr.req.readable, rr.req.readableLength);
|
|
60
|
+
return false;
|
|
61
|
+
} else {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async proceedAfter({ rr, app }: { rr: RequestResponse; app: SimpleBootHttpServer; carrier: Map<string, any> }) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -47,7 +47,9 @@ export class ResourceFilter implements Filter {
|
|
|
47
47
|
// console.log('resource filter!!**-------', url);
|
|
48
48
|
let sw = true;
|
|
49
49
|
const regExps = this.resources?.filter(it => {
|
|
50
|
+
// console.log('---it', it, it instanceof RegExp, typeof it)
|
|
50
51
|
if (it instanceof RegExp) {
|
|
52
|
+
// console.log('--------', it.test(url))
|
|
51
53
|
return it.test(url);
|
|
52
54
|
}
|
|
53
55
|
|
|
@@ -62,8 +64,11 @@ export class ResourceFilter implements Filter {
|
|
|
62
64
|
if (typeof it === 'object' && 'request' in it) {
|
|
63
65
|
if (typeof it.request === 'function') {
|
|
64
66
|
return it.request(rr, app);
|
|
67
|
+
} else if (it .request instanceof RegExp) {
|
|
68
|
+
return it.request.test(url);
|
|
69
|
+
} else {
|
|
70
|
+
return RegExp(it.request).test(url);
|
|
65
71
|
}
|
|
66
|
-
return RegExp(it.request).test(url);
|
|
67
72
|
}
|
|
68
73
|
})??[]
|
|
69
74
|
// console.log('regExps', regExps, url);
|