gomtm 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/clientlib.d.ts +6 -0
- package/dist/esm/clientlib.js +44 -0
- package/dist/esm/consts.d.ts +28 -0
- package/dist/esm/consts.js +58 -0
- package/dist/esm/http/cors.d.ts +15 -0
- package/dist/esm/http/cors.js +129 -0
- package/dist/esm/http/fetchMiddleWithCache.d.ts +2 -0
- package/dist/esm/http/fetchMiddleWithCache.js +87 -0
- package/dist/esm/http/fetchMiddleWithUrlProxy.d.ts +2 -0
- package/dist/esm/http/fetchMiddleWithUrlProxy.js +28 -0
- package/dist/esm/http/mthttp.d.ts +5 -0
- package/dist/esm/http/mthttp.js +61 -0
- package/dist/esm/http/response-cache.test.js +72 -0
- package/dist/esm/messageTypeRegistry.d.ts +4 -0
- package/dist/esm/messageTypeRegistry.js +36 -0
- package/dist/esm/mtmFetcher.d.ts +13 -0
- package/dist/esm/mtmFetcher.js +145 -0
- package/dist/esm/mtmcore.d.ts +5 -0
- package/dist/esm/mtmcore.js +19 -0
- package/dist/esm/providers/GomtmProvider.d.ts +68 -0
- package/dist/esm/providers/GomtmProvider.js +99 -0
- package/dist/esm/providers/MtConnectProvider.d.ts +2 -0
- package/dist/esm/providers/MtConnectProvider.js +18 -0
- package/dist/esm/providers/ReactQueryProvider.d.ts +4 -0
- package/dist/esm/providers/ReactQueryProvider.js +63 -0
- package/dist/esm/providers/userContext.d.ts +14 -0
- package/dist/esm/providers/userContext.js +99 -0
- package/dist/esm/utils.d.ts +1 -0
- package/dist/esm/utils.js +32 -0
- package/dist/tsconfig.type.tsbuildinfo +1 -1
- package/package.json +5 -2
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export declare function mergeRefs<T = any>(refs: Array<React.MutableRefObject<T> | React.LegacyRef<T>>): React.RefCallback<T>;
|
|
3
|
+
export declare function getCookie(name: string): string | undefined;
|
|
4
|
+
export declare function deleteCookie(name: string): void;
|
|
5
|
+
export declare function deleteAllCookies(): void;
|
|
6
|
+
export declare function setCookie(name: string, value: any): void;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
function mergeRefs(refs) {
|
|
3
|
+
return (value) => {
|
|
4
|
+
refs.forEach((ref) => {
|
|
5
|
+
if (typeof ref === "function") {
|
|
6
|
+
ref(value);
|
|
7
|
+
} else if (ref != null) {
|
|
8
|
+
;
|
|
9
|
+
ref.current = value;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function getCookie(name) {
|
|
15
|
+
const cookieArr = document.cookie.split(";");
|
|
16
|
+
for (let i = 0; i < cookieArr.length; i++) {
|
|
17
|
+
const cookiePair = cookieArr[i].split("=");
|
|
18
|
+
if (name == cookiePair[0].trim()) {
|
|
19
|
+
return decodeURIComponent(cookiePair[1]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function deleteCookie(name) {
|
|
24
|
+
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
|
25
|
+
}
|
|
26
|
+
function deleteAllCookies() {
|
|
27
|
+
const cookies = document.cookie.split(";");
|
|
28
|
+
for (let i = 0; i < cookies.length; i++) {
|
|
29
|
+
const cookie = cookies[i];
|
|
30
|
+
const eqPos = cookie.indexOf("=");
|
|
31
|
+
const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
|
|
32
|
+
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function setCookie(name, value) {
|
|
36
|
+
document.cookie = `${name}=${value}`;
|
|
37
|
+
}
|
|
38
|
+
export {
|
|
39
|
+
deleteAllCookies,
|
|
40
|
+
deleteCookie,
|
|
41
|
+
getCookie,
|
|
42
|
+
mergeRefs,
|
|
43
|
+
setCookie
|
|
44
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare const COOKIE_MTM_SITE_ID = "mtm-site-id";
|
|
2
|
+
export declare const COOKIE_IS_DEBUG = "DEBUG";
|
|
3
|
+
export declare const MTM_SITE_HOSTNAME = "mtm-site-hostname";
|
|
4
|
+
export declare const MtM_TOKEN_NAME = "mtm_token33";
|
|
5
|
+
export declare const fetchMaxRetry = 3;
|
|
6
|
+
export declare const HeaderMtmHost = "Mtm-Host";
|
|
7
|
+
export declare const HeaderMtmApi = "Mtm-Api";
|
|
8
|
+
export declare const Cookie_Site_Host = "site-host";
|
|
9
|
+
export declare const MTM_SERVER_COOKIE_ACTIVATE_URL = "mtm:serverUrl";
|
|
10
|
+
export declare const MTM_SERVER_COOKIE_ACTIVATE_SELFBACKEND_URL = "mtm:selfbackendUrl";
|
|
11
|
+
export declare const DEFAULT_revalidate_SECONDS = 3;
|
|
12
|
+
export declare const LOGIN_CALLBACK_URL_KEY = "back";
|
|
13
|
+
export declare const ExtKey_Hostname = "host";
|
|
14
|
+
export declare const ExtKey_mtmaccessToken = "access_token";
|
|
15
|
+
export declare const ExtKey_mtmbackend = "mtmbackend";
|
|
16
|
+
export declare const ExtKey_SelfBackend = "selfbackend";
|
|
17
|
+
export declare const ItemAction_list_new_item = "list_new_item";
|
|
18
|
+
export declare const Listview_list_filter = "Listview_list_filter";
|
|
19
|
+
export declare const ActionEdit = "edit";
|
|
20
|
+
export declare const ActionDelete = "delete";
|
|
21
|
+
export declare const ActionCreate = "create";
|
|
22
|
+
export declare const ActionFetchNextPage = "fetchNextPage";
|
|
23
|
+
export declare const ActionRefetch = "refetch";
|
|
24
|
+
export declare const ListView_DefaultPageSize = 12;
|
|
25
|
+
export declare const ImageNoExist = "/images/404.jpg";
|
|
26
|
+
export declare const ADMIN_ROLE = "admin";
|
|
27
|
+
export declare const MEMBER_ROLE = "member";
|
|
28
|
+
export declare const TRPC_API_PREFIX = "/api.v1/trpc";
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const COOKIE_MTM_SITE_ID = "mtm-site-id";
|
|
2
|
+
const COOKIE_IS_DEBUG = "DEBUG";
|
|
3
|
+
const MTM_SITE_HOSTNAME = "mtm-site-hostname";
|
|
4
|
+
const MtM_TOKEN_NAME = "mtm_token33";
|
|
5
|
+
const fetchMaxRetry = 3;
|
|
6
|
+
const HeaderMtmHost = "Mtm-Host";
|
|
7
|
+
const HeaderMtmApi = "Mtm-Api";
|
|
8
|
+
const Cookie_Site_Host = "site-host";
|
|
9
|
+
const MTM_SERVER_COOKIE_ACTIVATE_URL = "mtm:serverUrl";
|
|
10
|
+
const MTM_SERVER_COOKIE_ACTIVATE_SELFBACKEND_URL = "mtm:selfbackendUrl";
|
|
11
|
+
const DEFAULT_revalidate_SECONDS = 3;
|
|
12
|
+
const LOGIN_CALLBACK_URL_KEY = "back";
|
|
13
|
+
const ExtKey_Hostname = "host";
|
|
14
|
+
const ExtKey_mtmaccessToken = "access_token";
|
|
15
|
+
const ExtKey_mtmbackend = "mtmbackend";
|
|
16
|
+
const ExtKey_SelfBackend = "selfbackend";
|
|
17
|
+
const ItemAction_list_new_item = "list_new_item";
|
|
18
|
+
const Listview_list_filter = "Listview_list_filter";
|
|
19
|
+
const ActionEdit = "edit";
|
|
20
|
+
const ActionDelete = "delete";
|
|
21
|
+
const ActionCreate = "create";
|
|
22
|
+
const ActionFetchNextPage = "fetchNextPage";
|
|
23
|
+
const ActionRefetch = "refetch";
|
|
24
|
+
const ListView_DefaultPageSize = 12;
|
|
25
|
+
const ImageNoExist = "/images/404.jpg";
|
|
26
|
+
const ADMIN_ROLE = "admin";
|
|
27
|
+
const MEMBER_ROLE = "member";
|
|
28
|
+
const TRPC_API_PREFIX = "/api.v1/trpc";
|
|
29
|
+
export {
|
|
30
|
+
ADMIN_ROLE,
|
|
31
|
+
ActionCreate,
|
|
32
|
+
ActionDelete,
|
|
33
|
+
ActionEdit,
|
|
34
|
+
ActionFetchNextPage,
|
|
35
|
+
ActionRefetch,
|
|
36
|
+
COOKIE_IS_DEBUG,
|
|
37
|
+
COOKIE_MTM_SITE_ID,
|
|
38
|
+
Cookie_Site_Host,
|
|
39
|
+
DEFAULT_revalidate_SECONDS,
|
|
40
|
+
ExtKey_Hostname,
|
|
41
|
+
ExtKey_SelfBackend,
|
|
42
|
+
ExtKey_mtmaccessToken,
|
|
43
|
+
ExtKey_mtmbackend,
|
|
44
|
+
HeaderMtmApi,
|
|
45
|
+
HeaderMtmHost,
|
|
46
|
+
ImageNoExist,
|
|
47
|
+
ItemAction_list_new_item,
|
|
48
|
+
LOGIN_CALLBACK_URL_KEY,
|
|
49
|
+
ListView_DefaultPageSize,
|
|
50
|
+
Listview_list_filter,
|
|
51
|
+
MEMBER_ROLE,
|
|
52
|
+
MTM_SERVER_COOKIE_ACTIVATE_SELFBACKEND_URL,
|
|
53
|
+
MTM_SERVER_COOKIE_ACTIVATE_URL,
|
|
54
|
+
MTM_SITE_HOSTNAME,
|
|
55
|
+
MtM_TOKEN_NAME,
|
|
56
|
+
TRPC_API_PREFIX,
|
|
57
|
+
fetchMaxRetry
|
|
58
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
type StaticOrigin = boolean | string | RegExp | (boolean | string | RegExp)[];
|
|
2
|
+
type OriginFn = (origin: string | undefined, req: Request) => StaticOrigin | Promise<StaticOrigin>;
|
|
3
|
+
interface CorsOptions {
|
|
4
|
+
origin?: StaticOrigin | OriginFn;
|
|
5
|
+
methods?: string | string[];
|
|
6
|
+
allowedHeaders?: string | string[];
|
|
7
|
+
exposedHeaders?: string | string[];
|
|
8
|
+
credentials?: boolean;
|
|
9
|
+
maxAge?: number;
|
|
10
|
+
preflightContinue?: boolean;
|
|
11
|
+
optionsSuccessStatus?: number;
|
|
12
|
+
}
|
|
13
|
+
export default function cors(req: Request, res: Response, options?: CorsOptions): Promise<Response>;
|
|
14
|
+
export declare function initCors(options?: CorsOptions): (req: Request, res: Response) => Promise<Response>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __async = (__this, __arguments, generator) => {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
var fulfilled = (value) => {
|
|
20
|
+
try {
|
|
21
|
+
step(generator.next(value));
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var rejected = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.throw(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
34
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
const defaultOptions = {
|
|
38
|
+
origin: "*",
|
|
39
|
+
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
|
|
40
|
+
preflightContinue: false,
|
|
41
|
+
optionsSuccessStatus: 204
|
|
42
|
+
};
|
|
43
|
+
function isOriginAllowed(origin, allowed) {
|
|
44
|
+
return Array.isArray(allowed) ? allowed.some((o) => isOriginAllowed(origin, o)) : typeof allowed === "string" ? origin === allowed : allowed instanceof RegExp ? allowed.test(origin) : !!allowed;
|
|
45
|
+
}
|
|
46
|
+
function getOriginHeaders(reqOrigin, origin) {
|
|
47
|
+
const headers = new Headers();
|
|
48
|
+
if (origin === "*") {
|
|
49
|
+
headers.set("Access-Control-Allow-Origin", "*");
|
|
50
|
+
} else if (typeof origin === "string") {
|
|
51
|
+
headers.set("Access-Control-Allow-Origin", origin);
|
|
52
|
+
headers.append("Vary", "Origin");
|
|
53
|
+
} else {
|
|
54
|
+
const allowed = isOriginAllowed(reqOrigin != null ? reqOrigin : "", origin);
|
|
55
|
+
if (allowed && reqOrigin) {
|
|
56
|
+
headers.set("Access-Control-Allow-Origin", reqOrigin);
|
|
57
|
+
}
|
|
58
|
+
headers.append("Vary", "Origin");
|
|
59
|
+
}
|
|
60
|
+
return headers;
|
|
61
|
+
}
|
|
62
|
+
function originHeadersFromReq(req, origin) {
|
|
63
|
+
return __async(this, null, function* () {
|
|
64
|
+
const reqOrigin = req.headers.get("Origin") || void 0;
|
|
65
|
+
const value = typeof origin === "function" ? yield origin(reqOrigin, req) : origin;
|
|
66
|
+
if (!value)
|
|
67
|
+
return;
|
|
68
|
+
return getOriginHeaders(reqOrigin, value);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function getAllowedHeaders(req, allowed) {
|
|
72
|
+
const headers = new Headers();
|
|
73
|
+
if (!allowed) {
|
|
74
|
+
allowed = req.headers.get("Access-Control-Request-Headers");
|
|
75
|
+
headers.append("Vary", "Access-Control-Request-Headers");
|
|
76
|
+
} else if (Array.isArray(allowed)) {
|
|
77
|
+
allowed = allowed.join(",");
|
|
78
|
+
}
|
|
79
|
+
if (allowed) {
|
|
80
|
+
headers.set("Access-Control-Allow-Headers", allowed);
|
|
81
|
+
}
|
|
82
|
+
return headers;
|
|
83
|
+
}
|
|
84
|
+
function cors(req, res, options) {
|
|
85
|
+
return __async(this, null, function* () {
|
|
86
|
+
var _a;
|
|
87
|
+
const opts = __spreadValues(__spreadValues({}, defaultOptions), options);
|
|
88
|
+
const { headers } = res;
|
|
89
|
+
const originHeaders = yield originHeadersFromReq(req, (_a = opts.origin) != null ? _a : false);
|
|
90
|
+
const mergeHeaders = (v, k) => {
|
|
91
|
+
if (k === "Vary")
|
|
92
|
+
headers.append(k, v);
|
|
93
|
+
else
|
|
94
|
+
headers.set(k, v);
|
|
95
|
+
};
|
|
96
|
+
if (!originHeaders)
|
|
97
|
+
return res;
|
|
98
|
+
originHeaders.forEach(mergeHeaders);
|
|
99
|
+
if (opts.credentials) {
|
|
100
|
+
headers.set("Access-Control-Allow-Credentials", "true");
|
|
101
|
+
}
|
|
102
|
+
const exposed = Array.isArray(opts.exposedHeaders) ? opts.exposedHeaders.join(",") : opts.exposedHeaders;
|
|
103
|
+
if (exposed) {
|
|
104
|
+
headers.set("Access-Control-Expose-Headers", exposed);
|
|
105
|
+
}
|
|
106
|
+
if (req.method === "OPTIONS") {
|
|
107
|
+
if (opts.methods) {
|
|
108
|
+
const methods = Array.isArray(opts.methods) ? opts.methods.join(",") : opts.methods;
|
|
109
|
+
headers.set("Access-Control-Allow-Methods", methods);
|
|
110
|
+
}
|
|
111
|
+
getAllowedHeaders(req, opts.allowedHeaders).forEach(mergeHeaders);
|
|
112
|
+
if (typeof opts.maxAge === "number") {
|
|
113
|
+
headers.set("Access-Control-Max-Age", String(opts.maxAge));
|
|
114
|
+
}
|
|
115
|
+
if (opts.preflightContinue)
|
|
116
|
+
return res;
|
|
117
|
+
headers.set("Content-Length", "0");
|
|
118
|
+
return new Response(null, { status: opts.optionsSuccessStatus, headers });
|
|
119
|
+
}
|
|
120
|
+
return res;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
function initCors(options) {
|
|
124
|
+
return (req, res) => cors(req, res, options);
|
|
125
|
+
}
|
|
126
|
+
export {
|
|
127
|
+
cors as default,
|
|
128
|
+
initCors
|
|
129
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import { LRUCache } from "lru-cache";
|
|
22
|
+
import { sha256WithAlphabets } from "../utils";
|
|
23
|
+
const options = {
|
|
24
|
+
max: 500,
|
|
25
|
+
// for use with tracking overall storage size
|
|
26
|
+
maxSize: 5e3,
|
|
27
|
+
sizeCalculation: (value, key) => {
|
|
28
|
+
return 1;
|
|
29
|
+
},
|
|
30
|
+
// for use when you need to clean up something when objects
|
|
31
|
+
// are evicted from the cache
|
|
32
|
+
// dispose: (value, key) => {
|
|
33
|
+
// // freeFromMemoryOrWhatever(value)
|
|
34
|
+
// },
|
|
35
|
+
ttl: 1e3 * 30,
|
|
36
|
+
// return stale items before removing from cache?
|
|
37
|
+
allowStale: false,
|
|
38
|
+
updateAgeOnGet: false,
|
|
39
|
+
updateAgeOnHas: false
|
|
40
|
+
};
|
|
41
|
+
const cache = new LRUCache(options);
|
|
42
|
+
const makeCacheKey = (input, init) => {
|
|
43
|
+
const req = new Request(input, init);
|
|
44
|
+
const token = req.headers.get("authorization");
|
|
45
|
+
let body = "";
|
|
46
|
+
if (init == null ? void 0 : init.body) {
|
|
47
|
+
body = init.body.toString();
|
|
48
|
+
}
|
|
49
|
+
return sha256WithAlphabets(input + token + body);
|
|
50
|
+
};
|
|
51
|
+
function fetchMiddleWithCache(fetcher) {
|
|
52
|
+
return (input, init) => __async(this, null, function* () {
|
|
53
|
+
const key = yield makeCacheKey(input, init);
|
|
54
|
+
const cacheObj = cache.get(key);
|
|
55
|
+
if (cacheObj) {
|
|
56
|
+
return new Response(new Uint8Array(cacheObj));
|
|
57
|
+
} else {
|
|
58
|
+
const response = yield fetcher(input, init);
|
|
59
|
+
if (response.status == 200 && response.body) {
|
|
60
|
+
const [s1, s2] = response.body.tee();
|
|
61
|
+
writeStreamToCache(key, s2);
|
|
62
|
+
return new Response(s1, response);
|
|
63
|
+
}
|
|
64
|
+
return response.clone();
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function writeStreamToCache(key, steam) {
|
|
69
|
+
return __async(this, null, function* () {
|
|
70
|
+
const chunks = [];
|
|
71
|
+
let done = false;
|
|
72
|
+
const s2Reader = yield steam.getReader();
|
|
73
|
+
while (!done) {
|
|
74
|
+
const { value, done: readerDone } = yield s2Reader.read();
|
|
75
|
+
done = readerDone;
|
|
76
|
+
if (value)
|
|
77
|
+
chunks.push(value);
|
|
78
|
+
}
|
|
79
|
+
const byteArray = new Uint8Array(chunks.reduce((acc, chunk) => [...acc, ...Array.from(chunk)], []));
|
|
80
|
+
if (byteArray.buffer.byteLength > 0) {
|
|
81
|
+
cache.set(key, byteArray.buffer);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
fetchMiddleWithCache
|
|
87
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
function fetchMiddleWithUrlProxy(fetcher) {
|
|
22
|
+
return (input, init) => __async(this, null, function* () {
|
|
23
|
+
return fetcher(input, init);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
fetchMiddleWithUrlProxy
|
|
28
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type FetchType = typeof fetch;
|
|
2
|
+
export declare const responseJson: (data: any) => Response;
|
|
3
|
+
export declare function jsonResponse(data: any): Promise<Response>;
|
|
4
|
+
export declare function jsonSuccessResponse(data?: any): Promise<Response>;
|
|
5
|
+
export declare function getContentType(req: Request): string | null;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
const responseJson = (data) => {
|
|
22
|
+
return new Response(JSON.stringify(data), {
|
|
23
|
+
status: 200,
|
|
24
|
+
headers: {
|
|
25
|
+
"Content-type": "application/json"
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
function jsonResponse(data) {
|
|
30
|
+
return __async(this, null, function* () {
|
|
31
|
+
try {
|
|
32
|
+
return new Response(JSON.stringify(data), {
|
|
33
|
+
status: 200,
|
|
34
|
+
headers: {
|
|
35
|
+
"Content-Type": "application/json"
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
} catch (e) {
|
|
39
|
+
console.log(e);
|
|
40
|
+
return new Response(JSON.stringify({ error: e.message }));
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function jsonSuccessResponse(data) {
|
|
45
|
+
return __async(this, null, function* () {
|
|
46
|
+
return jsonResponse({
|
|
47
|
+
success: true,
|
|
48
|
+
data
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function getContentType(req) {
|
|
53
|
+
const a = req.headers.get("Content-Type");
|
|
54
|
+
return a;
|
|
55
|
+
}
|
|
56
|
+
export {
|
|
57
|
+
getContentType,
|
|
58
|
+
jsonResponse,
|
|
59
|
+
jsonSuccessResponse,
|
|
60
|
+
responseJson
|
|
61
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import { fetchMiddleWithCache } from "./fetchMiddleWithCache";
|
|
22
|
+
test("fetch \u7F13\u5B58\u6B63\u5E38\u60C5\u51B5", () => __async(void 0, null, function* () {
|
|
23
|
+
const fetchFn = jest.fn(fetch);
|
|
24
|
+
const fetchWithCache = fetchMiddleWithCache(fetchFn);
|
|
25
|
+
const count = 10;
|
|
26
|
+
for (let i = 0; i < count; i++) {
|
|
27
|
+
fetchFn.mockReturnValueOnce(Promise.resolve(new Response("abc", {})));
|
|
28
|
+
const res = yield fetchWithCache("https://www.bing.com", {
|
|
29
|
+
headers: {
|
|
30
|
+
authorization: "token2"
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
const content = yield res.text();
|
|
34
|
+
expect(content).toContain("abc");
|
|
35
|
+
}
|
|
36
|
+
expect(fetchFn.mock.calls.length).toBe(1);
|
|
37
|
+
}));
|
|
38
|
+
test("fetch \u975E200\u54CD\u5E94\u4E0D\u505A\u7F13\u5B58", () => __async(void 0, null, function* () {
|
|
39
|
+
const fetchFn = jest.fn(fetch);
|
|
40
|
+
const fetchWithCache = fetchMiddleWithCache(fetchFn);
|
|
41
|
+
const count = 10;
|
|
42
|
+
for (let i = 0; i < count; i++) {
|
|
43
|
+
fetchFn.mockReturnValueOnce(Promise.resolve(
|
|
44
|
+
new Response("some content", {
|
|
45
|
+
status: 500
|
|
46
|
+
})
|
|
47
|
+
));
|
|
48
|
+
const res = yield fetchWithCache("https://www.bing.com", {
|
|
49
|
+
headers: {
|
|
50
|
+
authorization: "token1"
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
const content = yield res.text();
|
|
54
|
+
expect(content).toContain("some conten");
|
|
55
|
+
}
|
|
56
|
+
expect(fetchFn.mock.calls.length).toBe(count);
|
|
57
|
+
}));
|
|
58
|
+
test("fetch \u7F13\u5B58\u4E0D\u540C\u7684url", () => __async(void 0, null, function* () {
|
|
59
|
+
const fetchFn = jest.fn(fetch);
|
|
60
|
+
const count = 10;
|
|
61
|
+
const fetchWithCache = fetchMiddleWithCache(fetchFn);
|
|
62
|
+
for (let i = 0; i < count; i++) {
|
|
63
|
+
fetchFn.mockReturnValueOnce(Promise.resolve(new Response("abc", {})));
|
|
64
|
+
const res = yield fetchWithCache(`https://www.bing.com/${i}`, {
|
|
65
|
+
headers: {
|
|
66
|
+
authorization: "token2"
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
expect(yield res.text()).toContain("abc");
|
|
70
|
+
}
|
|
71
|
+
expect(fetchFn.mock.calls.length).toBe(10);
|
|
72
|
+
}));
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { IMessageTypeRegistry, ServiceType } from "@bufbuild/protobuf";
|
|
2
|
+
export declare const anypbTypeReg: IMessageTypeRegistry;
|
|
3
|
+
export declare const allServices: Record<string, ServiceType>;
|
|
4
|
+
export declare const GetMtmServiceByTypeName: (serviceTypeName: string) => ServiceType;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createRegistry } from "@bufbuild/protobuf";
|
|
2
|
+
import { BotService } from "./gomtmpb/mtm/sppb/bot_connect";
|
|
3
|
+
import { CmdStartNoVnc, CmdStartSimpleCrawler, ConsumMessageReq, CxPageReq, HelloBotMessage } from "./gomtmpb/mtm/sppb/bot_pb";
|
|
4
|
+
import { CxService } from "./gomtmpb/mtm/sppb/cx_connect";
|
|
5
|
+
import { JobService } from "./gomtmpb/mtm/sppb/job_connect";
|
|
6
|
+
import { MtmService } from "./gomtmpb/mtm/sppb/mtm_connect";
|
|
7
|
+
import { SpService } from "./gomtmpb/mtm/sppb/sp_connect";
|
|
8
|
+
const anypbTypeReg = createRegistry(
|
|
9
|
+
HelloBotMessage,
|
|
10
|
+
CxPageReq,
|
|
11
|
+
ConsumMessageReq,
|
|
12
|
+
CmdStartNoVnc,
|
|
13
|
+
CmdStartSimpleCrawler
|
|
14
|
+
);
|
|
15
|
+
const allServices = {};
|
|
16
|
+
const registerService = (...s) => {
|
|
17
|
+
s.map((item) => {
|
|
18
|
+
const servieTypeName = item.typeName;
|
|
19
|
+
allServices[servieTypeName] = item;
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
const GetMtmServiceByTypeName = (serviceTypeName) => {
|
|
23
|
+
return allServices[serviceTypeName];
|
|
24
|
+
};
|
|
25
|
+
registerService(
|
|
26
|
+
MtmService,
|
|
27
|
+
SpService,
|
|
28
|
+
BotService,
|
|
29
|
+
JobService,
|
|
30
|
+
CxService
|
|
31
|
+
);
|
|
32
|
+
export {
|
|
33
|
+
GetMtmServiceByTypeName,
|
|
34
|
+
allServices,
|
|
35
|
+
anypbTypeReg
|
|
36
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Message, PartialMessage, ServiceType } from "@bufbuild/protobuf";
|
|
2
|
+
import { MtUnaryCallError } from "./connectquery/utils";
|
|
3
|
+
import { ExtInfo } from "./mtmcore";
|
|
4
|
+
type CustomRequestInit = RequestInit & {
|
|
5
|
+
enableCache?: boolean;
|
|
6
|
+
enableLog?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const newMtmFetch: (extInfo: ExtInfo, initGlobal?: CustomRequestInit) => (input: string | URL | globalThis.Request, init?: CustomRequestInit) => Promise<Response>;
|
|
9
|
+
export declare function getMtmApiUrl(backendUrl: string): string;
|
|
10
|
+
export declare function createMtConnectTransport(backendUrl: string, extInfo: ExtInfo): import("@connectrpc/connect").Transport;
|
|
11
|
+
export declare function createClient<T extends ServiceType>(extInfo: ExtInfo, svc: T, options: CustomRequestInit): import("@connectrpc/connect").PromiseClient<T>;
|
|
12
|
+
export declare function mtmCallUnaryMethod<I extends Message<I>, O extends Message<O>>(extInfo: ExtInfo, svc: string, method: string, input?: PartialMessage<I> | undefined): Promise<O | MtUnaryCallError>;
|
|
13
|
+
export {};
|