gomtm 0.0.1 → 0.0.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/dist/esm/clientlib.d.ts +6 -0
- package/dist/esm/clientlib.js +44 -0
- package/dist/esm/connectquery/call-unary-method.d.ts +8 -0
- package/dist/esm/connectquery/call-unary-method.js +47 -0
- package/dist/esm/connectquery/connect-query-key.d.ts +16 -0
- package/dist/esm/connectquery/connect-query-key.js +15 -0
- package/dist/esm/connectquery/create-use-infinite-query-options.d.ts +21 -0
- package/dist/esm/connectquery/create-use-infinite-query-options.js +92 -0
- package/dist/esm/connectquery/create-use-query-options.d.ts +19 -0
- package/dist/esm/connectquery/create-use-query-options.js +74 -0
- package/dist/esm/connectquery/default-options.d.ts +6 -0
- package/dist/esm/connectquery/default-options.js +9 -0
- package/dist/esm/connectquery/index.d.ts +16 -0
- package/dist/esm/connectquery/index.js +33 -0
- package/dist/esm/connectquery/method-unary-descriptor.d.ts +4 -0
- package/dist/esm/connectquery/method-unary-descriptor.js +0 -0
- package/dist/esm/connectquery/use-infinite-query.d.ts +12 -0
- package/dist/esm/connectquery/use-infinite-query.js +87 -0
- package/dist/esm/connectquery/use-mutation.d.ts +10 -0
- package/dist/esm/connectquery/use-mutation.js +79 -0
- package/dist/esm/connectquery/use-query.d.ts +12 -0
- package/dist/esm/connectquery/use-query.js +75 -0
- package/dist/esm/connectquery/use-transport.d.ts +7 -0
- package/dist/esm/connectquery/use-transport.js +23 -0
- package/dist/esm/connectquery/utils.d.ts +12 -0
- package/dist/esm/connectquery/utils.js +24 -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 +100 -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 +10 -3
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
32
|
+
import {
|
|
33
|
+
useQuery as tsUseQuery,
|
|
34
|
+
useSuspenseQuery as tsUseSuspenseQuery
|
|
35
|
+
} from "@tanstack/react-query";
|
|
36
|
+
import { createUseQueryOptions } from "./create-use-query-options";
|
|
37
|
+
import { useTransport } from "./use-transport";
|
|
38
|
+
function useQuery(methodSig, input, _a = {}) {
|
|
39
|
+
var _b = _a, {
|
|
40
|
+
transport,
|
|
41
|
+
callOptions
|
|
42
|
+
} = _b, queryOptions = __objRest(_b, [
|
|
43
|
+
"transport",
|
|
44
|
+
"callOptions"
|
|
45
|
+
]);
|
|
46
|
+
var _a2;
|
|
47
|
+
const transportFromCtx = useTransport();
|
|
48
|
+
const baseOptions = createUseQueryOptions(methodSig, input, {
|
|
49
|
+
transport: transport != null ? transport : transportFromCtx,
|
|
50
|
+
callOptions
|
|
51
|
+
});
|
|
52
|
+
const enabled = baseOptions.enabled && ((_a2 = queryOptions.enabled) != null ? _a2 : true);
|
|
53
|
+
return tsUseQuery(__spreadProps(__spreadValues(__spreadValues({}, queryOptions), baseOptions), {
|
|
54
|
+
enabled
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
function useSuspenseQuery(methodSig, input, _c = {}) {
|
|
58
|
+
var _d = _c, {
|
|
59
|
+
transport,
|
|
60
|
+
callOptions
|
|
61
|
+
} = _d, queryOptions = __objRest(_d, [
|
|
62
|
+
"transport",
|
|
63
|
+
"callOptions"
|
|
64
|
+
]);
|
|
65
|
+
const transportFromCtx = useTransport();
|
|
66
|
+
const baseOptions = createUseQueryOptions(methodSig, input, {
|
|
67
|
+
transport: transport != null ? transport : transportFromCtx,
|
|
68
|
+
callOptions
|
|
69
|
+
});
|
|
70
|
+
return tsUseSuspenseQuery(__spreadValues(__spreadValues({}, queryOptions), baseOptions));
|
|
71
|
+
}
|
|
72
|
+
export {
|
|
73
|
+
useQuery,
|
|
74
|
+
useSuspenseQuery
|
|
75
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Transport } from "@connectrpc/connect";
|
|
2
|
+
import type { FC, PropsWithChildren } from "react";
|
|
3
|
+
export declare const fallbackTransport: Transport;
|
|
4
|
+
export declare const useTransport: () => Transport;
|
|
5
|
+
export declare const TransportProvider: FC<PropsWithChildren<{
|
|
6
|
+
transport: Transport;
|
|
7
|
+
}>>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { ConnectError } from "@connectrpc/connect";
|
|
4
|
+
import { createContext, useContext } from "react";
|
|
5
|
+
const fallbackTransportError = new ConnectError(
|
|
6
|
+
"To use Connect, you must provide a `Transport`: a simple object that handles `unary` and `stream` requests. `Transport` objects can easily be created by using `@connectrpc/connect-web`'s exports `createConnectTransport` and `createGrpcWebTransport`. see: https://connectrpc.com/docs/web/getting-started for more info."
|
|
7
|
+
);
|
|
8
|
+
const fallbackTransport = {
|
|
9
|
+
unary: () => {
|
|
10
|
+
throw fallbackTransportError;
|
|
11
|
+
},
|
|
12
|
+
stream: () => {
|
|
13
|
+
throw fallbackTransportError;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const transportContext = createContext(fallbackTransport);
|
|
17
|
+
const useTransport = () => useContext(transportContext);
|
|
18
|
+
const TransportProvider = ({ children, transport }) => /* @__PURE__ */ jsx(transportContext.Provider, { value: transport, children });
|
|
19
|
+
export {
|
|
20
|
+
TransportProvider,
|
|
21
|
+
fallbackTransport,
|
|
22
|
+
useTransport
|
|
23
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Message, PartialMessage } from "@bufbuild/protobuf";
|
|
2
|
+
import type { MethodUnaryDescriptor } from "./method-unary-descriptor";
|
|
3
|
+
export interface MtUnaryCallError {
|
|
4
|
+
error_code: string;
|
|
5
|
+
error_message?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare const disableQuery: unique symbol;
|
|
8
|
+
export type DisableQuery = typeof disableQuery;
|
|
9
|
+
export declare function assert(condition: boolean, message: string): asserts condition;
|
|
10
|
+
export declare const isAbortController: (input: unknown) => input is AbortController;
|
|
11
|
+
export type ConnectUpdater<O extends Message<O>> = PartialMessage<O> | ((prev?: O) => PartialMessage<O> | undefined);
|
|
12
|
+
export declare const createProtobufSafeUpdater: <I extends Message<I>, O extends Message<O>>(methodSig: Pick<MethodUnaryDescriptor<I, O>, "O">, updater: ConnectUpdater<O>) => (prev?: O) => O;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const disableQuery = Symbol("disableQuery");
|
|
2
|
+
function assert(condition, message) {
|
|
3
|
+
if (!condition) {
|
|
4
|
+
throw new Error(`Invalid assertion: ${message}`);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
const isAbortController = (input) => {
|
|
8
|
+
if (typeof input === "object" && input !== null && "signal" in input && typeof input.signal === "object" && input.signal !== null && "aborted" in input.signal && typeof input.signal.aborted === "boolean" && "abort" in input && typeof input.abort === "function") {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
return false;
|
|
12
|
+
};
|
|
13
|
+
const createProtobufSafeUpdater = (methodSig, updater) => (prev) => {
|
|
14
|
+
if (typeof updater === "function") {
|
|
15
|
+
return new methodSig.O(updater(prev));
|
|
16
|
+
}
|
|
17
|
+
return new methodSig.O(updater);
|
|
18
|
+
};
|
|
19
|
+
export {
|
|
20
|
+
assert,
|
|
21
|
+
createProtobufSafeUpdater,
|
|
22
|
+
disableQuery,
|
|
23
|
+
isAbortController
|
|
24
|
+
};
|
|
@@ -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;
|