abret 0.1.2 → 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/chunk-m2mdqvmd.js +82 -0
- package/dist/html.js +3 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +7 -72
- package/dist/middleware/static/index.d.ts +0 -4
- package/dist/middleware/static/index.js +0 -3
- package/dist/middleware/transpiler/index.d.ts +21 -0
- package/dist/middleware/transpiler/index.js +157 -0
- package/package.json +7 -1
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
runWithContext
|
|
4
|
+
} from "./chunk-xw5b0251.js";
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
var wrapWithMiddleware = (handler, middlewares) => {
|
|
8
|
+
return (req, server) => {
|
|
9
|
+
return runWithContext(() => {
|
|
10
|
+
if (middlewares.length === 0) {
|
|
11
|
+
return handler(req, server);
|
|
12
|
+
}
|
|
13
|
+
let index = 0;
|
|
14
|
+
const next = () => {
|
|
15
|
+
if (index < middlewares.length) {
|
|
16
|
+
const middleware = middlewares[index++];
|
|
17
|
+
if (middleware)
|
|
18
|
+
return middleware(req, server, next);
|
|
19
|
+
}
|
|
20
|
+
return handler(req, server);
|
|
21
|
+
};
|
|
22
|
+
return next();
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
var wrapRouteValue = (value, middlewares) => {
|
|
27
|
+
if (middlewares.length === 0) {
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
if (value instanceof Response) {
|
|
31
|
+
return wrapWithMiddleware(() => value, middlewares);
|
|
32
|
+
}
|
|
33
|
+
if (typeof value === "function") {
|
|
34
|
+
return wrapWithMiddleware(value, middlewares);
|
|
35
|
+
}
|
|
36
|
+
if (typeof value === "object" && value !== null) {
|
|
37
|
+
const wrappedMethods = {};
|
|
38
|
+
for (const [method, methodHandler] of Object.entries(value)) {
|
|
39
|
+
if (methodHandler instanceof Response) {
|
|
40
|
+
wrappedMethods[method] = wrapWithMiddleware(() => methodHandler, middlewares);
|
|
41
|
+
} else if (typeof methodHandler === "function") {
|
|
42
|
+
wrappedMethods[method] = wrapWithMiddleware(methodHandler, middlewares);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return wrappedMethods;
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
};
|
|
49
|
+
var createRoute = (path, value, ...middlewares) => {
|
|
50
|
+
const wrappedValue = wrapRouteValue(value, middlewares);
|
|
51
|
+
return { [path]: wrappedValue };
|
|
52
|
+
};
|
|
53
|
+
var createMiddleware = (fn) => fn;
|
|
54
|
+
var composeMiddlewares = (...middlewares) => {
|
|
55
|
+
return (req, server, finalNext) => {
|
|
56
|
+
let index = 0;
|
|
57
|
+
const next = () => {
|
|
58
|
+
if (index < middlewares.length) {
|
|
59
|
+
const middleware = middlewares[index++];
|
|
60
|
+
if (middleware)
|
|
61
|
+
return middleware(req, server, next);
|
|
62
|
+
}
|
|
63
|
+
return finalNext();
|
|
64
|
+
};
|
|
65
|
+
return next();
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
var mergeRoutes = (...routes) => {
|
|
69
|
+
return Object.assign({}, ...routes);
|
|
70
|
+
};
|
|
71
|
+
var createRouteGroup = (prefix, middlewares = []) => {
|
|
72
|
+
return (path, value) => {
|
|
73
|
+
let fullPath = `/${prefix}/${path}`.replace(/\/+/g, "/");
|
|
74
|
+
if (fullPath.length > 1 && fullPath.endsWith("/")) {
|
|
75
|
+
fullPath = fullPath.slice(0, -1);
|
|
76
|
+
}
|
|
77
|
+
const normalizedPath = fullPath || "/";
|
|
78
|
+
return createRoute(normalizedPath, value, ...middlewares);
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export { createRoute, createMiddleware, composeMiddlewares, mergeRoutes, createRouteGroup };
|
package/dist/html.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
import {
|
|
3
|
-
getContextStore
|
|
4
|
-
} from "./chunk-xw5b0251.js";
|
|
5
2
|
import {
|
|
6
3
|
AsyncBuffer,
|
|
7
4
|
Fragment,
|
|
8
5
|
SafeString,
|
|
9
6
|
VNode
|
|
10
7
|
} from "./chunk-m9t91z6h.js";
|
|
8
|
+
import {
|
|
9
|
+
getContextStore
|
|
10
|
+
} from "./chunk-xw5b0251.js";
|
|
11
11
|
|
|
12
12
|
// src/html.ts
|
|
13
13
|
class HTMLResponse extends Response {
|
package/dist/index.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export type RouteValue<P extends string = string, S = undefined> = Bun.Serve.Bas
|
|
|
33
33
|
* );
|
|
34
34
|
* ```
|
|
35
35
|
*/
|
|
36
|
-
export declare const createRoute: <P extends string
|
|
36
|
+
export declare const createRoute: <P extends `/${string}`, S = undefined>(path: P, value: RouteValue<P, S>, ...middlewares: Middleware<P, S>[]) => Record<P, RouteValue<P, S>>;
|
|
37
37
|
/**
|
|
38
38
|
* Helper to create a middleware function with proper typing
|
|
39
39
|
*
|
|
@@ -100,4 +100,4 @@ type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) exten
|
|
|
100
100
|
* );
|
|
101
101
|
* ```
|
|
102
102
|
*/
|
|
103
|
-
export declare const createRouteGroup: <Prefix extends string
|
|
103
|
+
export declare const createRouteGroup: <Prefix extends `/${string}`, S = undefined>(prefix: Prefix, middlewares?: Middleware<string, S>[]) => <P extends `/${string}` | "">(path: P, value: RouteValue<string, S>) => Record<`/${string}`, RouteValue<`/${string}`, S>>;
|
package/dist/index.js
CHANGED
|
@@ -1,82 +1,17 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
import {
|
|
3
|
+
composeMiddlewares,
|
|
4
|
+
createMiddleware,
|
|
5
|
+
createRoute,
|
|
6
|
+
createRouteGroup,
|
|
7
|
+
mergeRoutes
|
|
8
|
+
} from "./chunk-m2mdqvmd.js";
|
|
2
9
|
import {
|
|
3
10
|
createContext,
|
|
4
11
|
runWithContext,
|
|
5
12
|
runWithContextValue,
|
|
6
13
|
useContext
|
|
7
14
|
} from "./chunk-xw5b0251.js";
|
|
8
|
-
|
|
9
|
-
// src/index.ts
|
|
10
|
-
var wrapWithMiddleware = (handler, middlewares) => {
|
|
11
|
-
return (req, server) => {
|
|
12
|
-
return runWithContext(() => {
|
|
13
|
-
if (middlewares.length === 0) {
|
|
14
|
-
return handler(req, server);
|
|
15
|
-
}
|
|
16
|
-
let index = 0;
|
|
17
|
-
const next = () => {
|
|
18
|
-
if (index < middlewares.length) {
|
|
19
|
-
const middleware = middlewares[index++];
|
|
20
|
-
if (middleware)
|
|
21
|
-
return middleware(req, server, next);
|
|
22
|
-
}
|
|
23
|
-
return handler(req, server);
|
|
24
|
-
};
|
|
25
|
-
return next();
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
var wrapRouteValue = (value, middlewares) => {
|
|
30
|
-
if (middlewares.length === 0) {
|
|
31
|
-
return value;
|
|
32
|
-
}
|
|
33
|
-
if (value instanceof Response) {
|
|
34
|
-
return wrapWithMiddleware(() => value, middlewares);
|
|
35
|
-
}
|
|
36
|
-
if (typeof value === "function") {
|
|
37
|
-
return wrapWithMiddleware(value, middlewares);
|
|
38
|
-
}
|
|
39
|
-
if (typeof value === "object" && value !== null) {
|
|
40
|
-
const wrappedMethods = {};
|
|
41
|
-
for (const [method, methodHandler] of Object.entries(value)) {
|
|
42
|
-
if (methodHandler instanceof Response) {
|
|
43
|
-
wrappedMethods[method] = wrapWithMiddleware(() => methodHandler, middlewares);
|
|
44
|
-
} else if (typeof methodHandler === "function") {
|
|
45
|
-
wrappedMethods[method] = wrapWithMiddleware(methodHandler, middlewares);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return wrappedMethods;
|
|
49
|
-
}
|
|
50
|
-
return value;
|
|
51
|
-
};
|
|
52
|
-
var createRoute = (path, value, ...middlewares) => {
|
|
53
|
-
const wrappedValue = wrapRouteValue(value, middlewares);
|
|
54
|
-
return { [path]: wrappedValue };
|
|
55
|
-
};
|
|
56
|
-
var createMiddleware = (fn) => fn;
|
|
57
|
-
var composeMiddlewares = (...middlewares) => {
|
|
58
|
-
return (req, server, finalNext) => {
|
|
59
|
-
let index = 0;
|
|
60
|
-
const next = () => {
|
|
61
|
-
if (index < middlewares.length) {
|
|
62
|
-
const middleware = middlewares[index++];
|
|
63
|
-
if (middleware)
|
|
64
|
-
return middleware(req, server, next);
|
|
65
|
-
}
|
|
66
|
-
return finalNext();
|
|
67
|
-
};
|
|
68
|
-
return next();
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
var mergeRoutes = (...routes) => {
|
|
72
|
-
return Object.assign({}, ...routes);
|
|
73
|
-
};
|
|
74
|
-
var createRouteGroup = (prefix, middlewares = []) => {
|
|
75
|
-
return (path, value) => {
|
|
76
|
-
const fullPath = `${prefix}${path}`;
|
|
77
|
-
return createRoute(fullPath, value, ...middlewares);
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
15
|
export {
|
|
81
16
|
useContext,
|
|
82
17
|
runWithContextValue,
|
|
@@ -19,10 +19,6 @@ export type ServeStaticOptions = {
|
|
|
19
19
|
* Extension (without dot) -> Mime Type
|
|
20
20
|
*/
|
|
21
21
|
mimes?: Record<string, string>;
|
|
22
|
-
/**
|
|
23
|
-
* Callback when file is not found.
|
|
24
|
-
*/
|
|
25
|
-
onNotFound?: (path: string, req: Request) => void;
|
|
26
22
|
};
|
|
27
23
|
/**
|
|
28
24
|
* Middleware to serve static files using Bun.file
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface TranspilerOptions {
|
|
2
|
+
/** Directory where source files (.ts, .tsx) are located */
|
|
3
|
+
sourcePath: string;
|
|
4
|
+
/** URL prefix to intercept (e.g., "/_modules") */
|
|
5
|
+
staticBasePath: string;
|
|
6
|
+
/** Optional sub-path for vendor modules. Defaults to 'vendor' */
|
|
7
|
+
vendorPath?: string;
|
|
8
|
+
/** Optional list of modules to bundle on startup */
|
|
9
|
+
prewarm?: string[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Transpiler middleware that handles on-the-fly TS/TSX transpilation
|
|
13
|
+
* and automatic npm module bundling (vendor modules).
|
|
14
|
+
*
|
|
15
|
+
* Usage:
|
|
16
|
+
* ```ts
|
|
17
|
+
* transpiler({ sourcePath: "./src", staticBasePath: "/_modules" })
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare const transpiler: (options: TranspilerOptions) => import("../..").Middleware<string, undefined>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
createMiddleware
|
|
4
|
+
} from "../../chunk-m2mdqvmd.js";
|
|
5
|
+
import"../../chunk-xw5b0251.js";
|
|
6
|
+
|
|
7
|
+
// src/middleware/transpiler/index.ts
|
|
8
|
+
import { existsSync, mkdirSync } from "fs";
|
|
9
|
+
import path from "path";
|
|
10
|
+
var transpiler = (options) => {
|
|
11
|
+
const {
|
|
12
|
+
sourcePath,
|
|
13
|
+
staticBasePath,
|
|
14
|
+
vendorPath = "vendor",
|
|
15
|
+
prewarm = []
|
|
16
|
+
} = options;
|
|
17
|
+
const cacheDir = path.resolve(process.cwd(), "node_modules", ".transpiler");
|
|
18
|
+
const basePrefix = staticBasePath.endsWith("/") ? staticBasePath : `${staticBasePath}/`;
|
|
19
|
+
const vendorPrefix = `${basePrefix}${vendorPath.replace(/^\/|\/$/g, "")}/`;
|
|
20
|
+
if (!existsSync(cacheDir)) {
|
|
21
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
22
|
+
}
|
|
23
|
+
async function bundleVendorModule(moduleName) {
|
|
24
|
+
const cacheKey = moduleName.replace(/\//g, "__");
|
|
25
|
+
const cachedFile = path.join(cacheDir, `${cacheKey}.js`);
|
|
26
|
+
if (existsSync(cachedFile))
|
|
27
|
+
return;
|
|
28
|
+
try {
|
|
29
|
+
const entryPoint = Bun.resolveSync(moduleName, process.cwd());
|
|
30
|
+
const result = await Bun.build({
|
|
31
|
+
entrypoints: [entryPoint],
|
|
32
|
+
target: "browser",
|
|
33
|
+
format: "esm",
|
|
34
|
+
minify: true,
|
|
35
|
+
plugins: [
|
|
36
|
+
{
|
|
37
|
+
name: "abret-external-vendor",
|
|
38
|
+
setup(build) {
|
|
39
|
+
build.onResolve({ filter: /^[^./]/ }, (args) => {
|
|
40
|
+
if (args.path === moduleName)
|
|
41
|
+
return null;
|
|
42
|
+
return { path: args.path, external: true };
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
});
|
|
48
|
+
if (!result.success || result.outputs.length === 0) {
|
|
49
|
+
console.error(`[Abret] Failed to bundle vendor module: ${moduleName}`, result.logs);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const output = result.outputs[0];
|
|
53
|
+
if (!output)
|
|
54
|
+
return;
|
|
55
|
+
const rawContent = await output.text();
|
|
56
|
+
const content = rawContent.replace(/((?:import|export)\s*[\s\S]*?from\s*['"]|import\s*\(['"])([^'"]+)(['"]\)?)/g, (match, prefix, path2, suffix) => {
|
|
57
|
+
if (/^(https?:|(?:\/\/))/.test(path2))
|
|
58
|
+
return match;
|
|
59
|
+
if (!path2.startsWith(".") && !path2.startsWith("/")) {
|
|
60
|
+
return `${prefix}${basePrefix}${vendorPath.replace(/^\/|\/$/g, "")}/${path2}${suffix}`;
|
|
61
|
+
}
|
|
62
|
+
return match;
|
|
63
|
+
});
|
|
64
|
+
await Bun.write(cachedFile, content);
|
|
65
|
+
console.log(`[Abret] Pre-bundled: ${moduleName}`);
|
|
66
|
+
} catch (err) {
|
|
67
|
+
console.error(`[Abret] Error bundling ${moduleName}:`, err);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (prewarm.length > 0) {
|
|
71
|
+
for (const moduleName of prewarm) {
|
|
72
|
+
bundleVendorModule(moduleName);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return createMiddleware(async (req, _server, next) => {
|
|
76
|
+
const url = new URL(req.url);
|
|
77
|
+
const pathname = url.pathname;
|
|
78
|
+
if (!pathname.startsWith(basePrefix) && pathname !== staticBasePath) {
|
|
79
|
+
return next();
|
|
80
|
+
}
|
|
81
|
+
if (pathname.startsWith(vendorPrefix)) {
|
|
82
|
+
const moduleName = pathname.slice(vendorPrefix.length);
|
|
83
|
+
const cacheKey = moduleName.replace(/\//g, "__");
|
|
84
|
+
const cachedFile = path.join(cacheDir, `${cacheKey}.js`);
|
|
85
|
+
if (existsSync(cachedFile)) {
|
|
86
|
+
return new Response(Bun.file(cachedFile), {
|
|
87
|
+
headers: {
|
|
88
|
+
"Content-Type": "application/javascript",
|
|
89
|
+
"Cache-Control": "public, max-age=31536000, immutable"
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
await bundleVendorModule(moduleName);
|
|
94
|
+
if (existsSync(cachedFile)) {
|
|
95
|
+
return new Response(Bun.file(cachedFile), {
|
|
96
|
+
headers: {
|
|
97
|
+
"Content-Type": "application/javascript",
|
|
98
|
+
"Cache-Control": "public, max-age=31536000, immutable"
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return next();
|
|
103
|
+
}
|
|
104
|
+
const internalPath = pathname.slice(basePrefix.length);
|
|
105
|
+
const baseFileName = internalPath.endsWith(".js") ? internalPath.slice(0, -3) : internalPath;
|
|
106
|
+
const possibleExtensions = [".tsx", ".ts", ".jsx", ".js"];
|
|
107
|
+
let sourceFile = "";
|
|
108
|
+
for (const ext of possibleExtensions) {
|
|
109
|
+
const p = path.join(path.resolve(sourcePath), (baseFileName.startsWith("/") ? baseFileName.slice(1) : baseFileName) + ext);
|
|
110
|
+
if (existsSync(p)) {
|
|
111
|
+
sourceFile = p;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (sourceFile) {
|
|
116
|
+
try {
|
|
117
|
+
const buildResult = await Bun.build({
|
|
118
|
+
entrypoints: [sourceFile],
|
|
119
|
+
target: "browser",
|
|
120
|
+
format: "esm",
|
|
121
|
+
external: ["*"]
|
|
122
|
+
});
|
|
123
|
+
if (!buildResult.success || buildResult.outputs.length === 0) {
|
|
124
|
+
console.error(`[Abret] Build error for ${sourceFile}:`, buildResult.logs);
|
|
125
|
+
return next();
|
|
126
|
+
}
|
|
127
|
+
const output = buildResult.outputs[0];
|
|
128
|
+
if (!output) {
|
|
129
|
+
console.error(`[Abret] No output files generated for ${sourceFile}`);
|
|
130
|
+
return next();
|
|
131
|
+
}
|
|
132
|
+
const transpiledCode = await output.text();
|
|
133
|
+
const finalCode = transpiledCode.replace(/((?:import|export)\s*[\s\S]*?from\s*['"]|import\s*\(['"])([^'"]+)(['"]\)?)/g, (match, prefix, path2, suffix) => {
|
|
134
|
+
if (/^(https?:|(?:\/\/))/.test(path2))
|
|
135
|
+
return match;
|
|
136
|
+
if (!path2.startsWith(".") && !path2.startsWith("/")) {
|
|
137
|
+
return `${prefix}${basePrefix}${vendorPath.replace(/^\/|\/$/g, "")}/${path2}${suffix}`;
|
|
138
|
+
}
|
|
139
|
+
if (path2.startsWith(".") && !path2.split("/").pop()?.includes(".")) {
|
|
140
|
+
return `${prefix}${path2}.js${suffix}`;
|
|
141
|
+
}
|
|
142
|
+
return match;
|
|
143
|
+
});
|
|
144
|
+
return new Response(finalCode, {
|
|
145
|
+
headers: { "Content-Type": "application/javascript" }
|
|
146
|
+
});
|
|
147
|
+
} catch (err) {
|
|
148
|
+
console.error(`[Abret] Transpilation error for ${sourceFile}:`, err);
|
|
149
|
+
return next();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return next();
|
|
153
|
+
});
|
|
154
|
+
};
|
|
155
|
+
export {
|
|
156
|
+
transpiler
|
|
157
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "abret",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Fast, type-safe web framework for Bun with built-in JSX and middleware support.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Arisris",
|
|
@@ -52,6 +52,10 @@
|
|
|
52
52
|
"./middleware/static": {
|
|
53
53
|
"types": "./dist/middleware/static/index.d.ts",
|
|
54
54
|
"import": "./dist/middleware/static/index.js"
|
|
55
|
+
},
|
|
56
|
+
"./middleware/transpiler": {
|
|
57
|
+
"types": "./dist/middleware/transpiler/index.d.ts",
|
|
58
|
+
"import": "./dist/middleware/transpiler/index.js"
|
|
55
59
|
}
|
|
56
60
|
},
|
|
57
61
|
"scripts": {
|
|
@@ -63,7 +67,9 @@
|
|
|
63
67
|
},
|
|
64
68
|
"devDependencies": {
|
|
65
69
|
"@biomejs/biome": "2.3.12",
|
|
70
|
+
"@preact/signals": "^2.6.2",
|
|
66
71
|
"@types/bun": "latest",
|
|
72
|
+
"preact": "^10.28.3",
|
|
67
73
|
"typescript": "^5"
|
|
68
74
|
}
|
|
69
75
|
}
|