@devbro/pashmak 0.1.7 → 0.1.9
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/{DatabaseServiceProvider.js → DatabaseServiceProvider.mjs} +5 -9
- package/dist/DatabaseServiceProvider.mjs.map +1 -0
- package/dist/app/console/{DefaultCommand.js → DefaultCommand.mjs} +2 -2
- package/dist/app/console/DefaultCommand.mjs.map +1 -0
- package/dist/app/console/{KeyGenerateCommand.js → KeyGenerateCommand.mjs} +6 -6
- package/dist/app/console/KeyGenerateCommand.mjs.map +1 -0
- package/dist/app/console/{StartCommand.js → StartCommand.mjs} +2 -2
- package/dist/app/console/StartCommand.mjs.map +1 -0
- package/dist/app/console/generate/{GenerateControllerCommand.js → GenerateControllerCommand.mjs} +3 -2
- package/dist/app/console/generate/GenerateControllerCommand.mjs.map +1 -0
- package/dist/app/console/generate/controller.tpl +14 -3
- package/dist/app/console/generate/{index.d.ts → index.d.mts} +1 -1
- package/dist/app/console/generate/{index.js → index.mjs} +1 -1
- package/dist/app/console/index.d.mts +9 -0
- package/dist/app/console/{index.js → index.mjs} +2 -2
- package/dist/app/console/index.mjs.map +1 -0
- package/dist/app/console/migrate/{MakeMigrateCommand.d.ts → GenerateMigrateCommand.d.mts} +2 -2
- package/dist/app/console/migrate/{MakeMigrateCommand.js → GenerateMigrateCommand.mjs} +8 -8
- package/dist/app/console/migrate/GenerateMigrateCommand.mjs.map +1 -0
- package/dist/app/console/migrate/{MigrateCommand.js → MigrateCommand.mjs} +2 -2
- package/dist/app/console/migrate/MigrateCommand.mjs.map +1 -0
- package/dist/app/console/migrate/{MigrateRollbackCommand.js → MigrateRollbackCommand.mjs} +2 -2
- package/dist/app/console/migrate/MigrateRollbackCommand.mjs.map +1 -0
- package/dist/app/console/migrate/index.d.mts +4 -0
- package/dist/app/console/migrate/{index.js → index.mjs} +2 -2
- package/dist/app/console/migrate/index.mjs.map +1 -0
- package/dist/app/console/project/{CreateProjectCommand.js → CreateProjectCommand.mjs} +2 -2
- package/dist/app/console/project/CreateProjectCommand.mjs.map +1 -0
- package/dist/app/console/project/base_project/package.json.tpl +1 -1
- package/dist/bin/DatabaseServiceProvider.cjs +88 -0
- package/dist/bin/app/console/DefaultCommand.cjs +506 -0
- package/dist/bin/app/console/KeyGenerateCommand.cjs +554 -0
- package/dist/bin/app/console/StartCommand.cjs +520 -0
- package/dist/bin/app/console/generate/GenerateControllerCommand.cjs +532 -0
- package/dist/bin/app/console/generate/index.cjs +532 -0
- package/dist/bin/app/console/index.cjs +2167 -0
- package/dist/bin/app/console/migrate/GenerateMigrateCommand.cjs +529 -0
- package/dist/bin/app/console/migrate/MigrateCommand.cjs +565 -0
- package/dist/bin/app/console/migrate/MigrateRollbackCommand.cjs +1684 -0
- package/dist/bin/app/console/migrate/index.cjs +1829 -0
- package/dist/bin/app/console/project/CreateProjectCommand.cjs +155 -0
- package/dist/bin/{pashmak_cli.cjs → bin/pashmak_cli.cjs} +4 -4
- package/dist/bin/config.cjs +24 -0
- package/dist/bin/context.cjs +24 -0
- package/dist/bin/facades.cjs +491 -0
- package/dist/bin/helper.cjs +24 -0
- package/dist/bin/http.cjs +24 -0
- package/dist/bin/index.cjs +2452 -0
- package/dist/bin/logger.cjs +24 -0
- package/dist/bin/mailer.cjs +24 -0
- package/dist/bin/middlewares.cjs +512 -0
- package/dist/bin/orm.cjs +24 -0
- package/dist/bin/{pashmak_cli.js → pashmak_cli.mjs} +1 -1
- package/dist/bin/router.cjs +547 -0
- package/dist/bin/sql.cjs +24 -0
- package/dist/{config.js → config.mjs} +1 -1
- package/dist/config.mjs.map +1 -0
- package/dist/{context.js → context.mjs} +1 -1
- package/dist/context.mjs.map +1 -0
- package/dist/{facades.js → facades.mjs} +30 -57
- package/dist/facades.mjs.map +1 -0
- package/dist/{helper.js → helper.mjs} +1 -1
- package/dist/helper.mjs.map +1 -0
- package/dist/{http.js → http.mjs} +1 -1
- package/dist/http.mjs.map +1 -0
- package/dist/{index.js → index.mjs} +2 -2
- package/dist/index.mjs.map +1 -0
- package/dist/{logger.js → logger.mjs} +1 -1
- package/dist/logger.mjs.map +1 -0
- package/dist/{mailer.js → mailer.mjs} +1 -1
- package/dist/mailer.mjs.map +1 -0
- package/dist/{middlewares.js → middlewares.mjs} +3 -5
- package/dist/middlewares.mjs.map +1 -0
- package/dist/{orm.js → orm.mjs} +1 -1
- package/dist/orm.mjs.map +1 -0
- package/dist/{router.d.ts → router.d.mts} +1 -1
- package/dist/{router.js → router.mjs} +2 -4
- package/dist/router.mjs.map +1 -0
- package/dist/{sql.js → sql.mjs} +1 -1
- package/dist/sql.mjs.map +1 -0
- package/package.json +14 -15
- package/dist/DatabaseServiceProvider.js.map +0 -1
- package/dist/app/console/DefaultCommand.js.map +0 -1
- package/dist/app/console/KeyGenerateCommand.js.map +0 -1
- package/dist/app/console/StartCommand.js.map +0 -1
- package/dist/app/console/generate/GenerateControllerCommand.js.map +0 -1
- package/dist/app/console/index.d.ts +0 -9
- package/dist/app/console/index.js.map +0 -1
- package/dist/app/console/migrate/MakeMigrateCommand.js.map +0 -1
- package/dist/app/console/migrate/MigrateCommand.js.map +0 -1
- package/dist/app/console/migrate/MigrateRollbackCommand.js.map +0 -1
- package/dist/app/console/migrate/index.d.ts +0 -4
- package/dist/app/console/migrate/index.js.map +0 -1
- package/dist/app/console/project/CreateProjectCommand.js.map +0 -1
- package/dist/config.js.map +0 -1
- package/dist/context.js.map +0 -1
- package/dist/facades.js.map +0 -1
- package/dist/helper.js.map +0 -1
- package/dist/http.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/logger.js.map +0 -1
- package/dist/mailer.js.map +0 -1
- package/dist/middlewares.js.map +0 -1
- package/dist/orm.js.map +0 -1
- package/dist/router.js.map +0 -1
- package/dist/sql.js.map +0 -1
- /package/dist/{DatabaseServiceProvider.d.ts → DatabaseServiceProvider.d.mts} +0 -0
- /package/dist/app/console/{DefaultCommand.d.ts → DefaultCommand.d.mts} +0 -0
- /package/dist/app/console/{KeyGenerateCommand.d.ts → KeyGenerateCommand.d.mts} +0 -0
- /package/dist/app/console/{StartCommand.d.ts → StartCommand.d.mts} +0 -0
- /package/dist/app/console/generate/{GenerateControllerCommand.d.ts → GenerateControllerCommand.d.mts} +0 -0
- /package/dist/app/console/generate/{index.js.map → index.mjs.map} +0 -0
- /package/dist/app/console/migrate/{MigrateCommand.d.ts → MigrateCommand.d.mts} +0 -0
- /package/dist/app/console/migrate/{MigrateRollbackCommand.d.ts → MigrateRollbackCommand.d.mts} +0 -0
- /package/dist/app/console/project/{CreateProjectCommand.d.ts → CreateProjectCommand.d.mts} +0 -0
- /package/dist/bin/{pashmak_cli.d.ts → pashmak_cli.d.mts} +0 -0
- /package/dist/bin/{pashmak_cli.js.map → pashmak_cli.mjs.map} +0 -0
- /package/dist/{config.d.ts → config.d.mts} +0 -0
- /package/dist/{context.d.ts → context.d.mts} +0 -0
- /package/dist/{facades.d.ts → facades.d.mts} +0 -0
- /package/dist/{helper.d.ts → helper.d.mts} +0 -0
- /package/dist/{http.d.ts → http.d.mts} +0 -0
- /package/dist/{index.d.ts → index.d.mts} +0 -0
- /package/dist/{logger.d.ts → logger.d.mts} +0 -0
- /package/dist/{mailer.d.ts → mailer.d.mts} +0 -0
- /package/dist/{middlewares.d.ts → middlewares.d.mts} +0 -0
- /package/dist/{orm.d.ts → orm.d.mts} +0 -0
- /package/dist/{sql.d.ts → sql.d.mts} +0 -0
|
@@ -0,0 +1,2452 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __esm = (fn, res) => function __init() {
|
|
10
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
11
|
+
};
|
|
12
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
13
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
14
|
+
};
|
|
15
|
+
var __export = (target, all) => {
|
|
16
|
+
for (var name in all)
|
|
17
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
28
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
29
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
30
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
31
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
32
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
33
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
34
|
+
mod
|
|
35
|
+
));
|
|
36
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
37
|
+
|
|
38
|
+
// ../neko-router/dist/types.mjs
|
|
39
|
+
var init_types = __esm({
|
|
40
|
+
"../neko-router/dist/types.mjs"() {
|
|
41
|
+
"use strict";
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// ../neko-router/dist/Middleware.mjs
|
|
46
|
+
var Middleware;
|
|
47
|
+
var init_Middleware = __esm({
|
|
48
|
+
"../neko-router/dist/Middleware.mjs"() {
|
|
49
|
+
"use strict";
|
|
50
|
+
Middleware = class {
|
|
51
|
+
static {
|
|
52
|
+
__name(this, "Middleware");
|
|
53
|
+
}
|
|
54
|
+
constructor(params = {}) {
|
|
55
|
+
}
|
|
56
|
+
static getInstance(params) {
|
|
57
|
+
throw new Error("Method not implemented. Please implement a static getInstance method.");
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// ../neko-router/dist/MiddlewareFactory.mjs
|
|
64
|
+
var MiddlewareFactory;
|
|
65
|
+
var init_MiddlewareFactory = __esm({
|
|
66
|
+
"../neko-router/dist/MiddlewareFactory.mjs"() {
|
|
67
|
+
"use strict";
|
|
68
|
+
init_Middleware();
|
|
69
|
+
MiddlewareFactory = class {
|
|
70
|
+
static {
|
|
71
|
+
__name(this, "MiddlewareFactory");
|
|
72
|
+
}
|
|
73
|
+
static create(func) {
|
|
74
|
+
const cls = class extends Middleware {
|
|
75
|
+
static {
|
|
76
|
+
__name(this, "cls");
|
|
77
|
+
}
|
|
78
|
+
call(req, res, next) {
|
|
79
|
+
return func(req, res, next);
|
|
80
|
+
}
|
|
81
|
+
constructor(params = {}) {
|
|
82
|
+
super(params);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
return new cls();
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// ../neko-router/dist/CompiledRoute.mjs
|
|
92
|
+
var CompiledRoute;
|
|
93
|
+
var init_CompiledRoute = __esm({
|
|
94
|
+
"../neko-router/dist/CompiledRoute.mjs"() {
|
|
95
|
+
"use strict";
|
|
96
|
+
init_Middleware();
|
|
97
|
+
init_MiddlewareFactory();
|
|
98
|
+
CompiledRoute = class {
|
|
99
|
+
static {
|
|
100
|
+
__name(this, "CompiledRoute");
|
|
101
|
+
}
|
|
102
|
+
constructor(route, request, response, globalMiddlewares = []) {
|
|
103
|
+
this.route = route;
|
|
104
|
+
this.request = request;
|
|
105
|
+
this.response = response;
|
|
106
|
+
this.globalMiddlewares = globalMiddlewares;
|
|
107
|
+
this.prepareMiddlewares();
|
|
108
|
+
}
|
|
109
|
+
middlewares = [];
|
|
110
|
+
getMiddlewares() {
|
|
111
|
+
return this.middlewares;
|
|
112
|
+
}
|
|
113
|
+
prepareMiddlewares() {
|
|
114
|
+
this.middlewares = [];
|
|
115
|
+
for (const middleware of [...this.globalMiddlewares, ...this.route.getMiddlewares()]) {
|
|
116
|
+
if (middleware instanceof Middleware) {
|
|
117
|
+
this.middlewares.push(middleware);
|
|
118
|
+
} else if (this.isClass(middleware)) {
|
|
119
|
+
this.middlewares.push(middleware.getInstance({}));
|
|
120
|
+
} else if (typeof middleware === "function") {
|
|
121
|
+
this.middlewares.push(MiddlewareFactory.create(middleware));
|
|
122
|
+
} else {
|
|
123
|
+
throw new Error("Invalid middleware type");
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
isClass(func) {
|
|
128
|
+
return typeof func === "function" && /^class\s/.test(Function.prototype.toString.call(func));
|
|
129
|
+
}
|
|
130
|
+
async run() {
|
|
131
|
+
return await this.runMiddlewares(this.middlewares, this.request, this.response);
|
|
132
|
+
}
|
|
133
|
+
prepareOutputJsonFormat(obj) {
|
|
134
|
+
function traverse(value) {
|
|
135
|
+
if (!value || typeof value !== "object") {
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
if (typeof value.toJson === "function") {
|
|
139
|
+
return traverse(value.toJson());
|
|
140
|
+
}
|
|
141
|
+
if (Array.isArray(value)) {
|
|
142
|
+
return value.map(traverse);
|
|
143
|
+
}
|
|
144
|
+
const result = {};
|
|
145
|
+
for (const key in value) {
|
|
146
|
+
if (Object.prototype.hasOwnProperty.call(value, key)) {
|
|
147
|
+
result[key] = traverse(value[key]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
__name(traverse, "traverse");
|
|
153
|
+
return traverse(obj);
|
|
154
|
+
}
|
|
155
|
+
convertToString(obj) {
|
|
156
|
+
if (typeof obj === "string") {
|
|
157
|
+
return obj;
|
|
158
|
+
} else if (obj instanceof Buffer) {
|
|
159
|
+
return obj.toString();
|
|
160
|
+
} else if (typeof obj === "object") {
|
|
161
|
+
return JSON.stringify(this.prepareOutputJsonFormat(obj));
|
|
162
|
+
}
|
|
163
|
+
return String(obj);
|
|
164
|
+
}
|
|
165
|
+
processResponseBody(res, controller_rc) {
|
|
166
|
+
if (controller_rc && res.writableEnded) {
|
|
167
|
+
throw new Error("cannot write to response, response has already ended");
|
|
168
|
+
}
|
|
169
|
+
if (res.writableEnded) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (controller_rc) {
|
|
173
|
+
const header_content_type = res.getHeader("Content-Type");
|
|
174
|
+
if (!header_content_type && typeof controller_rc === "object") {
|
|
175
|
+
res.setHeader("Content-Type", "application/json");
|
|
176
|
+
} else if (!header_content_type) {
|
|
177
|
+
res.setHeader("Content-Type", "text/plain");
|
|
178
|
+
}
|
|
179
|
+
res.end(this.convertToString(controller_rc));
|
|
180
|
+
return;
|
|
181
|
+
} else {
|
|
182
|
+
res.statusCode = [200].includes(res.statusCode) ? 204 : res.statusCode;
|
|
183
|
+
res.end();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async runMiddlewares(middlewares, req, res) {
|
|
187
|
+
let index = 0;
|
|
188
|
+
const me = this;
|
|
189
|
+
async function next() {
|
|
190
|
+
if (index >= middlewares.length) {
|
|
191
|
+
const controller_rc = await me.route.callHanlder(req, res);
|
|
192
|
+
await me.processResponseBody(res, controller_rc);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const middleware = middlewares[index++];
|
|
196
|
+
if (middleware instanceof Middleware) {
|
|
197
|
+
await middleware.call(req, res, next);
|
|
198
|
+
} else if (typeof middleware === "function") {
|
|
199
|
+
await middleware(req, res, next);
|
|
200
|
+
} else {
|
|
201
|
+
throw new Error("does not know how to run middleware");
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
__name(next, "next");
|
|
205
|
+
await next();
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// ../neko-router/dist/Controller.mjs
|
|
212
|
+
var init_Controller = __esm({
|
|
213
|
+
"../neko-router/dist/Controller.mjs"() {
|
|
214
|
+
"use strict";
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// ../neko-router/dist/Route.mjs
|
|
219
|
+
var Route;
|
|
220
|
+
var init_Route = __esm({
|
|
221
|
+
"../neko-router/dist/Route.mjs"() {
|
|
222
|
+
"use strict";
|
|
223
|
+
Route = class {
|
|
224
|
+
static {
|
|
225
|
+
__name(this, "Route");
|
|
226
|
+
}
|
|
227
|
+
constructor(methods, path8, handler) {
|
|
228
|
+
this.methods = methods;
|
|
229
|
+
this.path = path8;
|
|
230
|
+
this.handler = handler;
|
|
231
|
+
this.urlRegex = this.pathToRegex(path8);
|
|
232
|
+
}
|
|
233
|
+
middlewares = [];
|
|
234
|
+
urlRegex;
|
|
235
|
+
pathToRegex(path8) {
|
|
236
|
+
const lex = this.lexUrlPath(path8);
|
|
237
|
+
return this.tokensToRegex(lex);
|
|
238
|
+
}
|
|
239
|
+
lexUrlPath(path8) {
|
|
240
|
+
const tokens = [];
|
|
241
|
+
let i = 0;
|
|
242
|
+
while (i < path8.length) {
|
|
243
|
+
const char = path8[i];
|
|
244
|
+
if (char === "/") {
|
|
245
|
+
tokens.push({ type: "SLASH", value: "/" });
|
|
246
|
+
i++;
|
|
247
|
+
} else if (char === ":") {
|
|
248
|
+
let start = i + 1;
|
|
249
|
+
while (start < path8.length && /[a-zA-Z0-9_]/.test(path8[start])) {
|
|
250
|
+
start++;
|
|
251
|
+
}
|
|
252
|
+
tokens.push({ type: "PARAM", value: path8.slice(i + 1, start) });
|
|
253
|
+
i = start;
|
|
254
|
+
} else if (char === "*") {
|
|
255
|
+
let start = i + 1;
|
|
256
|
+
while (start < path8.length && /[a-zA-Z0-9_]/.test(path8[start])) {
|
|
257
|
+
start++;
|
|
258
|
+
}
|
|
259
|
+
tokens.push({ type: "WILDCARD", value: path8.slice(i + 1, start) });
|
|
260
|
+
i = start;
|
|
261
|
+
} else {
|
|
262
|
+
let start = i;
|
|
263
|
+
while (start < path8.length && !["/", ":", "*"].includes(path8[start])) {
|
|
264
|
+
start++;
|
|
265
|
+
}
|
|
266
|
+
tokens.push({ type: "TEXT", value: path8.slice(i, start) });
|
|
267
|
+
i = start;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return tokens;
|
|
271
|
+
}
|
|
272
|
+
tokensToRegex(tokens) {
|
|
273
|
+
const regexParts = [];
|
|
274
|
+
for (const token of tokens) {
|
|
275
|
+
if (token.type === "SLASH") {
|
|
276
|
+
regexParts.push("\\/");
|
|
277
|
+
} else if (token.type === "PARAM") {
|
|
278
|
+
regexParts.push(`(?<${token.value}>[^\\/]+)`);
|
|
279
|
+
} else if (token.type === "WILDCARD") {
|
|
280
|
+
regexParts.push("(.+)");
|
|
281
|
+
} else if (token.type === "TEXT") {
|
|
282
|
+
regexParts.push(token.value.replace(/[-\/\\^$.*+?()[\]{}|]/g, "\\$&"));
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (regexParts.length > 0 && regexParts[regexParts.length - 1] === "\\/") {
|
|
286
|
+
regexParts[regexParts.length - 1] = "\\/?";
|
|
287
|
+
} else {
|
|
288
|
+
regexParts.push("\\/?");
|
|
289
|
+
}
|
|
290
|
+
return new RegExp(`^${regexParts.join("")}$`);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* to evaludate if request is a match for this route
|
|
294
|
+
* @param request http request
|
|
295
|
+
* @returns return true if route is a match for this request
|
|
296
|
+
*/
|
|
297
|
+
test(request) {
|
|
298
|
+
if (this.methods.indexOf(request.method) === -1) {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
const url = new URL(request.url || "/", "http://localhost");
|
|
302
|
+
return this.testPath(url.pathname);
|
|
303
|
+
}
|
|
304
|
+
testPath(pathname) {
|
|
305
|
+
return this.urlRegex.test(pathname);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* returns details of the match, otherwise it returns false
|
|
309
|
+
* @param request the request to match
|
|
310
|
+
* @returns object cotaining details of match including matched params
|
|
311
|
+
*/
|
|
312
|
+
match(request) {
|
|
313
|
+
if (this.methods.indexOf(request.method) === -1) {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
const url = new URL(request.url || "/", "http://localhost");
|
|
317
|
+
const r = this.urlRegex.exec(url.pathname);
|
|
318
|
+
if (!r) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
url,
|
|
323
|
+
params: r.groups || {}
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
addMiddleware(middlewares) {
|
|
327
|
+
this.middlewares = this.middlewares.concat(middlewares);
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
getMiddlewares() {
|
|
331
|
+
return this.middlewares;
|
|
332
|
+
}
|
|
333
|
+
callHanlder(request, response) {
|
|
334
|
+
return this.handler(request, response);
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// ../neko-router/dist/Router.mjs
|
|
341
|
+
var import_path, Router;
|
|
342
|
+
var init_Router = __esm({
|
|
343
|
+
"../neko-router/dist/Router.mjs"() {
|
|
344
|
+
"use strict";
|
|
345
|
+
init_CompiledRoute();
|
|
346
|
+
init_Route();
|
|
347
|
+
import_path = __toESM(require("path"), 1);
|
|
348
|
+
Router = class {
|
|
349
|
+
static {
|
|
350
|
+
__name(this, "Router");
|
|
351
|
+
}
|
|
352
|
+
middlewares = [];
|
|
353
|
+
routes = [];
|
|
354
|
+
addRoute(methods, path22, handler) {
|
|
355
|
+
const route = new Route(methods, path22, handler);
|
|
356
|
+
this.routes.push(route);
|
|
357
|
+
return route;
|
|
358
|
+
}
|
|
359
|
+
getMiddlewares() {
|
|
360
|
+
return [...this.middlewares];
|
|
361
|
+
}
|
|
362
|
+
addController(controller) {
|
|
363
|
+
const basePath = controller.basePath || "";
|
|
364
|
+
for (const route of controller.routes) {
|
|
365
|
+
const urlPath = import_path.default.join(basePath, route.path);
|
|
366
|
+
this.addRoute(route.methods, urlPath, async (req, res) => {
|
|
367
|
+
const controllerInstance = controller.getInstance();
|
|
368
|
+
return await controllerInstance[route.handler]();
|
|
369
|
+
}).addMiddleware([...controller.baseMiddlewares, ...route.middlewares]);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
addGlobalMiddleware(middlewares) {
|
|
373
|
+
this.middlewares = this.middlewares.concat(middlewares);
|
|
374
|
+
}
|
|
375
|
+
resolve(request) {
|
|
376
|
+
for (const route of this.routes) {
|
|
377
|
+
if (route.test(request)) {
|
|
378
|
+
return route;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return void 0;
|
|
382
|
+
}
|
|
383
|
+
resolveMultiple(request) {
|
|
384
|
+
const rc = [];
|
|
385
|
+
const url = new URL(request.url || "/", "http://localhost");
|
|
386
|
+
for (const route of this.routes) {
|
|
387
|
+
if (route.testPath(url.pathname)) {
|
|
388
|
+
rc.push(route);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return rc;
|
|
392
|
+
}
|
|
393
|
+
getCompiledRoute(request, response) {
|
|
394
|
+
const route = this.resolve(request);
|
|
395
|
+
if (!route) {
|
|
396
|
+
return void 0;
|
|
397
|
+
}
|
|
398
|
+
const match = route.match(request);
|
|
399
|
+
if (!match) {
|
|
400
|
+
return void 0;
|
|
401
|
+
}
|
|
402
|
+
request.params = match.params;
|
|
403
|
+
return new CompiledRoute(route, request, response, this.middlewares);
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
// ../neko-router/dist/helpers.mjs
|
|
410
|
+
async function runNext(middlewares, req, res, final) {
|
|
411
|
+
let index = 0;
|
|
412
|
+
async function next() {
|
|
413
|
+
if (index >= middlewares.length) {
|
|
414
|
+
return await final(req, res);
|
|
415
|
+
}
|
|
416
|
+
const middleware = middlewares[index++];
|
|
417
|
+
if (middleware instanceof Middleware) {
|
|
418
|
+
await middleware.call(req, res, next);
|
|
419
|
+
} else if (typeof middleware === "function") {
|
|
420
|
+
await middleware(req, res, next);
|
|
421
|
+
} else {
|
|
422
|
+
throw new Error("does not know how to run middleware");
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
__name(next, "next");
|
|
426
|
+
await next();
|
|
427
|
+
}
|
|
428
|
+
var init_helpers = __esm({
|
|
429
|
+
"../neko-router/dist/helpers.mjs"() {
|
|
430
|
+
"use strict";
|
|
431
|
+
init_Middleware();
|
|
432
|
+
__name(runNext, "runNext");
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
// ../neko-router/dist/index.mjs
|
|
437
|
+
var init_dist = __esm({
|
|
438
|
+
"../neko-router/dist/index.mjs"() {
|
|
439
|
+
"use strict";
|
|
440
|
+
init_types();
|
|
441
|
+
init_CompiledRoute();
|
|
442
|
+
init_Controller();
|
|
443
|
+
init_Route();
|
|
444
|
+
init_Router();
|
|
445
|
+
init_Middleware();
|
|
446
|
+
init_MiddlewareFactory();
|
|
447
|
+
init_helpers();
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
// src/router.mts
|
|
452
|
+
var import_neko_context, import_errors;
|
|
453
|
+
var init_router = __esm({
|
|
454
|
+
"src/router.mts"() {
|
|
455
|
+
"use strict";
|
|
456
|
+
import_neko_context = require("@devbro/neko-context");
|
|
457
|
+
import_errors = require("@devbro/neko-http/errors");
|
|
458
|
+
init_dist();
|
|
459
|
+
init_dist();
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
// src/http.mts
|
|
464
|
+
var http_exports = {};
|
|
465
|
+
var init_http = __esm({
|
|
466
|
+
"src/http.mts"() {
|
|
467
|
+
"use strict";
|
|
468
|
+
__reExport(http_exports, require("@devbro/neko-http"));
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
// src/facades.mts
|
|
473
|
+
var import_neko_scheduler, import_neko_helper, import_neko_context2, import_neko_storage, import_neko_mailer, import_neko_config, import_clipanion, yup, import_neko_logger, router, scheduler, db, storage, cli, httpServer, logger, mailer;
|
|
474
|
+
var init_facades = __esm({
|
|
475
|
+
"src/facades.mts"() {
|
|
476
|
+
"use strict";
|
|
477
|
+
init_router();
|
|
478
|
+
import_neko_scheduler = require("@devbro/neko-scheduler");
|
|
479
|
+
import_neko_helper = require("@devbro/neko-helper");
|
|
480
|
+
import_neko_context2 = require("@devbro/neko-context");
|
|
481
|
+
import_neko_storage = require("@devbro/neko-storage");
|
|
482
|
+
import_neko_mailer = require("@devbro/neko-mailer");
|
|
483
|
+
import_neko_config = require("@devbro/neko-config");
|
|
484
|
+
import_clipanion = require("clipanion");
|
|
485
|
+
init_http();
|
|
486
|
+
init_http();
|
|
487
|
+
yup = __toESM(require("yup"), 1);
|
|
488
|
+
import_neko_logger = require("@devbro/neko-logger");
|
|
489
|
+
router = (0, import_neko_helper.createSingleton)(() => new Router());
|
|
490
|
+
scheduler = (0, import_neko_helper.createSingleton)(() => {
|
|
491
|
+
const rc = new import_neko_scheduler.Scheduler();
|
|
492
|
+
rc.setErrorHandler((err, job) => {
|
|
493
|
+
logger().error({
|
|
494
|
+
msg: "Scheduled job error",
|
|
495
|
+
err,
|
|
496
|
+
job_name: job.getName()
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
return rc;
|
|
500
|
+
});
|
|
501
|
+
db = /* @__PURE__ */ __name((label = "default") => (0, import_neko_context2.ctx)().getOrThrow(["database", label]), "db");
|
|
502
|
+
storage = (0, import_neko_helper.createSingleton)(
|
|
503
|
+
(label = "default") => import_neko_storage.StorageFactory.create(import_neko_config.config.get(["storages", label].join(".")))
|
|
504
|
+
);
|
|
505
|
+
cli = (0, import_neko_helper.createSingleton)(() => {
|
|
506
|
+
const [node, app, ...args] = process.argv;
|
|
507
|
+
return new import_clipanion.Cli({
|
|
508
|
+
binaryLabel: `My Application`,
|
|
509
|
+
binaryName: `${node} ${app}`,
|
|
510
|
+
binaryVersion: `1.0.0`
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
httpServer = (0, import_neko_helper.createSingleton)(() => {
|
|
514
|
+
const server = new http_exports.HttpServer();
|
|
515
|
+
server.setErrorHandler(async (err, req, res) => {
|
|
516
|
+
if (err instanceof http_exports.HttpError) {
|
|
517
|
+
res.writeHead(err.statusCode, { "Content-Type": "application/json" });
|
|
518
|
+
res.end(JSON.stringify({ message: err.message, error: err.code }));
|
|
519
|
+
logger().warn({ msg: "HttpError: " + err.message, err });
|
|
520
|
+
return;
|
|
521
|
+
} else if (err instanceof yup.ValidationError) {
|
|
522
|
+
res.writeHead(422, { "Content-Type": "application/json" });
|
|
523
|
+
const errs = {};
|
|
524
|
+
err.inner.forEach((e) => {
|
|
525
|
+
const sanitizedParams = { ...e.params };
|
|
526
|
+
if (/passw/i.test(e.path)) {
|
|
527
|
+
sanitizedParams.value = "******";
|
|
528
|
+
sanitizedParams.originalValue = "******";
|
|
529
|
+
}
|
|
530
|
+
errs[e.path] = {
|
|
531
|
+
type: e.type,
|
|
532
|
+
message: e.message,
|
|
533
|
+
params: sanitizedParams
|
|
534
|
+
};
|
|
535
|
+
});
|
|
536
|
+
res.end(JSON.stringify({ message: "validation error", errors: errs }));
|
|
537
|
+
logger().warn({ msg: "ValidationError: " + err.message, err });
|
|
538
|
+
return;
|
|
539
|
+
} else {
|
|
540
|
+
logger().error({ msg: "Error: " + err.message, err });
|
|
541
|
+
}
|
|
542
|
+
res.writeHead(500, { "Content-Type": "" });
|
|
543
|
+
res.end(JSON.stringify({ error: "Internal Server Error" }));
|
|
544
|
+
});
|
|
545
|
+
server.setRouter(router());
|
|
546
|
+
return server;
|
|
547
|
+
});
|
|
548
|
+
logger = (0, import_neko_helper.createSingleton)((label) => {
|
|
549
|
+
const logger_config = import_neko_config.config.get(["loggers", label].join("."));
|
|
550
|
+
const rc = new import_neko_logger.Logger(logger_config);
|
|
551
|
+
rc.setExtrasFunction((message) => {
|
|
552
|
+
message.requestId = (0, import_neko_context2.ctxSafe)()?.get("requestId") || "N/A";
|
|
553
|
+
return message;
|
|
554
|
+
});
|
|
555
|
+
return rc;
|
|
556
|
+
});
|
|
557
|
+
mailer = (0, import_neko_helper.createSingleton)((label) => {
|
|
558
|
+
const mailer_config = import_neko_config.config.get(["mailer", label].join("."));
|
|
559
|
+
let provider;
|
|
560
|
+
if (mailer_config.provider === "logger") {
|
|
561
|
+
provider = new import_neko_mailer.FunctionProvider((mail) => {
|
|
562
|
+
logger().info({
|
|
563
|
+
msg: "Sending email",
|
|
564
|
+
mail
|
|
565
|
+
});
|
|
566
|
+
});
|
|
567
|
+
} else if (mailer_config.provider === "SES") {
|
|
568
|
+
provider = new import_neko_mailer.SESProvider(mailer_config.config);
|
|
569
|
+
} else if (mailer_config.provider === "SMTP") {
|
|
570
|
+
provider = new import_neko_mailer.SMTPProvider(mailer_config.config);
|
|
571
|
+
} else if (mailer_config.provider === "MEMORY") {
|
|
572
|
+
provider = new import_neko_mailer.MemoryProvider();
|
|
573
|
+
}
|
|
574
|
+
if (!provider) {
|
|
575
|
+
throw new Error(
|
|
576
|
+
`cannot initiate mailer provider: ${mailer_config?.provider}`
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
const rc = new import_neko_mailer.Mailer(provider);
|
|
580
|
+
return rc;
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
// src/app/console/migrate/MigrateCommand.ts
|
|
586
|
+
var import_clipanion2, import_neko_context3, import_path2, import_promises, import_neko_config2, MigrateCommand;
|
|
587
|
+
var init_MigrateCommand = __esm({
|
|
588
|
+
"src/app/console/migrate/MigrateCommand.ts"() {
|
|
589
|
+
"use strict";
|
|
590
|
+
init_facades();
|
|
591
|
+
import_clipanion2 = require("clipanion");
|
|
592
|
+
import_neko_context3 = require("@devbro/neko-context");
|
|
593
|
+
import_path2 = __toESM(require("path"));
|
|
594
|
+
import_promises = __toESM(require("fs/promises"));
|
|
595
|
+
import_neko_config2 = require("@devbro/neko-config");
|
|
596
|
+
MigrateCommand = class extends import_clipanion2.Command {
|
|
597
|
+
static {
|
|
598
|
+
__name(this, "MigrateCommand");
|
|
599
|
+
}
|
|
600
|
+
static paths = [
|
|
601
|
+
[
|
|
602
|
+
`migrate`
|
|
603
|
+
]
|
|
604
|
+
];
|
|
605
|
+
fresh = import_clipanion2.Option.Boolean("--fresh", false);
|
|
606
|
+
async execute() {
|
|
607
|
+
await import_neko_context3.context_provider.run(async () => {
|
|
608
|
+
const db2 = db();
|
|
609
|
+
const schema = db2.getSchema();
|
|
610
|
+
if (this.fresh) {
|
|
611
|
+
logger().info("dropping all tables!!");
|
|
612
|
+
let retry = true;
|
|
613
|
+
let retry_count = 0;
|
|
614
|
+
while (retry && retry_count < 10) {
|
|
615
|
+
retry = false;
|
|
616
|
+
retry_count++;
|
|
617
|
+
const tables = await schema.tables();
|
|
618
|
+
for (const table of tables) {
|
|
619
|
+
logger().info(`dropping table ${table.name}`);
|
|
620
|
+
try {
|
|
621
|
+
await schema.dropTable(table.name);
|
|
622
|
+
} catch {
|
|
623
|
+
logger().info(`failed to drop ${table.name}`);
|
|
624
|
+
retry = true;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
if (!await schema.tableExists("migrations")) {
|
|
630
|
+
await schema.createTable("migrations", (blueprint) => {
|
|
631
|
+
blueprint.id();
|
|
632
|
+
blueprint.timestamps();
|
|
633
|
+
blueprint.string("filename");
|
|
634
|
+
blueprint.integer("batch");
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
const migrationsDir = import_neko_config2.config.get("migration.path");
|
|
638
|
+
let files = [];
|
|
639
|
+
const dirEntries = await import_promises.default.readdir(migrationsDir);
|
|
640
|
+
files = dirEntries.filter((entry) => entry.endsWith(".ts") || entry.endsWith(".js")).sort();
|
|
641
|
+
let batch_number = await db2.runQuery({
|
|
642
|
+
sql: "select max(batch) as next_batch from migrations",
|
|
643
|
+
bindings: []
|
|
644
|
+
});
|
|
645
|
+
batch_number = batch_number[0].next_batch || 0;
|
|
646
|
+
batch_number++;
|
|
647
|
+
const migrations = await db2.runQuery({
|
|
648
|
+
sql: "select * from migrations order by created_at ASC",
|
|
649
|
+
bindings: []
|
|
650
|
+
});
|
|
651
|
+
const completed_migrations = migrations.map((r) => r.filename);
|
|
652
|
+
const pending_migrations = files.filter((file) => !completed_migrations.includes(file));
|
|
653
|
+
let migrated_count = 0;
|
|
654
|
+
for (const class_to_migrate of pending_migrations) {
|
|
655
|
+
logger().info(`migrating up ${class_to_migrate}`);
|
|
656
|
+
const ClassToMigrate = (await import(import_path2.default.join(migrationsDir, class_to_migrate))).default;
|
|
657
|
+
const c = new ClassToMigrate();
|
|
658
|
+
await c.up(db2.getSchema());
|
|
659
|
+
await db2.runQuery({
|
|
660
|
+
sql: "insert into migrations (filename, batch) values ($1,$2)",
|
|
661
|
+
bindings: [
|
|
662
|
+
class_to_migrate,
|
|
663
|
+
batch_number
|
|
664
|
+
]
|
|
665
|
+
});
|
|
666
|
+
migrated_count++;
|
|
667
|
+
}
|
|
668
|
+
if (migrated_count === 0) {
|
|
669
|
+
logger().warn("no migrations to run!");
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
logger().info(`migrated ${migrated_count} migrations successfully!`);
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
};
|
|
676
|
+
cli().register(MigrateCommand);
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
|
|
680
|
+
// src/app/console/migrate/GenerateMigrateCommand.ts
|
|
681
|
+
var import_clipanion3, import_change_case_all, import_path3, fs2, import_neko_config3, import_handlebars, import_url, import_meta, GenerateMigrateCommand;
|
|
682
|
+
var init_GenerateMigrateCommand = __esm({
|
|
683
|
+
"src/app/console/migrate/GenerateMigrateCommand.ts"() {
|
|
684
|
+
"use strict";
|
|
685
|
+
init_facades();
|
|
686
|
+
import_clipanion3 = require("clipanion");
|
|
687
|
+
import_change_case_all = require("change-case-all");
|
|
688
|
+
import_path3 = __toESM(require("path"));
|
|
689
|
+
fs2 = __toESM(require("fs/promises"));
|
|
690
|
+
import_neko_config3 = require("@devbro/neko-config");
|
|
691
|
+
import_handlebars = __toESM(require("handlebars"));
|
|
692
|
+
import_url = require("url");
|
|
693
|
+
import_meta = {};
|
|
694
|
+
GenerateMigrateCommand = class extends import_clipanion3.Command {
|
|
695
|
+
static {
|
|
696
|
+
__name(this, "GenerateMigrateCommand");
|
|
697
|
+
}
|
|
698
|
+
static paths = [
|
|
699
|
+
[
|
|
700
|
+
`generate`,
|
|
701
|
+
`migrate`
|
|
702
|
+
],
|
|
703
|
+
[
|
|
704
|
+
"generate",
|
|
705
|
+
"migration"
|
|
706
|
+
]
|
|
707
|
+
];
|
|
708
|
+
name = import_clipanion3.Option.String({
|
|
709
|
+
required: true
|
|
710
|
+
});
|
|
711
|
+
async execute() {
|
|
712
|
+
const date = /* @__PURE__ */ new Date();
|
|
713
|
+
const year = date.getFullYear();
|
|
714
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
715
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
716
|
+
const secondsOfDay = String(date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds()).padStart(5, "0");
|
|
717
|
+
const fixed_name = import_change_case_all.Case.snake(this.name);
|
|
718
|
+
const filename = `${year}_${month}_${day}_${secondsOfDay}_${fixed_name}.ts`;
|
|
719
|
+
this.context.stdout.write(`creating migration file ${filename}
|
|
720
|
+
`);
|
|
721
|
+
await fs2.mkdir(import_neko_config3.config.get("migration.path"), {
|
|
722
|
+
recursive: true
|
|
723
|
+
});
|
|
724
|
+
let dirname = typeof __dirname === "string" ? __dirname : void 0;
|
|
725
|
+
if (!dirname) {
|
|
726
|
+
dirname = import_path3.default.dirname((0, import_url.fileURLToPath)(import_meta.url));
|
|
727
|
+
}
|
|
728
|
+
const compiledTemplate = import_handlebars.default.compile((await fs2.readFile(import_path3.default.join(dirname, "./make_migration.tpl"))).toString());
|
|
729
|
+
const template = await compiledTemplate({
|
|
730
|
+
className: import_change_case_all.Case.pascal(this.name) + "Migration",
|
|
731
|
+
tableName: import_change_case_all.Case.snake(this.name)
|
|
732
|
+
});
|
|
733
|
+
await fs2.writeFile(import_path3.default.join(import_neko_config3.config.get("migration.path"), filename), template);
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
cli().register(GenerateMigrateCommand);
|
|
737
|
+
}
|
|
738
|
+
});
|
|
739
|
+
|
|
740
|
+
// ../node_modules/typanion/lib/index.js
|
|
741
|
+
var require_lib = __commonJS({
|
|
742
|
+
"../node_modules/typanion/lib/index.js"(exports2) {
|
|
743
|
+
"use strict";
|
|
744
|
+
Object.defineProperty(exports2, "__esModule", {
|
|
745
|
+
value: true
|
|
746
|
+
});
|
|
747
|
+
var simpleKeyRegExp = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
748
|
+
function getPrintable(value) {
|
|
749
|
+
if (value === null) return `null`;
|
|
750
|
+
if (value === void 0) return `undefined`;
|
|
751
|
+
if (value === ``) return `an empty string`;
|
|
752
|
+
if (typeof value === "symbol") return `<${value.toString()}>`;
|
|
753
|
+
if (Array.isArray(value)) return `an array`;
|
|
754
|
+
return JSON.stringify(value);
|
|
755
|
+
}
|
|
756
|
+
__name(getPrintable, "getPrintable");
|
|
757
|
+
function getPrintableArray(value, conjunction) {
|
|
758
|
+
if (value.length === 0) return `nothing`;
|
|
759
|
+
if (value.length === 1) return getPrintable(value[0]);
|
|
760
|
+
const rest = value.slice(0, -1);
|
|
761
|
+
const trailing = value[value.length - 1];
|
|
762
|
+
const separator = value.length > 2 ? `, ${conjunction} ` : ` ${conjunction} `;
|
|
763
|
+
return `${rest.map((value2) => getPrintable(value2)).join(`, `)}${separator}${getPrintable(trailing)}`;
|
|
764
|
+
}
|
|
765
|
+
__name(getPrintableArray, "getPrintableArray");
|
|
766
|
+
function computeKey(state, key) {
|
|
767
|
+
var _a, _b, _c;
|
|
768
|
+
if (typeof key === `number`) {
|
|
769
|
+
return `${(_a = state === null || state === void 0 ? void 0 : state.p) !== null && _a !== void 0 ? _a : `.`}[${key}]`;
|
|
770
|
+
} else if (simpleKeyRegExp.test(key)) {
|
|
771
|
+
return `${(_b = state === null || state === void 0 ? void 0 : state.p) !== null && _b !== void 0 ? _b : ``}.${key}`;
|
|
772
|
+
} else {
|
|
773
|
+
return `${(_c = state === null || state === void 0 ? void 0 : state.p) !== null && _c !== void 0 ? _c : `.`}[${JSON.stringify(key)}]`;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
__name(computeKey, "computeKey");
|
|
777
|
+
function plural(n, singular, plural2) {
|
|
778
|
+
return n === 1 ? singular : plural2;
|
|
779
|
+
}
|
|
780
|
+
__name(plural, "plural");
|
|
781
|
+
var colorStringRegExp = /^#[0-9a-f]{6}$/i;
|
|
782
|
+
var colorStringAlphaRegExp = /^#[0-9a-f]{6}([0-9a-f]{2})?$/i;
|
|
783
|
+
var base64RegExp = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
784
|
+
var uuid4RegExp = /^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$/i;
|
|
785
|
+
var iso8601RegExp = /^(?:[1-9]\d{3}(-?)(?:(?:0[1-9]|1[0-2])\1(?:0[1-9]|1\d|2[0-8])|(?:0[13-9]|1[0-2])\1(?:29|30)|(?:0[13578]|1[02])(?:\1)31|00[1-9]|0[1-9]\d|[12]\d{2}|3(?:[0-5]\d|6[0-5]))|(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00)(?:(-?)02(?:\2)29|-?366))T(?:[01]\d|2[0-3])(:?)[0-5]\d(?:\3[0-5]\d)?(?:Z|[+-][01]\d(?:\3[0-5]\d)?)$/;
|
|
786
|
+
function pushError({ errors, p } = {}, message) {
|
|
787
|
+
errors === null || errors === void 0 ? void 0 : errors.push(`${p !== null && p !== void 0 ? p : `.`}: ${message}`);
|
|
788
|
+
return false;
|
|
789
|
+
}
|
|
790
|
+
__name(pushError, "pushError");
|
|
791
|
+
function makeSetter(target, key) {
|
|
792
|
+
return (v) => {
|
|
793
|
+
target[key] = v;
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
__name(makeSetter, "makeSetter");
|
|
797
|
+
function makeCoercionFn(target, key) {
|
|
798
|
+
return (v) => {
|
|
799
|
+
const previous = target[key];
|
|
800
|
+
target[key] = v;
|
|
801
|
+
return makeCoercionFn(target, key).bind(null, previous);
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
__name(makeCoercionFn, "makeCoercionFn");
|
|
805
|
+
function makeLazyCoercionFn(fn2, orig, generator) {
|
|
806
|
+
const commit = /* @__PURE__ */ __name(() => {
|
|
807
|
+
fn2(generator());
|
|
808
|
+
return revert;
|
|
809
|
+
}, "commit");
|
|
810
|
+
const revert = /* @__PURE__ */ __name(() => {
|
|
811
|
+
fn2(orig);
|
|
812
|
+
return commit;
|
|
813
|
+
}, "revert");
|
|
814
|
+
return commit;
|
|
815
|
+
}
|
|
816
|
+
__name(makeLazyCoercionFn, "makeLazyCoercionFn");
|
|
817
|
+
function isUnknown() {
|
|
818
|
+
return makeValidator({
|
|
819
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
820
|
+
return true;
|
|
821
|
+
}, "test")
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
__name(isUnknown, "isUnknown");
|
|
825
|
+
function isLiteral(expected) {
|
|
826
|
+
return makeValidator({
|
|
827
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
828
|
+
if (value !== expected) return pushError(state, `Expected ${getPrintable(expected)} (got ${getPrintable(value)})`);
|
|
829
|
+
return true;
|
|
830
|
+
}, "test")
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
__name(isLiteral, "isLiteral");
|
|
834
|
+
function isString() {
|
|
835
|
+
return makeValidator({
|
|
836
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
837
|
+
if (typeof value !== `string`) return pushError(state, `Expected a string (got ${getPrintable(value)})`);
|
|
838
|
+
return true;
|
|
839
|
+
}, "test")
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
__name(isString, "isString");
|
|
843
|
+
function isEnum(enumSpec) {
|
|
844
|
+
const valuesArray = Array.isArray(enumSpec) ? enumSpec : Object.values(enumSpec);
|
|
845
|
+
const isAlphaNum = valuesArray.every((item) => typeof item === "string" || typeof item === "number");
|
|
846
|
+
const values = new Set(valuesArray);
|
|
847
|
+
if (values.size === 1) return isLiteral([
|
|
848
|
+
...values
|
|
849
|
+
][0]);
|
|
850
|
+
return makeValidator({
|
|
851
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
852
|
+
if (!values.has(value)) {
|
|
853
|
+
if (isAlphaNum) {
|
|
854
|
+
return pushError(state, `Expected one of ${getPrintableArray(valuesArray, `or`)} (got ${getPrintable(value)})`);
|
|
855
|
+
} else {
|
|
856
|
+
return pushError(state, `Expected a valid enumeration value (got ${getPrintable(value)})`);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
return true;
|
|
860
|
+
}, "test")
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
__name(isEnum, "isEnum");
|
|
864
|
+
var BOOLEAN_COERCIONS = /* @__PURE__ */ new Map([
|
|
865
|
+
[
|
|
866
|
+
`true`,
|
|
867
|
+
true
|
|
868
|
+
],
|
|
869
|
+
[
|
|
870
|
+
`True`,
|
|
871
|
+
true
|
|
872
|
+
],
|
|
873
|
+
[
|
|
874
|
+
`1`,
|
|
875
|
+
true
|
|
876
|
+
],
|
|
877
|
+
[
|
|
878
|
+
1,
|
|
879
|
+
true
|
|
880
|
+
],
|
|
881
|
+
[
|
|
882
|
+
`false`,
|
|
883
|
+
false
|
|
884
|
+
],
|
|
885
|
+
[
|
|
886
|
+
`False`,
|
|
887
|
+
false
|
|
888
|
+
],
|
|
889
|
+
[
|
|
890
|
+
`0`,
|
|
891
|
+
false
|
|
892
|
+
],
|
|
893
|
+
[
|
|
894
|
+
0,
|
|
895
|
+
false
|
|
896
|
+
]
|
|
897
|
+
]);
|
|
898
|
+
function isBoolean() {
|
|
899
|
+
return makeValidator({
|
|
900
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
901
|
+
var _a;
|
|
902
|
+
if (typeof value !== `boolean`) {
|
|
903
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
904
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
905
|
+
const coercion = BOOLEAN_COERCIONS.get(value);
|
|
906
|
+
if (typeof coercion !== `undefined`) {
|
|
907
|
+
state.coercions.push([
|
|
908
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
909
|
+
state.coercion.bind(null, coercion)
|
|
910
|
+
]);
|
|
911
|
+
return true;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
return pushError(state, `Expected a boolean (got ${getPrintable(value)})`);
|
|
915
|
+
}
|
|
916
|
+
return true;
|
|
917
|
+
}, "test")
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
__name(isBoolean, "isBoolean");
|
|
921
|
+
function isNumber2() {
|
|
922
|
+
return makeValidator({
|
|
923
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
924
|
+
var _a;
|
|
925
|
+
if (typeof value !== `number`) {
|
|
926
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
927
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
928
|
+
let coercion;
|
|
929
|
+
if (typeof value === `string`) {
|
|
930
|
+
let val;
|
|
931
|
+
try {
|
|
932
|
+
val = JSON.parse(value);
|
|
933
|
+
} catch (_b) {
|
|
934
|
+
}
|
|
935
|
+
if (typeof val === `number`) {
|
|
936
|
+
if (JSON.stringify(val) === value) {
|
|
937
|
+
coercion = val;
|
|
938
|
+
} else {
|
|
939
|
+
return pushError(state, `Received a number that can't be safely represented by the runtime (${value})`);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
if (typeof coercion !== `undefined`) {
|
|
944
|
+
state.coercions.push([
|
|
945
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
946
|
+
state.coercion.bind(null, coercion)
|
|
947
|
+
]);
|
|
948
|
+
return true;
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
return pushError(state, `Expected a number (got ${getPrintable(value)})`);
|
|
952
|
+
}
|
|
953
|
+
return true;
|
|
954
|
+
}, "test")
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
__name(isNumber2, "isNumber");
|
|
958
|
+
function isPayload(spec) {
|
|
959
|
+
return makeValidator({
|
|
960
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
961
|
+
var _a;
|
|
962
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) === `undefined`) return pushError(state, `The isPayload predicate can only be used with coercion enabled`);
|
|
963
|
+
if (typeof state.coercion === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
964
|
+
if (typeof value !== `string`) return pushError(state, `Expected a string (got ${getPrintable(value)})`);
|
|
965
|
+
let inner;
|
|
966
|
+
try {
|
|
967
|
+
inner = JSON.parse(value);
|
|
968
|
+
} catch (_b) {
|
|
969
|
+
return pushError(state, `Expected a JSON string (got ${getPrintable(value)})`);
|
|
970
|
+
}
|
|
971
|
+
const wrapper = {
|
|
972
|
+
value: inner
|
|
973
|
+
};
|
|
974
|
+
if (!spec(inner, Object.assign(Object.assign({}, state), {
|
|
975
|
+
coercion: makeCoercionFn(wrapper, `value`)
|
|
976
|
+
}))) return false;
|
|
977
|
+
state.coercions.push([
|
|
978
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
979
|
+
state.coercion.bind(null, wrapper.value)
|
|
980
|
+
]);
|
|
981
|
+
return true;
|
|
982
|
+
}, "test")
|
|
983
|
+
});
|
|
984
|
+
}
|
|
985
|
+
__name(isPayload, "isPayload");
|
|
986
|
+
function isDate() {
|
|
987
|
+
return makeValidator({
|
|
988
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
989
|
+
var _a;
|
|
990
|
+
if (!(value instanceof Date)) {
|
|
991
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
992
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
993
|
+
let coercion;
|
|
994
|
+
if (typeof value === `string` && iso8601RegExp.test(value)) {
|
|
995
|
+
coercion = new Date(value);
|
|
996
|
+
} else {
|
|
997
|
+
let timestamp;
|
|
998
|
+
if (typeof value === `string`) {
|
|
999
|
+
let val;
|
|
1000
|
+
try {
|
|
1001
|
+
val = JSON.parse(value);
|
|
1002
|
+
} catch (_b) {
|
|
1003
|
+
}
|
|
1004
|
+
if (typeof val === `number`) {
|
|
1005
|
+
timestamp = val;
|
|
1006
|
+
}
|
|
1007
|
+
} else if (typeof value === `number`) {
|
|
1008
|
+
timestamp = value;
|
|
1009
|
+
}
|
|
1010
|
+
if (typeof timestamp !== `undefined`) {
|
|
1011
|
+
if (Number.isSafeInteger(timestamp) || !Number.isSafeInteger(timestamp * 1e3)) {
|
|
1012
|
+
coercion = new Date(timestamp * 1e3);
|
|
1013
|
+
} else {
|
|
1014
|
+
return pushError(state, `Received a timestamp that can't be safely represented by the runtime (${value})`);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
if (typeof coercion !== `undefined`) {
|
|
1019
|
+
state.coercions.push([
|
|
1020
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
1021
|
+
state.coercion.bind(null, coercion)
|
|
1022
|
+
]);
|
|
1023
|
+
return true;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
return pushError(state, `Expected a date (got ${getPrintable(value)})`);
|
|
1027
|
+
}
|
|
1028
|
+
return true;
|
|
1029
|
+
}, "test")
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
__name(isDate, "isDate");
|
|
1033
|
+
function isArray(spec, { delimiter } = {}) {
|
|
1034
|
+
return makeValidator({
|
|
1035
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1036
|
+
var _a;
|
|
1037
|
+
const originalValue = value;
|
|
1038
|
+
if (typeof value === `string` && typeof delimiter !== `undefined`) {
|
|
1039
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
1040
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
1041
|
+
value = value.split(delimiter);
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
if (!Array.isArray(value)) return pushError(state, `Expected an array (got ${getPrintable(value)})`);
|
|
1045
|
+
let valid = true;
|
|
1046
|
+
for (let t2 = 0, T = value.length; t2 < T; ++t2) {
|
|
1047
|
+
valid = spec(value[t2], Object.assign(Object.assign({}, state), {
|
|
1048
|
+
p: computeKey(state, t2),
|
|
1049
|
+
coercion: makeCoercionFn(value, t2)
|
|
1050
|
+
})) && valid;
|
|
1051
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
1052
|
+
break;
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
if (value !== originalValue) state.coercions.push([
|
|
1056
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
1057
|
+
state.coercion.bind(null, value)
|
|
1058
|
+
]);
|
|
1059
|
+
return valid;
|
|
1060
|
+
}, "test")
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1063
|
+
__name(isArray, "isArray");
|
|
1064
|
+
function isSet(spec, { delimiter } = {}) {
|
|
1065
|
+
const isArrayValidator = isArray(spec, {
|
|
1066
|
+
delimiter
|
|
1067
|
+
});
|
|
1068
|
+
return makeValidator({
|
|
1069
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1070
|
+
var _a, _b;
|
|
1071
|
+
if (Object.getPrototypeOf(value).toString() === `[object Set]`) {
|
|
1072
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
1073
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
1074
|
+
const originalValues = [
|
|
1075
|
+
...value
|
|
1076
|
+
];
|
|
1077
|
+
const coercedValues = [
|
|
1078
|
+
...value
|
|
1079
|
+
];
|
|
1080
|
+
if (!isArrayValidator(coercedValues, Object.assign(Object.assign({}, state), {
|
|
1081
|
+
coercion: void 0
|
|
1082
|
+
}))) return false;
|
|
1083
|
+
const updateValue = /* @__PURE__ */ __name(() => coercedValues.some((val, t2) => val !== originalValues[t2]) ? new Set(coercedValues) : value, "updateValue");
|
|
1084
|
+
state.coercions.push([
|
|
1085
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
1086
|
+
makeLazyCoercionFn(state.coercion, value, updateValue)
|
|
1087
|
+
]);
|
|
1088
|
+
return true;
|
|
1089
|
+
} else {
|
|
1090
|
+
let valid = true;
|
|
1091
|
+
for (const subValue of value) {
|
|
1092
|
+
valid = spec(subValue, Object.assign({}, state)) && valid;
|
|
1093
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
1094
|
+
break;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
return valid;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
1101
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
1102
|
+
const store = {
|
|
1103
|
+
value
|
|
1104
|
+
};
|
|
1105
|
+
if (!isArrayValidator(value, Object.assign(Object.assign({}, state), {
|
|
1106
|
+
coercion: makeCoercionFn(store, `value`)
|
|
1107
|
+
}))) return false;
|
|
1108
|
+
state.coercions.push([
|
|
1109
|
+
(_b = state.p) !== null && _b !== void 0 ? _b : `.`,
|
|
1110
|
+
makeLazyCoercionFn(state.coercion, value, () => new Set(store.value))
|
|
1111
|
+
]);
|
|
1112
|
+
return true;
|
|
1113
|
+
}
|
|
1114
|
+
return pushError(state, `Expected a set (got ${getPrintable(value)})`);
|
|
1115
|
+
}, "test")
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
__name(isSet, "isSet");
|
|
1119
|
+
function isMap(keySpec, valueSpec) {
|
|
1120
|
+
const isArrayValidator = isArray(isTuple([
|
|
1121
|
+
keySpec,
|
|
1122
|
+
valueSpec
|
|
1123
|
+
]));
|
|
1124
|
+
const isRecordValidator = isRecord(valueSpec, {
|
|
1125
|
+
keys: keySpec
|
|
1126
|
+
});
|
|
1127
|
+
return makeValidator({
|
|
1128
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1129
|
+
var _a, _b, _c;
|
|
1130
|
+
if (Object.getPrototypeOf(value).toString() === `[object Map]`) {
|
|
1131
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
1132
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
1133
|
+
const originalValues = [
|
|
1134
|
+
...value
|
|
1135
|
+
];
|
|
1136
|
+
const coercedValues = [
|
|
1137
|
+
...value
|
|
1138
|
+
];
|
|
1139
|
+
if (!isArrayValidator(coercedValues, Object.assign(Object.assign({}, state), {
|
|
1140
|
+
coercion: void 0
|
|
1141
|
+
}))) return false;
|
|
1142
|
+
const updateValue = /* @__PURE__ */ __name(() => coercedValues.some((val, t2) => val[0] !== originalValues[t2][0] || val[1] !== originalValues[t2][1]) ? new Map(coercedValues) : value, "updateValue");
|
|
1143
|
+
state.coercions.push([
|
|
1144
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
1145
|
+
makeLazyCoercionFn(state.coercion, value, updateValue)
|
|
1146
|
+
]);
|
|
1147
|
+
return true;
|
|
1148
|
+
} else {
|
|
1149
|
+
let valid = true;
|
|
1150
|
+
for (const [key, subValue] of value) {
|
|
1151
|
+
valid = keySpec(key, Object.assign({}, state)) && valid;
|
|
1152
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
1153
|
+
break;
|
|
1154
|
+
}
|
|
1155
|
+
valid = valueSpec(subValue, Object.assign(Object.assign({}, state), {
|
|
1156
|
+
p: computeKey(state, key)
|
|
1157
|
+
})) && valid;
|
|
1158
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
1159
|
+
break;
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
return valid;
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
1166
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
1167
|
+
const store = {
|
|
1168
|
+
value
|
|
1169
|
+
};
|
|
1170
|
+
if (Array.isArray(value)) {
|
|
1171
|
+
if (!isArrayValidator(value, Object.assign(Object.assign({}, state), {
|
|
1172
|
+
coercion: void 0
|
|
1173
|
+
}))) return false;
|
|
1174
|
+
state.coercions.push([
|
|
1175
|
+
(_b = state.p) !== null && _b !== void 0 ? _b : `.`,
|
|
1176
|
+
makeLazyCoercionFn(state.coercion, value, () => new Map(store.value))
|
|
1177
|
+
]);
|
|
1178
|
+
return true;
|
|
1179
|
+
} else {
|
|
1180
|
+
if (!isRecordValidator(value, Object.assign(Object.assign({}, state), {
|
|
1181
|
+
coercion: makeCoercionFn(store, `value`)
|
|
1182
|
+
}))) return false;
|
|
1183
|
+
state.coercions.push([
|
|
1184
|
+
(_c = state.p) !== null && _c !== void 0 ? _c : `.`,
|
|
1185
|
+
makeLazyCoercionFn(state.coercion, value, () => new Map(Object.entries(store.value)))
|
|
1186
|
+
]);
|
|
1187
|
+
return true;
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
return pushError(state, `Expected a map (got ${getPrintable(value)})`);
|
|
1191
|
+
}, "test")
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
__name(isMap, "isMap");
|
|
1195
|
+
function isTuple(spec, { delimiter } = {}) {
|
|
1196
|
+
const lengthValidator = hasExactLength(spec.length);
|
|
1197
|
+
return makeValidator({
|
|
1198
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1199
|
+
var _a;
|
|
1200
|
+
if (typeof value === `string` && typeof delimiter !== `undefined`) {
|
|
1201
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
1202
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
1203
|
+
value = value.split(delimiter);
|
|
1204
|
+
state.coercions.push([
|
|
1205
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
1206
|
+
state.coercion.bind(null, value)
|
|
1207
|
+
]);
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
if (!Array.isArray(value)) return pushError(state, `Expected a tuple (got ${getPrintable(value)})`);
|
|
1211
|
+
let valid = lengthValidator(value, Object.assign({}, state));
|
|
1212
|
+
for (let t2 = 0, T = value.length; t2 < T && t2 < spec.length; ++t2) {
|
|
1213
|
+
valid = spec[t2](value[t2], Object.assign(Object.assign({}, state), {
|
|
1214
|
+
p: computeKey(state, t2),
|
|
1215
|
+
coercion: makeCoercionFn(value, t2)
|
|
1216
|
+
})) && valid;
|
|
1217
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
1218
|
+
break;
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
return valid;
|
|
1222
|
+
}, "test")
|
|
1223
|
+
});
|
|
1224
|
+
}
|
|
1225
|
+
__name(isTuple, "isTuple");
|
|
1226
|
+
function isRecord(spec, { keys: keySpec = null } = {}) {
|
|
1227
|
+
const isArrayValidator = isArray(isTuple([
|
|
1228
|
+
keySpec !== null && keySpec !== void 0 ? keySpec : isString(),
|
|
1229
|
+
spec
|
|
1230
|
+
]));
|
|
1231
|
+
return makeValidator({
|
|
1232
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1233
|
+
var _a;
|
|
1234
|
+
if (Array.isArray(value)) {
|
|
1235
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
1236
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
1237
|
+
if (!isArrayValidator(value, Object.assign(Object.assign({}, state), {
|
|
1238
|
+
coercion: void 0
|
|
1239
|
+
}))) return false;
|
|
1240
|
+
value = Object.fromEntries(value);
|
|
1241
|
+
state.coercions.push([
|
|
1242
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
1243
|
+
state.coercion.bind(null, value)
|
|
1244
|
+
]);
|
|
1245
|
+
return true;
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
if (typeof value !== `object` || value === null) return pushError(state, `Expected an object (got ${getPrintable(value)})`);
|
|
1249
|
+
const keys = Object.keys(value);
|
|
1250
|
+
let valid = true;
|
|
1251
|
+
for (let t2 = 0, T = keys.length; t2 < T && (valid || (state === null || state === void 0 ? void 0 : state.errors) != null); ++t2) {
|
|
1252
|
+
const key = keys[t2];
|
|
1253
|
+
const sub = value[key];
|
|
1254
|
+
if (key === `__proto__` || key === `constructor`) {
|
|
1255
|
+
valid = pushError(Object.assign(Object.assign({}, state), {
|
|
1256
|
+
p: computeKey(state, key)
|
|
1257
|
+
}), `Unsafe property name`);
|
|
1258
|
+
continue;
|
|
1259
|
+
}
|
|
1260
|
+
if (keySpec !== null && !keySpec(key, state)) {
|
|
1261
|
+
valid = false;
|
|
1262
|
+
continue;
|
|
1263
|
+
}
|
|
1264
|
+
if (!spec(sub, Object.assign(Object.assign({}, state), {
|
|
1265
|
+
p: computeKey(state, key),
|
|
1266
|
+
coercion: makeCoercionFn(value, key)
|
|
1267
|
+
}))) {
|
|
1268
|
+
valid = false;
|
|
1269
|
+
continue;
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
return valid;
|
|
1273
|
+
}, "test")
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
__name(isRecord, "isRecord");
|
|
1277
|
+
function isDict(spec, opts = {}) {
|
|
1278
|
+
return isRecord(spec, opts);
|
|
1279
|
+
}
|
|
1280
|
+
__name(isDict, "isDict");
|
|
1281
|
+
function isObject(props, { extra: extraSpec = null } = {}) {
|
|
1282
|
+
const specKeys = Object.keys(props);
|
|
1283
|
+
const validator = makeValidator({
|
|
1284
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1285
|
+
if (typeof value !== `object` || value === null) return pushError(state, `Expected an object (got ${getPrintable(value)})`);
|
|
1286
|
+
const keys = /* @__PURE__ */ new Set([
|
|
1287
|
+
...specKeys,
|
|
1288
|
+
...Object.keys(value)
|
|
1289
|
+
]);
|
|
1290
|
+
const extra = {};
|
|
1291
|
+
let valid = true;
|
|
1292
|
+
for (const key of keys) {
|
|
1293
|
+
if (key === `constructor` || key === `__proto__`) {
|
|
1294
|
+
valid = pushError(Object.assign(Object.assign({}, state), {
|
|
1295
|
+
p: computeKey(state, key)
|
|
1296
|
+
}), `Unsafe property name`);
|
|
1297
|
+
} else {
|
|
1298
|
+
const spec = Object.prototype.hasOwnProperty.call(props, key) ? props[key] : void 0;
|
|
1299
|
+
const sub = Object.prototype.hasOwnProperty.call(value, key) ? value[key] : void 0;
|
|
1300
|
+
if (typeof spec !== `undefined`) {
|
|
1301
|
+
valid = spec(sub, Object.assign(Object.assign({}, state), {
|
|
1302
|
+
p: computeKey(state, key),
|
|
1303
|
+
coercion: makeCoercionFn(value, key)
|
|
1304
|
+
})) && valid;
|
|
1305
|
+
} else if (extraSpec === null) {
|
|
1306
|
+
valid = pushError(Object.assign(Object.assign({}, state), {
|
|
1307
|
+
p: computeKey(state, key)
|
|
1308
|
+
}), `Extraneous property (got ${getPrintable(sub)})`);
|
|
1309
|
+
} else {
|
|
1310
|
+
Object.defineProperty(extra, key, {
|
|
1311
|
+
enumerable: true,
|
|
1312
|
+
get: /* @__PURE__ */ __name(() => sub, "get"),
|
|
1313
|
+
set: makeSetter(value, key)
|
|
1314
|
+
});
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
if (!valid && (state === null || state === void 0 ? void 0 : state.errors) == null) {
|
|
1318
|
+
break;
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
if (extraSpec !== null && (valid || (state === null || state === void 0 ? void 0 : state.errors) != null)) valid = extraSpec(extra, state) && valid;
|
|
1322
|
+
return valid;
|
|
1323
|
+
}, "test")
|
|
1324
|
+
});
|
|
1325
|
+
return Object.assign(validator, {
|
|
1326
|
+
properties: props
|
|
1327
|
+
});
|
|
1328
|
+
}
|
|
1329
|
+
__name(isObject, "isObject");
|
|
1330
|
+
function isPartial(props) {
|
|
1331
|
+
return isObject(props, {
|
|
1332
|
+
extra: isRecord(isUnknown())
|
|
1333
|
+
});
|
|
1334
|
+
}
|
|
1335
|
+
__name(isPartial, "isPartial");
|
|
1336
|
+
var isInstanceOf = /* @__PURE__ */ __name((constructor) => makeValidator({
|
|
1337
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1338
|
+
if (!(value instanceof constructor)) return pushError(state, `Expected an instance of ${constructor.name} (got ${getPrintable(value)})`);
|
|
1339
|
+
return true;
|
|
1340
|
+
}, "test")
|
|
1341
|
+
}), "isInstanceOf");
|
|
1342
|
+
var isOneOf = /* @__PURE__ */ __name((specs, { exclusive = false } = {}) => makeValidator({
|
|
1343
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1344
|
+
var _a, _b, _c;
|
|
1345
|
+
const matches = [];
|
|
1346
|
+
const errorBuffer = typeof (state === null || state === void 0 ? void 0 : state.errors) !== `undefined` ? [] : void 0;
|
|
1347
|
+
for (let t2 = 0, T = specs.length; t2 < T; ++t2) {
|
|
1348
|
+
const subErrors = typeof (state === null || state === void 0 ? void 0 : state.errors) !== `undefined` ? [] : void 0;
|
|
1349
|
+
const subCoercions = typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined` ? [] : void 0;
|
|
1350
|
+
if (specs[t2](value, Object.assign(Object.assign({}, state), {
|
|
1351
|
+
errors: subErrors,
|
|
1352
|
+
coercions: subCoercions,
|
|
1353
|
+
p: `${(_a = state === null || state === void 0 ? void 0 : state.p) !== null && _a !== void 0 ? _a : `.`}#${t2 + 1}`
|
|
1354
|
+
}))) {
|
|
1355
|
+
matches.push([
|
|
1356
|
+
`#${t2 + 1}`,
|
|
1357
|
+
subCoercions
|
|
1358
|
+
]);
|
|
1359
|
+
if (!exclusive) {
|
|
1360
|
+
break;
|
|
1361
|
+
}
|
|
1362
|
+
} else {
|
|
1363
|
+
errorBuffer === null || errorBuffer === void 0 ? void 0 : errorBuffer.push(subErrors[0]);
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
if (matches.length === 1) {
|
|
1367
|
+
const [, subCoercions] = matches[0];
|
|
1368
|
+
if (typeof subCoercions !== `undefined`) (_b = state === null || state === void 0 ? void 0 : state.coercions) === null || _b === void 0 ? void 0 : _b.push(...subCoercions);
|
|
1369
|
+
return true;
|
|
1370
|
+
}
|
|
1371
|
+
if (matches.length > 1) pushError(state, `Expected to match exactly a single predicate (matched ${matches.join(`, `)})`);
|
|
1372
|
+
else (_c = state === null || state === void 0 ? void 0 : state.errors) === null || _c === void 0 ? void 0 : _c.push(...errorBuffer);
|
|
1373
|
+
return false;
|
|
1374
|
+
}, "test")
|
|
1375
|
+
}), "isOneOf");
|
|
1376
|
+
function makeTrait(value) {
|
|
1377
|
+
return () => {
|
|
1378
|
+
return value;
|
|
1379
|
+
};
|
|
1380
|
+
}
|
|
1381
|
+
__name(makeTrait, "makeTrait");
|
|
1382
|
+
function makeValidator({ test }) {
|
|
1383
|
+
return makeTrait(test)();
|
|
1384
|
+
}
|
|
1385
|
+
__name(makeValidator, "makeValidator");
|
|
1386
|
+
var TypeAssertionError = class TypeAssertionError extends Error {
|
|
1387
|
+
static {
|
|
1388
|
+
__name(this, "TypeAssertionError");
|
|
1389
|
+
}
|
|
1390
|
+
constructor({ errors } = {}) {
|
|
1391
|
+
let errorMessage = `Type mismatch`;
|
|
1392
|
+
if (errors && errors.length > 0) {
|
|
1393
|
+
errorMessage += `
|
|
1394
|
+
`;
|
|
1395
|
+
for (const error of errors) {
|
|
1396
|
+
errorMessage += `
|
|
1397
|
+
- ${error}`;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
super(errorMessage);
|
|
1401
|
+
}
|
|
1402
|
+
};
|
|
1403
|
+
function assert(val, validator) {
|
|
1404
|
+
if (!validator(val)) {
|
|
1405
|
+
throw new TypeAssertionError();
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
__name(assert, "assert");
|
|
1409
|
+
function assertWithErrors(val, validator) {
|
|
1410
|
+
const errors = [];
|
|
1411
|
+
if (!validator(val, {
|
|
1412
|
+
errors
|
|
1413
|
+
})) {
|
|
1414
|
+
throw new TypeAssertionError({
|
|
1415
|
+
errors
|
|
1416
|
+
});
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
__name(assertWithErrors, "assertWithErrors");
|
|
1420
|
+
function softAssert(val, validator) {
|
|
1421
|
+
}
|
|
1422
|
+
__name(softAssert, "softAssert");
|
|
1423
|
+
function as(value, validator, { coerce = false, errors: storeErrors, throw: throws } = {}) {
|
|
1424
|
+
const errors = storeErrors ? [] : void 0;
|
|
1425
|
+
if (!coerce) {
|
|
1426
|
+
if (validator(value, {
|
|
1427
|
+
errors
|
|
1428
|
+
})) {
|
|
1429
|
+
return throws ? value : {
|
|
1430
|
+
value,
|
|
1431
|
+
errors: void 0
|
|
1432
|
+
};
|
|
1433
|
+
} else if (!throws) {
|
|
1434
|
+
return {
|
|
1435
|
+
value: void 0,
|
|
1436
|
+
errors: errors !== null && errors !== void 0 ? errors : true
|
|
1437
|
+
};
|
|
1438
|
+
} else {
|
|
1439
|
+
throw new TypeAssertionError({
|
|
1440
|
+
errors
|
|
1441
|
+
});
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
const state = {
|
|
1445
|
+
value
|
|
1446
|
+
};
|
|
1447
|
+
const coercion = makeCoercionFn(state, `value`);
|
|
1448
|
+
const coercions = [];
|
|
1449
|
+
if (!validator(value, {
|
|
1450
|
+
errors,
|
|
1451
|
+
coercion,
|
|
1452
|
+
coercions
|
|
1453
|
+
})) {
|
|
1454
|
+
if (!throws) {
|
|
1455
|
+
return {
|
|
1456
|
+
value: void 0,
|
|
1457
|
+
errors: errors !== null && errors !== void 0 ? errors : true
|
|
1458
|
+
};
|
|
1459
|
+
} else {
|
|
1460
|
+
throw new TypeAssertionError({
|
|
1461
|
+
errors
|
|
1462
|
+
});
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
for (const [, apply] of coercions) apply();
|
|
1466
|
+
if (throws) {
|
|
1467
|
+
return state.value;
|
|
1468
|
+
} else {
|
|
1469
|
+
return {
|
|
1470
|
+
value: state.value,
|
|
1471
|
+
errors: void 0
|
|
1472
|
+
};
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
__name(as, "as");
|
|
1476
|
+
function fn(validators, fn2) {
|
|
1477
|
+
const isValidArgList = isTuple(validators);
|
|
1478
|
+
return (...args) => {
|
|
1479
|
+
const check = isValidArgList(args);
|
|
1480
|
+
if (!check) throw new TypeAssertionError();
|
|
1481
|
+
return fn2(...args);
|
|
1482
|
+
};
|
|
1483
|
+
}
|
|
1484
|
+
__name(fn, "fn");
|
|
1485
|
+
function hasMinLength(length) {
|
|
1486
|
+
return makeValidator({
|
|
1487
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1488
|
+
if (!(value.length >= length)) return pushError(state, `Expected to have a length of at least ${length} elements (got ${value.length})`);
|
|
1489
|
+
return true;
|
|
1490
|
+
}, "test")
|
|
1491
|
+
});
|
|
1492
|
+
}
|
|
1493
|
+
__name(hasMinLength, "hasMinLength");
|
|
1494
|
+
function hasMaxLength(length) {
|
|
1495
|
+
return makeValidator({
|
|
1496
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1497
|
+
if (!(value.length <= length)) return pushError(state, `Expected to have a length of at most ${length} elements (got ${value.length})`);
|
|
1498
|
+
return true;
|
|
1499
|
+
}, "test")
|
|
1500
|
+
});
|
|
1501
|
+
}
|
|
1502
|
+
__name(hasMaxLength, "hasMaxLength");
|
|
1503
|
+
function hasExactLength(length) {
|
|
1504
|
+
return makeValidator({
|
|
1505
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1506
|
+
if (!(value.length === length)) return pushError(state, `Expected to have a length of exactly ${length} elements (got ${value.length})`);
|
|
1507
|
+
return true;
|
|
1508
|
+
}, "test")
|
|
1509
|
+
});
|
|
1510
|
+
}
|
|
1511
|
+
__name(hasExactLength, "hasExactLength");
|
|
1512
|
+
function hasUniqueItems({ map } = {}) {
|
|
1513
|
+
return makeValidator({
|
|
1514
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1515
|
+
const set = /* @__PURE__ */ new Set();
|
|
1516
|
+
const dup = /* @__PURE__ */ new Set();
|
|
1517
|
+
for (let t2 = 0, T = value.length; t2 < T; ++t2) {
|
|
1518
|
+
const sub = value[t2];
|
|
1519
|
+
const key = typeof map !== `undefined` ? map(sub) : sub;
|
|
1520
|
+
if (set.has(key)) {
|
|
1521
|
+
if (dup.has(key)) continue;
|
|
1522
|
+
pushError(state, `Expected to contain unique elements; got a duplicate with ${getPrintable(value)}`);
|
|
1523
|
+
dup.add(key);
|
|
1524
|
+
} else {
|
|
1525
|
+
set.add(key);
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
return dup.size === 0;
|
|
1529
|
+
}, "test")
|
|
1530
|
+
});
|
|
1531
|
+
}
|
|
1532
|
+
__name(hasUniqueItems, "hasUniqueItems");
|
|
1533
|
+
function isNegative() {
|
|
1534
|
+
return makeValidator({
|
|
1535
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1536
|
+
if (!(value <= 0)) return pushError(state, `Expected to be negative (got ${value})`);
|
|
1537
|
+
return true;
|
|
1538
|
+
}, "test")
|
|
1539
|
+
});
|
|
1540
|
+
}
|
|
1541
|
+
__name(isNegative, "isNegative");
|
|
1542
|
+
function isPositive() {
|
|
1543
|
+
return makeValidator({
|
|
1544
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1545
|
+
if (!(value >= 0)) return pushError(state, `Expected to be positive (got ${value})`);
|
|
1546
|
+
return true;
|
|
1547
|
+
}, "test")
|
|
1548
|
+
});
|
|
1549
|
+
}
|
|
1550
|
+
__name(isPositive, "isPositive");
|
|
1551
|
+
function isAtLeast(n) {
|
|
1552
|
+
return makeValidator({
|
|
1553
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1554
|
+
if (!(value >= n)) return pushError(state, `Expected to be at least ${n} (got ${value})`);
|
|
1555
|
+
return true;
|
|
1556
|
+
}, "test")
|
|
1557
|
+
});
|
|
1558
|
+
}
|
|
1559
|
+
__name(isAtLeast, "isAtLeast");
|
|
1560
|
+
function isAtMost(n) {
|
|
1561
|
+
return makeValidator({
|
|
1562
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1563
|
+
if (!(value <= n)) return pushError(state, `Expected to be at most ${n} (got ${value})`);
|
|
1564
|
+
return true;
|
|
1565
|
+
}, "test")
|
|
1566
|
+
});
|
|
1567
|
+
}
|
|
1568
|
+
__name(isAtMost, "isAtMost");
|
|
1569
|
+
function isInInclusiveRange(a, b) {
|
|
1570
|
+
return makeValidator({
|
|
1571
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1572
|
+
if (!(value >= a && value <= b)) return pushError(state, `Expected to be in the [${a}; ${b}] range (got ${value})`);
|
|
1573
|
+
return true;
|
|
1574
|
+
}, "test")
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
__name(isInInclusiveRange, "isInInclusiveRange");
|
|
1578
|
+
function isInExclusiveRange(a, b) {
|
|
1579
|
+
return makeValidator({
|
|
1580
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1581
|
+
if (!(value >= a && value < b)) return pushError(state, `Expected to be in the [${a}; ${b}[ range (got ${value})`);
|
|
1582
|
+
return true;
|
|
1583
|
+
}, "test")
|
|
1584
|
+
});
|
|
1585
|
+
}
|
|
1586
|
+
__name(isInExclusiveRange, "isInExclusiveRange");
|
|
1587
|
+
function isInteger({ unsafe = false } = {}) {
|
|
1588
|
+
return makeValidator({
|
|
1589
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1590
|
+
if (value !== Math.round(value)) return pushError(state, `Expected to be an integer (got ${value})`);
|
|
1591
|
+
if (!unsafe && !Number.isSafeInteger(value)) return pushError(state, `Expected to be a safe integer (got ${value})`);
|
|
1592
|
+
return true;
|
|
1593
|
+
}, "test")
|
|
1594
|
+
});
|
|
1595
|
+
}
|
|
1596
|
+
__name(isInteger, "isInteger");
|
|
1597
|
+
function matchesRegExp(regExp) {
|
|
1598
|
+
return makeValidator({
|
|
1599
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1600
|
+
if (!regExp.test(value)) return pushError(state, `Expected to match the pattern ${regExp.toString()} (got ${getPrintable(value)})`);
|
|
1601
|
+
return true;
|
|
1602
|
+
}, "test")
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
1605
|
+
__name(matchesRegExp, "matchesRegExp");
|
|
1606
|
+
function isLowerCase() {
|
|
1607
|
+
return makeValidator({
|
|
1608
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1609
|
+
if (value !== value.toLowerCase()) return pushError(state, `Expected to be all-lowercase (got ${value})`);
|
|
1610
|
+
return true;
|
|
1611
|
+
}, "test")
|
|
1612
|
+
});
|
|
1613
|
+
}
|
|
1614
|
+
__name(isLowerCase, "isLowerCase");
|
|
1615
|
+
function isUpperCase() {
|
|
1616
|
+
return makeValidator({
|
|
1617
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1618
|
+
if (value !== value.toUpperCase()) return pushError(state, `Expected to be all-uppercase (got ${value})`);
|
|
1619
|
+
return true;
|
|
1620
|
+
}, "test")
|
|
1621
|
+
});
|
|
1622
|
+
}
|
|
1623
|
+
__name(isUpperCase, "isUpperCase");
|
|
1624
|
+
function isUUID4() {
|
|
1625
|
+
return makeValidator({
|
|
1626
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1627
|
+
if (!uuid4RegExp.test(value)) return pushError(state, `Expected to be a valid UUID v4 (got ${getPrintable(value)})`);
|
|
1628
|
+
return true;
|
|
1629
|
+
}, "test")
|
|
1630
|
+
});
|
|
1631
|
+
}
|
|
1632
|
+
__name(isUUID4, "isUUID4");
|
|
1633
|
+
function isISO8601() {
|
|
1634
|
+
return makeValidator({
|
|
1635
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1636
|
+
if (!iso8601RegExp.test(value)) return pushError(state, `Expected to be a valid ISO 8601 date string (got ${getPrintable(value)})`);
|
|
1637
|
+
return true;
|
|
1638
|
+
}, "test")
|
|
1639
|
+
});
|
|
1640
|
+
}
|
|
1641
|
+
__name(isISO8601, "isISO8601");
|
|
1642
|
+
function isHexColor({ alpha = false }) {
|
|
1643
|
+
return makeValidator({
|
|
1644
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1645
|
+
const res = alpha ? colorStringRegExp.test(value) : colorStringAlphaRegExp.test(value);
|
|
1646
|
+
if (!res) return pushError(state, `Expected to be a valid hexadecimal color string (got ${getPrintable(value)})`);
|
|
1647
|
+
return true;
|
|
1648
|
+
}, "test")
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1651
|
+
__name(isHexColor, "isHexColor");
|
|
1652
|
+
function isBase64() {
|
|
1653
|
+
return makeValidator({
|
|
1654
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1655
|
+
if (!base64RegExp.test(value)) return pushError(state, `Expected to be a valid base 64 string (got ${getPrintable(value)})`);
|
|
1656
|
+
return true;
|
|
1657
|
+
}, "test")
|
|
1658
|
+
});
|
|
1659
|
+
}
|
|
1660
|
+
__name(isBase64, "isBase64");
|
|
1661
|
+
function isJSON(spec = isUnknown()) {
|
|
1662
|
+
return makeValidator({
|
|
1663
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1664
|
+
let data;
|
|
1665
|
+
try {
|
|
1666
|
+
data = JSON.parse(value);
|
|
1667
|
+
} catch (_a) {
|
|
1668
|
+
return pushError(state, `Expected to be a valid JSON string (got ${getPrintable(value)})`);
|
|
1669
|
+
}
|
|
1670
|
+
return spec(data, state);
|
|
1671
|
+
}, "test")
|
|
1672
|
+
});
|
|
1673
|
+
}
|
|
1674
|
+
__name(isJSON, "isJSON");
|
|
1675
|
+
function cascade(spec, ...followups) {
|
|
1676
|
+
const resolvedFollowups = Array.isArray(followups[0]) ? followups[0] : followups;
|
|
1677
|
+
return makeValidator({
|
|
1678
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1679
|
+
var _a, _b;
|
|
1680
|
+
const context = {
|
|
1681
|
+
value
|
|
1682
|
+
};
|
|
1683
|
+
const subCoercion = typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined` ? makeCoercionFn(context, `value`) : void 0;
|
|
1684
|
+
const subCoercions = typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined` ? [] : void 0;
|
|
1685
|
+
if (!spec(value, Object.assign(Object.assign({}, state), {
|
|
1686
|
+
coercion: subCoercion,
|
|
1687
|
+
coercions: subCoercions
|
|
1688
|
+
}))) return false;
|
|
1689
|
+
const reverts = [];
|
|
1690
|
+
if (typeof subCoercions !== `undefined`) for (const [, coercion] of subCoercions) reverts.push(coercion());
|
|
1691
|
+
try {
|
|
1692
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercions) !== `undefined`) {
|
|
1693
|
+
if (context.value !== value) {
|
|
1694
|
+
if (typeof (state === null || state === void 0 ? void 0 : state.coercion) === `undefined`) return pushError(state, `Unbound coercion result`);
|
|
1695
|
+
state.coercions.push([
|
|
1696
|
+
(_a = state.p) !== null && _a !== void 0 ? _a : `.`,
|
|
1697
|
+
state.coercion.bind(null, context.value)
|
|
1698
|
+
]);
|
|
1699
|
+
}
|
|
1700
|
+
(_b = state === null || state === void 0 ? void 0 : state.coercions) === null || _b === void 0 ? void 0 : _b.push(...subCoercions);
|
|
1701
|
+
}
|
|
1702
|
+
return resolvedFollowups.every((spec2) => {
|
|
1703
|
+
return spec2(context.value, state);
|
|
1704
|
+
});
|
|
1705
|
+
} finally {
|
|
1706
|
+
for (const revert of reverts) {
|
|
1707
|
+
revert();
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
}, "test")
|
|
1711
|
+
});
|
|
1712
|
+
}
|
|
1713
|
+
__name(cascade, "cascade");
|
|
1714
|
+
function applyCascade(spec, ...followups) {
|
|
1715
|
+
const resolvedFollowups = Array.isArray(followups[0]) ? followups[0] : followups;
|
|
1716
|
+
return cascade(spec, resolvedFollowups);
|
|
1717
|
+
}
|
|
1718
|
+
__name(applyCascade, "applyCascade");
|
|
1719
|
+
function isOptional(spec) {
|
|
1720
|
+
return makeValidator({
|
|
1721
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1722
|
+
if (typeof value === `undefined`) return true;
|
|
1723
|
+
return spec(value, state);
|
|
1724
|
+
}, "test")
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
__name(isOptional, "isOptional");
|
|
1728
|
+
function isNullable(spec) {
|
|
1729
|
+
return makeValidator({
|
|
1730
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1731
|
+
if (value === null) return true;
|
|
1732
|
+
return spec(value, state);
|
|
1733
|
+
}, "test")
|
|
1734
|
+
});
|
|
1735
|
+
}
|
|
1736
|
+
__name(isNullable, "isNullable");
|
|
1737
|
+
var checks = {
|
|
1738
|
+
missing: /* @__PURE__ */ __name((keys, key) => keys.has(key), "missing"),
|
|
1739
|
+
undefined: /* @__PURE__ */ __name((keys, key, value) => keys.has(key) && typeof value[key] !== `undefined`, "undefined"),
|
|
1740
|
+
nil: /* @__PURE__ */ __name((keys, key, value) => keys.has(key) && value[key] != null, "nil"),
|
|
1741
|
+
falsy: /* @__PURE__ */ __name((keys, key, value) => keys.has(key) && !!value[key], "falsy")
|
|
1742
|
+
};
|
|
1743
|
+
function hasRequiredKeys(requiredKeys, options) {
|
|
1744
|
+
var _a;
|
|
1745
|
+
const requiredSet = new Set(requiredKeys);
|
|
1746
|
+
const check = checks[(_a = options === null || options === void 0 ? void 0 : options.missingIf) !== null && _a !== void 0 ? _a : "missing"];
|
|
1747
|
+
return makeValidator({
|
|
1748
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1749
|
+
const keys = new Set(Object.keys(value));
|
|
1750
|
+
const problems = [];
|
|
1751
|
+
for (const key of requiredSet) if (!check(keys, key, value)) problems.push(key);
|
|
1752
|
+
if (problems.length > 0) return pushError(state, `Missing required ${plural(problems.length, `property`, `properties`)} ${getPrintableArray(problems, `and`)}`);
|
|
1753
|
+
return true;
|
|
1754
|
+
}, "test")
|
|
1755
|
+
});
|
|
1756
|
+
}
|
|
1757
|
+
__name(hasRequiredKeys, "hasRequiredKeys");
|
|
1758
|
+
function hasAtLeastOneKey(requiredKeys, options) {
|
|
1759
|
+
var _a;
|
|
1760
|
+
const requiredSet = new Set(requiredKeys);
|
|
1761
|
+
const check = checks[(_a = options === null || options === void 0 ? void 0 : options.missingIf) !== null && _a !== void 0 ? _a : "missing"];
|
|
1762
|
+
return makeValidator({
|
|
1763
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1764
|
+
const keys = Object.keys(value);
|
|
1765
|
+
const valid = keys.some((key) => check(requiredSet, key, value));
|
|
1766
|
+
if (!valid) return pushError(state, `Missing at least one property from ${getPrintableArray(Array.from(requiredSet), `or`)}`);
|
|
1767
|
+
return true;
|
|
1768
|
+
}, "test")
|
|
1769
|
+
});
|
|
1770
|
+
}
|
|
1771
|
+
__name(hasAtLeastOneKey, "hasAtLeastOneKey");
|
|
1772
|
+
function hasForbiddenKeys(forbiddenKeys, options) {
|
|
1773
|
+
var _a;
|
|
1774
|
+
const forbiddenSet = new Set(forbiddenKeys);
|
|
1775
|
+
const check = checks[(_a = options === null || options === void 0 ? void 0 : options.missingIf) !== null && _a !== void 0 ? _a : "missing"];
|
|
1776
|
+
return makeValidator({
|
|
1777
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1778
|
+
const keys = new Set(Object.keys(value));
|
|
1779
|
+
const problems = [];
|
|
1780
|
+
for (const key of forbiddenSet) if (check(keys, key, value)) problems.push(key);
|
|
1781
|
+
if (problems.length > 0) return pushError(state, `Forbidden ${plural(problems.length, `property`, `properties`)} ${getPrintableArray(problems, `and`)}`);
|
|
1782
|
+
return true;
|
|
1783
|
+
}, "test")
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
__name(hasForbiddenKeys, "hasForbiddenKeys");
|
|
1787
|
+
function hasMutuallyExclusiveKeys(exclusiveKeys, options) {
|
|
1788
|
+
var _a;
|
|
1789
|
+
const exclusiveSet = new Set(exclusiveKeys);
|
|
1790
|
+
const check = checks[(_a = options === null || options === void 0 ? void 0 : options.missingIf) !== null && _a !== void 0 ? _a : "missing"];
|
|
1791
|
+
return makeValidator({
|
|
1792
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1793
|
+
const keys = new Set(Object.keys(value));
|
|
1794
|
+
const used = [];
|
|
1795
|
+
for (const key of exclusiveSet) if (check(keys, key, value)) used.push(key);
|
|
1796
|
+
if (used.length > 1) return pushError(state, `Mutually exclusive properties ${getPrintableArray(used, `and`)}`);
|
|
1797
|
+
return true;
|
|
1798
|
+
}, "test")
|
|
1799
|
+
});
|
|
1800
|
+
}
|
|
1801
|
+
__name(hasMutuallyExclusiveKeys, "hasMutuallyExclusiveKeys");
|
|
1802
|
+
(function(KeyRelationship) {
|
|
1803
|
+
KeyRelationship["Forbids"] = "Forbids";
|
|
1804
|
+
KeyRelationship["Requires"] = "Requires";
|
|
1805
|
+
})(exports2.KeyRelationship || (exports2.KeyRelationship = {}));
|
|
1806
|
+
var keyRelationships = {
|
|
1807
|
+
[exports2.KeyRelationship.Forbids]: {
|
|
1808
|
+
expect: false,
|
|
1809
|
+
message: `forbids using`
|
|
1810
|
+
},
|
|
1811
|
+
[exports2.KeyRelationship.Requires]: {
|
|
1812
|
+
expect: true,
|
|
1813
|
+
message: `requires using`
|
|
1814
|
+
}
|
|
1815
|
+
};
|
|
1816
|
+
function hasKeyRelationship(subject, relationship, others, options) {
|
|
1817
|
+
var _a, _b;
|
|
1818
|
+
const skipped = new Set((_a = options === null || options === void 0 ? void 0 : options.ignore) !== null && _a !== void 0 ? _a : []);
|
|
1819
|
+
const check = checks[(_b = options === null || options === void 0 ? void 0 : options.missingIf) !== null && _b !== void 0 ? _b : "missing"];
|
|
1820
|
+
const otherSet = new Set(others);
|
|
1821
|
+
const spec = keyRelationships[relationship];
|
|
1822
|
+
const conjunction = relationship === exports2.KeyRelationship.Forbids ? `or` : `and`;
|
|
1823
|
+
return makeValidator({
|
|
1824
|
+
test: /* @__PURE__ */ __name((value, state) => {
|
|
1825
|
+
const keys = new Set(Object.keys(value));
|
|
1826
|
+
if (!check(keys, subject, value) || skipped.has(value[subject])) return true;
|
|
1827
|
+
const problems = [];
|
|
1828
|
+
for (const key of otherSet) if ((check(keys, key, value) && !skipped.has(value[key])) !== spec.expect) problems.push(key);
|
|
1829
|
+
if (problems.length >= 1) return pushError(state, `Property "${subject}" ${spec.message} ${plural(problems.length, `property`, `properties`)} ${getPrintableArray(problems, conjunction)}`);
|
|
1830
|
+
return true;
|
|
1831
|
+
}, "test")
|
|
1832
|
+
});
|
|
1833
|
+
}
|
|
1834
|
+
__name(hasKeyRelationship, "hasKeyRelationship");
|
|
1835
|
+
exports2.TypeAssertionError = TypeAssertionError;
|
|
1836
|
+
exports2.applyCascade = applyCascade;
|
|
1837
|
+
exports2.as = as;
|
|
1838
|
+
exports2.assert = assert;
|
|
1839
|
+
exports2.assertWithErrors = assertWithErrors;
|
|
1840
|
+
exports2.cascade = cascade;
|
|
1841
|
+
exports2.fn = fn;
|
|
1842
|
+
exports2.hasAtLeastOneKey = hasAtLeastOneKey;
|
|
1843
|
+
exports2.hasExactLength = hasExactLength;
|
|
1844
|
+
exports2.hasForbiddenKeys = hasForbiddenKeys;
|
|
1845
|
+
exports2.hasKeyRelationship = hasKeyRelationship;
|
|
1846
|
+
exports2.hasMaxLength = hasMaxLength;
|
|
1847
|
+
exports2.hasMinLength = hasMinLength;
|
|
1848
|
+
exports2.hasMutuallyExclusiveKeys = hasMutuallyExclusiveKeys;
|
|
1849
|
+
exports2.hasRequiredKeys = hasRequiredKeys;
|
|
1850
|
+
exports2.hasUniqueItems = hasUniqueItems;
|
|
1851
|
+
exports2.isArray = isArray;
|
|
1852
|
+
exports2.isAtLeast = isAtLeast;
|
|
1853
|
+
exports2.isAtMost = isAtMost;
|
|
1854
|
+
exports2.isBase64 = isBase64;
|
|
1855
|
+
exports2.isBoolean = isBoolean;
|
|
1856
|
+
exports2.isDate = isDate;
|
|
1857
|
+
exports2.isDict = isDict;
|
|
1858
|
+
exports2.isEnum = isEnum;
|
|
1859
|
+
exports2.isHexColor = isHexColor;
|
|
1860
|
+
exports2.isISO8601 = isISO8601;
|
|
1861
|
+
exports2.isInExclusiveRange = isInExclusiveRange;
|
|
1862
|
+
exports2.isInInclusiveRange = isInInclusiveRange;
|
|
1863
|
+
exports2.isInstanceOf = isInstanceOf;
|
|
1864
|
+
exports2.isInteger = isInteger;
|
|
1865
|
+
exports2.isJSON = isJSON;
|
|
1866
|
+
exports2.isLiteral = isLiteral;
|
|
1867
|
+
exports2.isLowerCase = isLowerCase;
|
|
1868
|
+
exports2.isMap = isMap;
|
|
1869
|
+
exports2.isNegative = isNegative;
|
|
1870
|
+
exports2.isNullable = isNullable;
|
|
1871
|
+
exports2.isNumber = isNumber2;
|
|
1872
|
+
exports2.isObject = isObject;
|
|
1873
|
+
exports2.isOneOf = isOneOf;
|
|
1874
|
+
exports2.isOptional = isOptional;
|
|
1875
|
+
exports2.isPartial = isPartial;
|
|
1876
|
+
exports2.isPayload = isPayload;
|
|
1877
|
+
exports2.isPositive = isPositive;
|
|
1878
|
+
exports2.isRecord = isRecord;
|
|
1879
|
+
exports2.isSet = isSet;
|
|
1880
|
+
exports2.isString = isString;
|
|
1881
|
+
exports2.isTuple = isTuple;
|
|
1882
|
+
exports2.isUUID4 = isUUID4;
|
|
1883
|
+
exports2.isUnknown = isUnknown;
|
|
1884
|
+
exports2.isUpperCase = isUpperCase;
|
|
1885
|
+
exports2.makeTrait = makeTrait;
|
|
1886
|
+
exports2.makeValidator = makeValidator;
|
|
1887
|
+
exports2.matchesRegExp = matchesRegExp;
|
|
1888
|
+
exports2.softAssert = softAssert;
|
|
1889
|
+
}
|
|
1890
|
+
});
|
|
1891
|
+
|
|
1892
|
+
// src/app/console/migrate/MigrateRollbackCommand.ts
|
|
1893
|
+
var import_clipanion4, import_neko_context4, import_path4, import_promises2, import_neko_config4, t, MigrateRollbackCommand;
|
|
1894
|
+
var init_MigrateRollbackCommand = __esm({
|
|
1895
|
+
"src/app/console/migrate/MigrateRollbackCommand.ts"() {
|
|
1896
|
+
"use strict";
|
|
1897
|
+
init_facades();
|
|
1898
|
+
import_clipanion4 = require("clipanion");
|
|
1899
|
+
import_neko_context4 = require("@devbro/neko-context");
|
|
1900
|
+
import_path4 = __toESM(require("path"));
|
|
1901
|
+
import_promises2 = __toESM(require("fs/promises"));
|
|
1902
|
+
import_neko_config4 = require("@devbro/neko-config");
|
|
1903
|
+
t = __toESM(require_lib());
|
|
1904
|
+
MigrateRollbackCommand = class extends import_clipanion4.Command {
|
|
1905
|
+
static {
|
|
1906
|
+
__name(this, "MigrateRollbackCommand");
|
|
1907
|
+
}
|
|
1908
|
+
static paths = [
|
|
1909
|
+
[
|
|
1910
|
+
`migrate`,
|
|
1911
|
+
"rollback"
|
|
1912
|
+
]
|
|
1913
|
+
];
|
|
1914
|
+
steps = import_clipanion4.Option.String(`--steps`, {
|
|
1915
|
+
description: `how many migrations to rollback`,
|
|
1916
|
+
validator: t.isNumber()
|
|
1917
|
+
});
|
|
1918
|
+
async execute() {
|
|
1919
|
+
await import_neko_context4.context_provider.run(async () => {
|
|
1920
|
+
const db2 = db();
|
|
1921
|
+
const schema = db2.getSchema();
|
|
1922
|
+
const migrationsDir = import_neko_config4.config.get("migration.path");
|
|
1923
|
+
let files = [];
|
|
1924
|
+
const dirEntries = await import_promises2.default.readdir(migrationsDir);
|
|
1925
|
+
files = dirEntries.filter((entry) => entry.endsWith(".ts")).sort();
|
|
1926
|
+
const migrations = await db2.runQuery({
|
|
1927
|
+
sql: "select * from migrations order by created_at DESC",
|
|
1928
|
+
bindings: []
|
|
1929
|
+
});
|
|
1930
|
+
const count = 0;
|
|
1931
|
+
for (const migration of migrations) {
|
|
1932
|
+
const class_to_migrate = migration.filename;
|
|
1933
|
+
this.context.stdout.write(`rolling back ${class_to_migrate}`);
|
|
1934
|
+
const ClassToMigrate = (await import(import_path4.default.join(migrationsDir, class_to_migrate))).default;
|
|
1935
|
+
const c = new ClassToMigrate();
|
|
1936
|
+
await c.down(db2.getSchema());
|
|
1937
|
+
await db2.runQuery({
|
|
1938
|
+
sql: "delete from migrations where id = $1",
|
|
1939
|
+
bindings: [
|
|
1940
|
+
migration.id
|
|
1941
|
+
]
|
|
1942
|
+
});
|
|
1943
|
+
}
|
|
1944
|
+
});
|
|
1945
|
+
}
|
|
1946
|
+
};
|
|
1947
|
+
cli().register(MigrateRollbackCommand);
|
|
1948
|
+
}
|
|
1949
|
+
});
|
|
1950
|
+
|
|
1951
|
+
// src/app/console/migrate/index.ts
|
|
1952
|
+
var init_migrate = __esm({
|
|
1953
|
+
"src/app/console/migrate/index.ts"() {
|
|
1954
|
+
"use strict";
|
|
1955
|
+
init_MigrateCommand();
|
|
1956
|
+
init_GenerateMigrateCommand();
|
|
1957
|
+
init_MigrateRollbackCommand();
|
|
1958
|
+
}
|
|
1959
|
+
});
|
|
1960
|
+
|
|
1961
|
+
// src/app/console/StartCommand.ts
|
|
1962
|
+
var import_clipanion5, import_neko_config5, import_neko_sql, StartCommand;
|
|
1963
|
+
var init_StartCommand = __esm({
|
|
1964
|
+
"src/app/console/StartCommand.ts"() {
|
|
1965
|
+
"use strict";
|
|
1966
|
+
import_clipanion5 = require("clipanion");
|
|
1967
|
+
import_neko_config5 = require("@devbro/neko-config");
|
|
1968
|
+
init_facades();
|
|
1969
|
+
import_neko_sql = require("@devbro/neko-sql");
|
|
1970
|
+
StartCommand = class extends import_clipanion5.Command {
|
|
1971
|
+
static {
|
|
1972
|
+
__name(this, "StartCommand");
|
|
1973
|
+
}
|
|
1974
|
+
scheduler = import_clipanion5.Option.Boolean(`--scheduler`, false);
|
|
1975
|
+
http = import_clipanion5.Option.Boolean(`--http`, false);
|
|
1976
|
+
all = import_clipanion5.Option.Boolean("--all", false);
|
|
1977
|
+
static paths = [
|
|
1978
|
+
[
|
|
1979
|
+
`start`
|
|
1980
|
+
]
|
|
1981
|
+
];
|
|
1982
|
+
async execute() {
|
|
1983
|
+
if ([
|
|
1984
|
+
this.all,
|
|
1985
|
+
this.http,
|
|
1986
|
+
this.scheduler
|
|
1987
|
+
].filter((x) => x).length == 0) {
|
|
1988
|
+
this.context.stdout.write(`No service was selected. please check -h for details
|
|
1989
|
+
`);
|
|
1990
|
+
return;
|
|
1991
|
+
}
|
|
1992
|
+
logger().info(`Starting Server
|
|
1993
|
+
`);
|
|
1994
|
+
import_neko_sql.PostgresqlConnection.defaults.idleTimeoutMillis = 1e4;
|
|
1995
|
+
if (this.scheduler || this.all) {
|
|
1996
|
+
logger().info(`starting scheduler
|
|
1997
|
+
`);
|
|
1998
|
+
scheduler().start();
|
|
1999
|
+
}
|
|
2000
|
+
if (this.http || this.all) {
|
|
2001
|
+
const server = httpServer();
|
|
2002
|
+
await server.listen(import_neko_config5.config.get("port"), () => {
|
|
2003
|
+
logger().info("Server is running on http://localhost:" + import_neko_config5.config.get("port"));
|
|
2004
|
+
});
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
};
|
|
2008
|
+
cli().register(StartCommand);
|
|
2009
|
+
}
|
|
2010
|
+
});
|
|
2011
|
+
|
|
2012
|
+
// src/app/console/DefaultCommand.ts
|
|
2013
|
+
var import_clipanion6, DefaultCommand;
|
|
2014
|
+
var init_DefaultCommand = __esm({
|
|
2015
|
+
"src/app/console/DefaultCommand.ts"() {
|
|
2016
|
+
"use strict";
|
|
2017
|
+
import_clipanion6 = require("clipanion");
|
|
2018
|
+
init_facades();
|
|
2019
|
+
DefaultCommand = class extends import_clipanion6.Command {
|
|
2020
|
+
static {
|
|
2021
|
+
__name(this, "DefaultCommand");
|
|
2022
|
+
}
|
|
2023
|
+
static usage = import_clipanion6.Command.Usage({
|
|
2024
|
+
category: `Main`,
|
|
2025
|
+
description: `server management command line.`,
|
|
2026
|
+
details: `
|
|
2027
|
+
The base command for running and managing your server.
|
|
2028
|
+
|
|
2029
|
+
Make sure you understand how things work.
|
|
2030
|
+
`,
|
|
2031
|
+
examples: []
|
|
2032
|
+
});
|
|
2033
|
+
async execute() {
|
|
2034
|
+
const commandList = cli().registrations;
|
|
2035
|
+
const paths = [];
|
|
2036
|
+
commandList.forEach((index, val) => paths.push(index.builder.paths[0]?.join(" ") || ""));
|
|
2037
|
+
console.log("Available commands:");
|
|
2038
|
+
for (const cmd of paths) {
|
|
2039
|
+
if (cmd) {
|
|
2040
|
+
console.log(cmd);
|
|
2041
|
+
}
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2044
|
+
};
|
|
2045
|
+
cli().register(DefaultCommand);
|
|
2046
|
+
}
|
|
2047
|
+
});
|
|
2048
|
+
|
|
2049
|
+
// src/app/console/KeyGenerateCommand.ts
|
|
2050
|
+
var import_clipanion7, import_crypto, import_promises3, import_path5, KeyGenerateCommand;
|
|
2051
|
+
var init_KeyGenerateCommand = __esm({
|
|
2052
|
+
"src/app/console/KeyGenerateCommand.ts"() {
|
|
2053
|
+
"use strict";
|
|
2054
|
+
import_clipanion7 = require("clipanion");
|
|
2055
|
+
import_crypto = require("crypto");
|
|
2056
|
+
import_promises3 = __toESM(require("fs/promises"));
|
|
2057
|
+
import_path5 = __toESM(require("path"));
|
|
2058
|
+
init_facades();
|
|
2059
|
+
KeyGenerateCommand = class extends import_clipanion7.Command {
|
|
2060
|
+
static {
|
|
2061
|
+
__name(this, "KeyGenerateCommand");
|
|
2062
|
+
}
|
|
2063
|
+
static paths = [
|
|
2064
|
+
[
|
|
2065
|
+
`generate`,
|
|
2066
|
+
"key"
|
|
2067
|
+
]
|
|
2068
|
+
];
|
|
2069
|
+
static usage = import_clipanion7.Command.Usage({
|
|
2070
|
+
category: `Main`,
|
|
2071
|
+
description: `generate keys`,
|
|
2072
|
+
details: `
|
|
2073
|
+
This command generates RSA key pair for JWT signing.
|
|
2074
|
+
Use --rotate flag to preserve old public key.
|
|
2075
|
+
`,
|
|
2076
|
+
examples: [
|
|
2077
|
+
[
|
|
2078
|
+
`Generate new keys`,
|
|
2079
|
+
`generate key`
|
|
2080
|
+
],
|
|
2081
|
+
[
|
|
2082
|
+
`Rotate existing keys`,
|
|
2083
|
+
`generate key --rotate`
|
|
2084
|
+
]
|
|
2085
|
+
]
|
|
2086
|
+
});
|
|
2087
|
+
rotate = import_clipanion7.Option.Boolean(`--rotate`, false, {
|
|
2088
|
+
description: `Rotate existing keys (backup old keys before replacement)`
|
|
2089
|
+
});
|
|
2090
|
+
async execute() {
|
|
2091
|
+
logger().info("generating keys for jwt token and adding to .env file");
|
|
2092
|
+
const { publicKey, privateKey } = (0, import_crypto.generateKeyPairSync)("rsa", {
|
|
2093
|
+
modulusLength: 2048,
|
|
2094
|
+
publicKeyEncoding: {
|
|
2095
|
+
type: "spki",
|
|
2096
|
+
format: "pem"
|
|
2097
|
+
},
|
|
2098
|
+
privateKeyEncoding: {
|
|
2099
|
+
type: "pkcs8",
|
|
2100
|
+
format: "pem"
|
|
2101
|
+
}
|
|
2102
|
+
});
|
|
2103
|
+
let envfile = "";
|
|
2104
|
+
try {
|
|
2105
|
+
envfile = await import_promises3.default.readFile(import_path5.default.join(process.cwd(), ".env"), "utf-8");
|
|
2106
|
+
} catch {
|
|
2107
|
+
}
|
|
2108
|
+
let old_public_key = envfile.match(/^jwt_secret_public=(.*)/m);
|
|
2109
|
+
envfile = this.addEnvParam(envfile, "jwt_secret_public", this.stripPemHeaders(publicKey));
|
|
2110
|
+
envfile = this.addEnvParam(envfile, "jwt_secret_private", this.stripPemHeaders(privateKey));
|
|
2111
|
+
if (this.rotate && old_public_key && old_public_key[1]) {
|
|
2112
|
+
envfile = this.addEnvParam(envfile, "jwt_secret_public_retired", old_public_key[1]);
|
|
2113
|
+
}
|
|
2114
|
+
await import_promises3.default.writeFile(import_path5.default.join(process.cwd(), ".env"), envfile, "utf-8");
|
|
2115
|
+
}
|
|
2116
|
+
addEnvParam(file, key, value) {
|
|
2117
|
+
let regex = new RegExp(`^${key}=.*`, "gm");
|
|
2118
|
+
file = file.replace(regex, `${key}=${value}`);
|
|
2119
|
+
const match = file.match(regex);
|
|
2120
|
+
if (!match) {
|
|
2121
|
+
file = file + `
|
|
2122
|
+
${key}=${value}`;
|
|
2123
|
+
}
|
|
2124
|
+
return file;
|
|
2125
|
+
}
|
|
2126
|
+
stripPemHeaders(pem) {
|
|
2127
|
+
return pem.replace(/-----BEGIN [\w\s]+-----/g, "").replace(/-----END [\w\s]+-----/g, "").replace(/\r?\n|\r/g, "");
|
|
2128
|
+
}
|
|
2129
|
+
};
|
|
2130
|
+
cli().register(KeyGenerateCommand);
|
|
2131
|
+
}
|
|
2132
|
+
});
|
|
2133
|
+
|
|
2134
|
+
// src/app/console/generate/GenerateControllerCommand.ts
|
|
2135
|
+
var import_clipanion8, import_change_case_all2, import_path6, fs5, import_neko_config6, import_handlebars2, import_url2, import_pluralize, import_meta2, GenerateControllerCommand;
|
|
2136
|
+
var init_GenerateControllerCommand = __esm({
|
|
2137
|
+
"src/app/console/generate/GenerateControllerCommand.ts"() {
|
|
2138
|
+
"use strict";
|
|
2139
|
+
init_facades();
|
|
2140
|
+
import_clipanion8 = require("clipanion");
|
|
2141
|
+
import_change_case_all2 = require("change-case-all");
|
|
2142
|
+
import_path6 = __toESM(require("path"));
|
|
2143
|
+
fs5 = __toESM(require("fs/promises"));
|
|
2144
|
+
import_neko_config6 = require("@devbro/neko-config");
|
|
2145
|
+
import_handlebars2 = __toESM(require("handlebars"));
|
|
2146
|
+
import_url2 = require("url");
|
|
2147
|
+
import_pluralize = __toESM(require("pluralize"));
|
|
2148
|
+
import_meta2 = {};
|
|
2149
|
+
GenerateControllerCommand = class extends import_clipanion8.Command {
|
|
2150
|
+
static {
|
|
2151
|
+
__name(this, "GenerateControllerCommand");
|
|
2152
|
+
}
|
|
2153
|
+
static paths = [
|
|
2154
|
+
[
|
|
2155
|
+
`make`,
|
|
2156
|
+
`controller`
|
|
2157
|
+
],
|
|
2158
|
+
[
|
|
2159
|
+
`generate`,
|
|
2160
|
+
`controller`
|
|
2161
|
+
]
|
|
2162
|
+
];
|
|
2163
|
+
name = import_clipanion8.Option.String({
|
|
2164
|
+
required: true
|
|
2165
|
+
});
|
|
2166
|
+
async execute() {
|
|
2167
|
+
const rootDir = process.cwd();
|
|
2168
|
+
const date = /* @__PURE__ */ new Date();
|
|
2169
|
+
const year = date.getFullYear();
|
|
2170
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
2171
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
2172
|
+
const secondsOfDay = String(date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds()).padStart(5, "0");
|
|
2173
|
+
const fixed_name = import_change_case_all2.Case.snake(this.name);
|
|
2174
|
+
const filename = `${import_change_case_all2.Case.capital(this.name)}Controller.ts`;
|
|
2175
|
+
this.context.stdout.write(`creating migration file ${filename}
|
|
2176
|
+
`);
|
|
2177
|
+
await fs5.mkdir(import_neko_config6.config.get("migration.path"), {
|
|
2178
|
+
recursive: true
|
|
2179
|
+
});
|
|
2180
|
+
let dirname = typeof __dirname === "string" ? __dirname : void 0;
|
|
2181
|
+
if (!dirname) {
|
|
2182
|
+
dirname = import_path6.default.dirname((0, import_url2.fileURLToPath)(import_meta2.url));
|
|
2183
|
+
}
|
|
2184
|
+
const compiledTemplate = import_handlebars2.default.compile((await fs5.readFile(import_path6.default.join(dirname, "./controller.tpl"))).toString());
|
|
2185
|
+
const template = await compiledTemplate({
|
|
2186
|
+
className: import_change_case_all2.Case.pascal(this.name),
|
|
2187
|
+
classNameLower: import_change_case_all2.Case.snake(this.name),
|
|
2188
|
+
routeName: import_change_case_all2.Case.kebab((0, import_pluralize.default)(this.name))
|
|
2189
|
+
});
|
|
2190
|
+
await fs5.writeFile(import_path6.default.join(rootDir, "src/app/controllers", filename), template);
|
|
2191
|
+
}
|
|
2192
|
+
};
|
|
2193
|
+
cli().register(GenerateControllerCommand);
|
|
2194
|
+
}
|
|
2195
|
+
});
|
|
2196
|
+
|
|
2197
|
+
// src/app/console/generate/index.ts
|
|
2198
|
+
var init_generate = __esm({
|
|
2199
|
+
"src/app/console/generate/index.ts"() {
|
|
2200
|
+
"use strict";
|
|
2201
|
+
init_GenerateControllerCommand();
|
|
2202
|
+
}
|
|
2203
|
+
});
|
|
2204
|
+
|
|
2205
|
+
// src/app/console/project/CreateProjectCommand.ts
|
|
2206
|
+
var import_clipanion9, import_change_case_all3, import_path7, fs6, import_url3, import_handlebars3, import_child_process, import_meta3, CreateProjectCommand;
|
|
2207
|
+
var init_CreateProjectCommand = __esm({
|
|
2208
|
+
"src/app/console/project/CreateProjectCommand.ts"() {
|
|
2209
|
+
"use strict";
|
|
2210
|
+
import_clipanion9 = require("clipanion");
|
|
2211
|
+
import_change_case_all3 = require("change-case-all");
|
|
2212
|
+
import_path7 = __toESM(require("path"));
|
|
2213
|
+
fs6 = __toESM(require("fs/promises"));
|
|
2214
|
+
import_url3 = require("url");
|
|
2215
|
+
import_handlebars3 = __toESM(require("handlebars"));
|
|
2216
|
+
import_child_process = require("child_process");
|
|
2217
|
+
import_meta3 = {};
|
|
2218
|
+
CreateProjectCommand = class extends import_clipanion9.Command {
|
|
2219
|
+
static {
|
|
2220
|
+
__name(this, "CreateProjectCommand");
|
|
2221
|
+
}
|
|
2222
|
+
static paths = [
|
|
2223
|
+
[
|
|
2224
|
+
`create`,
|
|
2225
|
+
`project`
|
|
2226
|
+
]
|
|
2227
|
+
];
|
|
2228
|
+
static usage = import_clipanion9.Command.Usage({
|
|
2229
|
+
category: `Project`,
|
|
2230
|
+
description: `Create a new project`,
|
|
2231
|
+
details: `
|
|
2232
|
+
This command creates a new project with the specified name at the given path.
|
|
2233
|
+
If no path is provided, the project will be created in the current directory.
|
|
2234
|
+
`,
|
|
2235
|
+
examples: [
|
|
2236
|
+
[
|
|
2237
|
+
`Create a new project in specified directory`,
|
|
2238
|
+
`create project --path /path/to/my-project --git`
|
|
2239
|
+
],
|
|
2240
|
+
[
|
|
2241
|
+
`Create a new project at a specific path with git initialized`,
|
|
2242
|
+
`create project --path /path/to/my-project --git`
|
|
2243
|
+
]
|
|
2244
|
+
]
|
|
2245
|
+
});
|
|
2246
|
+
projectPath = import_clipanion9.Option.String("--path", {
|
|
2247
|
+
required: true
|
|
2248
|
+
});
|
|
2249
|
+
git = import_clipanion9.Option.Boolean(`--git`, false, {
|
|
2250
|
+
description: `Initialize a git repository in the new project`
|
|
2251
|
+
});
|
|
2252
|
+
async folderExists(folderPath) {
|
|
2253
|
+
try {
|
|
2254
|
+
const stats = await fs6.stat(folderPath);
|
|
2255
|
+
return stats.isDirectory();
|
|
2256
|
+
} catch (error) {
|
|
2257
|
+
if (error.code === "ENOENT") {
|
|
2258
|
+
return false;
|
|
2259
|
+
}
|
|
2260
|
+
throw error;
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
async execute() {
|
|
2264
|
+
const projectPath = import_path7.default.join(this.projectPath);
|
|
2265
|
+
try {
|
|
2266
|
+
await fs6.access(projectPath);
|
|
2267
|
+
console.error(`Error: Directory ${projectPath} already exists.`);
|
|
2268
|
+
return 1;
|
|
2269
|
+
} catch {
|
|
2270
|
+
}
|
|
2271
|
+
await fs6.mkdir(projectPath, {
|
|
2272
|
+
recursive: true
|
|
2273
|
+
});
|
|
2274
|
+
console.log(`Created project directory at: ${projectPath}`);
|
|
2275
|
+
const dirname = typeof __dirname === "undefined" ? import_path7.default.dirname((0, import_url3.fileURLToPath)(import_meta3.url)) : __dirname;
|
|
2276
|
+
let basePath = import_path7.default.join(dirname, `./base_project`);
|
|
2277
|
+
if (await this.folderExists(basePath) === false) {
|
|
2278
|
+
basePath = import_path7.default.join(dirname, `../app/console/project/base_project`);
|
|
2279
|
+
}
|
|
2280
|
+
console.log(`Using base project path: ${basePath}`);
|
|
2281
|
+
const baseProjectPath = basePath;
|
|
2282
|
+
await this.processTplFolder(baseProjectPath, projectPath, {});
|
|
2283
|
+
console.log(`Copied base project files to: ${projectPath}`);
|
|
2284
|
+
const packageJsonPath = import_path7.default.join(projectPath, `package.json`);
|
|
2285
|
+
const packageJson = JSON.parse(await fs6.readFile(packageJsonPath, `utf-8`));
|
|
2286
|
+
packageJson.name = import_change_case_all3.Case.snake(import_path7.default.basename(projectPath));
|
|
2287
|
+
await fs6.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
2288
|
+
console.log(`Updated package.json with project name: ${packageJson.name}`);
|
|
2289
|
+
if (this.git) {
|
|
2290
|
+
try {
|
|
2291
|
+
(0, import_child_process.execSync)('git init; git add --all; git commit --allow-empty -m "chore: first commit for pashmak"', {
|
|
2292
|
+
cwd: projectPath
|
|
2293
|
+
});
|
|
2294
|
+
} catch (error) {
|
|
2295
|
+
console.error(`Failed to create project.`, error);
|
|
2296
|
+
return 1;
|
|
2297
|
+
}
|
|
2298
|
+
}
|
|
2299
|
+
}
|
|
2300
|
+
async processTplFolder(src, dest, data = {}) {
|
|
2301
|
+
const files = await fs6.readdir(src, {
|
|
2302
|
+
withFileTypes: true
|
|
2303
|
+
});
|
|
2304
|
+
for (const file of files) {
|
|
2305
|
+
const srcPath = import_path7.default.join(src, file.name);
|
|
2306
|
+
const destPath = file.isFile() && file.name.endsWith(".tpl") ? import_path7.default.join(dest, file.name.substring(0, file.name.length - 4)) : import_path7.default.join(dest, file.name);
|
|
2307
|
+
if (file.isDirectory()) {
|
|
2308
|
+
await fs6.mkdir(destPath, {
|
|
2309
|
+
recursive: true
|
|
2310
|
+
});
|
|
2311
|
+
await this.processTplFolder(srcPath, destPath, data);
|
|
2312
|
+
} else if (file.name.endsWith(".tpl")) {
|
|
2313
|
+
await this.processTplFile(srcPath, destPath, {});
|
|
2314
|
+
} else {
|
|
2315
|
+
throw new Error("unexpected non tpl file: " + srcPath + " " + file.name);
|
|
2316
|
+
}
|
|
2317
|
+
}
|
|
2318
|
+
}
|
|
2319
|
+
async processTplFile(src, dest, data = {}) {
|
|
2320
|
+
const compiledTemplate = import_handlebars3.default.compile((await fs6.readFile(src)).toString());
|
|
2321
|
+
const template = await compiledTemplate(data);
|
|
2322
|
+
await fs6.writeFile(dest, template);
|
|
2323
|
+
}
|
|
2324
|
+
};
|
|
2325
|
+
}
|
|
2326
|
+
});
|
|
2327
|
+
|
|
2328
|
+
// src/app/console/index.ts
|
|
2329
|
+
var console_exports = {};
|
|
2330
|
+
__export(console_exports, {
|
|
2331
|
+
CreateProjectCommand: () => CreateProjectCommand,
|
|
2332
|
+
DefaultCommand: () => DefaultCommand,
|
|
2333
|
+
GenerateControllerCommand: () => GenerateControllerCommand,
|
|
2334
|
+
GenerateMigrateCommand: () => GenerateMigrateCommand,
|
|
2335
|
+
KeyGenerateCommand: () => KeyGenerateCommand,
|
|
2336
|
+
MigrateCommand: () => MigrateCommand,
|
|
2337
|
+
MigrateRollbackCommand: () => MigrateRollbackCommand,
|
|
2338
|
+
StartCommand: () => StartCommand
|
|
2339
|
+
});
|
|
2340
|
+
var init_console = __esm({
|
|
2341
|
+
"src/app/console/index.ts"() {
|
|
2342
|
+
"use strict";
|
|
2343
|
+
init_migrate();
|
|
2344
|
+
init_StartCommand();
|
|
2345
|
+
init_DefaultCommand();
|
|
2346
|
+
init_KeyGenerateCommand();
|
|
2347
|
+
init_generate();
|
|
2348
|
+
init_CreateProjectCommand();
|
|
2349
|
+
init_facades();
|
|
2350
|
+
init_CreateProjectCommand();
|
|
2351
|
+
cli().register(CreateProjectCommand);
|
|
2352
|
+
}
|
|
2353
|
+
});
|
|
2354
|
+
|
|
2355
|
+
// src/DatabaseServiceProvider.mts
|
|
2356
|
+
var DatabaseServiceProvider_exports = {};
|
|
2357
|
+
__export(DatabaseServiceProvider_exports, {
|
|
2358
|
+
DatabaseServiceProvider: () => DatabaseServiceProvider
|
|
2359
|
+
});
|
|
2360
|
+
var import_neko_sql2, import_neko_orm, import_neko_context5, import_neko_config7, DatabaseServiceProvider;
|
|
2361
|
+
var init_DatabaseServiceProvider = __esm({
|
|
2362
|
+
"src/DatabaseServiceProvider.mts"() {
|
|
2363
|
+
"use strict";
|
|
2364
|
+
init_dist();
|
|
2365
|
+
import_neko_sql2 = require("@devbro/neko-sql");
|
|
2366
|
+
import_neko_orm = require("@devbro/neko-orm");
|
|
2367
|
+
import_neko_context5 = require("@devbro/neko-context");
|
|
2368
|
+
import_neko_config7 = require("@devbro/neko-config");
|
|
2369
|
+
DatabaseServiceProvider = class _DatabaseServiceProvider extends Middleware {
|
|
2370
|
+
static {
|
|
2371
|
+
__name(this, "DatabaseServiceProvider");
|
|
2372
|
+
}
|
|
2373
|
+
async call(req, res, next) {
|
|
2374
|
+
const db_configs = import_neko_config7.config.get("databases");
|
|
2375
|
+
const conns = [];
|
|
2376
|
+
try {
|
|
2377
|
+
for (const [name, db_config] of Object.entries(db_configs)) {
|
|
2378
|
+
const conn = await this.getConnection(db_config);
|
|
2379
|
+
(0, import_neko_context5.ctx)().set(["database", name], conn);
|
|
2380
|
+
conns.push(conn);
|
|
2381
|
+
}
|
|
2382
|
+
import_neko_orm.BaseModel.setConnection(
|
|
2383
|
+
() => (0, import_neko_context5.ctx)().getOrThrow(["database", "default"])
|
|
2384
|
+
);
|
|
2385
|
+
await next();
|
|
2386
|
+
} finally {
|
|
2387
|
+
for (const conn of conns) {
|
|
2388
|
+
await conn.disconnect();
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
2391
|
+
}
|
|
2392
|
+
static instance;
|
|
2393
|
+
async register() {
|
|
2394
|
+
}
|
|
2395
|
+
static getInstance() {
|
|
2396
|
+
if (!_DatabaseServiceProvider.instance) {
|
|
2397
|
+
_DatabaseServiceProvider.instance = new _DatabaseServiceProvider();
|
|
2398
|
+
}
|
|
2399
|
+
return _DatabaseServiceProvider.instance;
|
|
2400
|
+
}
|
|
2401
|
+
async getConnection(db_config) {
|
|
2402
|
+
const conn = new import_neko_sql2.PostgresqlConnection(db_config);
|
|
2403
|
+
if (!await conn.connect()) {
|
|
2404
|
+
throw new Error("Failed to connect to the database");
|
|
2405
|
+
}
|
|
2406
|
+
return conn;
|
|
2407
|
+
}
|
|
2408
|
+
};
|
|
2409
|
+
}
|
|
2410
|
+
});
|
|
2411
|
+
|
|
2412
|
+
// src/index.ts
|
|
2413
|
+
var index_exports = {};
|
|
2414
|
+
__export(index_exports, {
|
|
2415
|
+
bootstrap: () => bootstrap,
|
|
2416
|
+
config: () => import_neko_config8.config
|
|
2417
|
+
});
|
|
2418
|
+
module.exports = __toCommonJS(index_exports);
|
|
2419
|
+
var import_dotenv = __toESM(require("dotenv"));
|
|
2420
|
+
var import_neko_context6 = require("@devbro/neko-context");
|
|
2421
|
+
init_dist();
|
|
2422
|
+
var import_neko_config8 = require("@devbro/neko-config");
|
|
2423
|
+
async function bootstrap(options) {
|
|
2424
|
+
import_dotenv.default.config();
|
|
2425
|
+
console.log("Bootstrapping application...");
|
|
2426
|
+
console.log(`Root directory: ${options.root_dir}`);
|
|
2427
|
+
let a = (await import(`${options.root_dir}/config/default`)).default;
|
|
2428
|
+
import_neko_config8.config.load(a);
|
|
2429
|
+
console.log("Loading application modules...");
|
|
2430
|
+
await Promise.resolve().then(() => (init_console(), console_exports));
|
|
2431
|
+
console.log("Loading Database Provider ...");
|
|
2432
|
+
const { DatabaseServiceProvider: DatabaseServiceProvider2 } = await Promise.resolve().then(() => (init_DatabaseServiceProvider(), DatabaseServiceProvider_exports));
|
|
2433
|
+
console.log("Setting up pre-loader for context provider...");
|
|
2434
|
+
import_neko_context6.context_provider.setPreLoader(async (f) => {
|
|
2435
|
+
const middlewares = [];
|
|
2436
|
+
middlewares.push(DatabaseServiceProvider2.getInstance());
|
|
2437
|
+
return await runNext(
|
|
2438
|
+
middlewares,
|
|
2439
|
+
{},
|
|
2440
|
+
{},
|
|
2441
|
+
// @ts-ignore
|
|
2442
|
+
f
|
|
2443
|
+
);
|
|
2444
|
+
});
|
|
2445
|
+
console.log("Application bootstrapped successfully.");
|
|
2446
|
+
}
|
|
2447
|
+
__name(bootstrap, "bootstrap");
|
|
2448
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
2449
|
+
0 && (module.exports = {
|
|
2450
|
+
bootstrap,
|
|
2451
|
+
config
|
|
2452
|
+
});
|