barejs 0.1.45 → 0.1.48
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/LICENSE +20 -20
- package/README.md +184 -179
- package/dist/index.js +111 -192
- package/dist/types/core/bare.d.ts +2 -1
- package/dist/types/core/context.d.ts +2 -0
- package/dist/types/core/router.d.ts +1 -9
- package/dist/types/index.d.ts +0 -1
- package/dist/types/security/auth.d.ts +2 -2
- package/package.json +1 -1
- package/src/core/bare.ts +315 -286
- package/src/core/config.ts +31 -31
- package/src/core/context.ts +111 -81
- package/src/core/loader.ts +12 -12
- package/src/core/radix.ts +184 -176
- package/src/core/router.ts +60 -68
- package/src/index.ts +60 -60
- package/src/security/auth.ts +174 -157
- package/src/security/cors.ts +45 -26
- package/src/security/hash.ts +22 -19
- package/src/security/validators.ts +133 -132
- package/src/utils/static.ts +42 -25
- package/dist/types/utils/logger.d.ts +0 -2
- package/src/utils/logger.ts +0 -23
package/src/security/hash.ts
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
// src/security/hash.ts
|
|
2
|
-
import { BARE_CONFIG } from '../core/config';
|
|
3
|
-
|
|
4
|
-
export const Hash = {
|
|
5
|
-
make: (data: string): Promise<string> =>
|
|
6
|
-
Bun.password.hash(data, BARE_CONFIG.hash),
|
|
7
|
-
|
|
8
|
-
verify: (data: string, hash: string): Promise<boolean> =>
|
|
9
|
-
Bun.password.verify(data, hash),
|
|
10
|
-
|
|
11
|
-
needsRehash: (hash: string): boolean => {
|
|
12
|
-
const { algorithm, memoryCost, timeCost } = BARE_CONFIG.hash;
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
);
|
|
19
|
-
|
|
1
|
+
// src/security/hash.ts
|
|
2
|
+
import { BARE_CONFIG } from '../core/config';
|
|
3
|
+
|
|
4
|
+
export const Hash = {
|
|
5
|
+
make: (data: string): Promise<string> =>
|
|
6
|
+
Bun.password.hash(data, BARE_CONFIG.hash),
|
|
7
|
+
|
|
8
|
+
verify: (data: string, hash: string): Promise<boolean> =>
|
|
9
|
+
Bun.password.verify(data, hash),
|
|
10
|
+
|
|
11
|
+
needsRehash: (hash: string): boolean => {
|
|
12
|
+
const { algorithm, memoryCost, timeCost } = BARE_CONFIG.hash;
|
|
13
|
+
if (algorithm === 'bcrypt') {
|
|
14
|
+
if (!/^\$2[aby]\$/.test(hash)) return true;
|
|
15
|
+
const costMatch = hash.match(/^\$2[aby]\$(\d+)\$/);
|
|
16
|
+
return !costMatch || Number(costMatch[1]) !== timeCost;
|
|
17
|
+
}
|
|
18
|
+
if (!hash.startsWith(`$${algorithm}$`)) return true;
|
|
19
|
+
const mMatch = hash.match(/(?:^|[,$])m=(\d+)(?:[,$]|$)/);
|
|
20
|
+
const tMatch = hash.match(/(?:^|[,$])t=(\d+)(?:[,$]|$)/);
|
|
21
|
+
return !mMatch || Number(mMatch[1]) !== memoryCost || !tMatch || Number(tMatch[1]) !== timeCost;
|
|
22
|
+
}
|
|
20
23
|
};
|
|
@@ -1,133 +1,134 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* BareJS Ultimate Validation Engines
|
|
3
|
-
* All comments must be in English [2026-01-06]
|
|
4
|
-
* Optimized for Bun Runtime & Mechanical Sympathy [2026-01-05]
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as Compiler from '@sinclair/typebox/compiler';
|
|
8
|
-
import { z } from 'zod';
|
|
9
|
-
import type { Context, Middleware, Next } from '../core/context';
|
|
10
|
-
|
|
11
|
-
// --- Types & Constants ---
|
|
12
|
-
export const Z = z;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Standardized High-Performance Error Response
|
|
16
|
-
* Uses Bun's native Response.json for speed.
|
|
17
|
-
*/
|
|
18
|
-
const errorJSON = (data: any, status = 400) =>
|
|
19
|
-
Response.json(data, {
|
|
20
|
-
status,
|
|
21
|
-
headers: { 'Content-Type': 'application/json' }
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
// --- Engines ---
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* TypeBox JIT Engine (t)
|
|
28
|
-
* Pre-compiled JIT validation to outperform competitors by 55% [2026-01-05].
|
|
29
|
-
*/
|
|
30
|
-
export const typebox = (schema: any): Middleware => {
|
|
31
|
-
const check = Compiler.TypeCompiler.Compile(schema);
|
|
32
|
-
|
|
33
|
-
return async (ctx: Context, next?: Next) => {
|
|
34
|
-
try {
|
|
35
|
-
const body = await ctx.req.json();
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (schema &&
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
1
|
+
/**
|
|
2
|
+
* BareJS Ultimate Validation Engines
|
|
3
|
+
* All comments must be in English [2026-01-06]
|
|
4
|
+
* Optimized for Bun Runtime & Mechanical Sympathy [2026-01-05]
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as Compiler from '@sinclair/typebox/compiler';
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import type { Context, Middleware, Next } from '../core/context';
|
|
10
|
+
|
|
11
|
+
// --- Types & Constants ---
|
|
12
|
+
export const Z = z;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Standardized High-Performance Error Response
|
|
16
|
+
* Uses Bun's native Response.json for speed.
|
|
17
|
+
*/
|
|
18
|
+
const errorJSON = (data: any, status = 400) =>
|
|
19
|
+
Response.json(data, {
|
|
20
|
+
status,
|
|
21
|
+
headers: { 'Content-Type': 'application/json' }
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// --- Engines ---
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* TypeBox JIT Engine (t)
|
|
28
|
+
* Pre-compiled JIT validation to outperform competitors by 55% [2026-01-05].
|
|
29
|
+
*/
|
|
30
|
+
export const typebox = (schema: any): Middleware => {
|
|
31
|
+
const check = Compiler.TypeCompiler.Compile(schema);
|
|
32
|
+
|
|
33
|
+
return async (ctx: Context, next?: Next) => {
|
|
34
|
+
try {
|
|
35
|
+
// const body = await ctx.req.json();
|
|
36
|
+
const body = await ctx.jsonBody();
|
|
37
|
+
|
|
38
|
+
if (!check.Check(body)) {
|
|
39
|
+
const error = check.Errors(body).First();
|
|
40
|
+
return errorJSON({
|
|
41
|
+
status: 'error',
|
|
42
|
+
code: 'VALIDATION_FAILED',
|
|
43
|
+
message: error?.message || 'Invalid input',
|
|
44
|
+
path: error?.path || 'body'
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ✅ FIX TS18046: Cast to 'any' to allow assignment to 'unknown' body
|
|
49
|
+
(ctx as any).body = body;
|
|
50
|
+
return next ? next() : undefined;
|
|
51
|
+
} catch {
|
|
52
|
+
return errorJSON({ status: 'error', message: 'Malformed JSON payload' });
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Native Engine (n)
|
|
59
|
+
*/
|
|
60
|
+
export const native = (schema: any): Middleware => {
|
|
61
|
+
const props = schema.properties || {};
|
|
62
|
+
const keys = Object.keys(props);
|
|
63
|
+
const kLen = keys.length;
|
|
64
|
+
|
|
65
|
+
return async (ctx: any, next?: any) => {
|
|
66
|
+
try {
|
|
67
|
+
const isCtx = ctx instanceof Object && 'req' in ctx;
|
|
68
|
+
const body = isCtx ? await ctx.jsonBody() : await ctx.json();
|
|
69
|
+
|
|
70
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) {
|
|
71
|
+
return errorJSON({ status: 'error', message: 'Invalid JSON object payload' });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (let i = 0; i < kLen; i++) {
|
|
75
|
+
const k = keys[i]!;
|
|
76
|
+
if (!Object.hasOwn(body, k) || typeof body[k] !== props[k]?.type) {
|
|
77
|
+
return errorJSON({
|
|
78
|
+
status: 'error',
|
|
79
|
+
code: 'TYPE_MISMATCH',
|
|
80
|
+
message: `Field '${k}' must be of type ${props[k]?.type}`,
|
|
81
|
+
field: k
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (isCtx) (ctx as any).body = body;
|
|
87
|
+
|
|
88
|
+
return next ? next() : undefined;
|
|
89
|
+
} catch {
|
|
90
|
+
return errorJSON({ status: 'error', message: 'Invalid JSON payload' });
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Zod Engine (z)
|
|
97
|
+
*/
|
|
98
|
+
export const zod = (schema: z.ZodSchema): Middleware => {
|
|
99
|
+
return async (ctx: any, next?: any) => {
|
|
100
|
+
try {
|
|
101
|
+
const isCtx = ctx instanceof Object && 'req' in ctx;
|
|
102
|
+
const body = isCtx ? await ctx.jsonBody() : await ctx.json();
|
|
103
|
+
|
|
104
|
+
const result = await schema.safeParseAsync(body);
|
|
105
|
+
|
|
106
|
+
if (!result.success) {
|
|
107
|
+
return errorJSON({
|
|
108
|
+
status: 'error',
|
|
109
|
+
code: 'ZOD_ERROR',
|
|
110
|
+
errors: result.error.flatten()
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (isCtx) (ctx as any).body = result.data;
|
|
115
|
+
|
|
116
|
+
return next ? next() : undefined;
|
|
117
|
+
} catch {
|
|
118
|
+
return errorJSON({ status: 'error', message: 'Invalid JSON payload' });
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Smart Dispatcher (v)
|
|
125
|
+
*/
|
|
126
|
+
export const validate = (schema: any): Middleware => {
|
|
127
|
+
if (schema && '_def' in schema) return zod(schema);
|
|
128
|
+
if (schema && (schema.kind || schema.type || schema[Symbol.for('TypeBox.Kind')])) {
|
|
129
|
+
return typebox(schema);
|
|
130
|
+
}
|
|
131
|
+
return native(schema);
|
|
132
|
+
};
|
|
133
|
+
|
|
133
134
|
export { typebox as t, zod as z, native as n, validate as v };
|
package/src/utils/static.ts
CHANGED
|
@@ -1,26 +1,43 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export const staticFile = (root: string = 'public') => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
1
|
+
import { resolve, sep } from 'node:path';
|
|
2
|
+
|
|
3
|
+
export const staticFile = (root: string = 'public') => {
|
|
4
|
+
const rootDir = resolve(process.cwd(), root);
|
|
5
|
+
const rootPrefix = rootDir.endsWith(sep) ? rootDir : rootDir + sep;
|
|
6
|
+
|
|
7
|
+
return async (ctx: any, next: any) => {
|
|
8
|
+
const method = ctx.req.method;
|
|
9
|
+
if (method !== 'GET' && method !== 'HEAD') return next();
|
|
10
|
+
|
|
11
|
+
const rawUrl: string = ctx.req.url;
|
|
12
|
+
const pathStart = rawUrl.indexOf('/', 8);
|
|
13
|
+
const queryStart = pathStart === -1 ? -1 : rawUrl.indexOf('?', pathStart);
|
|
14
|
+
const rawPath = pathStart === -1
|
|
15
|
+
? ''
|
|
16
|
+
: (queryStart === -1 ? rawUrl.slice(pathStart) : rawUrl.slice(pathStart, queryStart));
|
|
17
|
+
|
|
18
|
+
let decodedPath: string;
|
|
19
|
+
try {
|
|
20
|
+
decodedPath = decodeURIComponent(rawPath);
|
|
21
|
+
} catch {
|
|
22
|
+
return ctx.status(400).send('Bad Request');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (decodedPath.includes('\0')) {
|
|
26
|
+
return ctx.status(400).send('Bad Request');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const relativePath = decodedPath.replace(/^\/+/, '');
|
|
30
|
+
const filePath = resolve(rootDir, relativePath);
|
|
31
|
+
|
|
32
|
+
if (filePath !== rootDir && !filePath.startsWith(rootPrefix)) {
|
|
33
|
+
return ctx.status(403).send('Forbidden');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const file = Bun.file(filePath);
|
|
37
|
+
if (await file.exists()) {
|
|
38
|
+
return new Response(file);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return next();
|
|
42
|
+
};
|
|
26
43
|
};
|
package/src/utils/logger.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { Middleware } from '../core/context';
|
|
2
|
-
|
|
3
|
-
export const logger: Middleware = async (ctx, next) => {
|
|
4
|
-
const start = performance.now();
|
|
5
|
-
const path = new URL(ctx.req.url).pathname;
|
|
6
|
-
|
|
7
|
-
const response = await next?.();
|
|
8
|
-
|
|
9
|
-
const duration = (performance.now() - start).toFixed(2);
|
|
10
|
-
const status = response instanceof Response ? response.status : 200;
|
|
11
|
-
|
|
12
|
-
let statusColor = status >= 500 ? '\x1b[31m' : status >= 400 ? '\x1b[33m' : '\x1b[32m';
|
|
13
|
-
|
|
14
|
-
console.log(
|
|
15
|
-
` \x1b[90m${new Date().toLocaleTimeString()}\x1b[0m ` +
|
|
16
|
-
`\x1b[1m\x1b[38;5;117m${ctx.req.method.padEnd(7)}\x1b[0m ` +
|
|
17
|
-
`\x1b[38;5;250m${path}\x1b[0m ` +
|
|
18
|
-
`${statusColor}${status}\x1b[0m ` +
|
|
19
|
-
`\x1b[90m(${duration}ms)\x1b[0m`
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
return response;
|
|
23
|
-
};
|