resuxjs 0.2.32 → 0.2.34

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.
Files changed (45) hide show
  1. package/README.md +21 -0
  2. package/dist/bin.d.ts +2 -0
  3. package/dist/bin.js +4 -0
  4. package/dist/bin.js.map +1 -0
  5. package/dist/cli.d.ts +1 -1
  6. package/dist/cli.js +264 -87
  7. package/dist/cli.js.map +1 -1
  8. package/dist/compiler/index.js +5 -2
  9. package/dist/compiler/index.js.map +1 -1
  10. package/dist/create-bin.d.ts +2 -0
  11. package/dist/create-bin.js +4 -0
  12. package/dist/create-bin.js.map +1 -0
  13. package/dist/create.d.ts +0 -1
  14. package/dist/create.js +0 -1
  15. package/dist/create.js.map +1 -1
  16. package/dist/deploy/cloudflare.d.ts +2 -0
  17. package/dist/deploy/cloudflare.js +29 -0
  18. package/dist/deploy/cloudflare.js.map +1 -0
  19. package/dist/deploy/common.d.ts +17 -0
  20. package/dist/deploy/common.js +202 -0
  21. package/dist/deploy/common.js.map +1 -0
  22. package/dist/deploy/index.d.ts +3 -0
  23. package/dist/deploy/index.js +134 -0
  24. package/dist/deploy/index.js.map +1 -0
  25. package/dist/deploy/netlify.d.ts +2 -0
  26. package/dist/deploy/netlify.js +44 -0
  27. package/dist/deploy/netlify.js.map +1 -0
  28. package/dist/deploy/node.d.ts +2 -0
  29. package/dist/deploy/node.js +24 -0
  30. package/dist/deploy/node.js.map +1 -0
  31. package/dist/deploy/static.d.ts +2 -0
  32. package/dist/deploy/static.js +19 -0
  33. package/dist/deploy/static.js.map +1 -0
  34. package/dist/deploy/types.d.ts +34 -0
  35. package/dist/deploy/types.js +2 -0
  36. package/dist/deploy/types.js.map +1 -0
  37. package/dist/deploy/vercel.d.ts +2 -3
  38. package/dist/deploy/vercel.js +107 -104
  39. package/dist/deploy/vercel.js.map +1 -1
  40. package/dist/globals.d.ts +3 -2
  41. package/dist/runtime/index.d.ts +43 -1
  42. package/dist/runtime/index.js +1372 -151
  43. package/dist/runtime/index.js.map +1 -1
  44. package/package.json +5 -4
  45. package/templates/default/resux.config.ts +3 -0
package/README.md CHANGED
@@ -258,6 +258,27 @@ node .output/server/index.mjs
258
258
 
259
259
  The generated Nitro config is conservative for resumability: SSR pages, API routes, and route payloads use `cache-control: no-store`, while build-stable Resux runtime and handler assets use long immutable caching. Nitro prerender crawling is disabled by default.
260
260
 
261
+ Deployment target selection is automatic by default (`deploy.target = "auto"`). Resux resolves target/preset from:
262
+
263
+ - explicit `deploy.target` / `deploy.nitroPreset` in `resux.config.ts`
264
+ - `NITRO_PRESET` / `RESUX_NITRO_PRESET`
265
+ - provider environment variables
266
+ - project build config detection (`vercel.json`, `netlify.toml`, `wrangler.*`)
267
+ - package scripts heuristics
268
+
269
+ You can pin target behavior explicitly:
270
+
271
+ ```ts
272
+ export default defineResuxConfig({
273
+ deploy: {
274
+ target: "vercel", // "auto" | "node" | "netlify" | "cloudflare" | "static"
275
+ nitroPreset: "vercel"
276
+ }
277
+ })
278
+ ```
279
+
280
+ Cloudflare worker presets are opt-in via `deploy.target = "cloudflare"` (or explicit `deploy.nitroPreset`) so Node-style Nitro adapters do not get selected automatically in auto mode.
281
+
261
282
  Production and dev servers expose a health endpoint for hosts and uptime checks:
262
283
 
263
284
  ```txt
package/dist/bin.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/bin.js ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import { runResuxCli } from "./cli.js";
3
+ await runResuxCli(process.argv.slice(2));
4
+ //# sourceMappingURL=bin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvC,MAAM,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC"}
package/dist/cli.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env node
2
1
  import { type IncomingMessage, type ServerResponse } from "node:http";
3
2
  export * from "./runtime/index.js";
4
3
  export interface ResuxNodeHandlerOptions {
@@ -7,3 +6,4 @@ export interface ResuxNodeHandlerOptions {
7
6
  securityHeaders?: boolean;
8
7
  }
9
8
  export declare function createResuxNodeHandler(options?: ResuxNodeHandlerOptions): (request: IncomingMessage, response: ServerResponse) => void;
9
+ export declare function runResuxCli(args: string[]): Promise<void>;
package/dist/cli.js CHANGED
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env node
2
1
  import { spawn } from "node:child_process";
3
2
  import { readdirSync, statSync, watch } from "node:fs";
4
3
  import { createServer as createHttpServer, } from "node:http";
@@ -7,7 +6,7 @@ import { createRequire } from "node:module";
7
6
  import path from "node:path";
8
7
  import { fileURLToPath, pathToFileURL } from "node:url";
9
8
  import { runCreateResux } from "./create.js";
10
- import { patchNitroPublicClientAssets, patchVercelBuildOutput, shouldPreferVercelPreset, } from "./deploy/vercel.js";
9
+ import { applyDeploymentPostBuild, resolveDeployment, } from "./deploy/index.js";
11
10
  import { renderApp, renderDocument, } from "./runtime/index.js";
12
11
  export * from "./runtime/index.js";
13
12
  const require = createRequire(import.meta.url);
@@ -37,9 +36,9 @@ export function createResuxNodeHandler(options = {}) {
37
36
  };
38
37
  }
39
38
  if (isMainModule()) {
40
- await runCli(process.argv.slice(2));
39
+ await runResuxCli(process.argv.slice(2));
41
40
  }
42
- async function runCli(args) {
41
+ export async function runResuxCli(args) {
43
42
  const command = readCommand(args);
44
43
  const commandArgs = command === args[0] ? args.slice(1) : args;
45
44
  if (command === "help") {
@@ -70,7 +69,7 @@ async function runCli(args) {
70
69
  });
71
70
  console.log(`Built ${result.routes.length} route(s) into ${path.relative(process.cwd(), outDir)}`);
72
71
  await ensureNitroBuildFiles(appRoot);
73
- await runNitroBuild(appRoot);
72
+ await runNitroBuild(appRoot, outDir);
74
73
  }
75
74
  else if (command === "compile") {
76
75
  await runTailwindBuild(appRoot, tailwind, true);
@@ -487,87 +486,17 @@ async function ensureNitroBuildFiles(appRoot) {
487
486
  await writeDeploymentFile(appRoot, ".resux-nitro/handler.ts", createNitroHandler(), false);
488
487
  }
489
488
  }
490
- function normalizeProviderPreset(provider) {
491
- switch (provider) {
492
- case "aws_amplify":
493
- return "aws-amplify";
494
- case "azure_static":
495
- return "azure";
496
- case "cloudflare_pages":
497
- return "cloudflare-pages";
498
- case "netlify":
499
- return "netlify";
500
- case "render":
501
- return "render-com";
502
- case "stormkit":
503
- return "stormkit";
504
- case "vercel":
505
- return "vercel";
506
- case "cleavr":
507
- return "cleavr";
508
- case "zeabur":
509
- return "zeabur";
510
- default:
511
- return provider;
512
- }
513
- }
514
- function detectDeployProviderFromEnv(env) {
515
- if (env.NOW_BUILDER || env.VERCEL || env.VERCEL_ENV) {
516
- return "vercel";
517
- }
518
- if (env.NETLIFY && !env.NETLIFY_LOCAL) {
519
- return "netlify";
520
- }
521
- if (env.CF_PAGES || env.CLOUDFLARE_PAGES) {
522
- return "cloudflare_pages";
523
- }
524
- if (env.INPUT_AZURE_STATIC_WEB_APPS_API_TOKEN || env.AZURE_STATIC) {
525
- return "azure_static";
526
- }
527
- if (env.AWS_AMPLIFY || env.AWS_APP_ID) {
528
- return "aws_amplify";
529
- }
530
- if (env.STORMKIT) {
531
- return "stormkit";
532
- }
533
- if (env.RENDER) {
534
- return "render";
535
- }
536
- if (env.CLEAVR) {
537
- return "cleavr";
538
- }
539
- if (env.ZEABUR) {
540
- return "zeabur";
541
- }
542
- return null;
543
- }
544
- async function resolveNitroPreset(appRoot, env) {
545
- const explicitPreset = (env.NITRO_PRESET ?? env.RESUX_NITRO_PRESET)?.trim();
546
- if (explicitPreset) {
547
- return explicitPreset;
548
- }
549
- const provider = detectDeployProviderFromEnv(env);
550
- if (provider) {
551
- return normalizeProviderPreset(provider);
552
- }
553
- if (await shouldPreferVercelPreset(appRoot, env)) {
554
- return "vercel";
555
- }
556
- return null;
557
- }
558
- async function runNitroBuild(appRoot) {
489
+ async function runNitroBuild(appRoot, outDir) {
559
490
  const nitroPackageJson = require.resolve("nitropack/package.json");
560
491
  const nitroCli = path.join(path.dirname(nitroPackageJson), "dist", "cli", "index.mjs");
561
492
  const nitroEnv = { ...process.env };
562
- const resolvedPreset = await resolveNitroPreset(appRoot, nitroEnv);
563
- if (resolvedPreset && !nitroEnv.NITRO_PRESET) {
564
- nitroEnv.NITRO_PRESET = resolvedPreset;
565
- console.log(`Detected Nitro preset: ${resolvedPreset}`);
566
- }
567
- const outputLabel = resolvedPreset?.startsWith("vercel")
568
- ? ".vercel/output"
569
- : ".output";
570
- console.log(`Building Nitro output into ${outputLabel}`);
493
+ const deployment = await resolveDeployment(appRoot, outDir, nitroEnv);
494
+ if (deployment.nitroPreset && !nitroEnv.NITRO_PRESET) {
495
+ nitroEnv.NITRO_PRESET = deployment.nitroPreset;
496
+ console.log(`Detected deploy target: ${deployment.target} (${deployment.reason})`);
497
+ console.log(`Detected Nitro preset: ${deployment.nitroPreset}`);
498
+ }
499
+ console.log(`Building Nitro output into ${deployment.outputLabel}`);
571
500
  await new Promise((resolve, reject) => {
572
501
  const child = spawn(process.execPath, [nitroCli, "build"], {
573
502
  cwd: appRoot,
@@ -584,10 +513,13 @@ async function runNitroBuild(appRoot) {
584
513
  }
585
514
  });
586
515
  });
587
- await patchNitroPublicClientAssets(appRoot);
588
- if (resolvedPreset?.startsWith("vercel")) {
589
- await patchVercelBuildOutput(appRoot);
590
- }
516
+ await applyDeploymentPostBuild({
517
+ appRoot,
518
+ outDir,
519
+ env: nitroEnv,
520
+ nitroPreset: deployment.nitroPreset,
521
+ target: deployment.target,
522
+ });
591
523
  }
592
524
  async function assertDirectory(directory) {
593
525
  const stats = await stat(directory);
@@ -746,6 +678,11 @@ export default defineNitroConfig({
746
678
  "cache-control": "public, max-age=31536000, immutable"
747
679
  }
748
680
  },
681
+ "/__resux/image": {
682
+ headers: {
683
+ "cache-control": "public, max-age=31536000, immutable"
684
+ }
685
+ },
749
686
  "/api/**": {
750
687
  headers: {
751
688
  "cache-control": "no-store"
@@ -1052,6 +989,10 @@ async function handleRequest(request, response, options) {
1052
989
  await serveRoutePayload(response, requestUrl, options, requestOrigin);
1053
990
  return;
1054
991
  }
992
+ if (requestUrl.pathname === "/__resux/image") {
993
+ await serveResuxImage(request, response, requestUrl, requestOrigin);
994
+ return;
995
+ }
1055
996
  if (requestUrl.pathname.startsWith("/__resux/")) {
1056
997
  if (activeDevBuild) {
1057
998
  await activeDevBuild;
@@ -1284,6 +1225,242 @@ async function serveRoutePayload(response, requestUrl, options, requestOrigin) {
1284
1225
  });
1285
1226
  response.end(JSON.stringify(rendered));
1286
1227
  }
1228
+ const RESUX_IMAGE_SUPPORTED_FORMATS = new Set([
1229
+ "avif",
1230
+ "gif",
1231
+ "jpeg",
1232
+ "jpg",
1233
+ "png",
1234
+ "webp",
1235
+ ]);
1236
+ let sharpFactoryPromise = null;
1237
+ function parseImageInteger(value, min, max) {
1238
+ if (!value) {
1239
+ return undefined;
1240
+ }
1241
+ const parsed = Number.parseInt(value, 10);
1242
+ if (!Number.isFinite(parsed)) {
1243
+ return undefined;
1244
+ }
1245
+ return Math.min(max, Math.max(min, parsed));
1246
+ }
1247
+ function normalizeImageFormat(value) {
1248
+ if (!value) {
1249
+ return undefined;
1250
+ }
1251
+ const normalized = value.trim().toLowerCase();
1252
+ if (!normalized) {
1253
+ return undefined;
1254
+ }
1255
+ if (!RESUX_IMAGE_SUPPORTED_FORMATS.has(normalized)) {
1256
+ return undefined;
1257
+ }
1258
+ return normalized === "jpg" ? "jpeg" : normalized;
1259
+ }
1260
+ function normalizeImageFit(value) {
1261
+ if (!value) {
1262
+ return undefined;
1263
+ }
1264
+ const normalized = value.trim().toLowerCase();
1265
+ if (normalized === "cover"
1266
+ || normalized === "contain"
1267
+ || normalized === "fill"
1268
+ || normalized === "inside"
1269
+ || normalized === "outside") {
1270
+ return normalized;
1271
+ }
1272
+ return undefined;
1273
+ }
1274
+ function parseFormatFromContentType(contentType) {
1275
+ if (!contentType) {
1276
+ return undefined;
1277
+ }
1278
+ const normalized = contentType.toLowerCase();
1279
+ if (normalized.includes("image/jpeg")) {
1280
+ return "jpeg";
1281
+ }
1282
+ if (normalized.includes("image/png")) {
1283
+ return "png";
1284
+ }
1285
+ if (normalized.includes("image/webp")) {
1286
+ return "webp";
1287
+ }
1288
+ if (normalized.includes("image/avif")) {
1289
+ return "avif";
1290
+ }
1291
+ if (normalized.includes("image/gif")) {
1292
+ return "gif";
1293
+ }
1294
+ return undefined;
1295
+ }
1296
+ function mimeTypeFromImageFormat(format) {
1297
+ if (format === "jpeg") {
1298
+ return "image/jpeg";
1299
+ }
1300
+ return `image/${format}`;
1301
+ }
1302
+ function shouldTransformImage(options) {
1303
+ return Boolean(options.width
1304
+ || options.height
1305
+ || options.quality
1306
+ || options.format);
1307
+ }
1308
+ function resolveResuxImageSource(rawSource, requestOrigin) {
1309
+ const source = rawSource.trim();
1310
+ if (!source) {
1311
+ return null;
1312
+ }
1313
+ if (source.startsWith("/")) {
1314
+ return new URL(source, requestOrigin);
1315
+ }
1316
+ try {
1317
+ const parsed = new URL(source);
1318
+ if (parsed.protocol === "http:" || parsed.protocol === "https:") {
1319
+ return parsed;
1320
+ }
1321
+ }
1322
+ catch {
1323
+ // Ignore invalid absolute URLs.
1324
+ }
1325
+ return null;
1326
+ }
1327
+ async function loadSharpFactory() {
1328
+ if (sharpFactoryPromise) {
1329
+ return sharpFactoryPromise;
1330
+ }
1331
+ sharpFactoryPromise = (async () => {
1332
+ try {
1333
+ const sharpPath = require.resolve("sharp");
1334
+ const sharpModule = await import(pathToFileURL(sharpPath).href);
1335
+ const factory = sharpModule.default
1336
+ ?? sharpModule;
1337
+ return typeof factory === "function"
1338
+ ? factory
1339
+ : null;
1340
+ }
1341
+ catch {
1342
+ return null;
1343
+ }
1344
+ })();
1345
+ return sharpFactoryPromise;
1346
+ }
1347
+ async function transformResuxImage(source, contentType, options) {
1348
+ if (!shouldTransformImage(options)) {
1349
+ return null;
1350
+ }
1351
+ const sharp = await loadSharpFactory();
1352
+ if (!sharp) {
1353
+ return null;
1354
+ }
1355
+ const inputFormat = parseFormatFromContentType(contentType);
1356
+ const outputFormat = options.format
1357
+ ?? (options.quality && inputFormat ? inputFormat : undefined);
1358
+ try {
1359
+ let pipeline = sharp(source, { failOn: "none" });
1360
+ if (options.width || options.height) {
1361
+ pipeline = pipeline.resize({
1362
+ ...(options.width ? { width: options.width } : {}),
1363
+ ...(options.height ? { height: options.height } : {}),
1364
+ fit: options.fit ?? "cover",
1365
+ withoutEnlargement: true,
1366
+ });
1367
+ }
1368
+ if (outputFormat) {
1369
+ const quality = options.quality;
1370
+ const formatOptions = Number.isFinite(quality)
1371
+ ? { quality: Math.min(100, Math.max(1, quality)) }
1372
+ : undefined;
1373
+ pipeline = pipeline.toFormat(outputFormat, formatOptions);
1374
+ }
1375
+ const transformed = await pipeline.toBuffer();
1376
+ return {
1377
+ buffer: transformed,
1378
+ contentType: outputFormat
1379
+ ? mimeTypeFromImageFormat(outputFormat)
1380
+ : contentType ?? "application/octet-stream",
1381
+ };
1382
+ }
1383
+ catch {
1384
+ return null;
1385
+ }
1386
+ }
1387
+ async function serveResuxImage(request, response, requestUrl, requestOrigin) {
1388
+ const method = request.method ?? "GET";
1389
+ if (method !== "GET" && method !== "HEAD") {
1390
+ response.writeHead(405, {
1391
+ "content-type": "text/plain; charset=utf-8",
1392
+ allow: "GET, HEAD",
1393
+ });
1394
+ response.end("Method Not Allowed");
1395
+ return;
1396
+ }
1397
+ const sourceParam = requestUrl.searchParams.get("src")
1398
+ ?? requestUrl.searchParams.get("url")
1399
+ ?? "";
1400
+ const sourceUrl = resolveResuxImageSource(sourceParam, requestOrigin);
1401
+ if (!sourceUrl) {
1402
+ response.writeHead(400, {
1403
+ "content-type": "application/json; charset=utf-8",
1404
+ });
1405
+ response.end(JSON.stringify({
1406
+ error: 'Image requests require "src" (or "url") with an absolute path or http(s) URL.',
1407
+ }));
1408
+ return;
1409
+ }
1410
+ const options = {
1411
+ width: parseImageInteger(requestUrl.searchParams.get("w"), 1, 8192)
1412
+ ?? parseImageInteger(requestUrl.searchParams.get("width"), 1, 8192),
1413
+ height: parseImageInteger(requestUrl.searchParams.get("h"), 1, 8192)
1414
+ ?? parseImageInteger(requestUrl.searchParams.get("height"), 1, 8192),
1415
+ quality: parseImageInteger(requestUrl.searchParams.get("q"), 1, 100)
1416
+ ?? parseImageInteger(requestUrl.searchParams.get("quality"), 1, 100),
1417
+ format: normalizeImageFormat(requestUrl.searchParams.get("f"))
1418
+ ?? normalizeImageFormat(requestUrl.searchParams.get("format")),
1419
+ fit: normalizeImageFit(requestUrl.searchParams.get("fit"))
1420
+ ?? normalizeImageFit(requestUrl.searchParams.get("mode")),
1421
+ };
1422
+ const forwardedAccept = firstHeaderValue(request.headers.accept);
1423
+ const upstream = await fetch(sourceUrl, {
1424
+ headers: forwardedAccept ? { accept: forwardedAccept } : undefined,
1425
+ redirect: "follow",
1426
+ }).catch(() => null);
1427
+ if (!upstream) {
1428
+ response.writeHead(502, {
1429
+ "content-type": "application/json; charset=utf-8",
1430
+ "cache-control": "no-store",
1431
+ });
1432
+ response.end(JSON.stringify({ error: "Failed to fetch image source." }));
1433
+ return;
1434
+ }
1435
+ if (!upstream.ok) {
1436
+ response.writeHead(upstream.status, {
1437
+ "content-type": "application/json; charset=utf-8",
1438
+ "cache-control": "no-store",
1439
+ });
1440
+ response.end(JSON.stringify({
1441
+ error: `Image source request failed with status ${upstream.status}.`,
1442
+ }));
1443
+ return;
1444
+ }
1445
+ const sourceBuffer = Buffer.from(await upstream.arrayBuffer());
1446
+ const sourceContentType = upstream.headers.get("content-type");
1447
+ const transformed = await transformResuxImage(sourceBuffer, sourceContentType, options);
1448
+ const body = transformed?.buffer ?? sourceBuffer;
1449
+ const contentType = transformed?.contentType ?? sourceContentType ?? "application/octet-stream";
1450
+ const cacheControl = shouldTransformImage(options)
1451
+ ? "public, max-age=31536000, immutable"
1452
+ : "public, max-age=86400";
1453
+ response.writeHead(200, {
1454
+ "content-type": contentType,
1455
+ "cache-control": cacheControl,
1456
+ vary: "Accept",
1457
+ });
1458
+ if (method === "HEAD") {
1459
+ response.end();
1460
+ return;
1461
+ }
1462
+ response.end(body);
1463
+ }
1287
1464
  function applyDefaultSecurityHeaders(response, enabled) {
1288
1465
  if (!enabled) {
1289
1466
  return;