@flight-framework/core 0.2.2 → 0.2.4

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 CHANGED
@@ -5,7 +5,7 @@ export { MetricsAggregator, createHttpObserver, createInstrumentedStream, create
5
5
  export { DEFAULT_BOT_PATTERNS, addStreamingHeaders, createConditionalStreamer, createStreamingResponse as createConditionalStreamingResponse, createStaticResponse, isBot, isSlowConnection, prefersNoStream, streamIf, supportsStreaming } from './chunk-XOIYNY4I.js';
6
6
  export { createHTMXStreamAdapter, createReactStreamAdapter, createSolidStreamAdapter, createStreamAdapter, createSvelteStreamAdapter, createVueStreamAdapter } from './chunk-MQQLYWZZ.js';
7
7
  export { createFileRouter, loadRoutes, scanRoutes } from './chunk-54HPVE7N.js';
8
- export { RedirectError, redirect as actionRedirect, cookies, executeAction, executeFormAction, getAction, handleActionRequest, isRedirectError, parseFormData, registerAction } from './chunk-JIW55ZVD.js';
8
+ export { RedirectError, redirect as actionRedirect, cookies, executeAction, executeFormAction, getAction, handleActionRequest, isRedirectError, parseFormData, registerAction } from './chunk-3QP3E7HS.js';
9
9
  export { createRouteContext, error, json, parseBody, redirect } from './chunk-W6D62JCI.js';
10
10
  export { createLazyContent, createStreamingResponse, createStreamingSSR, renderWithStreaming, streamParallel, streamSequential } from './chunk-RSVA2EYO.js';
11
11
  import './chunk-63SCEXD7.js';
@@ -22,12 +22,12 @@ import './chunk-K2CQZPCG.js';
22
22
  export { detectBoundaryType, hasUseClientDirective, hasUseServerDirective } from './chunk-PDW5WCMW.js';
23
23
  export { createServerContext, isNotFoundError, isRedirectError as isRscRedirectError, notFound, redirect as rscRedirect } from './chunk-62C7LX2E.js';
24
24
  import './chunk-ZVC3ZWLM.js';
25
- export { createCache } from './chunk-3AIQVGTM.js';
25
+ export { cacheKey, cached, createCache, dedupe, jsonSerializer, memory } from './chunk-R7SQAREQ.js';
26
26
  import './chunk-SUILH4ID.js';
27
- export { createServer } from './chunk-6LYV4VQX.js';
27
+ export { createServer } from './chunk-VOBQDQKX.js';
28
28
  export { createRouter } from './chunk-GCQZ4FHI.js';
29
29
  export { createMiddlewareChain } from './chunk-KWFX6WHG.js';
30
- export { defineConfig } from './chunk-3TPAA52K.js';
30
+ export { defineConfig } from './chunk-IXMD5QH2.js';
31
31
  import { promises } from 'fs';
32
32
  import { dirname, join } from 'path';
33
33
 
@@ -488,17 +488,17 @@ async function generateAllStaticPaths(pageModules) {
488
488
  }
489
489
  var pageCache = /* @__PURE__ */ new Map();
490
490
  async function getOrGeneratePage(path, generateFn, revalidate) {
491
- const cached = pageCache.get(path);
491
+ const cached2 = pageCache.get(path);
492
492
  const now = Date.now();
493
- if (cached) {
493
+ if (cached2) {
494
494
  if (revalidate === false) {
495
- return { html: cached.html, fromCache: true };
495
+ return { html: cached2.html, fromCache: true };
496
496
  }
497
- if (now < cached.revalidateAfter) {
498
- return { html: cached.html, fromCache: true };
497
+ if (now < cached2.revalidateAfter) {
498
+ return { html: cached2.html, fromCache: true };
499
499
  }
500
500
  regeneratePageInBackground(path, generateFn, revalidate || 60);
501
- return { html: cached.html, fromCache: true };
501
+ return { html: cached2.html, fromCache: true };
502
502
  }
503
503
  const html = await generateFn();
504
504
  if (revalidate !== false) {
@@ -875,9 +875,244 @@ function createDevToolsData(startTime, options = {}) {
875
875
  };
876
876
  }
877
877
 
878
+ // src/utils/env.ts
879
+ function isProduction() {
880
+ if (typeof globalThis !== "undefined" && "process" in globalThis) {
881
+ const proc = globalThis.process;
882
+ return proc?.env?.NODE_ENV === "production";
883
+ }
884
+ if (typeof import.meta !== "undefined" && "env" in import.meta) {
885
+ const env = import.meta.env;
886
+ return env?.MODE === "production" || env?.PROD === true;
887
+ }
888
+ return false;
889
+ }
890
+ function isDevelopment() {
891
+ if (typeof globalThis !== "undefined" && "process" in globalThis) {
892
+ const proc = globalThis.process;
893
+ return proc?.env?.NODE_ENV === "development";
894
+ }
895
+ if (typeof import.meta !== "undefined" && "env" in import.meta) {
896
+ const env = import.meta.env;
897
+ return env?.MODE === "development" || env?.DEV === true;
898
+ }
899
+ return true;
900
+ }
901
+ function isTest() {
902
+ if (typeof globalThis !== "undefined" && "process" in globalThis) {
903
+ const proc = globalThis.process;
904
+ return proc?.env?.NODE_ENV === "test";
905
+ }
906
+ return false;
907
+ }
908
+ function isServer() {
909
+ return typeof window === "undefined";
910
+ }
911
+ function isBrowser() {
912
+ return typeof window !== "undefined";
913
+ }
914
+ function getEnvironment() {
915
+ if (isProduction()) return "production";
916
+ if (isDevelopment()) return "development";
917
+ if (isTest()) return "test";
918
+ return "unknown";
919
+ }
920
+
921
+ // src/errors/index.ts
922
+ var FlightError = class extends Error {
923
+ /** HTTP status code */
924
+ statusCode;
925
+ /** Short status message */
926
+ statusMessage;
927
+ /** Additional error data */
928
+ data;
929
+ /** Whether this is a fatal error (shows full-screen) */
930
+ fatal;
931
+ /** Unique digest for production error correlation */
932
+ digest;
933
+ constructor(options) {
934
+ super(options.message || options.statusMessage || "An error occurred");
935
+ this.name = "FlightError";
936
+ this.statusCode = options.statusCode;
937
+ this.statusMessage = options.statusMessage || getDefaultStatusMessage(options.statusCode);
938
+ this.data = options.data;
939
+ this.fatal = options.fatal ?? false;
940
+ if (isProduction()) {
941
+ this.digest = generateDigest();
942
+ }
943
+ if (options.cause) {
944
+ this.cause = options.cause;
945
+ if (options.cause.stack) {
946
+ this.stack = `${this.stack}
947
+ Caused by: ${options.cause.stack}`;
948
+ }
949
+ }
950
+ }
951
+ /**
952
+ * Convert to plain object for serialization
953
+ */
954
+ toJSON() {
955
+ return {
956
+ name: this.name,
957
+ message: this.message,
958
+ statusCode: this.statusCode,
959
+ statusMessage: this.statusMessage,
960
+ data: this.data,
961
+ fatal: this.fatal,
962
+ digest: this.digest
963
+ };
964
+ }
965
+ };
966
+ var BadRequestError = class extends FlightError {
967
+ constructor(message, data) {
968
+ super({ statusCode: 400, message, data });
969
+ this.name = "BadRequestError";
970
+ }
971
+ };
972
+ var UnauthorizedError = class extends FlightError {
973
+ constructor(message, data) {
974
+ super({ statusCode: 401, message: message || "Unauthorized", data });
975
+ this.name = "UnauthorizedError";
976
+ }
977
+ };
978
+ var ForbiddenError = class extends FlightError {
979
+ constructor(message, data) {
980
+ super({ statusCode: 403, message: message || "Forbidden", data });
981
+ this.name = "ForbiddenError";
982
+ }
983
+ };
984
+ var NotFoundError = class extends FlightError {
985
+ constructor(message, data) {
986
+ super({ statusCode: 404, message: message || "Not Found", data });
987
+ this.name = "NotFoundError";
988
+ }
989
+ };
990
+ var InternalError = class extends FlightError {
991
+ constructor(message, data) {
992
+ super({ statusCode: 500, message: message || "Internal Server Error", data });
993
+ this.name = "InternalError";
994
+ }
995
+ };
996
+ function createError(options) {
997
+ if (typeof options === "string") {
998
+ return new FlightError({ statusCode: 500, message: options });
999
+ }
1000
+ return new FlightError(options);
1001
+ }
1002
+ function notFound2(message, data) {
1003
+ throw new NotFoundError(message, data);
1004
+ }
1005
+ function forbidden(message, data) {
1006
+ throw new ForbiddenError(message, data);
1007
+ }
1008
+ function unauthorized(message, data) {
1009
+ throw new UnauthorizedError(message, data);
1010
+ }
1011
+ function showError(error2) {
1012
+ let flightError;
1013
+ if (typeof error2 === "string") {
1014
+ flightError = new FlightError({ statusCode: 500, message: error2 });
1015
+ } else if (error2 instanceof FlightError) {
1016
+ flightError = error2;
1017
+ } else {
1018
+ flightError = new FlightError(error2);
1019
+ }
1020
+ if (typeof window !== "undefined") {
1021
+ window.__FLIGHT_ERROR__ = flightError;
1022
+ window.dispatchEvent(new CustomEvent("flight:error", {
1023
+ detail: flightError,
1024
+ bubbles: true
1025
+ }));
1026
+ }
1027
+ }
1028
+ function clearError(options) {
1029
+ if (typeof window !== "undefined") {
1030
+ window.__FLIGHT_ERROR__ = null;
1031
+ window.dispatchEvent(new CustomEvent("flight:error-clear", { bubbles: true }));
1032
+ if (options?.redirect) {
1033
+ window.location.href = options.redirect;
1034
+ }
1035
+ }
1036
+ }
1037
+ function getError() {
1038
+ if (typeof window !== "undefined") {
1039
+ return window.__FLIGHT_ERROR__ ?? null;
1040
+ }
1041
+ return null;
1042
+ }
1043
+ function isFlightError(error2) {
1044
+ return error2 instanceof FlightError;
1045
+ }
1046
+ function isNotFoundError2(error2) {
1047
+ return error2 instanceof NotFoundError || isFlightError(error2) && error2.statusCode === 404;
1048
+ }
1049
+ function isForbiddenError(error2) {
1050
+ return error2 instanceof ForbiddenError || isFlightError(error2) && error2.statusCode === 403;
1051
+ }
1052
+ function isUnauthorizedError(error2) {
1053
+ return error2 instanceof UnauthorizedError || isFlightError(error2) && error2.statusCode === 401;
1054
+ }
1055
+ function getErrorStatusCode(error2) {
1056
+ if (isFlightError(error2)) {
1057
+ return error2.statusCode;
1058
+ }
1059
+ return 500;
1060
+ }
1061
+ function createErrorResponse(error2) {
1062
+ const flightError = isFlightError(error2) ? error2 : new InternalError(error2 instanceof Error ? error2.message : "Unknown error");
1063
+ return new Response(
1064
+ JSON.stringify({
1065
+ error: flightError.statusMessage,
1066
+ message: flightError.message,
1067
+ statusCode: flightError.statusCode,
1068
+ digest: flightError.digest,
1069
+ ...isDevelopment() && flightError.data ? { data: flightError.data } : {}
1070
+ }),
1071
+ {
1072
+ status: flightError.statusCode,
1073
+ headers: {
1074
+ "Content-Type": "application/json"
1075
+ }
1076
+ }
1077
+ );
1078
+ }
1079
+ function getDefaultStatusMessage(statusCode) {
1080
+ const messages = {
1081
+ 400: "Bad Request",
1082
+ 401: "Unauthorized",
1083
+ 403: "Forbidden",
1084
+ 404: "Not Found",
1085
+ 405: "Method Not Allowed",
1086
+ 408: "Request Timeout",
1087
+ 409: "Conflict",
1088
+ 410: "Gone",
1089
+ 422: "Unprocessable Entity",
1090
+ 429: "Too Many Requests",
1091
+ 500: "Internal Server Error",
1092
+ 501: "Not Implemented",
1093
+ 502: "Bad Gateway",
1094
+ 503: "Service Unavailable",
1095
+ 504: "Gateway Timeout"
1096
+ };
1097
+ return messages[statusCode] || "Error";
1098
+ }
1099
+ function generateDigest() {
1100
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
1101
+ return crypto.randomUUID().slice(0, 8);
1102
+ }
1103
+ return Math.random().toString(36).slice(2, 10);
1104
+ }
1105
+ function wrapWithDigest(error2) {
1106
+ const wrapped = error2;
1107
+ if (!wrapped.digest) {
1108
+ wrapped.digest = generateDigest();
1109
+ }
1110
+ return wrapped;
1111
+ }
1112
+
878
1113
  // src/index.ts
879
1114
  var VERSION = "0.0.1";
880
1115
 
881
- export { VERSION, buildCacheControlHeader, createDefaultRouteRules, createDevToolsData, createHtmlStream, createISRCacheFromFlightCache, createMemoryISRCache, createRevalidateHandler, createStreamingResponse3 as createStaticStreamingResponse, expandDynamicRoutes, extractLinks, generateAllStaticPaths, getCacheOptionsFromRule, getISRCache, getOrGeneratePage, getRenderModeFromRule, getRevalidateTime, injectDevTools, isDevToolsEnabled, matchRouteRule, mergeMetadata, mergeRouteRules, prerenderRoutes, purgeAllPages, purgePage, renderMetadataToHead, resolveMetadata, revalidatePath2 as revalidatePath, revalidatePaths, revalidateTag2 as revalidateTag, setISRCache, shouldDynamicallyRender };
1116
+ export { BadRequestError, FlightError, ForbiddenError, InternalError, NotFoundError, UnauthorizedError, VERSION, buildCacheControlHeader, clearError, createDefaultRouteRules, createDevToolsData, createError, createErrorResponse, forbidden as createForbidden, createHtmlStream, createISRCacheFromFlightCache, createMemoryISRCache, notFound2 as createNotFound, createRevalidateHandler, createStreamingResponse3 as createStaticStreamingResponse, unauthorized as createUnauthorized, expandDynamicRoutes, extractLinks, generateAllStaticPaths, getCacheOptionsFromRule, getEnvironment, getError, getErrorStatusCode, getISRCache, getOrGeneratePage, getRenderModeFromRule, getRevalidateTime, injectDevTools, isBrowser, isDevToolsEnabled, isDevelopment, isFlightError, isForbiddenError as isForbidden, isNotFoundError2 as isNotFound, isProduction, isServer, isTest, isUnauthorizedError as isUnauthorized, matchRouteRule, mergeMetadata, mergeRouteRules, prerenderRoutes, purgeAllPages, purgePage, renderMetadataToHead, resolveMetadata, revalidatePath2 as revalidatePath, revalidatePaths, revalidateTag2 as revalidateTag, setISRCache, shouldDynamicallyRender, showError, wrapWithDigest };
882
1117
  //# sourceMappingURL=index.js.map
883
1118
  //# sourceMappingURL=index.js.map