@simulacrum/auth0-simulator 0.11.2 → 0.11.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/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/_virtual/rolldown_runtime.mjs +7 -0
- package/dist/auth/constants.cjs +15 -0
- package/dist/auth/constants.mjs +14 -0
- package/dist/auth/constants.mjs.map +1 -0
- package/dist/auth/date.cjs +8 -0
- package/dist/auth/date.mjs +7 -0
- package/dist/auth/date.mjs.map +1 -0
- package/dist/auth/jwt.cjs +16 -0
- package/dist/auth/jwt.mjs +15 -0
- package/dist/auth/jwt.mjs.map +1 -0
- package/dist/auth/refresh-token.cjs +24 -0
- package/dist/auth/refresh-token.mjs +23 -0
- package/dist/auth/refresh-token.mjs.map +1 -0
- package/dist/config/get-config.cjs +39 -0
- package/dist/config/get-config.mjs +39 -0
- package/dist/config/get-config.mjs.map +1 -0
- package/dist/handlers/auth0-handlers.cjs +207 -0
- package/dist/handlers/auth0-handlers.mjs +207 -0
- package/dist/handlers/auth0-handlers.mjs.map +1 -0
- package/dist/handlers/index.cjs +29 -0
- package/dist/handlers/index.mjs +27 -0
- package/dist/handlers/index.mjs.map +1 -0
- package/dist/handlers/login-redirect.cjs +24 -0
- package/dist/handlers/login-redirect.mjs +24 -0
- package/dist/handlers/login-redirect.mjs.map +1 -0
- package/dist/handlers/oauth-handlers.cjs +144 -0
- package/dist/handlers/oauth-handlers.mjs +144 -0
- package/dist/handlers/oauth-handlers.mjs.map +1 -0
- package/dist/handlers/openid-handlers.cjs +32 -0
- package/dist/handlers/openid-handlers.mjs +33 -0
- package/dist/handlers/openid-handlers.mjs.map +1 -0
- package/dist/handlers/url.cjs +6 -0
- package/dist/handlers/url.mjs +6 -0
- package/dist/handlers/url.mjs.map +1 -0
- package/dist/handlers/utils.cjs +25 -0
- package/dist/handlers/utils.mjs +24 -0
- package/dist/handlers/utils.mjs.map +1 -0
- package/dist/handlers/web-message.cjs +23 -0
- package/dist/handlers/web-message.mjs +23 -0
- package/dist/handlers/web-message.mjs.map +1 -0
- package/dist/index.cjs +14 -38722
- package/dist/index.d.cts +4 -176
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +21 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +22 -0
- package/dist/index.mjs.map +1 -0
- package/dist/middleware/create-cors.cjs +15 -0
- package/dist/middleware/create-cors.mjs +14 -0
- package/dist/middleware/create-cors.mjs.map +1 -0
- package/dist/middleware/error-handling.cjs +25 -0
- package/dist/middleware/error-handling.mjs +25 -0
- package/dist/middleware/error-handling.mjs.map +1 -0
- package/dist/middleware/no-cache.cjs +10 -0
- package/dist/middleware/no-cache.mjs +10 -0
- package/dist/middleware/no-cache.mjs.map +1 -0
- package/dist/middleware/session.cjs +19 -0
- package/dist/middleware/session.mjs +18 -0
- package/dist/middleware/session.mjs.map +1 -0
- package/dist/rules/extensionless-file-name.cjs +6 -0
- package/dist/rules/extensionless-file-name.mjs +6 -0
- package/dist/rules/extensionless-file-name.mjs.map +1 -0
- package/dist/rules/parse-rules-files.cjs +28 -0
- package/dist/rules/parse-rules-files.mjs +26 -0
- package/dist/rules/parse-rules-files.mjs.map +1 -0
- package/dist/rules/rules-runner.cjs +67 -0
- package/dist/rules/rules-runner.mjs +65 -0
- package/dist/rules/rules-runner.mjs.map +1 -0
- package/dist/store/entities.cjs +36 -0
- package/dist/store/entities.d.cts +99 -0
- package/dist/store/entities.d.cts.map +1 -0
- package/dist/store/entities.d.mts +100 -0
- package/dist/store/entities.d.mts.map +1 -0
- package/dist/store/entities.mjs +33 -0
- package/dist/store/entities.mjs.map +1 -0
- package/dist/store/index.cjs +44 -0
- package/dist/store/index.d.cts +22 -0
- package/dist/store/index.d.cts.map +1 -0
- package/dist/store/index.d.mts +22 -0
- package/dist/store/index.d.mts.map +1 -0
- package/dist/store/index.mjs +45 -0
- package/dist/store/index.mjs.map +1 -0
- package/dist/types.cjs +24 -0
- package/dist/types.d.cts +66 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +66 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +24 -0
- package/dist/types.mjs.map +1 -0
- package/dist/views/login.cjs +145 -0
- package/dist/views/login.mjs +146 -0
- package/dist/views/login.mjs.map +1 -0
- package/dist/views/username-password.cjs +39 -0
- package/dist/views/username-password.mjs +39 -0
- package/dist/views/username-password.mjs.map +1 -0
- package/dist/views/web-message.cjs +66 -0
- package/dist/views/web-message.mjs +65 -0
- package/dist/views/web-message.mjs.map +1 -0
- package/package.json +7 -7
- package/dist/index.d.ts +0 -193
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -38720
- package/dist/index.js.map +0 -1
- /package/dist/{public → views/public}/img/frontside-logo.png +0 -0
package/dist/index.d.cts
CHANGED
|
@@ -1,181 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Auth0InitialStore, auth0UserSchema, defaultUser } from "./store/entities.cjs";
|
|
2
|
+
import { Auth0ExtendStoreInput, ExtendedSimulationStore } from "./store/index.cjs";
|
|
3
|
+
import { Auth0Configuration } from "./types.cjs";
|
|
4
|
+
import { FoundationSimulator, SimulationHandlers } from "@simulacrum/foundation-simulator";
|
|
3
5
|
import { Router } from "express";
|
|
4
6
|
|
|
5
|
-
//#region src/store/entities.d.ts
|
|
6
|
-
declare const auth0UserSchema: z.ZodEffects<z.ZodObject<{
|
|
7
|
-
id: z.ZodDefault<z.ZodString>;
|
|
8
|
-
name: z.ZodString;
|
|
9
|
-
password: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
10
|
-
email: z.ZodOptional<z.ZodString>;
|
|
11
|
-
picture: z.ZodOptional<z.ZodString>;
|
|
12
|
-
}, "strip", z.ZodTypeAny, {
|
|
13
|
-
id: string;
|
|
14
|
-
name: string;
|
|
15
|
-
password: string;
|
|
16
|
-
email?: string | undefined;
|
|
17
|
-
picture?: string | undefined;
|
|
18
|
-
}, {
|
|
19
|
-
name: string;
|
|
20
|
-
id?: string | undefined;
|
|
21
|
-
password?: string | undefined;
|
|
22
|
-
email?: string | undefined;
|
|
23
|
-
picture?: string | undefined;
|
|
24
|
-
}>, {
|
|
25
|
-
id: string;
|
|
26
|
-
name: string;
|
|
27
|
-
password: string;
|
|
28
|
-
email?: string | undefined;
|
|
29
|
-
picture?: string | undefined;
|
|
30
|
-
}, {
|
|
31
|
-
name: string;
|
|
32
|
-
id?: string | undefined;
|
|
33
|
-
password?: string | undefined;
|
|
34
|
-
email?: string | undefined;
|
|
35
|
-
picture?: string | undefined;
|
|
36
|
-
}>;
|
|
37
|
-
type Auth0User = z.infer<typeof auth0UserSchema>;
|
|
38
|
-
declare const defaultUser: {
|
|
39
|
-
id: string;
|
|
40
|
-
name: string;
|
|
41
|
-
password: string;
|
|
42
|
-
email?: string | undefined;
|
|
43
|
-
picture?: string | undefined;
|
|
44
|
-
};
|
|
45
|
-
declare const auth0InitialStoreSchema: z.ZodObject<{
|
|
46
|
-
users: z.ZodArray<z.ZodEffects<z.ZodObject<{
|
|
47
|
-
id: z.ZodDefault<z.ZodString>;
|
|
48
|
-
name: z.ZodString;
|
|
49
|
-
password: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
50
|
-
email: z.ZodOptional<z.ZodString>;
|
|
51
|
-
picture: z.ZodOptional<z.ZodString>;
|
|
52
|
-
}, "strip", z.ZodTypeAny, {
|
|
53
|
-
id: string;
|
|
54
|
-
name: string;
|
|
55
|
-
password: string;
|
|
56
|
-
email?: string | undefined;
|
|
57
|
-
picture?: string | undefined;
|
|
58
|
-
}, {
|
|
59
|
-
name: string;
|
|
60
|
-
id?: string | undefined;
|
|
61
|
-
password?: string | undefined;
|
|
62
|
-
email?: string | undefined;
|
|
63
|
-
picture?: string | undefined;
|
|
64
|
-
}>, {
|
|
65
|
-
id: string;
|
|
66
|
-
name: string;
|
|
67
|
-
password: string;
|
|
68
|
-
email?: string | undefined;
|
|
69
|
-
picture?: string | undefined;
|
|
70
|
-
}, {
|
|
71
|
-
name: string;
|
|
72
|
-
id?: string | undefined;
|
|
73
|
-
password?: string | undefined;
|
|
74
|
-
email?: string | undefined;
|
|
75
|
-
picture?: string | undefined;
|
|
76
|
-
}>, "many">;
|
|
77
|
-
}, "strip", z.ZodTypeAny, {
|
|
78
|
-
users: {
|
|
79
|
-
id: string;
|
|
80
|
-
name: string;
|
|
81
|
-
password: string;
|
|
82
|
-
email?: string | undefined;
|
|
83
|
-
picture?: string | undefined;
|
|
84
|
-
}[];
|
|
85
|
-
}, {
|
|
86
|
-
users: {
|
|
87
|
-
name: string;
|
|
88
|
-
id?: string | undefined;
|
|
89
|
-
password?: string | undefined;
|
|
90
|
-
email?: string | undefined;
|
|
91
|
-
picture?: string | undefined;
|
|
92
|
-
}[];
|
|
93
|
-
}>;
|
|
94
|
-
type AuthSession = {
|
|
95
|
-
username: string;
|
|
96
|
-
nonce: string;
|
|
97
|
-
};
|
|
98
|
-
type Auth0InitialStore = z.input<typeof auth0InitialStoreSchema>;
|
|
99
|
-
//#endregion
|
|
100
|
-
//#region src/store/index.d.ts
|
|
101
|
-
type ExtendedSchema = ({
|
|
102
|
-
slice
|
|
103
|
-
}: ExtendSimulationSchema) => {
|
|
104
|
-
sessions: (n: string) => TableOutput<AuthSession, AnyState, AuthSession | undefined>;
|
|
105
|
-
users: (n: string) => TableOutput<Auth0User, AnyState, Auth0User | undefined>;
|
|
106
|
-
};
|
|
107
|
-
type ExtendActions = typeof inputActions;
|
|
108
|
-
type ExtendSelectors = typeof inputSelectors;
|
|
109
|
-
type Auth0Schema = ReturnType<ExtendedSchema>;
|
|
110
|
-
type Auth0Actions = ReturnType<ExtendActions>;
|
|
111
|
-
type Auth0Selectors = ReturnType<ExtendSelectors>;
|
|
112
|
-
type ExtendedSimulationStore = SimulationStore<Auth0Schema, Auth0Actions, Auth0Selectors>;
|
|
113
|
-
declare const inputActions: (_args: ExtendSimulationActions<ExtendedSchema>) => ExtendSimulationActions<ExtendedSchema>;
|
|
114
|
-
declare const inputSelectors: (_args: ExtendSimulationSelectors<ExtendedSchema>) => ExtendSimulationSelectors<ExtendedSchema>;
|
|
115
|
-
type Auth0ExtendStoreInput = ExtendStoreConfig<Auth0Schema, Auth0Actions, Auth0Selectors>;
|
|
116
|
-
//#endregion
|
|
117
|
-
//#region src/types.d.ts
|
|
118
|
-
declare const configurationSchema: z.ZodObject<{
|
|
119
|
-
port: z.ZodOptional<z.ZodNumber>;
|
|
120
|
-
domain: z.ZodOptional<z.ZodString>;
|
|
121
|
-
audience: z.ZodOptional<z.ZodString>;
|
|
122
|
-
clientID: z.ZodOptional<z.ZodString>;
|
|
123
|
-
scope: z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodObject<{
|
|
124
|
-
clientID: z.ZodString;
|
|
125
|
-
audience: z.ZodOptional<z.ZodString>;
|
|
126
|
-
scope: z.ZodString;
|
|
127
|
-
}, "strip", z.ZodTypeAny, {
|
|
128
|
-
clientID: string;
|
|
129
|
-
scope: string;
|
|
130
|
-
audience?: string | undefined;
|
|
131
|
-
}, {
|
|
132
|
-
clientID: string;
|
|
133
|
-
scope: string;
|
|
134
|
-
audience?: string | undefined;
|
|
135
|
-
}>, "many">]>;
|
|
136
|
-
clientSecret: z.ZodOptional<z.ZodString>;
|
|
137
|
-
rulesDirectory: z.ZodOptional<z.ZodString>;
|
|
138
|
-
auth0SessionCookieName: z.ZodOptional<z.ZodString>;
|
|
139
|
-
auth0CookieSecret: z.ZodOptional<z.ZodString>;
|
|
140
|
-
connection: z.ZodOptional<z.ZodString>;
|
|
141
|
-
cookieSecret: z.ZodOptional<z.ZodString>;
|
|
142
|
-
}, "strip", z.ZodTypeAny, {
|
|
143
|
-
scope: string | {
|
|
144
|
-
clientID: string;
|
|
145
|
-
scope: string;
|
|
146
|
-
audience?: string | undefined;
|
|
147
|
-
}[];
|
|
148
|
-
port?: number | undefined;
|
|
149
|
-
domain?: string | undefined;
|
|
150
|
-
audience?: string | undefined;
|
|
151
|
-
clientID?: string | undefined;
|
|
152
|
-
clientSecret?: string | undefined;
|
|
153
|
-
rulesDirectory?: string | undefined;
|
|
154
|
-
auth0SessionCookieName?: string | undefined;
|
|
155
|
-
auth0CookieSecret?: string | undefined;
|
|
156
|
-
connection?: string | undefined;
|
|
157
|
-
cookieSecret?: string | undefined;
|
|
158
|
-
}, {
|
|
159
|
-
scope: string | {
|
|
160
|
-
clientID: string;
|
|
161
|
-
scope: string;
|
|
162
|
-
audience?: string | undefined;
|
|
163
|
-
}[];
|
|
164
|
-
port?: number | undefined;
|
|
165
|
-
domain?: string | undefined;
|
|
166
|
-
audience?: string | undefined;
|
|
167
|
-
clientID?: string | undefined;
|
|
168
|
-
clientSecret?: string | undefined;
|
|
169
|
-
rulesDirectory?: string | undefined;
|
|
170
|
-
auth0SessionCookieName?: string | undefined;
|
|
171
|
-
auth0CookieSecret?: string | undefined;
|
|
172
|
-
connection?: string | undefined;
|
|
173
|
-
cookieSecret?: string | undefined;
|
|
174
|
-
}>;
|
|
175
|
-
type ConfigSchema = z.infer<typeof configurationSchema>;
|
|
176
|
-
type ReadonlyFields = "audience" | "clientID" | "scope" | "port";
|
|
177
|
-
type Auth0Configuration = Required<Pick<ConfigSchema, ReadonlyFields>> & Omit<ConfigSchema, ReadonlyFields>;
|
|
178
|
-
//#endregion
|
|
179
7
|
//#region src/index.d.ts
|
|
180
8
|
type Auth0Simulator = (args?: {
|
|
181
9
|
debug?: boolean;
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;KAiBY,cAAA,QAaA;EAbA,KAAA,CAAA,EAAA,OAAA;EAEK,YAAA,CAAA,EAAA,iBAAA;EAEC,MAAA,CAAA,EAAA;IAEK,WAAA,CAAA,EAFL,qBAEK;IACd,eAAA,CAAA,EAAA,CAAA,eAAA,EADc,uBACd,EAAA,GAAA,kBAAA;IAEK,YAAA,CAAA,EAAA,CAAA,MAAA,EAAA,MAAA,EAAA,eAAA,EACS,uBADT,EAAA,GAAA,IAAA;EACS,CAAA;EAGH,OAAA,CAAA,EAAR,OAAQ,CAAA,kBAAA,CAAA;CAAR,EAAA,GACN,mBADM,CACc,uBADd,CAAA;AACc,cAEb,UAFa,EAED,cAFC"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Auth0InitialStore, auth0UserSchema, defaultUser } from "./store/entities.mjs";
|
|
2
|
+
import { Auth0ExtendStoreInput, ExtendedSimulationStore } from "./store/index.mjs";
|
|
3
|
+
import { Auth0Configuration } from "./types.mjs";
|
|
4
|
+
import { FoundationSimulator, SimulationHandlers } from "@simulacrum/foundation-simulator";
|
|
5
|
+
import { Router } from "express";
|
|
6
|
+
|
|
7
|
+
//#region src/index.d.ts
|
|
8
|
+
type Auth0Simulator = (args?: {
|
|
9
|
+
debug?: boolean;
|
|
10
|
+
initialState?: Auth0InitialStore;
|
|
11
|
+
extend?: {
|
|
12
|
+
extendStore?: Auth0ExtendStoreInput;
|
|
13
|
+
openapiHandlers?: (simulationStore: ExtendedSimulationStore) => SimulationHandlers;
|
|
14
|
+
extendRouter?: (router: Router, simulationStore: ExtendedSimulationStore) => void;
|
|
15
|
+
};
|
|
16
|
+
options?: Partial<Auth0Configuration>;
|
|
17
|
+
}) => FoundationSimulator<ExtendedSimulationStore>;
|
|
18
|
+
declare const simulation: Auth0Simulator;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { Auth0Simulator, auth0UserSchema, defaultUser, simulation };
|
|
21
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;KAiBY,cAAA,QAaA;EAbA,KAAA,CAAA,EAAA,OAAA;EAEK,YAAA,CAAA,EAAA,iBAAA;EAEC,MAAA,CAAA,EAAA;IAEK,WAAA,CAAA,EAFL,qBAEK;IACd,eAAA,CAAA,EAAA,CAAA,eAAA,EADc,uBACd,EAAA,GAAA,kBAAA;IAEK,YAAA,CAAA,EAAA,CAAA,MAAA,EAAA,MAAA,EAAA,eAAA,EACS,uBADT,EAAA,GAAA,IAAA;EACS,CAAA;EAGH,OAAA,CAAA,EAAR,OAAQ,CAAA,kBAAA,CAAA;CAAR,EAAA,GACN,mBADM,CACc,uBADd,CAAA;AACc,cAEb,UAFa,EAED,cAFC"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { auth0InitialStoreSchema, auth0UserSchema, defaultUser } from "./store/entities.mjs";
|
|
2
|
+
import { extendStore } from "./store/index.mjs";
|
|
3
|
+
import "./types.mjs";
|
|
4
|
+
import { extendRouter } from "./handlers/index.mjs";
|
|
5
|
+
import { getConfig } from "./config/get-config.mjs";
|
|
6
|
+
import { createFoundationSimulationServer } from "@simulacrum/foundation-simulator";
|
|
7
|
+
|
|
8
|
+
//#region src/index.ts
|
|
9
|
+
const simulation = (args = {}) => {
|
|
10
|
+
const config = getConfig(args.options);
|
|
11
|
+
const parsedInitialState = !args?.initialState ? void 0 : auth0InitialStoreSchema.parse(args?.initialState);
|
|
12
|
+
return createFoundationSimulationServer({
|
|
13
|
+
port: config.port ?? 4400,
|
|
14
|
+
protocol: "https",
|
|
15
|
+
extendStore: extendStore(parsedInitialState, args?.extend?.extendStore),
|
|
16
|
+
extendRouter: extendRouter(config, args.extend?.extendRouter, args.debug)
|
|
17
|
+
})();
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { auth0UserSchema, defaultUser, simulation };
|
|
22
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["simulation: Auth0Simulator"],"sources":["../src/index.ts"],"sourcesContent":["import {\n createFoundationSimulationServer,\n type SimulationHandlers,\n type FoundationSimulator,\n} from \"@simulacrum/foundation-simulator\";\nimport type { ExtendedSimulationStore } from \"./store/index.ts\";\nimport { extendStore } from \"./store/index.ts\";\nimport type { Router } from \"express\";\nimport type { Auth0ExtendStoreInput } from \"./store/index.ts\";\nimport { extendRouter } from \"./handlers/index.ts\";\nimport {\n type Auth0InitialStore,\n auth0InitialStoreSchema,\n} from \"./store/entities.ts\";\nimport { getConfig } from \"./config/get-config.ts\";\nimport { type Auth0Configuration } from \"./types.ts\";\n\nexport type Auth0Simulator = (args?: {\n debug?: boolean;\n initialState?: Auth0InitialStore;\n extend?: {\n extendStore?: Auth0ExtendStoreInput;\n openapiHandlers?: (\n simulationStore: ExtendedSimulationStore\n ) => SimulationHandlers;\n extendRouter?: (\n router: Router,\n simulationStore: ExtendedSimulationStore\n ) => void;\n };\n options?: Partial<Auth0Configuration>;\n}) => FoundationSimulator<ExtendedSimulationStore>;\n\nexport const simulation: Auth0Simulator = (args = {}) => {\n const config = getConfig(args.options);\n const parsedInitialState = !args?.initialState\n ? undefined\n : auth0InitialStoreSchema.parse(args?.initialState);\n return createFoundationSimulationServer({\n port: config.port ?? 4400, // default port\n protocol: \"https\",\n extendStore: extendStore(parsedInitialState, args?.extend?.extendStore),\n extendRouter: extendRouter(config, args.extend?.extendRouter, args.debug),\n })();\n};\n\nexport { auth0UserSchema, defaultUser } from \"./store/entities.ts\";\n"],"mappings":";;;;;;;;AAiCA,MAAaA,cAA8B,OAAO,EAAE,KAAK;CACvD,MAAM,SAAS,UAAU,KAAK,QAAQ;CACtC,MAAM,qBAAqB,CAAC,MAAM,eAC9B,SACA,wBAAwB,MAAM,MAAM,aAAa;AACrD,QAAO,iCAAiC;EACtC,MAAM,OAAO,QAAQ;EACrB,UAAU;EACV,aAAa,YAAY,oBAAoB,MAAM,QAAQ,YAAY;EACvE,cAAc,aAAa,QAAQ,KAAK,QAAQ,cAAc,KAAK,MAAM;EAC1E,CAAC,EAAE"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let cors = require("cors");
|
|
3
|
+
cors = require_rolldown_runtime.__toESM(cors);
|
|
4
|
+
|
|
5
|
+
//#region src/middleware/create-cors.ts
|
|
6
|
+
const createCors = () => (0, cors.default)({
|
|
7
|
+
origin: (origin, cb) => {
|
|
8
|
+
if (typeof origin === "string") return cb(null, [origin]);
|
|
9
|
+
cb(null, "*");
|
|
10
|
+
},
|
|
11
|
+
credentials: true
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
exports.createCors = createCors;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import cors from "cors";
|
|
2
|
+
|
|
3
|
+
//#region src/middleware/create-cors.ts
|
|
4
|
+
const createCors = () => cors({
|
|
5
|
+
origin: (origin, cb) => {
|
|
6
|
+
if (typeof origin === "string") return cb(null, [origin]);
|
|
7
|
+
cb(null, "*");
|
|
8
|
+
},
|
|
9
|
+
credentials: true
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
export { createCors };
|
|
14
|
+
//# sourceMappingURL=create-cors.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-cors.mjs","names":[],"sources":["../../src/middleware/create-cors.ts"],"sourcesContent":["import type { RequestHandler } from \"express\";\nimport cors from \"cors\";\n\nexport const createCors = (): RequestHandler =>\n cors({\n origin: (origin, cb) => {\n if (typeof origin === \"string\") {\n return cb(null, [origin]);\n }\n\n cb(null, \"*\");\n },\n credentials: true,\n });\n"],"mappings":";;;AAGA,MAAa,mBACX,KAAK;CACH,SAAS,QAAQ,OAAO;AACtB,MAAI,OAAO,WAAW,SACpB,QAAO,GAAG,MAAM,CAAC,OAAO,CAAC;AAG3B,KAAG,MAAM,IAAI;;CAEf,aAAa;CACd,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/middleware/error-handling.ts
|
|
3
|
+
function defaultErrorHandler(error, _req, res, next) {
|
|
4
|
+
if (res.headersSent) return next(error);
|
|
5
|
+
if (error?.message?.startsWith("Assert condition failed: ")) {
|
|
6
|
+
let errorCode = 500;
|
|
7
|
+
let errorResponse = error.message;
|
|
8
|
+
if (error.message.includes("::")) {
|
|
9
|
+
let errorMessage = error.message.slice(25);
|
|
10
|
+
errorCode = parseInt(errorMessage.slice(0, 3));
|
|
11
|
+
errorResponse = errorMessage.slice(5);
|
|
12
|
+
}
|
|
13
|
+
res.status(errorCode).send(errorResponse);
|
|
14
|
+
} else {
|
|
15
|
+
console.error(error);
|
|
16
|
+
res.status(500).json({ error: {
|
|
17
|
+
name: error.name,
|
|
18
|
+
message: error.message,
|
|
19
|
+
stack: error.stack
|
|
20
|
+
} });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
exports.defaultErrorHandler = defaultErrorHandler;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/middleware/error-handling.ts
|
|
2
|
+
function defaultErrorHandler(error, _req, res, next) {
|
|
3
|
+
if (res.headersSent) return next(error);
|
|
4
|
+
if (error?.message?.startsWith("Assert condition failed: ")) {
|
|
5
|
+
let errorCode = 500;
|
|
6
|
+
let errorResponse = error.message;
|
|
7
|
+
if (error.message.includes("::")) {
|
|
8
|
+
let errorMessage = error.message.slice(25);
|
|
9
|
+
errorCode = parseInt(errorMessage.slice(0, 3));
|
|
10
|
+
errorResponse = errorMessage.slice(5);
|
|
11
|
+
}
|
|
12
|
+
res.status(errorCode).send(errorResponse);
|
|
13
|
+
} else {
|
|
14
|
+
console.error(error);
|
|
15
|
+
res.status(500).json({ error: {
|
|
16
|
+
name: error.name,
|
|
17
|
+
message: error.message,
|
|
18
|
+
stack: error.stack
|
|
19
|
+
} });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { defaultErrorHandler };
|
|
25
|
+
//# sourceMappingURL=error-handling.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-handling.mjs","names":[],"sources":["../../src/middleware/error-handling.ts"],"sourcesContent":["import type { Request, Response, NextFunction } from \"express\";\n\nexport function defaultErrorHandler(\n error: Error,\n _req: Request,\n res: Response,\n next: NextFunction\n) {\n if (res.headersSent) {\n return next(error);\n }\n\n let assertCondition = \"Assert condition failed: \";\n\n if (error?.message?.startsWith(assertCondition)) {\n let errorCode = 500;\n let errorResponse = error.message;\n\n if (error.message.includes(\"::\")) {\n let errorMessage = error.message.slice(assertCondition.length);\n errorCode = parseInt(errorMessage.slice(0, 3));\n errorResponse = errorMessage.slice(5);\n }\n\n res.status(errorCode).send(errorResponse);\n } else {\n console.error(error);\n res.status(500).json({\n error: {\n name: error.name,\n message: error.message,\n stack: error.stack,\n },\n });\n }\n}\n"],"mappings":";AAEA,SAAgB,oBACd,OACA,MACA,KACA,MACA;AACA,KAAI,IAAI,YACN,QAAO,KAAK,MAAM;AAKpB,KAAI,OAAO,SAAS,WAFE,4BAEyB,EAAE;EAC/C,IAAI,YAAY;EAChB,IAAI,gBAAgB,MAAM;AAE1B,MAAI,MAAM,QAAQ,SAAS,KAAK,EAAE;GAChC,IAAI,eAAe,MAAM,QAAQ,MAAM,GAAuB;AAC9D,eAAY,SAAS,aAAa,MAAM,GAAG,EAAE,CAAC;AAC9C,mBAAgB,aAAa,MAAM,EAAE;;AAGvC,MAAI,OAAO,UAAU,CAAC,KAAK,cAAc;QACpC;AACL,UAAQ,MAAM,MAAM;AACpB,MAAI,OAAO,IAAI,CAAC,KAAK,EACnB,OAAO;GACL,MAAM,MAAM;GACZ,SAAS,MAAM;GACf,OAAO,MAAM;GACd,EACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-cache.mjs","names":["noCache: () => RequestHandler"],"sources":["../../src/middleware/no-cache.ts"],"sourcesContent":["import type { RequestHandler } from \"express\";\n\nexport const noCache: () => RequestHandler = () => (_, res, next) => {\n res.set(\"Pragma\", \"no-cache\");\n res.set(\"Cache-Control\", \"no-cache, no-store\");\n next();\n};\n"],"mappings":";AAEA,MAAaA,iBAAuC,GAAG,KAAK,SAAS;AACnE,KAAI,IAAI,UAAU,WAAW;AAC7B,KAAI,IAAI,iBAAiB,qBAAqB;AAC9C,OAAM"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let cookie_session = require("cookie-session");
|
|
3
|
+
cookie_session = require_rolldown_runtime.__toESM(cookie_session);
|
|
4
|
+
|
|
5
|
+
//#region src/middleware/session.ts
|
|
6
|
+
const twentyFourHours = 1440 * 60 * 1e3;
|
|
7
|
+
const createSession = () => {
|
|
8
|
+
return (0, cookie_session.default)({
|
|
9
|
+
name: "session",
|
|
10
|
+
keys: ["shhh"],
|
|
11
|
+
secure: true,
|
|
12
|
+
httpOnly: false,
|
|
13
|
+
maxAge: twentyFourHours,
|
|
14
|
+
sameSite: "none"
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
exports.createSession = createSession;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import cookieSession from "cookie-session";
|
|
2
|
+
|
|
3
|
+
//#region src/middleware/session.ts
|
|
4
|
+
const twentyFourHours = 1440 * 60 * 1e3;
|
|
5
|
+
const createSession = () => {
|
|
6
|
+
return cookieSession({
|
|
7
|
+
name: "session",
|
|
8
|
+
keys: ["shhh"],
|
|
9
|
+
secure: true,
|
|
10
|
+
httpOnly: false,
|
|
11
|
+
maxAge: twentyFourHours,
|
|
12
|
+
sameSite: "none"
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { createSession };
|
|
18
|
+
//# sourceMappingURL=session.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.mjs","names":[],"sources":["../../src/middleware/session.ts"],"sourcesContent":["import type { RequestHandler } from \"express\";\nimport cookieSession from \"cookie-session\";\nconst twentyFourHours = 24 * 60 * 60 * 1000;\n\nexport const createSession = (): RequestHandler => {\n return cookieSession({\n name: \"session\",\n keys: [\"shhh\"],\n secure: true,\n httpOnly: false,\n maxAge: twentyFourHours,\n sameSite: \"none\",\n });\n};\n"],"mappings":";;;AAEA,MAAM,kBAAkB,OAAU,KAAK;AAEvC,MAAa,sBAAsC;AACjD,QAAO,cAAc;EACnB,MAAM;EACN,MAAM,CAAC,OAAO;EACd,QAAQ;EACR,UAAU;EACV,QAAQ;EACR,UAAU;EACX,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
//#region src/rules/extensionless-file-name.ts
|
|
2
|
+
const extensionlessFileName = (fileName) => fileName.indexOf(".") === -1 ? fileName : fileName.split(".").slice(0, -1).join(".");
|
|
3
|
+
|
|
4
|
+
//#endregion
|
|
5
|
+
export { extensionlessFileName };
|
|
6
|
+
//# sourceMappingURL=extensionless-file-name.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extensionless-file-name.mjs","names":[],"sources":["../../src/rules/extensionless-file-name.ts"],"sourcesContent":["export const extensionlessFileName = (fileName: string): string =>\n fileName.indexOf(\".\") === -1\n ? fileName\n : fileName.split(\".\").slice(0, -1).join(\".\");\n"],"mappings":";AAAA,MAAa,yBAAyB,aACpC,SAAS,QAAQ,IAAI,KAAK,KACtB,WACA,SAAS,MAAM,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC,KAAK,IAAI"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_extensionless_file_name = require('./extensionless-file-name.cjs');
|
|
3
|
+
let assert_ts = require("assert-ts");
|
|
4
|
+
let path = require("path");
|
|
5
|
+
path = require_rolldown_runtime.__toESM(path);
|
|
6
|
+
let fs = require("fs");
|
|
7
|
+
fs = require_rolldown_runtime.__toESM(fs);
|
|
8
|
+
|
|
9
|
+
//#region src/rules/parse-rules-files.ts
|
|
10
|
+
function parseRulesFiles(rulesPath) {
|
|
11
|
+
return fs.default.readdirSync(rulesPath).filter((f) => path.default.extname(f) === ".js").map((r) => {
|
|
12
|
+
let filename = path.default.join(rulesPath, r);
|
|
13
|
+
let jsonFile = `${require_extensionless_file_name.extensionlessFileName(filename)}.json`;
|
|
14
|
+
(0, assert_ts.assert)(!!jsonFile, `no corresponding rule file for ${r}`);
|
|
15
|
+
let rawRule = fs.default.readFileSync(jsonFile, "utf8");
|
|
16
|
+
let { enabled, order = 0, stage = "login_success" } = JSON.parse(rawRule);
|
|
17
|
+
if (!enabled) return;
|
|
18
|
+
return {
|
|
19
|
+
code: fs.default.readFileSync(filename, { encoding: "utf-8" }),
|
|
20
|
+
filename,
|
|
21
|
+
order,
|
|
22
|
+
stage
|
|
23
|
+
};
|
|
24
|
+
}).flatMap((x) => !!x ? x : []).sort((left, right) => left.order - right.order) ?? [];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
exports.parseRulesFiles = parseRulesFiles;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { extensionlessFileName } from "./extensionless-file-name.mjs";
|
|
2
|
+
import { assert } from "assert-ts";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
|
|
6
|
+
//#region src/rules/parse-rules-files.ts
|
|
7
|
+
function parseRulesFiles(rulesPath) {
|
|
8
|
+
return fs.readdirSync(rulesPath).filter((f) => path.extname(f) === ".js").map((r) => {
|
|
9
|
+
let filename = path.join(rulesPath, r);
|
|
10
|
+
let jsonFile = `${extensionlessFileName(filename)}.json`;
|
|
11
|
+
assert(!!jsonFile, `no corresponding rule file for ${r}`);
|
|
12
|
+
let rawRule = fs.readFileSync(jsonFile, "utf8");
|
|
13
|
+
let { enabled, order = 0, stage = "login_success" } = JSON.parse(rawRule);
|
|
14
|
+
if (!enabled) return;
|
|
15
|
+
return {
|
|
16
|
+
code: fs.readFileSync(filename, { encoding: "utf-8" }),
|
|
17
|
+
filename,
|
|
18
|
+
order,
|
|
19
|
+
stage
|
|
20
|
+
};
|
|
21
|
+
}).flatMap((x) => !!x ? x : []).sort((left, right) => left.order - right.order) ?? [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
export { parseRulesFiles };
|
|
26
|
+
//# sourceMappingURL=parse-rules-files.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-rules-files.mjs","names":[],"sources":["../../src/rules/parse-rules-files.ts"],"sourcesContent":["import { extensionlessFileName } from \"./extensionless-file-name.ts\";\nimport { assert } from \"assert-ts\";\nimport fs from \"fs\";\nimport path from \"path\";\n\nexport function parseRulesFiles(\n rulesPath: string\n): { code: string; filename: string }[] {\n let ruleFiles = fs\n .readdirSync(rulesPath)\n .filter((f) => path.extname(f) === \".js\");\n\n return (\n ruleFiles\n .map((r) => {\n let filename = path.join(rulesPath, r);\n\n let jsonFile = `${extensionlessFileName(filename)}.json`;\n\n assert(!!jsonFile, `no corresponding rule file for ${r}`);\n\n let rawRule = fs.readFileSync(jsonFile, \"utf8\");\n\n let {\n enabled,\n order = 0,\n stage = \"login_success\",\n } = JSON.parse(rawRule);\n\n if (!enabled) {\n return undefined;\n }\n\n let code = fs.readFileSync(filename, {\n encoding: \"utf-8\",\n });\n\n return { code, filename, order, stage };\n })\n .flatMap((x) => (!!x ? x : []))\n .sort((left, right) => left.order - right.order) ?? []\n );\n}\n"],"mappings":";;;;;;AAKA,SAAgB,gBACd,WACsC;AAKtC,QAJgB,GACb,YAAY,UAAU,CACtB,QAAQ,MAAM,KAAK,QAAQ,EAAE,KAAK,MAAM,CAItC,KAAK,MAAM;EACV,IAAI,WAAW,KAAK,KAAK,WAAW,EAAE;EAEtC,IAAI,WAAW,GAAG,sBAAsB,SAAS,CAAC;AAElD,SAAO,CAAC,CAAC,UAAU,kCAAkC,IAAI;EAEzD,IAAI,UAAU,GAAG,aAAa,UAAU,OAAO;EAE/C,IAAI,EACF,SACA,QAAQ,GACR,QAAQ,oBACN,KAAK,MAAM,QAAQ;AAEvB,MAAI,CAAC,QACH;AAOF,SAAO;GAAE,MAJE,GAAG,aAAa,UAAU,EACnC,UAAU,SACX,CAAC;GAEa;GAAU;GAAO;GAAO;GACvC,CACD,SAAS,MAAO,CAAC,CAAC,IAAI,IAAI,EAAE,CAAE,CAC9B,MAAM,MAAM,UAAU,KAAK,QAAQ,MAAM,MAAM,IAAI,EAAE"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_parse_rules_files = require('./parse-rules-files.cjs');
|
|
3
|
+
let assert_ts = require("assert-ts");
|
|
4
|
+
let path = require("path");
|
|
5
|
+
path = require_rolldown_runtime.__toESM(path);
|
|
6
|
+
let vm = require("vm");
|
|
7
|
+
vm = require_rolldown_runtime.__toESM(vm);
|
|
8
|
+
let fs = require("fs");
|
|
9
|
+
fs = require_rolldown_runtime.__toESM(fs);
|
|
10
|
+
|
|
11
|
+
//#region src/rules/rules-runner.ts
|
|
12
|
+
async function runRule(user, context, rule) {
|
|
13
|
+
await new Promise((resolve, reject) => {
|
|
14
|
+
let sandbox = {
|
|
15
|
+
process,
|
|
16
|
+
Buffer,
|
|
17
|
+
clearImmediate,
|
|
18
|
+
clearInterval,
|
|
19
|
+
clearTimeout,
|
|
20
|
+
setImmediate,
|
|
21
|
+
setInterval,
|
|
22
|
+
setTimeout,
|
|
23
|
+
console,
|
|
24
|
+
require,
|
|
25
|
+
module,
|
|
26
|
+
resolve,
|
|
27
|
+
reject,
|
|
28
|
+
__simulator: {
|
|
29
|
+
user,
|
|
30
|
+
context: { ...context }
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
let vmContext = vm.default.createContext(sandbox);
|
|
34
|
+
(0, assert_ts.assert)(typeof rule !== "undefined", "undefined rule");
|
|
35
|
+
let { code, filename } = rule;
|
|
36
|
+
console.debug(`executing rule ${path.default.basename(filename)}`);
|
|
37
|
+
new vm.default.Script(`
|
|
38
|
+
(async function(exports) {
|
|
39
|
+
try {
|
|
40
|
+
await (${code})(__simulator.user, __simulator.context, resolve);
|
|
41
|
+
} catch (err) {
|
|
42
|
+
console.error(err);
|
|
43
|
+
reject();
|
|
44
|
+
}
|
|
45
|
+
})(module.exports)
|
|
46
|
+
`).runInContext(vmContext, {
|
|
47
|
+
filename,
|
|
48
|
+
displayErrors: true,
|
|
49
|
+
timeout: 2e4
|
|
50
|
+
});
|
|
51
|
+
}).catch((error) => console.error(error));
|
|
52
|
+
}
|
|
53
|
+
function createRulesRunner(rulesPath) {
|
|
54
|
+
let callback = (_user, _context) => {};
|
|
55
|
+
if (typeof rulesPath === "undefined") return callback;
|
|
56
|
+
let fullPath = path.default.join(process.cwd(), rulesPath);
|
|
57
|
+
(0, assert_ts.assert)(fs.default.existsSync(fullPath), `no rules directory at ${fullPath}`);
|
|
58
|
+
let rules = require_parse_rules_files.parseRulesFiles(rulesPath);
|
|
59
|
+
if (rules.length === 0) return callback;
|
|
60
|
+
return async (user, context) => {
|
|
61
|
+
console.debug(`applying ${rules.length} rules`);
|
|
62
|
+
for (let rule of rules) await runRule(user, context, rule);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
exports.createRulesRunner = createRulesRunner;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { __require } from "../_virtual/rolldown_runtime.mjs";
|
|
2
|
+
import { parseRulesFiles } from "./parse-rules-files.mjs";
|
|
3
|
+
import { assert } from "assert-ts";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import vm from "vm";
|
|
6
|
+
import fs from "fs";
|
|
7
|
+
|
|
8
|
+
//#region src/rules/rules-runner.ts
|
|
9
|
+
async function runRule(user, context, rule) {
|
|
10
|
+
await new Promise((resolve, reject) => {
|
|
11
|
+
let sandbox = {
|
|
12
|
+
process,
|
|
13
|
+
Buffer,
|
|
14
|
+
clearImmediate,
|
|
15
|
+
clearInterval,
|
|
16
|
+
clearTimeout,
|
|
17
|
+
setImmediate,
|
|
18
|
+
setInterval,
|
|
19
|
+
setTimeout,
|
|
20
|
+
console,
|
|
21
|
+
require: __require,
|
|
22
|
+
module,
|
|
23
|
+
resolve,
|
|
24
|
+
reject,
|
|
25
|
+
__simulator: {
|
|
26
|
+
user,
|
|
27
|
+
context: { ...context }
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
let vmContext = vm.createContext(sandbox);
|
|
31
|
+
assert(typeof rule !== "undefined", "undefined rule");
|
|
32
|
+
let { code, filename } = rule;
|
|
33
|
+
console.debug(`executing rule ${path.basename(filename)}`);
|
|
34
|
+
new vm.Script(`
|
|
35
|
+
(async function(exports) {
|
|
36
|
+
try {
|
|
37
|
+
await (${code})(__simulator.user, __simulator.context, resolve);
|
|
38
|
+
} catch (err) {
|
|
39
|
+
console.error(err);
|
|
40
|
+
reject();
|
|
41
|
+
}
|
|
42
|
+
})(module.exports)
|
|
43
|
+
`).runInContext(vmContext, {
|
|
44
|
+
filename,
|
|
45
|
+
displayErrors: true,
|
|
46
|
+
timeout: 2e4
|
|
47
|
+
});
|
|
48
|
+
}).catch((error) => console.error(error));
|
|
49
|
+
}
|
|
50
|
+
function createRulesRunner(rulesPath) {
|
|
51
|
+
let callback = (_user, _context) => {};
|
|
52
|
+
if (typeof rulesPath === "undefined") return callback;
|
|
53
|
+
let fullPath = path.join(process.cwd(), rulesPath);
|
|
54
|
+
assert(fs.existsSync(fullPath), `no rules directory at ${fullPath}`);
|
|
55
|
+
let rules = parseRulesFiles(rulesPath);
|
|
56
|
+
if (rules.length === 0) return callback;
|
|
57
|
+
return async (user, context) => {
|
|
58
|
+
console.debug(`applying ${rules.length} rules`);
|
|
59
|
+
for (let rule of rules) await runRule(user, context, rule);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { createRulesRunner };
|
|
65
|
+
//# sourceMappingURL=rules-runner.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules-runner.mjs","names":[],"sources":["../../src/rules/rules-runner.ts"],"sourcesContent":["import path from \"path\";\nimport vm from \"vm\";\nimport fs from \"fs\";\nimport { assert } from \"assert-ts\";\nimport { parseRulesFiles } from \"./parse-rules-files.ts\";\nimport type { Rule, RuleContext, RuleUser } from \"./types.ts\";\n\nexport type RulesRunner = <A, I>(\n user: RuleUser,\n context: RuleContext<A, I>\n) => void;\n\nasync function runRule<A, I>(\n user: RuleUser,\n context: RuleContext<A, I>,\n rule: Rule\n) {\n await new Promise((resolve, reject) => {\n let sandbox = {\n process,\n Buffer,\n clearImmediate,\n clearInterval,\n clearTimeout,\n setImmediate,\n setInterval,\n setTimeout,\n console,\n require,\n module,\n resolve,\n reject,\n __simulator: {\n ...{\n user,\n context: { ...context },\n },\n },\n };\n\n let vmContext = vm.createContext(sandbox);\n assert(typeof rule !== \"undefined\", \"undefined rule\");\n\n let { code, filename } = rule;\n\n console.debug(`executing rule ${path.basename(filename)}`);\n\n let script = new vm.Script(`\n (async function(exports) {\n try {\n await (${code})(__simulator.user, __simulator.context, resolve);\n } catch (err) {\n console.error(err);\n reject();\n }\n })(module.exports)\n `);\n\n script.runInContext(vmContext, {\n filename,\n displayErrors: true,\n timeout: 20000,\n });\n }).catch((error) => console.error(error));\n}\n\nexport function createRulesRunner(rulesPath?: string): RulesRunner {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n let callback = (\n _user: RuleUser,\n _context: RuleContext<unknown, unknown>\n ) => {};\n\n if (typeof rulesPath === \"undefined\") {\n return callback;\n }\n\n let fullPath = path.join(process.cwd(), rulesPath);\n\n assert(fs.existsSync(fullPath), `no rules directory at ${fullPath}`);\n\n let rules = parseRulesFiles(rulesPath);\n\n if (rules.length === 0) {\n return callback;\n }\n\n return async <A, I>(user: RuleUser, context: RuleContext<A, I>) => {\n console.debug(`applying ${rules.length} rules`);\n\n for (let rule of rules) {\n await runRule(user, context, rule);\n }\n };\n}\n"],"mappings":";;;;;;;;AAYA,eAAe,QACb,MACA,SACA,MACA;AACA,OAAM,IAAI,SAAS,SAAS,WAAW;EACrC,IAAI,UAAU;GACZ;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA,aAAa;IAET;IACA,SAAS,EAAE,GAAG,SAAS;IAE1B;GACF;EAED,IAAI,YAAY,GAAG,cAAc,QAAQ;AACzC,SAAO,OAAO,SAAS,aAAa,iBAAiB;EAErD,IAAI,EAAE,MAAM,aAAa;AAEzB,UAAQ,MAAM,kBAAkB,KAAK,SAAS,SAAS,GAAG;AAa1D,EAXa,IAAI,GAAG,OAAO;;;mBAGZ,KAAK;;;;;;MAMlB,CAEK,aAAa,WAAW;GAC7B;GACA,eAAe;GACf,SAAS;GACV,CAAC;GACF,CAAC,OAAO,UAAU,QAAQ,MAAM,MAAM,CAAC;;AAG3C,SAAgB,kBAAkB,WAAiC;CAEjE,IAAI,YACF,OACA,aACG;AAEL,KAAI,OAAO,cAAc,YACvB,QAAO;CAGT,IAAI,WAAW,KAAK,KAAK,QAAQ,KAAK,EAAE,UAAU;AAElD,QAAO,GAAG,WAAW,SAAS,EAAE,yBAAyB,WAAW;CAEpE,IAAI,QAAQ,gBAAgB,UAAU;AAEtC,KAAI,MAAM,WAAW,EACnB,QAAO;AAGT,QAAO,OAAa,MAAgB,YAA+B;AACjE,UAAQ,MAAM,YAAY,MAAM,OAAO,QAAQ;AAE/C,OAAK,IAAI,QAAQ,MACf,OAAM,QAAQ,MAAM,SAAS,KAAK"}
|