@sqrzro/server 2.0.0-bz.12 → 2.0.0-bz.13
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/auth.js +81 -450
- package/dist/cache.js +6 -44
- package/dist/chunk-7XG6NQUS.js +268 -0
- package/dist/chunk-ALBKLI2J.js +30 -0
- package/dist/chunk-G4EG2ECB.js +47 -0
- package/dist/chunk-GOSRKBZX.js +20 -0
- package/dist/forms.js +9 -302
- package/dist/lists.js +4 -30
- package/dist/mail.js +2 -40
- package/dist/middleware.js +6 -30
- package/dist/schema.js +12 -69
- package/dist/url.js +6 -54
- package/package.json +3 -2
package/dist/cache.js
CHANGED
|
@@ -1,46 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// src/cache/index.ts
|
|
20
|
-
var cache_exports = {};
|
|
21
|
-
__export(cache_exports, {
|
|
22
|
-
getFromCache: () => getFromCache,
|
|
23
|
-
setToCache: () => setToCache
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(cache_exports);
|
|
26
|
-
|
|
27
|
-
// src/cache/CacheService.ts
|
|
28
|
-
var import_redis = require("redis");
|
|
29
|
-
async function getClient() {
|
|
30
|
-
const client = (0, import_redis.createClient)();
|
|
31
|
-
await client.connect();
|
|
32
|
-
return client;
|
|
33
|
-
}
|
|
34
|
-
async function getFromCache(key) {
|
|
35
|
-
const client = await getClient();
|
|
36
|
-
return client.get(key);
|
|
37
|
-
}
|
|
38
|
-
async function setToCache(key, value) {
|
|
39
|
-
const client = await getClient();
|
|
40
|
-
await client.set(key, value);
|
|
41
|
-
}
|
|
42
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
-
0 && (module.exports = {
|
|
1
|
+
import {
|
|
44
2
|
getFromCache,
|
|
45
3
|
setToCache
|
|
46
|
-
}
|
|
4
|
+
} from "./chunk-GOSRKBZX.js";
|
|
5
|
+
export {
|
|
6
|
+
getFromCache,
|
|
7
|
+
setToCache
|
|
8
|
+
};
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
// src/forms/ValidationService.ts
|
|
2
|
+
import Joi from "joi";
|
|
3
|
+
|
|
4
|
+
// src/forms/lang.ts
|
|
5
|
+
var messages = {
|
|
6
|
+
"alternatives.all": "",
|
|
7
|
+
"alternatives.any": "",
|
|
8
|
+
"alternatives.match": "",
|
|
9
|
+
"alternatives.one": "",
|
|
10
|
+
"alternatives.types": "",
|
|
11
|
+
"any.custom": "",
|
|
12
|
+
"any.default": "",
|
|
13
|
+
"any.failover": "",
|
|
14
|
+
"any.invalid": "",
|
|
15
|
+
"any.only": "",
|
|
16
|
+
"any.ref": "",
|
|
17
|
+
"any.required": "{{#label}} is required",
|
|
18
|
+
"any.unknown": "",
|
|
19
|
+
"array.base": "",
|
|
20
|
+
"array.excludes": "",
|
|
21
|
+
"array.includesRequiredBoth": "",
|
|
22
|
+
"array.includesRequiredKnowns": "",
|
|
23
|
+
"array.includesRequiredUnknowns": "",
|
|
24
|
+
"array.includes": "",
|
|
25
|
+
"array.length": "",
|
|
26
|
+
"array.max": "",
|
|
27
|
+
"array.min": "",
|
|
28
|
+
"array.orderedLength": "",
|
|
29
|
+
"array.sort": "",
|
|
30
|
+
"array.sort.mismatching": "",
|
|
31
|
+
"array.sort.unsupported": "",
|
|
32
|
+
"array.sparse": "",
|
|
33
|
+
"array.unique": "",
|
|
34
|
+
"array.hasKnown": "",
|
|
35
|
+
"array.hasUnknown": "",
|
|
36
|
+
"binary.base": "",
|
|
37
|
+
"binary.length": "",
|
|
38
|
+
"binary.max": "",
|
|
39
|
+
"binary.min": "",
|
|
40
|
+
"boolean.base": "",
|
|
41
|
+
"date.base": "",
|
|
42
|
+
"date.format": "",
|
|
43
|
+
"date.greater": "",
|
|
44
|
+
"date.less": "",
|
|
45
|
+
"date.max": "",
|
|
46
|
+
"date.min": "",
|
|
47
|
+
"date.strict": "",
|
|
48
|
+
"function.arity": "",
|
|
49
|
+
"function.class": "",
|
|
50
|
+
"function.maxArity": "",
|
|
51
|
+
"function.minArity": "",
|
|
52
|
+
"number.base": "{{#label}} should be a number",
|
|
53
|
+
"number.greater": "",
|
|
54
|
+
"number.infinity": "",
|
|
55
|
+
"number.integer": "",
|
|
56
|
+
"number.less": "",
|
|
57
|
+
"number.max": "",
|
|
58
|
+
"number.min": "{{#label}} should be greater than or equal to {{#limit}}",
|
|
59
|
+
"number.multiple": "",
|
|
60
|
+
"number.negative": "",
|
|
61
|
+
"number.port": "",
|
|
62
|
+
"number.positive": "",
|
|
63
|
+
"number.precision": "",
|
|
64
|
+
"number.unsafe": "",
|
|
65
|
+
"object.unknown": "",
|
|
66
|
+
"object.and": "",
|
|
67
|
+
"object.assert": "",
|
|
68
|
+
"object.base": "",
|
|
69
|
+
"object.length": "",
|
|
70
|
+
"object.max": "",
|
|
71
|
+
"object.min": "",
|
|
72
|
+
"object.missing": "",
|
|
73
|
+
"object.nand": "",
|
|
74
|
+
"object.pattern.match": "",
|
|
75
|
+
"object.refType": "",
|
|
76
|
+
"object.regex": "",
|
|
77
|
+
"object.rename.multiple": "",
|
|
78
|
+
"object.rename.override": "",
|
|
79
|
+
"object.schema": "",
|
|
80
|
+
"object.instance": "",
|
|
81
|
+
"object.with": "",
|
|
82
|
+
"object.without": "",
|
|
83
|
+
"object.xor": "",
|
|
84
|
+
"object.oxor": "",
|
|
85
|
+
"string.alphanum": "",
|
|
86
|
+
"string.base64": "",
|
|
87
|
+
"string.base": "",
|
|
88
|
+
"string.creditCard": "",
|
|
89
|
+
"string.dataUri": "",
|
|
90
|
+
"string.domain": "",
|
|
91
|
+
"string.email": "",
|
|
92
|
+
"string.empty": "{{#label}} is required",
|
|
93
|
+
"string.guid": "",
|
|
94
|
+
"string.hexAlign": "",
|
|
95
|
+
"string.hex": "",
|
|
96
|
+
"string.hostname": "",
|
|
97
|
+
"string.ipVersion": "",
|
|
98
|
+
"string.ip": "",
|
|
99
|
+
"string.isoDate": "",
|
|
100
|
+
"string.isoDuration": "",
|
|
101
|
+
"string.length": "",
|
|
102
|
+
"string.lowercase": "",
|
|
103
|
+
"string.max": "",
|
|
104
|
+
"string.min": "",
|
|
105
|
+
"string.normalize": "",
|
|
106
|
+
"string.pattern.base": "",
|
|
107
|
+
"string.pattern.name": "",
|
|
108
|
+
"string.pattern.invert.base": "",
|
|
109
|
+
"string.pattern.invert.name": "",
|
|
110
|
+
"string.token": "",
|
|
111
|
+
"string.trim": "",
|
|
112
|
+
"string.uppercase": "",
|
|
113
|
+
"string.uri": "",
|
|
114
|
+
"string.uriCustomScheme": "",
|
|
115
|
+
"string.uriRelativeOnly": "",
|
|
116
|
+
"symbol.base": "",
|
|
117
|
+
"symbol.map": ""
|
|
118
|
+
};
|
|
119
|
+
var lang_default = messages;
|
|
120
|
+
|
|
121
|
+
// src/forms/ValidationError.ts
|
|
122
|
+
var ValidationError = class extends Error {
|
|
123
|
+
constructor(messages2) {
|
|
124
|
+
super(JSON.stringify(messages2));
|
|
125
|
+
this.name = "ValidationError";
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
var ValidationError_default = ValidationError;
|
|
129
|
+
|
|
130
|
+
// src/forms/ValidationService.ts
|
|
131
|
+
function validate() {
|
|
132
|
+
return Joi;
|
|
133
|
+
}
|
|
134
|
+
function getErrorMessages() {
|
|
135
|
+
return Object.entries(lang_default).reduce((acc, [key, value]) => {
|
|
136
|
+
if (!value) {
|
|
137
|
+
return acc;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
...acc,
|
|
141
|
+
[key]: value
|
|
142
|
+
};
|
|
143
|
+
}, {});
|
|
144
|
+
}
|
|
145
|
+
function transformErrors(error) {
|
|
146
|
+
const messages2 = error.details.reduce(
|
|
147
|
+
(acc, cur) => ({
|
|
148
|
+
...acc,
|
|
149
|
+
[cur.path.join(".")]: cur.message.replace(/"/gu, "")
|
|
150
|
+
}),
|
|
151
|
+
{}
|
|
152
|
+
);
|
|
153
|
+
return new ValidationError_default(messages2);
|
|
154
|
+
}
|
|
155
|
+
async function validateSchema(formData, validation) {
|
|
156
|
+
try {
|
|
157
|
+
const validated = await validation.validateAsync(formData, {
|
|
158
|
+
abortEarly: false,
|
|
159
|
+
messages: getErrorMessages()
|
|
160
|
+
});
|
|
161
|
+
return [validated, null];
|
|
162
|
+
} catch (err) {
|
|
163
|
+
if (err instanceof Joi.ValidationError) {
|
|
164
|
+
return [null, transformErrors(err)];
|
|
165
|
+
}
|
|
166
|
+
if (err instanceof Error) {
|
|
167
|
+
return [null, err];
|
|
168
|
+
}
|
|
169
|
+
return [null, new Error("Unknown validation error occured")];
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function createSchema(schema) {
|
|
173
|
+
return Joi.object(schema);
|
|
174
|
+
}
|
|
175
|
+
function extendSchema(schema, appends) {
|
|
176
|
+
return schema.append(appends);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/forms/FormService.ts
|
|
180
|
+
function serializeError(err) {
|
|
181
|
+
return {
|
|
182
|
+
cause: err.cause,
|
|
183
|
+
message: err.message,
|
|
184
|
+
name: err.name,
|
|
185
|
+
stack: err.stack
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function hasFn(args) {
|
|
189
|
+
return Boolean(Object.prototype.hasOwnProperty.call(args, "fn"));
|
|
190
|
+
}
|
|
191
|
+
async function submitForm(args) {
|
|
192
|
+
let data = { ...args.formData };
|
|
193
|
+
if (args.request) {
|
|
194
|
+
const [validated, validationError] = await validateSchema(args.formData, args.request);
|
|
195
|
+
if (validationError !== null) {
|
|
196
|
+
if (validationError instanceof ValidationError_default) {
|
|
197
|
+
args.onValidationError?.(validationError);
|
|
198
|
+
}
|
|
199
|
+
return [null, serializeError(validationError)];
|
|
200
|
+
}
|
|
201
|
+
data = validated;
|
|
202
|
+
}
|
|
203
|
+
if (!hasFn(args)) {
|
|
204
|
+
try {
|
|
205
|
+
await args.onSuccess?.(data);
|
|
206
|
+
} catch (err) {
|
|
207
|
+
if (err instanceof Error) {
|
|
208
|
+
return [null, serializeError(err)];
|
|
209
|
+
}
|
|
210
|
+
return [
|
|
211
|
+
null,
|
|
212
|
+
serializeError(
|
|
213
|
+
new Error("The submitForm onSuccess function encountered an unknown error")
|
|
214
|
+
)
|
|
215
|
+
];
|
|
216
|
+
}
|
|
217
|
+
return [data, null];
|
|
218
|
+
}
|
|
219
|
+
let model = null;
|
|
220
|
+
try {
|
|
221
|
+
model = await args.fn(data);
|
|
222
|
+
} catch (err) {
|
|
223
|
+
if (err instanceof ValidationError_default) {
|
|
224
|
+
args.onValidationError?.(err);
|
|
225
|
+
return [null, serializeError(err)];
|
|
226
|
+
}
|
|
227
|
+
if (err instanceof Error) {
|
|
228
|
+
return [null, serializeError(err)];
|
|
229
|
+
}
|
|
230
|
+
return [
|
|
231
|
+
null,
|
|
232
|
+
serializeError(
|
|
233
|
+
new Error("The function supplied to submitForm encountered an unknown error")
|
|
234
|
+
)
|
|
235
|
+
];
|
|
236
|
+
}
|
|
237
|
+
if (!model) {
|
|
238
|
+
return [
|
|
239
|
+
null,
|
|
240
|
+
serializeError(
|
|
241
|
+
new Error("No model has been returned from the function supplied to submitForm")
|
|
242
|
+
)
|
|
243
|
+
];
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
await args.onSuccess?.(model);
|
|
247
|
+
} catch (err) {
|
|
248
|
+
if (err instanceof Error) {
|
|
249
|
+
return [null, serializeError(err)];
|
|
250
|
+
}
|
|
251
|
+
return [
|
|
252
|
+
null,
|
|
253
|
+
serializeError(
|
|
254
|
+
new Error("The submitForm onSuccess function encountered an unknown error")
|
|
255
|
+
)
|
|
256
|
+
];
|
|
257
|
+
}
|
|
258
|
+
return [model, null];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export {
|
|
262
|
+
ValidationError_default,
|
|
263
|
+
validate,
|
|
264
|
+
validateSchema,
|
|
265
|
+
createSchema,
|
|
266
|
+
extendSchema,
|
|
267
|
+
submitForm
|
|
268
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// src/url/URLService.ts
|
|
2
|
+
import { headers } from "next/headers";
|
|
3
|
+
function getOrigin() {
|
|
4
|
+
const origin = headers().get("x-origin");
|
|
5
|
+
if (origin) {
|
|
6
|
+
return origin;
|
|
7
|
+
}
|
|
8
|
+
const proto = headers().get("x-forwarded-proto");
|
|
9
|
+
const host = headers().get("x-forwarded-host");
|
|
10
|
+
if (proto && host) {
|
|
11
|
+
return `${proto}://${host}`;
|
|
12
|
+
}
|
|
13
|
+
throw new Error("No origin could be determined");
|
|
14
|
+
}
|
|
15
|
+
function getURL(pathname) {
|
|
16
|
+
const origin = getOrigin();
|
|
17
|
+
if (!pathname) {
|
|
18
|
+
return origin;
|
|
19
|
+
}
|
|
20
|
+
const isSecure = origin.startsWith("https://");
|
|
21
|
+
const protocol = isSecure ? "https://" : "http://";
|
|
22
|
+
const originWithoutProtocol = origin.replace(/^https?:\/\//u, "");
|
|
23
|
+
const cleanURL = `${originWithoutProtocol}/${pathname}`.replace(/\/+/gu, "/");
|
|
24
|
+
return `${protocol}${cleanURL}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
getOrigin,
|
|
29
|
+
getURL
|
|
30
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// src/database/schema.ts
|
|
2
|
+
import { integer, pgEnum, pgSchema, text, timestamp } from "drizzle-orm/pg-core";
|
|
3
|
+
var DEFAULT_ROLE = 10;
|
|
4
|
+
var mfaType = pgEnum("mfaType", ["TOTP", "HARDWARE"]);
|
|
5
|
+
var scope = pgEnum("scope", ["ANON", "MFA", "AUTHED"]);
|
|
6
|
+
var authSchema = pgSchema("auth");
|
|
7
|
+
var authUserTable = authSchema.table("user_credentials", {
|
|
8
|
+
id: text("id").primaryKey(),
|
|
9
|
+
email: text("email").notNull().unique(),
|
|
10
|
+
password: text("password"),
|
|
11
|
+
role: integer("role").notNull().default(DEFAULT_ROLE)
|
|
12
|
+
});
|
|
13
|
+
var authSessionTable = authSchema.table("sessions", {
|
|
14
|
+
id: text("id").primaryKey(),
|
|
15
|
+
userId: text("userId").notNull().references(() => authUserTable.id),
|
|
16
|
+
scope: scope("scope").notNull().default("ANON"),
|
|
17
|
+
expiresAt: timestamp("expiresAt").notNull()
|
|
18
|
+
});
|
|
19
|
+
var authResetTable = authSchema.table("resets", {
|
|
20
|
+
id: text("id").primaryKey(),
|
|
21
|
+
userId: text("userId").notNull().references(() => authUserTable.id),
|
|
22
|
+
expiresAt: timestamp("expiresAt").notNull()
|
|
23
|
+
});
|
|
24
|
+
var authMFATable = authSchema.table("mfas", {
|
|
25
|
+
id: text("id").primaryKey(),
|
|
26
|
+
name: text("name").notNull(),
|
|
27
|
+
userId: text("userId").notNull().references(() => authUserTable.id),
|
|
28
|
+
type: mfaType("type").notNull().default("TOTP"),
|
|
29
|
+
secret: text("secret").notNull(),
|
|
30
|
+
verifiedAt: timestamp("verifiedAt")
|
|
31
|
+
});
|
|
32
|
+
var authClientTable = authSchema.table("client_credentials", {
|
|
33
|
+
id: text("id").primaryKey(),
|
|
34
|
+
alias: text("alias").notNull().unique(),
|
|
35
|
+
secret: text("secret").notNull().unique()
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export {
|
|
39
|
+
mfaType,
|
|
40
|
+
scope,
|
|
41
|
+
authSchema,
|
|
42
|
+
authUserTable,
|
|
43
|
+
authSessionTable,
|
|
44
|
+
authResetTable,
|
|
45
|
+
authMFATable,
|
|
46
|
+
authClientTable
|
|
47
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/cache/CacheService.ts
|
|
2
|
+
import { createClient } from "redis";
|
|
3
|
+
async function getClient() {
|
|
4
|
+
const client = createClient();
|
|
5
|
+
await client.connect();
|
|
6
|
+
return client;
|
|
7
|
+
}
|
|
8
|
+
async function getFromCache(key) {
|
|
9
|
+
const client = await getClient();
|
|
10
|
+
return client.get(key);
|
|
11
|
+
}
|
|
12
|
+
async function setToCache(key, value) {
|
|
13
|
+
const client = await getClient();
|
|
14
|
+
await client.set(key, value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
getFromCache,
|
|
19
|
+
setToCache
|
|
20
|
+
};
|