@cloudwerk/cli 0.0.6 → 0.1.0
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 +227 -77
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
import { program } from "commander";
|
|
5
5
|
|
|
6
6
|
// src/commands/dev.ts
|
|
7
|
-
import * as
|
|
8
|
-
import * as
|
|
7
|
+
import * as path4 from "path";
|
|
8
|
+
import * as fs3 from "fs";
|
|
9
9
|
import * as os from "os";
|
|
10
10
|
import { serve } from "@hono/node-server";
|
|
11
11
|
import {
|
|
@@ -112,11 +112,11 @@ function printError(message, suggestion) {
|
|
|
112
112
|
}
|
|
113
113
|
console.log();
|
|
114
114
|
}
|
|
115
|
-
function logRequest(method,
|
|
115
|
+
function logRequest(method, path5, status, duration) {
|
|
116
116
|
const methodColor = getMethodColor(method);
|
|
117
117
|
const statusColor = status >= 400 ? pc.red : status >= 300 ? pc.yellow : pc.green;
|
|
118
118
|
console.log(
|
|
119
|
-
pc.dim("[") + methodColor(method.padEnd(6)) + pc.dim("]") + " " +
|
|
119
|
+
pc.dim("[") + methodColor(method.padEnd(6)) + pc.dim("]") + " " + path5 + " " + statusColor(String(status)) + " " + pc.dim(`${duration}ms`)
|
|
120
120
|
);
|
|
121
121
|
}
|
|
122
122
|
|
|
@@ -237,26 +237,26 @@ var handleParsingNestedValues = (form, key, value) => {
|
|
|
237
237
|
};
|
|
238
238
|
|
|
239
239
|
// ../../node_modules/.pnpm/hono@4.11.5/node_modules/hono/dist/utils/url.js
|
|
240
|
-
var splitPath = (
|
|
241
|
-
const paths =
|
|
240
|
+
var splitPath = (path5) => {
|
|
241
|
+
const paths = path5.split("/");
|
|
242
242
|
if (paths[0] === "") {
|
|
243
243
|
paths.shift();
|
|
244
244
|
}
|
|
245
245
|
return paths;
|
|
246
246
|
};
|
|
247
247
|
var splitRoutingPath = (routePath) => {
|
|
248
|
-
const { groups, path:
|
|
249
|
-
const paths = splitPath(
|
|
248
|
+
const { groups, path: path5 } = extractGroupsFromPath(routePath);
|
|
249
|
+
const paths = splitPath(path5);
|
|
250
250
|
return replaceGroupMarks(paths, groups);
|
|
251
251
|
};
|
|
252
|
-
var extractGroupsFromPath = (
|
|
252
|
+
var extractGroupsFromPath = (path5) => {
|
|
253
253
|
const groups = [];
|
|
254
|
-
|
|
254
|
+
path5 = path5.replace(/\{[^}]+\}/g, (match2, index) => {
|
|
255
255
|
const mark = `@${index}`;
|
|
256
256
|
groups.push([mark, match2]);
|
|
257
257
|
return mark;
|
|
258
258
|
});
|
|
259
|
-
return { groups, path:
|
|
259
|
+
return { groups, path: path5 };
|
|
260
260
|
};
|
|
261
261
|
var replaceGroupMarks = (paths, groups) => {
|
|
262
262
|
for (let i = groups.length - 1; i >= 0; i--) {
|
|
@@ -311,8 +311,8 @@ var getPath = (request) => {
|
|
|
311
311
|
const charCode = url.charCodeAt(i);
|
|
312
312
|
if (charCode === 37) {
|
|
313
313
|
const queryIndex = url.indexOf("?", i);
|
|
314
|
-
const
|
|
315
|
-
return tryDecodeURI(
|
|
314
|
+
const path5 = url.slice(start, queryIndex === -1 ? void 0 : queryIndex);
|
|
315
|
+
return tryDecodeURI(path5.includes("%25") ? path5.replace(/%25/g, "%2525") : path5);
|
|
316
316
|
} else if (charCode === 63) {
|
|
317
317
|
break;
|
|
318
318
|
}
|
|
@@ -329,11 +329,11 @@ var mergePath = (base, sub, ...rest) => {
|
|
|
329
329
|
}
|
|
330
330
|
return `${base?.[0] === "/" ? "" : "/"}${base}${sub === "/" ? "" : `${base?.at(-1) === "/" ? "" : "/"}${sub?.[0] === "/" ? sub.slice(1) : sub}`}`;
|
|
331
331
|
};
|
|
332
|
-
var checkOptionalParameter = (
|
|
333
|
-
if (
|
|
332
|
+
var checkOptionalParameter = (path5) => {
|
|
333
|
+
if (path5.charCodeAt(path5.length - 1) !== 63 || !path5.includes(":")) {
|
|
334
334
|
return null;
|
|
335
335
|
}
|
|
336
|
-
const segments =
|
|
336
|
+
const segments = path5.split("/");
|
|
337
337
|
const results = [];
|
|
338
338
|
let basePath = "";
|
|
339
339
|
segments.forEach((segment) => {
|
|
@@ -474,9 +474,9 @@ var HonoRequest = class {
|
|
|
474
474
|
*/
|
|
475
475
|
path;
|
|
476
476
|
bodyCache = {};
|
|
477
|
-
constructor(request,
|
|
477
|
+
constructor(request, path5 = "/", matchResult = [[]]) {
|
|
478
478
|
this.raw = request;
|
|
479
|
-
this.path =
|
|
479
|
+
this.path = path5;
|
|
480
480
|
this.#matchResult = matchResult;
|
|
481
481
|
this.#validatedData = {};
|
|
482
482
|
}
|
|
@@ -1212,8 +1212,8 @@ var Hono = class _Hono {
|
|
|
1212
1212
|
return this;
|
|
1213
1213
|
};
|
|
1214
1214
|
});
|
|
1215
|
-
this.on = (method,
|
|
1216
|
-
for (const p of [
|
|
1215
|
+
this.on = (method, path5, ...handlers) => {
|
|
1216
|
+
for (const p of [path5].flat()) {
|
|
1217
1217
|
this.#path = p;
|
|
1218
1218
|
for (const m of [method].flat()) {
|
|
1219
1219
|
handlers.map((handler) => {
|
|
@@ -1270,8 +1270,8 @@ var Hono = class _Hono {
|
|
|
1270
1270
|
* app.route("/api", app2) // GET /api/user
|
|
1271
1271
|
* ```
|
|
1272
1272
|
*/
|
|
1273
|
-
route(
|
|
1274
|
-
const subApp = this.basePath(
|
|
1273
|
+
route(path5, app) {
|
|
1274
|
+
const subApp = this.basePath(path5);
|
|
1275
1275
|
app.routes.map((r) => {
|
|
1276
1276
|
let handler;
|
|
1277
1277
|
if (app.errorHandler === errorHandler) {
|
|
@@ -1297,9 +1297,9 @@ var Hono = class _Hono {
|
|
|
1297
1297
|
* const api = new Hono().basePath('/api')
|
|
1298
1298
|
* ```
|
|
1299
1299
|
*/
|
|
1300
|
-
basePath(
|
|
1300
|
+
basePath(path5) {
|
|
1301
1301
|
const subApp = this.#clone();
|
|
1302
|
-
subApp._basePath = mergePath(this._basePath,
|
|
1302
|
+
subApp._basePath = mergePath(this._basePath, path5);
|
|
1303
1303
|
return subApp;
|
|
1304
1304
|
}
|
|
1305
1305
|
/**
|
|
@@ -1373,7 +1373,7 @@ var Hono = class _Hono {
|
|
|
1373
1373
|
* })
|
|
1374
1374
|
* ```
|
|
1375
1375
|
*/
|
|
1376
|
-
mount(
|
|
1376
|
+
mount(path5, applicationHandler, options) {
|
|
1377
1377
|
let replaceRequest;
|
|
1378
1378
|
let optionHandler;
|
|
1379
1379
|
if (options) {
|
|
@@ -1400,7 +1400,7 @@ var Hono = class _Hono {
|
|
|
1400
1400
|
return [c.env, executionContext];
|
|
1401
1401
|
};
|
|
1402
1402
|
replaceRequest ||= (() => {
|
|
1403
|
-
const mergedPath = mergePath(this._basePath,
|
|
1403
|
+
const mergedPath = mergePath(this._basePath, path5);
|
|
1404
1404
|
const pathPrefixLength = mergedPath === "/" ? 0 : mergedPath.length;
|
|
1405
1405
|
return (request) => {
|
|
1406
1406
|
const url = new URL(request.url);
|
|
@@ -1415,14 +1415,14 @@ var Hono = class _Hono {
|
|
|
1415
1415
|
}
|
|
1416
1416
|
await next();
|
|
1417
1417
|
};
|
|
1418
|
-
this.#addRoute(METHOD_NAME_ALL, mergePath(
|
|
1418
|
+
this.#addRoute(METHOD_NAME_ALL, mergePath(path5, "*"), handler);
|
|
1419
1419
|
return this;
|
|
1420
1420
|
}
|
|
1421
|
-
#addRoute(method,
|
|
1421
|
+
#addRoute(method, path5, handler) {
|
|
1422
1422
|
method = method.toUpperCase();
|
|
1423
|
-
|
|
1424
|
-
const r = { basePath: this._basePath, path:
|
|
1425
|
-
this.router.add(method,
|
|
1423
|
+
path5 = mergePath(this._basePath, path5);
|
|
1424
|
+
const r = { basePath: this._basePath, path: path5, method, handler };
|
|
1425
|
+
this.router.add(method, path5, [handler, r]);
|
|
1426
1426
|
this.routes.push(r);
|
|
1427
1427
|
}
|
|
1428
1428
|
#handleError(err, c) {
|
|
@@ -1435,10 +1435,10 @@ var Hono = class _Hono {
|
|
|
1435
1435
|
if (method === "HEAD") {
|
|
1436
1436
|
return (async () => new Response(null, await this.#dispatch(request, executionCtx, env, "GET")))();
|
|
1437
1437
|
}
|
|
1438
|
-
const
|
|
1439
|
-
const matchResult = this.router.match(method,
|
|
1438
|
+
const path5 = this.getPath(request, { env });
|
|
1439
|
+
const matchResult = this.router.match(method, path5);
|
|
1440
1440
|
const c = new Context(request, {
|
|
1441
|
-
path:
|
|
1441
|
+
path: path5,
|
|
1442
1442
|
matchResult,
|
|
1443
1443
|
env,
|
|
1444
1444
|
executionCtx,
|
|
@@ -1538,7 +1538,7 @@ var Hono = class _Hono {
|
|
|
1538
1538
|
|
|
1539
1539
|
// ../../node_modules/.pnpm/hono@4.11.5/node_modules/hono/dist/router/reg-exp-router/matcher.js
|
|
1540
1540
|
var emptyParam = [];
|
|
1541
|
-
function match(method,
|
|
1541
|
+
function match(method, path5) {
|
|
1542
1542
|
const matchers = this.buildAllMatchers();
|
|
1543
1543
|
const match2 = ((method2, path22) => {
|
|
1544
1544
|
const matcher = matchers[method2] || matchers[METHOD_NAME_ALL];
|
|
@@ -1554,7 +1554,7 @@ function match(method, path3) {
|
|
|
1554
1554
|
return [matcher[1][index], match3];
|
|
1555
1555
|
});
|
|
1556
1556
|
this.match = match2;
|
|
1557
|
-
return match2(method,
|
|
1557
|
+
return match2(method, path5);
|
|
1558
1558
|
}
|
|
1559
1559
|
|
|
1560
1560
|
// ../../node_modules/.pnpm/hono@4.11.5/node_modules/hono/dist/router/reg-exp-router/node.js
|
|
@@ -1669,12 +1669,12 @@ var Node = class _Node {
|
|
|
1669
1669
|
var Trie = class {
|
|
1670
1670
|
#context = { varIndex: 0 };
|
|
1671
1671
|
#root = new Node();
|
|
1672
|
-
insert(
|
|
1672
|
+
insert(path5, index, pathErrorCheckOnly) {
|
|
1673
1673
|
const paramAssoc = [];
|
|
1674
1674
|
const groups = [];
|
|
1675
1675
|
for (let i = 0; ; ) {
|
|
1676
1676
|
let replaced = false;
|
|
1677
|
-
|
|
1677
|
+
path5 = path5.replace(/\{[^}]+\}/g, (m) => {
|
|
1678
1678
|
const mark = `@\\${i}`;
|
|
1679
1679
|
groups[i] = [mark, m];
|
|
1680
1680
|
i++;
|
|
@@ -1685,7 +1685,7 @@ var Trie = class {
|
|
|
1685
1685
|
break;
|
|
1686
1686
|
}
|
|
1687
1687
|
}
|
|
1688
|
-
const tokens =
|
|
1688
|
+
const tokens = path5.match(/(?::[^\/]+)|(?:\/\*$)|./g) || [];
|
|
1689
1689
|
for (let i = groups.length - 1; i >= 0; i--) {
|
|
1690
1690
|
const [mark] = groups[i];
|
|
1691
1691
|
for (let j = tokens.length - 1; j >= 0; j--) {
|
|
@@ -1724,9 +1724,9 @@ var Trie = class {
|
|
|
1724
1724
|
// ../../node_modules/.pnpm/hono@4.11.5/node_modules/hono/dist/router/reg-exp-router/router.js
|
|
1725
1725
|
var nullMatcher = [/^$/, [], /* @__PURE__ */ Object.create(null)];
|
|
1726
1726
|
var wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
1727
|
-
function buildWildcardRegExp(
|
|
1728
|
-
return wildcardRegExpCache[
|
|
1729
|
-
|
|
1727
|
+
function buildWildcardRegExp(path5) {
|
|
1728
|
+
return wildcardRegExpCache[path5] ??= new RegExp(
|
|
1729
|
+
path5 === "*" ? "" : `^${path5.replace(
|
|
1730
1730
|
/\/\*$|([.\\+*[^\]$()])/g,
|
|
1731
1731
|
(_, metaChar) => metaChar ? `\\${metaChar}` : "(?:|/.*)"
|
|
1732
1732
|
)}$`
|
|
@@ -1748,17 +1748,17 @@ function buildMatcherFromPreprocessedRoutes(routes) {
|
|
|
1748
1748
|
);
|
|
1749
1749
|
const staticMap = /* @__PURE__ */ Object.create(null);
|
|
1750
1750
|
for (let i = 0, j = -1, len = routesWithStaticPathFlag.length; i < len; i++) {
|
|
1751
|
-
const [pathErrorCheckOnly,
|
|
1751
|
+
const [pathErrorCheckOnly, path5, handlers] = routesWithStaticPathFlag[i];
|
|
1752
1752
|
if (pathErrorCheckOnly) {
|
|
1753
|
-
staticMap[
|
|
1753
|
+
staticMap[path5] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), emptyParam];
|
|
1754
1754
|
} else {
|
|
1755
1755
|
j++;
|
|
1756
1756
|
}
|
|
1757
1757
|
let paramAssoc;
|
|
1758
1758
|
try {
|
|
1759
|
-
paramAssoc = trie.insert(
|
|
1759
|
+
paramAssoc = trie.insert(path5, j, pathErrorCheckOnly);
|
|
1760
1760
|
} catch (e) {
|
|
1761
|
-
throw e === PATH_ERROR ? new UnsupportedPathError(
|
|
1761
|
+
throw e === PATH_ERROR ? new UnsupportedPathError(path5) : e;
|
|
1762
1762
|
}
|
|
1763
1763
|
if (pathErrorCheckOnly) {
|
|
1764
1764
|
continue;
|
|
@@ -1792,12 +1792,12 @@ function buildMatcherFromPreprocessedRoutes(routes) {
|
|
|
1792
1792
|
}
|
|
1793
1793
|
return [regexp, handlerMap, staticMap];
|
|
1794
1794
|
}
|
|
1795
|
-
function findMiddleware(middleware,
|
|
1795
|
+
function findMiddleware(middleware, path5) {
|
|
1796
1796
|
if (!middleware) {
|
|
1797
1797
|
return void 0;
|
|
1798
1798
|
}
|
|
1799
1799
|
for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
|
|
1800
|
-
if (buildWildcardRegExp(k).test(
|
|
1800
|
+
if (buildWildcardRegExp(k).test(path5)) {
|
|
1801
1801
|
return [...middleware[k]];
|
|
1802
1802
|
}
|
|
1803
1803
|
}
|
|
@@ -1811,7 +1811,7 @@ var RegExpRouter = class {
|
|
|
1811
1811
|
this.#middleware = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
1812
1812
|
this.#routes = { [METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
1813
1813
|
}
|
|
1814
|
-
add(method,
|
|
1814
|
+
add(method, path5, handler) {
|
|
1815
1815
|
const middleware = this.#middleware;
|
|
1816
1816
|
const routes = this.#routes;
|
|
1817
1817
|
if (!middleware || !routes) {
|
|
@@ -1826,18 +1826,18 @@ var RegExpRouter = class {
|
|
|
1826
1826
|
});
|
|
1827
1827
|
});
|
|
1828
1828
|
}
|
|
1829
|
-
if (
|
|
1830
|
-
|
|
1829
|
+
if (path5 === "/*") {
|
|
1830
|
+
path5 = "*";
|
|
1831
1831
|
}
|
|
1832
|
-
const paramCount = (
|
|
1833
|
-
if (/\*$/.test(
|
|
1834
|
-
const re = buildWildcardRegExp(
|
|
1832
|
+
const paramCount = (path5.match(/\/:/g) || []).length;
|
|
1833
|
+
if (/\*$/.test(path5)) {
|
|
1834
|
+
const re = buildWildcardRegExp(path5);
|
|
1835
1835
|
if (method === METHOD_NAME_ALL) {
|
|
1836
1836
|
Object.keys(middleware).forEach((m) => {
|
|
1837
|
-
middleware[m][
|
|
1837
|
+
middleware[m][path5] ||= findMiddleware(middleware[m], path5) || findMiddleware(middleware[METHOD_NAME_ALL], path5) || [];
|
|
1838
1838
|
});
|
|
1839
1839
|
} else {
|
|
1840
|
-
middleware[method][
|
|
1840
|
+
middleware[method][path5] ||= findMiddleware(middleware[method], path5) || findMiddleware(middleware[METHOD_NAME_ALL], path5) || [];
|
|
1841
1841
|
}
|
|
1842
1842
|
Object.keys(middleware).forEach((m) => {
|
|
1843
1843
|
if (method === METHOD_NAME_ALL || method === m) {
|
|
@@ -1855,7 +1855,7 @@ var RegExpRouter = class {
|
|
|
1855
1855
|
});
|
|
1856
1856
|
return;
|
|
1857
1857
|
}
|
|
1858
|
-
const paths = checkOptionalParameter(
|
|
1858
|
+
const paths = checkOptionalParameter(path5) || [path5];
|
|
1859
1859
|
for (let i = 0, len = paths.length; i < len; i++) {
|
|
1860
1860
|
const path22 = paths[i];
|
|
1861
1861
|
Object.keys(routes).forEach((m) => {
|
|
@@ -1882,13 +1882,13 @@ var RegExpRouter = class {
|
|
|
1882
1882
|
const routes = [];
|
|
1883
1883
|
let hasOwnRoute = method === METHOD_NAME_ALL;
|
|
1884
1884
|
[this.#middleware, this.#routes].forEach((r) => {
|
|
1885
|
-
const ownRoute = r[method] ? Object.keys(r[method]).map((
|
|
1885
|
+
const ownRoute = r[method] ? Object.keys(r[method]).map((path5) => [path5, r[method][path5]]) : [];
|
|
1886
1886
|
if (ownRoute.length !== 0) {
|
|
1887
1887
|
hasOwnRoute ||= true;
|
|
1888
1888
|
routes.push(...ownRoute);
|
|
1889
1889
|
} else if (method !== METHOD_NAME_ALL) {
|
|
1890
1890
|
routes.push(
|
|
1891
|
-
...Object.keys(r[METHOD_NAME_ALL]).map((
|
|
1891
|
+
...Object.keys(r[METHOD_NAME_ALL]).map((path5) => [path5, r[METHOD_NAME_ALL][path5]])
|
|
1892
1892
|
);
|
|
1893
1893
|
}
|
|
1894
1894
|
});
|
|
@@ -1908,13 +1908,13 @@ var SmartRouter = class {
|
|
|
1908
1908
|
constructor(init) {
|
|
1909
1909
|
this.#routers = init.routers;
|
|
1910
1910
|
}
|
|
1911
|
-
add(method,
|
|
1911
|
+
add(method, path5, handler) {
|
|
1912
1912
|
if (!this.#routes) {
|
|
1913
1913
|
throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
1914
1914
|
}
|
|
1915
|
-
this.#routes.push([method,
|
|
1915
|
+
this.#routes.push([method, path5, handler]);
|
|
1916
1916
|
}
|
|
1917
|
-
match(method,
|
|
1917
|
+
match(method, path5) {
|
|
1918
1918
|
if (!this.#routes) {
|
|
1919
1919
|
throw new Error("Fatal error");
|
|
1920
1920
|
}
|
|
@@ -1929,7 +1929,7 @@ var SmartRouter = class {
|
|
|
1929
1929
|
for (let i2 = 0, len2 = routes.length; i2 < len2; i2++) {
|
|
1930
1930
|
router.add(...routes[i2]);
|
|
1931
1931
|
}
|
|
1932
|
-
res = router.match(method,
|
|
1932
|
+
res = router.match(method, path5);
|
|
1933
1933
|
} catch (e) {
|
|
1934
1934
|
if (e instanceof UnsupportedPathError) {
|
|
1935
1935
|
continue;
|
|
@@ -1973,10 +1973,10 @@ var Node2 = class _Node2 {
|
|
|
1973
1973
|
}
|
|
1974
1974
|
this.#patterns = [];
|
|
1975
1975
|
}
|
|
1976
|
-
insert(method,
|
|
1976
|
+
insert(method, path5, handler) {
|
|
1977
1977
|
this.#order = ++this.#order;
|
|
1978
1978
|
let curNode = this;
|
|
1979
|
-
const parts = splitRoutingPath(
|
|
1979
|
+
const parts = splitRoutingPath(path5);
|
|
1980
1980
|
const possibleKeys = [];
|
|
1981
1981
|
for (let i = 0, len = parts.length; i < len; i++) {
|
|
1982
1982
|
const p = parts[i];
|
|
@@ -2027,12 +2027,12 @@ var Node2 = class _Node2 {
|
|
|
2027
2027
|
}
|
|
2028
2028
|
return handlerSets;
|
|
2029
2029
|
}
|
|
2030
|
-
search(method,
|
|
2030
|
+
search(method, path5) {
|
|
2031
2031
|
const handlerSets = [];
|
|
2032
2032
|
this.#params = emptyParams;
|
|
2033
2033
|
const curNode = this;
|
|
2034
2034
|
let curNodes = [curNode];
|
|
2035
|
-
const parts = splitPath(
|
|
2035
|
+
const parts = splitPath(path5);
|
|
2036
2036
|
const curNodesQueue = [];
|
|
2037
2037
|
for (let i = 0, len = parts.length; i < len; i++) {
|
|
2038
2038
|
const part = parts[i];
|
|
@@ -2120,18 +2120,18 @@ var TrieRouter = class {
|
|
|
2120
2120
|
constructor() {
|
|
2121
2121
|
this.#node = new Node2();
|
|
2122
2122
|
}
|
|
2123
|
-
add(method,
|
|
2124
|
-
const results = checkOptionalParameter(
|
|
2123
|
+
add(method, path5, handler) {
|
|
2124
|
+
const results = checkOptionalParameter(path5);
|
|
2125
2125
|
if (results) {
|
|
2126
2126
|
for (let i = 0, len = results.length; i < len; i++) {
|
|
2127
2127
|
this.#node.insert(method, results[i], handler);
|
|
2128
2128
|
}
|
|
2129
2129
|
return;
|
|
2130
2130
|
}
|
|
2131
|
-
this.#node.insert(method,
|
|
2131
|
+
this.#node.insert(method, path5, handler);
|
|
2132
2132
|
}
|
|
2133
|
-
match(method,
|
|
2134
|
-
return this.#node.search(method,
|
|
2133
|
+
match(method, path5) {
|
|
2134
|
+
return this.#node.search(method, path5);
|
|
2135
2135
|
}
|
|
2136
2136
|
};
|
|
2137
2137
|
|
|
@@ -2150,12 +2150,21 @@ var Hono2 = class extends Hono {
|
|
|
2150
2150
|
}
|
|
2151
2151
|
};
|
|
2152
2152
|
|
|
2153
|
+
// src/server/createApp.ts
|
|
2154
|
+
import { contextMiddleware } from "@cloudwerk/core";
|
|
2155
|
+
import { setActiveRenderer, getAvailableRenderers } from "@cloudwerk/ui";
|
|
2156
|
+
|
|
2157
|
+
// src/server/registerRoutes.ts
|
|
2158
|
+
import * as path3 from "path";
|
|
2159
|
+
import { createHandlerAdapter, setRouteConfig } from "@cloudwerk/core";
|
|
2160
|
+
|
|
2153
2161
|
// src/server/loadHandler.ts
|
|
2154
2162
|
import * as fs from "fs";
|
|
2155
2163
|
import * as path from "path";
|
|
2156
2164
|
import { builtinModules } from "module";
|
|
2157
2165
|
import { build } from "esbuild";
|
|
2158
2166
|
import { pathToFileURL } from "url";
|
|
2167
|
+
import { validateRouteConfig } from "@cloudwerk/core";
|
|
2159
2168
|
var moduleCache = /* @__PURE__ */ new Map();
|
|
2160
2169
|
async function loadRouteHandler(absolutePath, verbose = false) {
|
|
2161
2170
|
try {
|
|
@@ -2193,7 +2202,15 @@ async function loadRouteHandler(absolutePath, verbose = false) {
|
|
|
2193
2202
|
const tempFile = path.join(tempDir, `.cloudwerk-route-${cacheKey}.mjs`);
|
|
2194
2203
|
fs.writeFileSync(tempFile, code);
|
|
2195
2204
|
try {
|
|
2196
|
-
const
|
|
2205
|
+
const rawModule = await import(pathToFileURL(tempFile).href);
|
|
2206
|
+
let validatedConfig = void 0;
|
|
2207
|
+
if ("config" in rawModule) {
|
|
2208
|
+
validatedConfig = validateRouteConfig(rawModule.config, absolutePath);
|
|
2209
|
+
}
|
|
2210
|
+
const module = {
|
|
2211
|
+
...rawModule,
|
|
2212
|
+
config: validatedConfig
|
|
2213
|
+
};
|
|
2197
2214
|
moduleCache.set(absolutePath, { module, mtime });
|
|
2198
2215
|
return module;
|
|
2199
2216
|
} finally {
|
|
@@ -2220,6 +2237,101 @@ function findSafeTempDir(filePath) {
|
|
|
2220
2237
|
return dir;
|
|
2221
2238
|
}
|
|
2222
2239
|
|
|
2240
|
+
// src/server/loadMiddleware.ts
|
|
2241
|
+
import * as fs2 from "fs";
|
|
2242
|
+
import * as path2 from "path";
|
|
2243
|
+
import { builtinModules as builtinModules2 } from "module";
|
|
2244
|
+
import { build as build2 } from "esbuild";
|
|
2245
|
+
import { pathToFileURL as pathToFileURL2 } from "url";
|
|
2246
|
+
import { createMiddlewareAdapter } from "@cloudwerk/core";
|
|
2247
|
+
var middlewareCache = /* @__PURE__ */ new Map();
|
|
2248
|
+
async function loadMiddlewareModule(absolutePath, verbose = false) {
|
|
2249
|
+
if (!fs2.existsSync(absolutePath)) {
|
|
2250
|
+
if (verbose) {
|
|
2251
|
+
console.warn(`Middleware not found: ${absolutePath}`);
|
|
2252
|
+
}
|
|
2253
|
+
return null;
|
|
2254
|
+
}
|
|
2255
|
+
try {
|
|
2256
|
+
const stat = fs2.statSync(absolutePath);
|
|
2257
|
+
const mtime = stat.mtimeMs;
|
|
2258
|
+
const cached = middlewareCache.get(absolutePath);
|
|
2259
|
+
if (cached && cached.mtime === mtime) {
|
|
2260
|
+
return extractMiddleware(cached.module, absolutePath, verbose);
|
|
2261
|
+
}
|
|
2262
|
+
const nodeVersion = process.versions.node.split(".")[0];
|
|
2263
|
+
const target = `node${nodeVersion}`;
|
|
2264
|
+
const result = await build2({
|
|
2265
|
+
entryPoints: [absolutePath],
|
|
2266
|
+
bundle: true,
|
|
2267
|
+
write: false,
|
|
2268
|
+
format: "esm",
|
|
2269
|
+
platform: "node",
|
|
2270
|
+
target,
|
|
2271
|
+
external: [
|
|
2272
|
+
"@cloudwerk/core",
|
|
2273
|
+
"hono",
|
|
2274
|
+
// Use builtinModules for comprehensive Node.js built-in coverage
|
|
2275
|
+
...builtinModules2,
|
|
2276
|
+
...builtinModules2.map((m) => `node:${m}`)
|
|
2277
|
+
],
|
|
2278
|
+
logLevel: verbose ? "warning" : "silent",
|
|
2279
|
+
sourcemap: "inline"
|
|
2280
|
+
});
|
|
2281
|
+
if (!result.outputFiles || result.outputFiles.length === 0) {
|
|
2282
|
+
throw new Error("No output from esbuild");
|
|
2283
|
+
}
|
|
2284
|
+
const cacheKey = `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
2285
|
+
const tempDir = findSafeTempDir2(absolutePath);
|
|
2286
|
+
const tempFile = path2.join(tempDir, `.cloudwerk-middleware-${cacheKey}.mjs`);
|
|
2287
|
+
fs2.writeFileSync(tempFile, result.outputFiles[0].text);
|
|
2288
|
+
try {
|
|
2289
|
+
const module = await import(pathToFileURL2(tempFile).href);
|
|
2290
|
+
middlewareCache.set(absolutePath, { module, mtime });
|
|
2291
|
+
return extractMiddleware(module, absolutePath, verbose);
|
|
2292
|
+
} finally {
|
|
2293
|
+
try {
|
|
2294
|
+
fs2.unlinkSync(tempFile);
|
|
2295
|
+
} catch {
|
|
2296
|
+
}
|
|
2297
|
+
}
|
|
2298
|
+
} catch (error) {
|
|
2299
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2300
|
+
console.error(`Failed to compile middleware at ${absolutePath}: ${message}`);
|
|
2301
|
+
return null;
|
|
2302
|
+
}
|
|
2303
|
+
}
|
|
2304
|
+
function extractMiddleware(module, absolutePath, verbose) {
|
|
2305
|
+
const middleware = module.default ?? module.middleware;
|
|
2306
|
+
if (!middleware) {
|
|
2307
|
+
if (verbose) {
|
|
2308
|
+
console.warn(
|
|
2309
|
+
`Middleware at ${absolutePath} must export a default function or named 'middleware' export`
|
|
2310
|
+
);
|
|
2311
|
+
}
|
|
2312
|
+
return null;
|
|
2313
|
+
}
|
|
2314
|
+
if (typeof middleware !== "function") {
|
|
2315
|
+
console.warn(
|
|
2316
|
+
`Middleware in ${absolutePath} must export a function, got ${typeof middleware}`
|
|
2317
|
+
);
|
|
2318
|
+
return null;
|
|
2319
|
+
}
|
|
2320
|
+
return createMiddlewareAdapter(middleware);
|
|
2321
|
+
}
|
|
2322
|
+
function findSafeTempDir2(filePath) {
|
|
2323
|
+
let dir = path2.dirname(filePath);
|
|
2324
|
+
const hasSpecialChars = (p) => /\[|\]|\(|\)/.test(path2.basename(p));
|
|
2325
|
+
while (hasSpecialChars(dir)) {
|
|
2326
|
+
const parent = path2.dirname(dir);
|
|
2327
|
+
if (parent === dir) {
|
|
2328
|
+
break;
|
|
2329
|
+
}
|
|
2330
|
+
dir = parent;
|
|
2331
|
+
}
|
|
2332
|
+
return dir;
|
|
2333
|
+
}
|
|
2334
|
+
|
|
2223
2335
|
// src/server/registerRoutes.ts
|
|
2224
2336
|
var HTTP_METHODS = [
|
|
2225
2337
|
"GET",
|
|
@@ -2230,6 +2342,15 @@ var HTTP_METHODS = [
|
|
|
2230
2342
|
"OPTIONS",
|
|
2231
2343
|
"HEAD"
|
|
2232
2344
|
];
|
|
2345
|
+
function isCloudwerkHandler(fn) {
|
|
2346
|
+
return typeof fn === "function" && fn.length === 2;
|
|
2347
|
+
}
|
|
2348
|
+
function createConfigMiddleware(config) {
|
|
2349
|
+
return async (c, next) => {
|
|
2350
|
+
setRouteConfig(config);
|
|
2351
|
+
await next();
|
|
2352
|
+
};
|
|
2353
|
+
}
|
|
2233
2354
|
async function registerRoutes(app, manifest, logger, verbose = false) {
|
|
2234
2355
|
const registeredRoutes = [];
|
|
2235
2356
|
for (const route of manifest.routes) {
|
|
@@ -2238,8 +2359,23 @@ async function registerRoutes(app, manifest, logger, verbose = false) {
|
|
|
2238
2359
|
continue;
|
|
2239
2360
|
}
|
|
2240
2361
|
try {
|
|
2362
|
+
for (const middlewarePath of route.middleware) {
|
|
2363
|
+
const middlewareHandler = await loadMiddlewareModule(middlewarePath, verbose);
|
|
2364
|
+
if (middlewareHandler) {
|
|
2365
|
+
app.use(route.urlPattern, middlewareHandler);
|
|
2366
|
+
if (verbose) {
|
|
2367
|
+
logger.info(`Applied middleware: ${path3.basename(middlewarePath)} -> ${route.urlPattern}`);
|
|
2368
|
+
}
|
|
2369
|
+
}
|
|
2370
|
+
}
|
|
2241
2371
|
logger.debug(`Loading route handler: ${route.filePath}`);
|
|
2242
2372
|
const module = await loadRouteHandler(route.absolutePath, verbose);
|
|
2373
|
+
if (module.config) {
|
|
2374
|
+
app.use(route.urlPattern, createConfigMiddleware(module.config));
|
|
2375
|
+
if (verbose) {
|
|
2376
|
+
logger.info(`Applied route config: ${route.filePath} -> ${route.urlPattern}`);
|
|
2377
|
+
}
|
|
2378
|
+
}
|
|
2243
2379
|
for (const method of HTTP_METHODS) {
|
|
2244
2380
|
const handler = module[method];
|
|
2245
2381
|
if (handler && typeof handler === "function") {
|
|
@@ -2260,7 +2396,7 @@ async function registerRoutes(app, manifest, logger, verbose = false) {
|
|
|
2260
2396
|
return registeredRoutes;
|
|
2261
2397
|
}
|
|
2262
2398
|
function registerMethod(app, method, pattern, handler) {
|
|
2263
|
-
const h = handler;
|
|
2399
|
+
const h = isCloudwerkHandler(handler) ? createHandlerAdapter(handler) : handler;
|
|
2264
2400
|
switch (method) {
|
|
2265
2401
|
case "GET":
|
|
2266
2402
|
app.get(pattern, h);
|
|
@@ -2297,9 +2433,23 @@ var HTTP_STATUS = {
|
|
|
2297
2433
|
|
|
2298
2434
|
// src/server/createApp.ts
|
|
2299
2435
|
async function createApp(manifest, config, logger, verbose = false) {
|
|
2436
|
+
const rendererName = config.ui?.renderer ?? "hono-jsx";
|
|
2437
|
+
try {
|
|
2438
|
+
setActiveRenderer(rendererName);
|
|
2439
|
+
if (verbose) {
|
|
2440
|
+
logger.info(`Using UI renderer: ${rendererName}`);
|
|
2441
|
+
}
|
|
2442
|
+
} catch (error) {
|
|
2443
|
+
const available = getAvailableRenderers().join(", ");
|
|
2444
|
+
throw new Error(
|
|
2445
|
+
`Failed to initialize UI renderer "${rendererName}". Available renderers: ${available}.`,
|
|
2446
|
+
{ cause: error }
|
|
2447
|
+
);
|
|
2448
|
+
}
|
|
2300
2449
|
const app = new Hono2({
|
|
2301
2450
|
strict: false
|
|
2302
2451
|
});
|
|
2452
|
+
app.use("*", contextMiddleware());
|
|
2303
2453
|
if (verbose) {
|
|
2304
2454
|
app.use("*", async (c, next) => {
|
|
2305
2455
|
const start = Date.now();
|
|
@@ -2350,8 +2500,8 @@ async function dev(pathArg, options) {
|
|
|
2350
2500
|
const verbose = options.verbose ?? false;
|
|
2351
2501
|
const logger = createLogger(verbose);
|
|
2352
2502
|
try {
|
|
2353
|
-
const cwd = pathArg ?
|
|
2354
|
-
if (!
|
|
2503
|
+
const cwd = pathArg ? path4.resolve(process.cwd(), pathArg) : process.cwd();
|
|
2504
|
+
if (!fs3.existsSync(cwd)) {
|
|
2355
2505
|
throw new CliError(
|
|
2356
2506
|
`Directory does not exist: ${cwd}`,
|
|
2357
2507
|
"ENOENT",
|
|
@@ -2364,7 +2514,7 @@ async function dev(pathArg, options) {
|
|
|
2364
2514
|
logger.debug(`Config loaded: routesDir=${config.routesDir}, extensions=${config.extensions.join(", ")}`);
|
|
2365
2515
|
const routesDir = resolveRoutesDir(config, cwd);
|
|
2366
2516
|
logger.debug(`Routes directory: ${routesDir}`);
|
|
2367
|
-
if (!
|
|
2517
|
+
if (!fs3.existsSync(routesDir)) {
|
|
2368
2518
|
throw new CliError(
|
|
2369
2519
|
`Routes directory does not exist: ${routesDir}`,
|
|
2370
2520
|
"ENOENT",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudwerk/cli",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Dev server, build, deploy commands for Cloudwerk",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"commander": "^12.1.0",
|
|
26
26
|
"esbuild": "^0.25.0",
|
|
27
27
|
"picocolors": "^1.1.0",
|
|
28
|
-
"@cloudwerk/core": "^0.0
|
|
28
|
+
"@cloudwerk/core": "^0.1.0",
|
|
29
|
+
"@cloudwerk/ui": "^0.1.0"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@types/node": "^20.0.0",
|