vafast 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +100 -39
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -51226,6 +51226,12 @@ function matchPath(pattern, path) {
|
|
|
51226
51226
|
|
|
51227
51227
|
// src/utils/response.ts
|
|
51228
51228
|
function json(data, status = 200, headers = {}) {
|
|
51229
|
+
if (Object.keys(headers).length === 0) {
|
|
51230
|
+
return new Response(JSON.stringify(data), {
|
|
51231
|
+
status,
|
|
51232
|
+
headers: { "Content-Type": "application/json" }
|
|
51233
|
+
});
|
|
51234
|
+
}
|
|
51229
51235
|
const h = new Headers({
|
|
51230
51236
|
"Content-Type": "application/json",
|
|
51231
51237
|
...headers
|
|
@@ -55378,10 +55384,29 @@ var TypeCompiler;
|
|
|
55378
55384
|
})(TypeCompiler || (TypeCompiler = {}));
|
|
55379
55385
|
|
|
55380
55386
|
// src/utils/validators/schema-validators-ultra.ts
|
|
55387
|
+
var SCHEMA_FLAGS = {
|
|
55388
|
+
BODY: 1 << 0,
|
|
55389
|
+
QUERY: 1 << 1,
|
|
55390
|
+
PARAMS: 1 << 2,
|
|
55391
|
+
HEADERS: 1 << 3,
|
|
55392
|
+
COOKIES: 1 << 4
|
|
55393
|
+
};
|
|
55381
55394
|
var ultraSchemaCache = new Map;
|
|
55382
55395
|
var schemaCacheHits = new Map;
|
|
55383
55396
|
var errorPool = [];
|
|
55384
|
-
var ERROR_POOL_SIZE =
|
|
55397
|
+
var ERROR_POOL_SIZE = 200;
|
|
55398
|
+
var errorMessagePool = new Map;
|
|
55399
|
+
var commonMessages = [
|
|
55400
|
+
"\u8BF7\u6C42\u4F53\u9A8C\u8BC1\u5931\u8D25",
|
|
55401
|
+
"Query\u53C2\u6570\u9A8C\u8BC1\u5931\u8D25",
|
|
55402
|
+
"\u8DEF\u5F84\u53C2\u6570\u9A8C\u8BC1\u5931\u8D25",
|
|
55403
|
+
"\u8BF7\u6C42\u5934\u9A8C\u8BC1\u5931\u8D25",
|
|
55404
|
+
"Cookie\u9A8C\u8BC1\u5931\u8D25",
|
|
55405
|
+
"\u7C7B\u578B\u9A8C\u8BC1\u5931\u8D25",
|
|
55406
|
+
"Schema\u7F16\u8BD1\u5931\u8D25",
|
|
55407
|
+
"\u672A\u77E5\u9519\u8BEF"
|
|
55408
|
+
];
|
|
55409
|
+
commonMessages.forEach((msg) => errorMessagePool.set(msg, msg));
|
|
55385
55410
|
for (let i = 0;i < ERROR_POOL_SIZE; i++) {
|
|
55386
55411
|
errorPool.push(new Error);
|
|
55387
55412
|
}
|
|
@@ -55392,6 +55417,14 @@ function getErrorFromPool(message) {
|
|
|
55392
55417
|
errorPoolIndex = (errorPoolIndex + 1) % ERROR_POOL_SIZE;
|
|
55393
55418
|
return error;
|
|
55394
55419
|
}
|
|
55420
|
+
function getPooledString(key) {
|
|
55421
|
+
let pooled = errorMessagePool.get(key);
|
|
55422
|
+
if (!pooled) {
|
|
55423
|
+
pooled = key;
|
|
55424
|
+
errorMessagePool.set(key, key);
|
|
55425
|
+
}
|
|
55426
|
+
return pooled;
|
|
55427
|
+
}
|
|
55395
55428
|
function getUltraSchemaCompiler(schema) {
|
|
55396
55429
|
let compiler = ultraSchemaCache.get(schema);
|
|
55397
55430
|
if (compiler) {
|
|
@@ -55406,6 +55439,20 @@ function getUltraSchemaCompiler(schema) {
|
|
|
55406
55439
|
return null;
|
|
55407
55440
|
}
|
|
55408
55441
|
}
|
|
55442
|
+
function getSchemaFlags(config) {
|
|
55443
|
+
let flags = 0;
|
|
55444
|
+
if (config.body)
|
|
55445
|
+
flags |= SCHEMA_FLAGS.BODY;
|
|
55446
|
+
if (config.query)
|
|
55447
|
+
flags |= SCHEMA_FLAGS.QUERY;
|
|
55448
|
+
if (config.params)
|
|
55449
|
+
flags |= SCHEMA_FLAGS.PARAMS;
|
|
55450
|
+
if (config.headers)
|
|
55451
|
+
flags |= SCHEMA_FLAGS.HEADERS;
|
|
55452
|
+
if (config.cookies)
|
|
55453
|
+
flags |= SCHEMA_FLAGS.COOKIES;
|
|
55454
|
+
return flags;
|
|
55455
|
+
}
|
|
55409
55456
|
function validateSchemaUltra(schema, data, context) {
|
|
55410
55457
|
if (!schema)
|
|
55411
55458
|
return data;
|
|
@@ -55416,52 +55463,58 @@ function validateSchemaUltra(schema, data, context) {
|
|
|
55416
55463
|
compiler = TypeCompiler.Compile(schema);
|
|
55417
55464
|
ultraSchemaCache.set(schema, compiler);
|
|
55418
55465
|
} catch (error) {
|
|
55419
|
-
|
|
55466
|
+
const message = getPooledString(`${context}\u9A8C\u8BC1\u5931\u8D25: Schema\u7F16\u8BD1\u5931\u8D25`);
|
|
55467
|
+
throw getErrorFromPool(message);
|
|
55420
55468
|
}
|
|
55421
55469
|
}
|
|
55422
|
-
|
|
55423
|
-
|
|
55470
|
+
const result = compiler.Check(data);
|
|
55471
|
+
if (!result) {
|
|
55472
|
+
const message = getPooledString(`${context}\u9A8C\u8BC1\u5931\u8D25`);
|
|
55473
|
+
throw getErrorFromPool(message);
|
|
55424
55474
|
}
|
|
55425
55475
|
return data;
|
|
55426
55476
|
} catch (error) {
|
|
55427
55477
|
if (error instanceof Error && error.message.includes("\u9A8C\u8BC1\u5931\u8D25")) {
|
|
55428
55478
|
throw error;
|
|
55429
55479
|
}
|
|
55430
|
-
|
|
55480
|
+
const message = getPooledString(`${context}\u9A8C\u8BC1\u5931\u8D25: ${error instanceof Error ? error.message : "\u672A\u77E5\u9519\u8BEF"}`);
|
|
55481
|
+
throw getErrorFromPool(message);
|
|
55431
55482
|
}
|
|
55432
55483
|
}
|
|
55433
55484
|
function validateAllSchemasUltra(config, data) {
|
|
55434
|
-
|
|
55485
|
+
const flags = getSchemaFlags(config);
|
|
55486
|
+
if (flags & SCHEMA_FLAGS.BODY) {
|
|
55435
55487
|
validateSchemaUltra(config.body, data.body, "\u8BF7\u6C42\u4F53");
|
|
55436
55488
|
}
|
|
55437
|
-
if (
|
|
55489
|
+
if (flags & SCHEMA_FLAGS.QUERY) {
|
|
55438
55490
|
validateSchemaUltra(config.query, data.query, "Query\u53C2\u6570");
|
|
55439
55491
|
}
|
|
55440
|
-
if (
|
|
55492
|
+
if (flags & SCHEMA_FLAGS.PARAMS) {
|
|
55441
55493
|
validateSchemaUltra(config.params, data.params, "\u8DEF\u5F84\u53C2\u6570");
|
|
55442
55494
|
}
|
|
55443
|
-
if (
|
|
55495
|
+
if (flags & SCHEMA_FLAGS.HEADERS) {
|
|
55444
55496
|
validateSchemaUltra(config.headers, data.headers, "\u8BF7\u6C42\u5934");
|
|
55445
55497
|
}
|
|
55446
|
-
if (
|
|
55498
|
+
if (flags & SCHEMA_FLAGS.COOKIES) {
|
|
55447
55499
|
validateSchemaUltra(config.cookies, data.cookies, "Cookie");
|
|
55448
55500
|
}
|
|
55449
55501
|
return data;
|
|
55450
55502
|
}
|
|
55451
55503
|
function precompileSchemasUltra(config) {
|
|
55452
|
-
|
|
55504
|
+
const flags = getSchemaFlags(config);
|
|
55505
|
+
if (flags & SCHEMA_FLAGS.BODY && config.body) {
|
|
55453
55506
|
getUltraSchemaCompiler(config.body);
|
|
55454
55507
|
}
|
|
55455
|
-
if (config.query) {
|
|
55508
|
+
if (flags & SCHEMA_FLAGS.QUERY && config.query) {
|
|
55456
55509
|
getUltraSchemaCompiler(config.query);
|
|
55457
55510
|
}
|
|
55458
|
-
if (config.params) {
|
|
55511
|
+
if (flags & SCHEMA_FLAGS.PARAMS && config.params) {
|
|
55459
55512
|
getUltraSchemaCompiler(config.params);
|
|
55460
55513
|
}
|
|
55461
|
-
if (config.headers) {
|
|
55514
|
+
if (flags & SCHEMA_FLAGS.HEADERS && config.headers) {
|
|
55462
55515
|
getUltraSchemaCompiler(config.headers);
|
|
55463
55516
|
}
|
|
55464
|
-
if (config.cookies) {
|
|
55517
|
+
if (flags & SCHEMA_FLAGS.COOKIES && config.cookies) {
|
|
55465
55518
|
getUltraSchemaCompiler(config.cookies);
|
|
55466
55519
|
}
|
|
55467
55520
|
}
|
|
@@ -55481,33 +55534,41 @@ function autoResponse(result) {
|
|
|
55481
55534
|
if (result instanceof Response) {
|
|
55482
55535
|
return result;
|
|
55483
55536
|
}
|
|
55484
|
-
if (result && typeof result === "object" && "data" in result) {
|
|
55485
|
-
const {
|
|
55486
|
-
data,
|
|
55487
|
-
status = 200,
|
|
55488
|
-
headers = {}
|
|
55489
|
-
} = result;
|
|
55490
|
-
const h = new Headers(headers);
|
|
55491
|
-
if (data === null || data === undefined) {
|
|
55492
|
-
return new Response("", { status: status ?? 204, headers: h });
|
|
55493
|
-
}
|
|
55494
|
-
if (typeof data === "string" || typeof data === "number" || typeof data === "boolean") {
|
|
55495
|
-
if (!h.has("Content-Type")) {
|
|
55496
|
-
h.set("Content-Type", "text/plain; charset=utf-8");
|
|
55497
|
-
}
|
|
55498
|
-
return new Response(String(data), { status, headers: h });
|
|
55499
|
-
}
|
|
55500
|
-
return json(data, status, h);
|
|
55501
|
-
}
|
|
55502
|
-
if (typeof result === "string" || typeof result === "number" || typeof result === "boolean") {
|
|
55503
|
-
return new Response(String(result), {
|
|
55504
|
-
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
55505
|
-
});
|
|
55506
|
-
}
|
|
55507
55537
|
if (result === null || result === undefined) {
|
|
55508
55538
|
return new Response("", { status: 204 });
|
|
55509
55539
|
}
|
|
55510
|
-
|
|
55540
|
+
switch (typeof result) {
|
|
55541
|
+
case "string":
|
|
55542
|
+
return new Response(result, {
|
|
55543
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
55544
|
+
});
|
|
55545
|
+
case "number":
|
|
55546
|
+
case "boolean":
|
|
55547
|
+
return new Response(String(result), {
|
|
55548
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
55549
|
+
});
|
|
55550
|
+
case "object":
|
|
55551
|
+
if ("data" in result) {
|
|
55552
|
+
const { data, status = 200, headers = {} } = result;
|
|
55553
|
+
if (data === null || data === undefined) {
|
|
55554
|
+
return new Response("", {
|
|
55555
|
+
status: status === 200 ? 204 : status,
|
|
55556
|
+
headers
|
|
55557
|
+
});
|
|
55558
|
+
}
|
|
55559
|
+
if (typeof data === "string" || typeof data === "number" || typeof data === "boolean") {
|
|
55560
|
+
const finalHeaders = {
|
|
55561
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
55562
|
+
...headers
|
|
55563
|
+
};
|
|
55564
|
+
return new Response(String(data), { status, headers: finalHeaders });
|
|
55565
|
+
}
|
|
55566
|
+
return json(data, status, headers);
|
|
55567
|
+
}
|
|
55568
|
+
return json(result);
|
|
55569
|
+
default:
|
|
55570
|
+
return new Response("", { status: 204 });
|
|
55571
|
+
}
|
|
55511
55572
|
}
|
|
55512
55573
|
function createRouteHandler(config, handler) {
|
|
55513
55574
|
const hasBodySchema = config.body !== undefined;
|