barejs 0.2.3 → 0.2.5
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/README.md +62 -33
- package/dist/edge.js +75 -0
- package/dist/index.js +185 -178
- package/dist/node.js +184 -177
- package/dist/types/client/index.d.ts +57 -0
- package/dist/types/core/bare.d.ts +4 -1
- package/dist/types/core/context.d.ts +1 -0
- package/dist/types/core/router.d.ts +6 -0
- package/dist/types/edge.d.ts +98 -0
- package/dist/types/index.d.ts +3 -3
- package/dist/types/runtime/types.d.ts +15 -0
- package/dist/types/security/validators.d.ts +33 -0
- package/llms.txt +9 -6
- package/package.json +5 -1
- package/src/client/index.ts +294 -6
- package/src/core/bare.ts +15 -5
- package/src/core/context.ts +10 -3
- package/src/core/router.ts +2 -1
- package/src/edge.ts +492 -0
- package/src/index.ts +11 -3
- package/src/runtime/bun.ts +12 -0
- package/src/runtime/node.ts +21 -3
- package/src/runtime/types.ts +16 -0
- package/src/runtime/uws.ts +14 -4
- package/src/security/auth.ts +7 -5
- package/src/security/validators.ts +194 -37
|
@@ -8,6 +8,27 @@ export interface BareClientOptions {
|
|
|
8
8
|
headers?: Record<string, string>;
|
|
9
9
|
fetch?: typeof fetch;
|
|
10
10
|
}
|
|
11
|
+
export type BareClientResult<T = any, E = any> = {
|
|
12
|
+
ok: true;
|
|
13
|
+
data: T;
|
|
14
|
+
error: null;
|
|
15
|
+
status: number;
|
|
16
|
+
headers: Headers;
|
|
17
|
+
} | {
|
|
18
|
+
ok: false;
|
|
19
|
+
data: null;
|
|
20
|
+
error: E;
|
|
21
|
+
status: number;
|
|
22
|
+
headers: Headers;
|
|
23
|
+
};
|
|
24
|
+
export interface BareWsClient<In = any, Out = In> {
|
|
25
|
+
send(data: Out): void;
|
|
26
|
+
onMessage(cb: (data: In) => void): () => void;
|
|
27
|
+
onOpen(cb: () => void): () => void;
|
|
28
|
+
onClose(cb: (code: number, reason: string) => void): () => void;
|
|
29
|
+
close(code?: number, reason?: string): void;
|
|
30
|
+
readonly readyState: number;
|
|
31
|
+
}
|
|
11
32
|
type ExtractParams<Path extends string> = Path extends `${string}:${infer Param}/${infer Rest}` ? {
|
|
12
33
|
[K in Param | keyof ExtractParams<`/${Rest}`>]: string | number;
|
|
13
34
|
} : Path extends `${string}:${infer Param}` ? {
|
|
@@ -35,16 +56,52 @@ type EndpointOptions<R extends RouteEndpoint, P extends string, WithBody extends
|
|
|
35
56
|
} & (WithBody extends true ? {
|
|
36
57
|
body?: R['body'];
|
|
37
58
|
} : {});
|
|
59
|
+
export interface BareSafeClient<Routes extends RouteSchema = RouteSchema> {
|
|
60
|
+
get<P extends PathsForMethod<Routes, 'GET'> | (string & {})>(path: P, options?: LookupRoute<Routes, 'GET', P & string> extends infer R extends RouteEndpoint ? EndpointOptions<R, P & string, false> : ClientRequestOptions): Promise<BareClientResult<LookupRoute<Routes, 'GET', P & string> extends infer R extends RouteEndpoint ? R['response'] : any>>;
|
|
61
|
+
post<P extends PathsForMethod<Routes, 'POST'> | (string & {})>(path: P, options?: LookupRoute<Routes, 'POST', P & string> extends infer R extends RouteEndpoint ? EndpointOptions<R, P & string, true> : ClientRequestOptions): Promise<BareClientResult<LookupRoute<Routes, 'POST', P & string> extends infer R extends RouteEndpoint ? R['response'] : any>>;
|
|
62
|
+
put<P extends PathsForMethod<Routes, 'PUT'> | (string & {})>(path: P, options?: LookupRoute<Routes, 'PUT', P & string> extends infer R extends RouteEndpoint ? EndpointOptions<R, P & string, true> : ClientRequestOptions): Promise<BareClientResult<LookupRoute<Routes, 'PUT', P & string> extends infer R extends RouteEndpoint ? R['response'] : any>>;
|
|
63
|
+
patch<P extends PathsForMethod<Routes, 'PATCH'> | (string & {})>(path: P, options?: LookupRoute<Routes, 'PATCH', P & string> extends infer R extends RouteEndpoint ? EndpointOptions<R, P & string, true> : ClientRequestOptions): Promise<BareClientResult<LookupRoute<Routes, 'PATCH', P & string> extends infer R extends RouteEndpoint ? R['response'] : any>>;
|
|
64
|
+
delete<P extends PathsForMethod<Routes, 'DELETE'> | (string & {})>(path: P, options?: LookupRoute<Routes, 'DELETE', P & string> extends infer R extends RouteEndpoint ? EndpointOptions<R, P & string, false> : ClientRequestOptions): Promise<BareClientResult<LookupRoute<Routes, 'DELETE', P & string> extends infer R extends RouteEndpoint ? R['response'] : any>>;
|
|
65
|
+
}
|
|
66
|
+
export type BareTreatyNode = {
|
|
67
|
+
get<T = any>(options?: ClientRequestOptions): Promise<BareClientResult<T>>;
|
|
68
|
+
post<T = any>(options?: ClientRequestOptions): Promise<BareClientResult<T>>;
|
|
69
|
+
put<T = any>(options?: ClientRequestOptions): Promise<BareClientResult<T>>;
|
|
70
|
+
patch<T = any>(options?: ClientRequestOptions): Promise<BareClientResult<T>>;
|
|
71
|
+
delete<T = any>(options?: ClientRequestOptions): Promise<BareClientResult<T>>;
|
|
72
|
+
subscribe<In = any, Out = In>(options?: {
|
|
73
|
+
query?: Record<string, string | number | boolean | undefined>;
|
|
74
|
+
}): BareWsClient<In, Out>;
|
|
75
|
+
(params: Record<string, string | number> | string | number): BareTreatyNode;
|
|
76
|
+
} & {
|
|
77
|
+
[segment: string]: BareTreatyNode;
|
|
78
|
+
};
|
|
38
79
|
export interface BareClient<Routes extends RouteSchema = RouteSchema> {
|
|
39
80
|
get<P extends PathsForMethod<Routes, 'GET'> | (string & {})>(path: P, options?: LookupRoute<Routes, 'GET', P & string> extends infer R extends RouteEndpoint ? EndpointOptions<R, P & string, false> : ClientRequestOptions): Promise<LookupRoute<Routes, 'GET', P & string> extends infer R extends RouteEndpoint ? R['response'] : any>;
|
|
40
81
|
post<P extends PathsForMethod<Routes, 'POST'> | (string & {})>(path: P, options?: LookupRoute<Routes, 'POST', P & string> extends infer R extends RouteEndpoint ? EndpointOptions<R, P & string, true> : ClientRequestOptions): Promise<LookupRoute<Routes, 'POST', P & string> extends infer R extends RouteEndpoint ? R['response'] : any>;
|
|
41
82
|
put<P extends PathsForMethod<Routes, 'PUT'> | (string & {})>(path: P, options?: LookupRoute<Routes, 'PUT', P & string> extends infer R extends RouteEndpoint ? EndpointOptions<R, P & string, true> : ClientRequestOptions): Promise<LookupRoute<Routes, 'PUT', P & string> extends infer R extends RouteEndpoint ? R['response'] : any>;
|
|
42
83
|
patch<P extends PathsForMethod<Routes, 'PATCH'> | (string & {})>(path: P, options?: LookupRoute<Routes, 'PATCH', P & string> extends infer R extends RouteEndpoint ? EndpointOptions<R, P & string, true> : ClientRequestOptions): Promise<LookupRoute<Routes, 'PATCH', P & string> extends infer R extends RouteEndpoint ? R['response'] : any>;
|
|
43
84
|
delete<P extends PathsForMethod<Routes, 'DELETE'> | (string & {})>(path: P, options?: LookupRoute<Routes, 'DELETE', P & string> extends infer R extends RouteEndpoint ? EndpointOptions<R, P & string, false> : ClientRequestOptions): Promise<LookupRoute<Routes, 'DELETE', P & string> extends infer R extends RouteEndpoint ? R['response'] : any>;
|
|
85
|
+
/** Discriminated Result Union client (never throws on 4xx/5xx) */
|
|
86
|
+
safe: BareSafeClient<Routes>;
|
|
87
|
+
/** Chained Proxy RPC client (api.users({ id: 42 }).get()) returning BareClientResult */
|
|
88
|
+
treaty: BareTreatyNode;
|
|
89
|
+
/** Typed WebSocket client (supports both ws:// network URLs and in-memory BareJS app testing) */
|
|
90
|
+
ws<In = any, Out = In>(path: string, options?: {
|
|
91
|
+
params?: Record<string, string | number>;
|
|
92
|
+
query?: Record<string, string | number | boolean | undefined>;
|
|
93
|
+
}): BareWsClient<In, Out>;
|
|
44
94
|
}
|
|
45
95
|
export declare const bareClient: <AppOrRoutes = any>(baseUrlOrApp: string | ({
|
|
46
96
|
fetch: (req: Request) => any;
|
|
47
97
|
} & AppOrRoutes), clientOptions?: BareClientOptions) => BareClient<AppOrRoutes extends {
|
|
48
98
|
__routes?: infer R extends RouteSchema;
|
|
49
99
|
} ? R : RouteSchema>;
|
|
100
|
+
/**
|
|
101
|
+
* Chained Proxy RPC client (Eden Treaty-style) returning discriminated BareClientResult unions.
|
|
102
|
+
* Works both in-memory (`bareTreaty(app)`) and over network (`bareTreaty('https://api.example.com')`).
|
|
103
|
+
*/
|
|
104
|
+
export declare const bareTreaty: <AppOrRoutes = any>(baseUrlOrApp: string | ({
|
|
105
|
+
fetch: (req: Request) => any;
|
|
106
|
+
} & AppOrRoutes), clientOptions?: BareClientOptions) => BareTreatyNode;
|
|
50
107
|
export {};
|
|
@@ -4,8 +4,9 @@ import { Type as typebox, Type as t } from '@sinclair/typebox';
|
|
|
4
4
|
import { type OpenAPIOptions } from "../utils/openapi";
|
|
5
5
|
import type { RouteSchemas } from "../security/validators";
|
|
6
6
|
import type { RouteSchema } from "../client/index";
|
|
7
|
+
import type { ServeTlsOptions } from "../runtime/types";
|
|
7
8
|
import { type WebSocketRouteHandler } from "./ws-match";
|
|
8
|
-
export type { WebSocketRouteHandler };
|
|
9
|
+
export type { WebSocketRouteHandler, ServeTlsOptions };
|
|
9
10
|
export declare function normalizePoolSize(poolSize: number, defaultSize?: number, maximumSize?: number): number;
|
|
10
11
|
export interface CronOptions {
|
|
11
12
|
/** Execute the cron job immediately upon registration (default: false) */
|
|
@@ -159,11 +160,13 @@ export declare class BareJS<Derived extends Record<string, any> = {}, Routes ext
|
|
|
159
160
|
maxRequestBodySize?: number;
|
|
160
161
|
trustProxy?: boolean | string[];
|
|
161
162
|
engine?: 'auto' | 'http' | 'uws';
|
|
163
|
+
tls?: ServeTlsOptions;
|
|
162
164
|
}): any;
|
|
163
165
|
listenCluster(port?: number, workers?: number, hostname?: string, options?: {
|
|
164
166
|
maxRequestBodySize?: number;
|
|
165
167
|
trustProxy?: boolean | string[];
|
|
166
168
|
engine?: 'auto' | 'http' | 'uws';
|
|
169
|
+
tls?: ServeTlsOptions;
|
|
167
170
|
}): Promise<{
|
|
168
171
|
stop: () => Promise<void> | void;
|
|
169
172
|
workers: number;
|
|
@@ -89,6 +89,7 @@ export declare class Context {
|
|
|
89
89
|
get(key: string): any;
|
|
90
90
|
setHeader(name: string, value: string): this;
|
|
91
91
|
get user(): AuthUser | undefined;
|
|
92
|
+
set user(value: AuthUser | undefined);
|
|
92
93
|
send(message: string, extra?: Record<string, any>): Response;
|
|
93
94
|
html(content: string, status?: number): Response;
|
|
94
95
|
text(content: string, status?: number): Response;
|
|
@@ -3,6 +3,12 @@ import { type RouteSchemas } from '../security/validators';
|
|
|
3
3
|
import type { RouteEndpoint, RouteSchema } from '../client/index';
|
|
4
4
|
type HandlersChain = (Middleware | Handler | RouteSchemas)[];
|
|
5
5
|
export type InferSchema<S> = S extends {
|
|
6
|
+
'~standard': {
|
|
7
|
+
types?: {
|
|
8
|
+
output: infer T;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
} ? T : S extends {
|
|
6
12
|
static: infer T;
|
|
7
13
|
} ? T : S extends {
|
|
8
14
|
_output: infer T;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { BareRouter } from './core/router';
|
|
2
|
+
import { Type, Type as t } from '@sinclair/typebox';
|
|
3
|
+
import { Z, Z as z, native, standard } from './security/validators';
|
|
4
|
+
import { bareClient, bareTreaty } from './client/index';
|
|
5
|
+
import { cors } from './security/cors';
|
|
6
|
+
import { securityHeaders } from './security/headers';
|
|
7
|
+
import { rateLimit } from './security/rateLimit';
|
|
8
|
+
import { timeout } from './security/timeout';
|
|
9
|
+
import { circuitBreaker } from './security/resilience';
|
|
10
|
+
import { redisStore } from './security/store';
|
|
11
|
+
import { healthcheck } from './utils/health';
|
|
12
|
+
import { logger } from './utils/logger';
|
|
13
|
+
import { generateOpenAPISpec, renderDocsHTML } from './utils/openapi';
|
|
14
|
+
export type EdgeNext = () => Promise<any> | any;
|
|
15
|
+
export type EdgeMiddleware<Env = Record<string, any>> = (ctx: EdgeContext<Env>, next: EdgeNext) => Promise<any> | any;
|
|
16
|
+
export type EdgeHandler<Env = Record<string, any>> = (ctx: EdgeContext<Env>) => Promise<any> | any;
|
|
17
|
+
/**
|
|
18
|
+
* Pure Web-Standard request/response Context for Edge runtimes
|
|
19
|
+
* (Cloudflare Workers, Vercel Edge, Deno Deploy, Fastly Compute, Bun).
|
|
20
|
+
* Zero `node:*` imports.
|
|
21
|
+
*/
|
|
22
|
+
export declare class EdgeContext<Env = Record<string, any>> {
|
|
23
|
+
req: Request;
|
|
24
|
+
params: Record<string, string>;
|
|
25
|
+
env: Env;
|
|
26
|
+
executionCtx: any;
|
|
27
|
+
store: Map<string, any>;
|
|
28
|
+
body: any;
|
|
29
|
+
user: any;
|
|
30
|
+
_status: number;
|
|
31
|
+
_headers: Record<string, string> | undefined;
|
|
32
|
+
_setCookies: string[] | undefined;
|
|
33
|
+
private _query;
|
|
34
|
+
private _cookies;
|
|
35
|
+
private _jsonCache;
|
|
36
|
+
private _jsonParsed;
|
|
37
|
+
private _requestId;
|
|
38
|
+
constructor(req: Request, params?: Record<string, string>, env?: Env, executionCtx?: any);
|
|
39
|
+
get requestId(): string;
|
|
40
|
+
get ip(): string;
|
|
41
|
+
get query(): Record<string, string>;
|
|
42
|
+
get cookies(): Record<string, string>;
|
|
43
|
+
get(key: string): any;
|
|
44
|
+
set(key: string, value: any): this;
|
|
45
|
+
status(code: number): this;
|
|
46
|
+
setHeader(name: string, value: string): this;
|
|
47
|
+
appendHeader(name: string, value: string): this;
|
|
48
|
+
setCookie(name: string, value: string, opts?: {
|
|
49
|
+
maxAge?: number;
|
|
50
|
+
path?: string;
|
|
51
|
+
domain?: string;
|
|
52
|
+
httpOnly?: boolean;
|
|
53
|
+
secure?: boolean;
|
|
54
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
55
|
+
}): this;
|
|
56
|
+
deleteCookie(name: string, path?: string): this;
|
|
57
|
+
jsonBody(maxBytes?: number): Promise<any>;
|
|
58
|
+
textBody(): Promise<string>;
|
|
59
|
+
private buildHeaders;
|
|
60
|
+
json(data: any, status?: number): Response;
|
|
61
|
+
text(data: string, status?: number): Response;
|
|
62
|
+
html(data: string, status?: number): Response;
|
|
63
|
+
send(msg: string, extra?: Record<string, any>): Response;
|
|
64
|
+
redirect(url: string, status?: number): Response;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Zero-Node Edge Web Framework (`barejs/edge`).
|
|
68
|
+
* Drop-in `export default app` for Cloudflare Workers, Vercel Edge, Deno Deploy, and Bun.
|
|
69
|
+
*/
|
|
70
|
+
export declare class BareEdge<Env = Record<string, any>> {
|
|
71
|
+
private staticRoutes;
|
|
72
|
+
private dynamicTrees;
|
|
73
|
+
private middlewares;
|
|
74
|
+
private requestHooks;
|
|
75
|
+
private responseHooks;
|
|
76
|
+
private errorHandler;
|
|
77
|
+
use(mw: EdgeMiddleware<Env>): this;
|
|
78
|
+
onRequest(fn: (ctx: EdgeContext<Env>) => Promise<void> | void): this;
|
|
79
|
+
onResponse(fn: (ctx: EdgeContext<Env>, res: Response) => Promise<void> | void): this;
|
|
80
|
+
onError(fn: (err: any, ctx: EdgeContext<Env>) => Response | Promise<Response>): this;
|
|
81
|
+
private add;
|
|
82
|
+
get(path: string, ...fns: EdgeMiddleware<Env>[]): this;
|
|
83
|
+
post(path: string, ...fns: EdgeMiddleware<Env>[]): this;
|
|
84
|
+
put(path: string, ...fns: EdgeMiddleware<Env>[]): this;
|
|
85
|
+
patch(path: string, ...fns: EdgeMiddleware<Env>[]): this;
|
|
86
|
+
delete(path: string, ...fns: EdgeMiddleware<Env>[]): this;
|
|
87
|
+
options(path: string, ...fns: EdgeMiddleware<Env>[]): this;
|
|
88
|
+
head(path: string, ...fns: EdgeMiddleware<Env>[]): this;
|
|
89
|
+
all(path: string, ...fns: EdgeMiddleware<Env>[]): this;
|
|
90
|
+
private normalizeResponse;
|
|
91
|
+
fetch: (req: Request, env?: Env, executionCtx?: any) => Promise<Response>;
|
|
92
|
+
request(path: string, init?: RequestInit, env?: Env): Promise<Response>;
|
|
93
|
+
}
|
|
94
|
+
export declare function createSingleflight(): {
|
|
95
|
+
do<T>(key: string, fn: () => Promise<T>): Promise<T>;
|
|
96
|
+
size(): number;
|
|
97
|
+
};
|
|
98
|
+
export { BareEdge as BareJS, BareRouter, Type, t, Z, z, native, standard, bareClient, bareTreaty, cors, securityHeaders, rateLimit, timeout, circuitBreaker, redisStore, healthcheck, logger, generateOpenAPISpec, renderDocsHTML };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -16,8 +16,8 @@ export { Context } from './core/context';
|
|
|
16
16
|
export type { Middleware, Handler, Next, Params, AuthUser, CookieOptions, UploadOptions, UploadedFile, UploadResult, UploadStreamOptions, UploadStreamPart } from './core/context';
|
|
17
17
|
import { Type } from "@sinclair/typebox";
|
|
18
18
|
export { Type, Type as T, Type as TypeBox };
|
|
19
|
-
import { typebox, zod, native, validate, schemaValidator, createResponseFilter, Z, type RouteSchemas, type RouteDetail } from './security/validators';
|
|
20
|
-
export { typebox, typebox as t, zod, zod as z, Z, Z as Zod, native, native as n, validate, validate as v, schemaValidator, createResponseFilter, type RouteSchemas, type RouteDetail };
|
|
19
|
+
import { typebox, zod, native, standard, validate, schemaValidator, createResponseFilter, Z, type RouteSchemas, type RouteDetail, type StandardSchemaV1 } from './security/validators';
|
|
20
|
+
export { typebox, typebox as t, zod, zod as z, Z, Z as Zod, native, native as n, standard, validate, validate as v, schemaValidator, createResponseFilter, type RouteSchemas, type RouteDetail, type StandardSchemaV1 };
|
|
21
21
|
export { logger, type LoggerOptions, type LogEntry } from './utils/logger';
|
|
22
22
|
export { healthcheck, type HealthcheckOptions, type HealthCheckFn } from './utils/health';
|
|
23
23
|
export { cors, type CorsOptions } from './security/cors';
|
|
@@ -33,4 +33,4 @@ export { compress, type CompressOptions, type CompressionEncoding } from './util
|
|
|
33
33
|
export { generateOpenAPISpec, renderDocsHTML, type OpenAPIOptions } from './utils/openapi';
|
|
34
34
|
export { bareAuth, createToken, basicAuth, hasRole, revokeToken, isTokenRevoked, type BareAuthOptions, type JwtAlgorithm } from './security/auth';
|
|
35
35
|
export { Hash as Password, Hash } from './security/hash';
|
|
36
|
-
export { bareClient, type BareClient, type BareClientOptions, type ClientRequestOptions, type RouteEndpoint, type RouteSchema } from './client/index';
|
|
36
|
+
export { bareClient, bareTreaty, type BareClient, type BareSafeClient, type BareTreatyNode, type BareClientResult, type BareWsClient, type BareClientOptions, type ClientRequestOptions, type RouteEndpoint, type RouteSchema } from './client/index';
|
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
import type { WebSocketRouteHandler } from '../core/ws-match';
|
|
2
|
+
export interface ServeTlsOptions {
|
|
3
|
+
/** Path to TLS private key file, or PEM string/bytes (Bun & Node https) */
|
|
4
|
+
key?: string | Uint8Array;
|
|
5
|
+
/** Path to TLS certificate file, or PEM string/bytes (Bun & Node https) */
|
|
6
|
+
cert?: string | Uint8Array;
|
|
7
|
+
/** Optional key file path alias (uWebSockets.js parity) */
|
|
8
|
+
keyFileName?: string;
|
|
9
|
+
/** Optional cert file path alias (uWebSockets.js parity) */
|
|
10
|
+
certFileName?: string;
|
|
11
|
+
/** Optional passphrase for encrypted private key */
|
|
12
|
+
passphrase?: string;
|
|
13
|
+
/** Optional CA certificate file path or PEM bytes */
|
|
14
|
+
ca?: string | Uint8Array;
|
|
15
|
+
}
|
|
2
16
|
export interface ServeOptions {
|
|
3
17
|
port: number;
|
|
4
18
|
hostname: string;
|
|
@@ -7,6 +21,7 @@ export interface ServeOptions {
|
|
|
7
21
|
fetch: (req: Request, peerIp?: string) => Response | Promise<Response>;
|
|
8
22
|
routes?: Record<string, any>;
|
|
9
23
|
engine?: 'auto' | 'http' | 'uws';
|
|
24
|
+
tls?: ServeTlsOptions;
|
|
10
25
|
uwsRoutes?: any[];
|
|
11
26
|
ws?: {
|
|
12
27
|
routes: Map<string, WebSocketRouteHandler>;
|
|
@@ -19,6 +19,39 @@ export declare const native: (schema: any) => Middleware;
|
|
|
19
19
|
* Zod Engine (z)
|
|
20
20
|
*/
|
|
21
21
|
export declare const zod: (schema: z.ZodSchema) => Middleware;
|
|
22
|
+
export interface StandardSchemaV1Issue {
|
|
23
|
+
readonly message: string;
|
|
24
|
+
readonly path?: ReadonlyArray<PropertyKey | {
|
|
25
|
+
readonly key: PropertyKey;
|
|
26
|
+
}>;
|
|
27
|
+
}
|
|
28
|
+
export type StandardSchemaV1Result<Output = unknown> = {
|
|
29
|
+
readonly value: Output;
|
|
30
|
+
readonly issues?: undefined;
|
|
31
|
+
} | {
|
|
32
|
+
readonly issues: ReadonlyArray<StandardSchemaV1Issue>;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Standard Schema v1 interface (https://standardschema.dev)
|
|
36
|
+
* Supported natively by Zod 4, Valibot, ArkType, Effect, Typia, and custom validators.
|
|
37
|
+
*/
|
|
38
|
+
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
39
|
+
readonly '~standard': {
|
|
40
|
+
readonly version: 1;
|
|
41
|
+
readonly vendor: string;
|
|
42
|
+
readonly validate: (value: unknown) => StandardSchemaV1Result<Output> | Promise<StandardSchemaV1Result<Output>>;
|
|
43
|
+
readonly types?: {
|
|
44
|
+
readonly input: Input;
|
|
45
|
+
readonly output: Output;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export declare const isStandardSchema: (schema: any) => schema is StandardSchemaV1;
|
|
50
|
+
/**
|
|
51
|
+
* Universal Standard Schema v1 Engine (Valibot, ArkType, Zod 4, Effect, etc.)
|
|
52
|
+
* Always awaits async validators and fails closed on issues or thrown errors.
|
|
53
|
+
*/
|
|
54
|
+
export declare const standard: (schema: StandardSchemaV1) => Middleware;
|
|
22
55
|
/**
|
|
23
56
|
* Smart Dispatcher (v)
|
|
24
57
|
*/
|
package/llms.txt
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# BareJS (`barejs`) — AI Quick Reference (`llms.txt`)
|
|
2
2
|
|
|
3
|
-
> BareJS is an ultra-fast, zero-allocation JIT-compiled web framework for **Bun** (`>= 1.1` with `Bun.serve({ routes })`), **Node.js** (`>= 20` via `barejs/node` with optional `uWebSockets.js` auto-detection), and **
|
|
3
|
+
> BareJS is an ultra-fast, zero-allocation JIT-compiled web framework for **Bun** (`>= 1.1` with `Bun.serve({ routes })`), **Node.js** (`>= 20` via `barejs/node` with optional `uWebSockets.js` auto-detection & `uWS.SSLApp` TLS), **Deno**, and **Edge Workers** (`barejs/edge` — zero `node:*` imports) with built-in security (Argon2id/scrypt, HS256/RS256/ES256 JWT, HMAC-signed HttpOnly cookies, CRLF injection protection, CSRF, Rate Limiting, Circuit Breaker, Idempotency-Key double-charge protection, Singleflight request coalescing, Mass-Assignment `stripUnknown`, Native Gzip/Brotli/Zstd `compress`, `app.cron`, Security Headers, DoS limits) and **Full Multi-Runtime Test Suite (483 tests / 6,740 assertions verified in CI across Bun, Node.js 20/22, and Deno 2)**.
|
|
4
4
|
|
|
5
|
-
## 1. Installation & Minimal App (Bun, Node.js, Deno)
|
|
5
|
+
## 1. Installation & Minimal App (Bun, Node.js, Deno, Edge)
|
|
6
6
|
```ts
|
|
7
|
-
import { BareJS, Context, Type } from 'barejs'; // Use 'barejs/node' on Node.js (>= 20)
|
|
7
|
+
import { BareJS, Context, Type } from 'barejs'; // Use 'barejs/node' on Node.js (>= 20) or 'barejs/edge' on Cloudflare/Vercel/Deno Deploy
|
|
8
8
|
|
|
9
9
|
// Optional poolSize (0..65536, default 1024): lazy bounded LIFO Context pool with in-flight slot clearing, shared state across JIT recompiles, & dev-mode UAF guard (0 overhead in prod)
|
|
10
10
|
const app = new BareJS();
|
|
@@ -16,7 +16,8 @@ app.get('/ping', () => 'pong');
|
|
|
16
16
|
// Wildcard catch-all route (`ctx.params['*']`)
|
|
17
17
|
app.get('/files/*', (ctx: Context) => ({ filePath: ctx.params['*'] }));
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
// Native TLS works across Bun, uWebSockets.js (SSLApp), and Node https:
|
|
20
|
+
app.listen(3000); // or app.listen(8443, '0.0.0.0', false, { tls: { keyFileName, certFileName } })
|
|
20
21
|
```
|
|
21
22
|
|
|
22
23
|
## 2. Routing, Plugins & Unified Schema Validation
|
|
@@ -38,7 +39,7 @@ app.post('/users/:id', {
|
|
|
38
39
|
password_hash: 'secret' // automatically stripped before sending to client!
|
|
39
40
|
}));
|
|
40
41
|
```
|
|
41
|
-
Supports **TypeBox** (`Type`, `t`), **Zod** (`Z`, `z`), or **Native** (`native({ properties: { a: { type: 'string' }, b: { type: 'number' } }, required: ['a'] })`).
|
|
42
|
+
Supports **Standard Schema v1** (`~standard` — Valibot, ArkType, Zod 4, Effect; sync & async fail-closed), **TypeBox** (`Type`, `t`), **Zod** (`Z`, `z`), or **Native** (`native({ properties: { a: { type: 'string' }, b: { type: 'number' } }, required: ['a'] })`).
|
|
42
43
|
|
|
43
44
|
### Modular Routers & Plugins
|
|
44
45
|
```ts
|
|
@@ -125,9 +126,11 @@ app.get('/sso', bareAuth({ algorithms: ['RS256', 'ES256'], jwksUri: 'https://aut
|
|
|
125
126
|
- **SSE**: `ctx.sse(async (send, close) => { send(data, event?, id?); })` or `ctx.sse(asyncIterable)` (works across Bun, Node.js, Deno)
|
|
126
127
|
- **JSON-RPC 2.0**: `app.rpc('/rpc', { add: ({ a, b }, ctx) => a + b })` (supports single, batch, notifications, and standard `-32700`/`-32600`/`-32601`/`-32603` error codes)
|
|
127
128
|
- **OpenAPI 3.1 + Scalar UI**: `app.docs('/docs', { title: 'My API', version: '1.0.0' })` (serves `/docs` & `/docs/json` with full `params`, `query`, `headers`, `body`, `response`, and `detail`)
|
|
128
|
-
- **Type-Safe Client (URL or Zero-Network In-Memory `app`)**:
|
|
129
|
+
- **Type-Safe Client & Chained RPC (`bareClient` / `bareTreaty`, URL or Zero-Network In-Memory `app`)**:
|
|
129
130
|
- Network: `const api = bareClient('http://localhost:3000');`
|
|
130
131
|
- In-Memory (SSR / Unit Tests): `const api = bareClient(app); await api.get('/users/:id', { params: { id: 1 } });`
|
|
132
|
+
- Non-Throwing Result Union: `const res = await api.safe.get('/users/:id', { params: { id: 1 } }); // { ok: true, data } | { ok: false, error, status }`
|
|
133
|
+
- Chained Proxy RPC & Typed WebSockets: `const treaty = bareTreaty(app); await treaty.users({ id: 1 }).get(); const ws = treaty.ws.chat.subscribe();`
|
|
131
134
|
|
|
132
135
|
## 7. Lifecycle & Error Handling
|
|
133
136
|
```ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "barejs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"author": "xarhang",
|
|
5
5
|
"description": "Batteries-included web framework for Bun, Node.js, and Deno with O(1) JIT routing, TypeBox/Zod validation, OpenAPI 3.1, WebSockets, JSON-RPC 2.0, and built-in security.",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,6 +17,10 @@
|
|
|
17
17
|
"./node": {
|
|
18
18
|
"types": "./dist/types/node.d.ts",
|
|
19
19
|
"import": "./dist/node.js"
|
|
20
|
+
},
|
|
21
|
+
"./edge": {
|
|
22
|
+
"types": "./dist/types/edge.d.ts",
|
|
23
|
+
"import": "./dist/edge.js"
|
|
20
24
|
}
|
|
21
25
|
},
|
|
22
26
|
"engines": {
|