Initial boiler plate project

This commit is contained in:
2024-09-24 03:52:46 +00:00
parent 6120b2d6c3
commit 154b93e267
10034 changed files with 2079352 additions and 2 deletions

View File

@ -0,0 +1,17 @@
/// <reference types="node" />
import type { AsyncLocalStorage } from 'async_hooks';
/**
* Implementations provide a wrapping function that will provide the storage to
* async calls derived from the provided callback function.
*/
export interface AsyncStorageWrapper<Store extends {}, Context extends {}> {
/**
* Wraps the callback with the underlying storage.
*
* @param storage underlying storage object
* @param context context used to create the storage object
* @param callback function to call within the scope of the storage
* @returns the result of the callback
*/
wrap<Result>(storage: AsyncLocalStorage<Store>, context: Context, callback: (store: Store) => Result): Result;
}

View File

@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
//# sourceMappingURL=async-storage-wrapper.js.map

View File

@ -0,0 +1 @@
{"version":3,"sources":[],"names":[],"mappings":""}

View File

@ -0,0 +1,13 @@
/// <reference types="node" />
import type { IncomingMessage } from 'http';
import type { ReadonlyRequestCookies } from '../web/spec-extension/adapters/request-cookies';
import type { ResponseCookies } from '../web/spec-extension/cookies';
import type { BaseNextRequest } from '../base-http';
import type { NextRequest } from '../web/spec-extension/request';
import type { __ApiPreviewProps } from '../api-utils';
export declare class DraftModeProvider {
readonly isEnabled: boolean;
constructor(previewProps: __ApiPreviewProps | undefined, req: IncomingMessage | BaseNextRequest<unknown> | NextRequest, cookies: ReadonlyRequestCookies, mutableCookies: ResponseCookies);
enable(): void;
disable(): void;
}

View File

@ -0,0 +1,53 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "DraftModeProvider", {
enumerable: true,
get: function() {
return DraftModeProvider;
}
});
const _apiutils = require("../api-utils");
class DraftModeProvider {
constructor(previewProps, req, cookies, mutableCookies){
var _cookies_get;
// The logic for draftMode() is very similar to tryGetPreviewData()
// but Draft Mode does not have any data associated with it.
const isOnDemandRevalidate = previewProps && (0, _apiutils.checkIsOnDemandRevalidate)(req, previewProps).isOnDemandRevalidate;
const cookieValue = (_cookies_get = cookies.get(_apiutils.COOKIE_NAME_PRERENDER_BYPASS)) == null ? void 0 : _cookies_get.value;
this.isEnabled = Boolean(!isOnDemandRevalidate && cookieValue && previewProps && (cookieValue === previewProps.previewModeId || // In dev mode, the cookie can be actual hash value preview id but the preview props can still be `development-id`.
process.env.NODE_ENV !== "production" && previewProps.previewModeId === "development-id"));
this._previewModeId = previewProps == null ? void 0 : previewProps.previewModeId;
this._mutableCookies = mutableCookies;
}
enable() {
if (!this._previewModeId) {
throw new Error("Invariant: previewProps missing previewModeId this should never happen");
}
this._mutableCookies.set({
name: _apiutils.COOKIE_NAME_PRERENDER_BYPASS,
value: this._previewModeId,
httpOnly: true,
sameSite: process.env.NODE_ENV !== "development" ? "none" : "lax",
secure: process.env.NODE_ENV !== "development",
path: "/"
});
}
disable() {
// To delete a cookie, set `expires` to a date in the past:
// https://tools.ietf.org/html/rfc6265#section-4.1.1
// `Max-Age: 0` is not valid, thus ignored, and the cookie is persisted.
this._mutableCookies.set({
name: _apiutils.COOKIE_NAME_PRERENDER_BYPASS,
value: "",
httpOnly: true,
sameSite: process.env.NODE_ENV !== "development" ? "none" : "lax",
secure: process.env.NODE_ENV !== "development",
path: "/",
expires: new Date(0)
});
}
}
//# sourceMappingURL=draft-mode-provider.js.map

View File

@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/async-storage/draft-mode-provider.ts"],"names":["DraftModeProvider","constructor","previewProps","req","cookies","mutableCookies","isOnDemandRevalidate","checkIsOnDemandRevalidate","cookieValue","get","COOKIE_NAME_PRERENDER_BYPASS","value","isEnabled","Boolean","previewModeId","process","env","NODE_ENV","_previewModeId","_mutableCookies","enable","Error","set","name","httpOnly","sameSite","secure","path","disable","expires","Date"],"mappings":";;;;+BAYaA;;;eAAAA;;;0BAHN;AAGA,MAAMA;IAaXC,YACEC,YAA2C,EAC3CC,GAA6D,EAC7DC,OAA+B,EAC/BC,cAA+B,CAC/B;YAOoBD;QANpB,mEAAmE;QACnE,4DAA4D;QAC5D,MAAME,uBACJJ,gBACAK,IAAAA,mCAAyB,EAACJ,KAAKD,cAAcI,oBAAoB;QAEnE,MAAME,eAAcJ,eAAAA,QAAQK,GAAG,CAACC,sCAA4B,sBAAxCN,aAA2CO,KAAK;QAEpE,IAAI,CAACC,SAAS,GAAGC,QACf,CAACP,wBACCE,eACAN,gBACCM,CAAAA,gBAAgBN,aAAaY,aAAa,IACzC,mHAAmH;QAClHC,QAAQC,GAAG,CAACC,QAAQ,KAAK,gBACxBf,aAAaY,aAAa,KAAK,gBAAgB;QAGvD,IAAI,CAACI,cAAc,GAAGhB,gCAAAA,aAAcY,aAAa;QACjD,IAAI,CAACK,eAAe,GAAGd;IACzB;IAEAe,SAAS;QACP,IAAI,CAAC,IAAI,CAACF,cAAc,EAAE;YACxB,MAAM,IAAIG,MACR;QAEJ;QAEA,IAAI,CAACF,eAAe,CAACG,GAAG,CAAC;YACvBC,MAAMb,sCAA4B;YAClCC,OAAO,IAAI,CAACO,cAAc;YAC1BM,UAAU;YACVC,UAAUV,QAAQC,GAAG,CAACC,QAAQ,KAAK,gBAAgB,SAAS;YAC5DS,QAAQX,QAAQC,GAAG,CAACC,QAAQ,KAAK;YACjCU,MAAM;QACR;IACF;IAEAC,UAAU;QACR,2DAA2D;QAC3D,oDAAoD;QACpD,wEAAwE;QACxE,IAAI,CAACT,eAAe,CAACG,GAAG,CAAC;YACvBC,MAAMb,sCAA4B;YAClCC,OAAO;YACPa,UAAU;YACVC,UAAUV,QAAQC,GAAG,CAACC,QAAQ,KAAK,gBAAgB,SAAS;YAC5DS,QAAQX,QAAQC,GAAG,CAACC,QAAQ,KAAK;YACjCU,MAAM;YACNE,SAAS,IAAIC,KAAK;QACpB;IACF;AACF"}

View File

@ -0,0 +1,13 @@
/// <reference types="node" />
import type { BaseNextRequest, BaseNextResponse } from '../base-http';
import type { IncomingMessage, ServerResponse } from 'http';
import type { RequestStore } from '../../client/components/request-async-storage.external';
import type { RenderOpts } from '../app-render/types';
import type { AsyncStorageWrapper } from './async-storage-wrapper';
import type { NextRequest } from '../web/spec-extension/request';
export type RequestContext = {
req: IncomingMessage | BaseNextRequest | NextRequest;
res?: ServerResponse | BaseNextResponse;
renderOpts?: RenderOpts;
};
export declare const RequestAsyncStorageWrapper: AsyncStorageWrapper<RequestStore, RequestContext>;

View File

@ -0,0 +1,109 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "RequestAsyncStorageWrapper", {
enumerable: true,
get: function() {
return RequestAsyncStorageWrapper;
}
});
const _approuterheaders = require("../../client/components/app-router-headers");
const _headers = require("../web/spec-extension/adapters/headers");
const _requestcookies = require("../web/spec-extension/adapters/request-cookies");
const _cookies = require("../web/spec-extension/cookies");
const _draftmodeprovider = require("./draft-mode-provider");
const _utils = require("../web/utils");
function getHeaders(headers) {
const cleaned = _headers.HeadersAdapter.from(headers);
for (const param of _approuterheaders.FLIGHT_PARAMETERS){
cleaned.delete(param.toString().toLowerCase());
}
return _headers.HeadersAdapter.seal(cleaned);
}
function getMutableCookies(headers, onUpdateCookies) {
const cookies = new _cookies.RequestCookies(_headers.HeadersAdapter.from(headers));
return _requestcookies.MutableRequestCookiesAdapter.wrap(cookies, onUpdateCookies);
}
/**
* If middleware set cookies in this request (indicated by `x-middleware-set-cookie`),
* then merge those into the existing cookie object, so that when `cookies()` is accessed
* it's able to read the newly set cookies.
*/ function mergeMiddlewareCookies(req, existingCookies) {
if ("x-middleware-set-cookie" in req.headers && typeof req.headers["x-middleware-set-cookie"] === "string") {
const setCookieValue = req.headers["x-middleware-set-cookie"];
const responseHeaders = new Headers();
for (const cookie of (0, _utils.splitCookiesString)(setCookieValue)){
responseHeaders.append("set-cookie", cookie);
}
const responseCookies = new _cookies.ResponseCookies(responseHeaders);
// Transfer cookies from ResponseCookies to RequestCookies
for (const cookie of responseCookies.getAll()){
existingCookies.set(cookie);
}
}
}
const RequestAsyncStorageWrapper = {
/**
* Wrap the callback with the given store so it can access the underlying
* store using hooks.
*
* @param storage underlying storage object returned by the module
* @param context context to seed the store
* @param callback function to call within the scope of the context
* @returns the result returned by the callback
*/ wrap (storage, { req, res, renderOpts }, callback) {
let previewProps = undefined;
if (renderOpts && "previewProps" in renderOpts) {
// TODO: investigate why previewProps isn't on RenderOpts
previewProps = renderOpts.previewProps;
}
function defaultOnUpdateCookies(cookies) {
if (res) {
res.setHeader("Set-Cookie", cookies);
}
}
const cache = {};
const store = {
get headers () {
if (!cache.headers) {
// Seal the headers object that'll freeze out any methods that could
// mutate the underlying data.
cache.headers = getHeaders(req.headers);
}
return cache.headers;
},
get cookies () {
if (!cache.cookies) {
// if middleware is setting cookie(s), then include those in
// the initial cached cookies so they can be read in render
const requestCookies = new _cookies.RequestCookies(_headers.HeadersAdapter.from(req.headers));
mergeMiddlewareCookies(req, requestCookies);
// Seal the cookies object that'll freeze out any methods that could
// mutate the underlying data.
cache.cookies = _requestcookies.RequestCookiesAdapter.seal(requestCookies);
}
return cache.cookies;
},
get mutableCookies () {
if (!cache.mutableCookies) {
const mutableCookies = getMutableCookies(req.headers, (renderOpts == null ? void 0 : renderOpts.onUpdateCookies) || (res ? defaultOnUpdateCookies : undefined));
mergeMiddlewareCookies(req, mutableCookies);
cache.mutableCookies = mutableCookies;
}
return cache.mutableCookies;
},
get draftMode () {
if (!cache.draftMode) {
cache.draftMode = new _draftmodeprovider.DraftModeProvider(previewProps, req, this.cookies, this.mutableCookies);
}
return cache.draftMode;
},
reactLoadableManifest: (renderOpts == null ? void 0 : renderOpts.reactLoadableManifest) || {},
assetPrefix: (renderOpts == null ? void 0 : renderOpts.assetPrefix) || ""
};
return storage.run(store, callback, store);
}
};
//# sourceMappingURL=request-async-storage-wrapper.js.map

View File

@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/async-storage/request-async-storage-wrapper.ts"],"names":["RequestAsyncStorageWrapper","getHeaders","headers","cleaned","HeadersAdapter","from","param","FLIGHT_PARAMETERS","delete","toString","toLowerCase","seal","getMutableCookies","onUpdateCookies","cookies","RequestCookies","MutableRequestCookiesAdapter","wrap","mergeMiddlewareCookies","req","existingCookies","setCookieValue","responseHeaders","Headers","cookie","splitCookiesString","append","responseCookies","ResponseCookies","getAll","set","storage","res","renderOpts","callback","previewProps","undefined","defaultOnUpdateCookies","setHeader","cache","store","requestCookies","RequestCookiesAdapter","mutableCookies","draftMode","DraftModeProvider","reactLoadableManifest","assetPrefix","run"],"mappings":";;;;+BA2EaA;;;eAAAA;;;kCAlEqB;yBAI3B;gCAKA;yBACyC;mCACd;uBACC;AAEnC,SAASC,WAAWC,OAAsC;IACxD,MAAMC,UAAUC,uBAAc,CAACC,IAAI,CAACH;IACpC,KAAK,MAAMI,SAASC,mCAAiB,CAAE;QACrCJ,QAAQK,MAAM,CAACF,MAAMG,QAAQ,GAAGC,WAAW;IAC7C;IAEA,OAAON,uBAAc,CAACO,IAAI,CAACR;AAC7B;AAEA,SAASS,kBACPV,OAAsC,EACtCW,eAA6C;IAE7C,MAAMC,UAAU,IAAIC,uBAAc,CAACX,uBAAc,CAACC,IAAI,CAACH;IACvD,OAAOc,4CAA4B,CAACC,IAAI,CAACH,SAASD;AACpD;AAQA;;;;CAIC,GACD,SAASK,uBACPC,GAA0B,EAC1BC,eAAiD;IAEjD,IACE,6BAA6BD,IAAIjB,OAAO,IACxC,OAAOiB,IAAIjB,OAAO,CAAC,0BAA0B,KAAK,UAClD;QACA,MAAMmB,iBAAiBF,IAAIjB,OAAO,CAAC,0BAA0B;QAC7D,MAAMoB,kBAAkB,IAAIC;QAE5B,KAAK,MAAMC,UAAUC,IAAAA,yBAAkB,EAACJ,gBAAiB;YACvDC,gBAAgBI,MAAM,CAAC,cAAcF;QACvC;QAEA,MAAMG,kBAAkB,IAAIC,wBAAe,CAACN;QAE5C,0DAA0D;QAC1D,KAAK,MAAME,UAAUG,gBAAgBE,MAAM,GAAI;YAC7CT,gBAAgBU,GAAG,CAACN;QACtB;IACF;AACF;AAEO,MAAMxB,6BAGT;IACF;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,SAAS,EAAE;oBACpBL,MAAMK,SAAS,GAAG,IAAIC,oCAAiB,CACrCV,cACAhB,KACA,IAAI,CAACL,OAAO,EACZ,IAAI,CAAC6B,cAAc;gBAEvB;gBAEA,OAAOJ,MAAMK,SAAS;YACxB;YACAE,uBAAuBb,CAAAA,8BAAAA,WAAYa,qBAAqB,KAAI,CAAC;YAC7DC,aAAad,CAAAA,8BAAAA,WAAYc,WAAW,KAAI;QAC1C;QAEA,OAAOhB,QAAQiB,GAAG,CAACR,OAAON,UAAUM;IACtC;AACF"}

View File

@ -0,0 +1,27 @@
import type { AsyncStorageWrapper } from './async-storage-wrapper';
import type { StaticGenerationStore } from '../../client/components/static-generation-async-storage.external';
import type { IncrementalCache } from '../lib/incremental-cache';
import type { RenderOptsPartial } from '../app-render/types';
import type { FetchMetric } from '../base-http';
export type StaticGenerationContext = {
urlPathname: string;
requestEndedState?: {
ended?: boolean;
};
renderOpts: {
incrementalCache?: IncrementalCache;
isOnDemandRevalidate?: boolean;
fetchCache?: StaticGenerationStore['fetchCache'];
isServerAction?: boolean;
waitUntil?: Promise<any>;
experimental: {
ppr: boolean;
missingSuspenseWithCSRBailout?: boolean;
};
/**
* Fetch metrics attached in patch-fetch.ts
**/
fetchMetrics?: FetchMetric[];
} & Pick<RenderOptsPartial, 'originalPathname' | 'supportsDynamicResponse' | 'isRevalidate' | 'nextExport' | 'isDraftMode' | 'isDebugPPRSkeleton'>;
};
export declare const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper<StaticGenerationStore, StaticGenerationContext>;

View File

@ -0,0 +1,53 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "StaticGenerationAsyncStorageWrapper", {
enumerable: true,
get: function() {
return StaticGenerationAsyncStorageWrapper;
}
});
const _dynamicrendering = require("../../server/app-render/dynamic-rendering");
const StaticGenerationAsyncStorageWrapper = {
wrap (storage, { urlPathname, renderOpts, requestEndedState }, callback) {
/**
* Rules of Static & Dynamic HTML:
*
* 1.) We must generate static HTML unless the caller explicitly opts
* in to dynamic HTML support.
*
* 2.) If dynamic HTML support is requested, we must honor that request
* or throw an error. It is the sole responsibility of the caller to
* ensure they aren't e.g. requesting dynamic HTML for an AMP page.
*
* 3.) If the request is in draft mode, we must generate dynamic HTML.
*
* 4.) If the request is a server action, we must generate dynamic HTML.
*
* These rules help ensure that other existing features like request caching,
* coalescing, and ISR continue working as intended.
*/ const isStaticGeneration = !renderOpts.supportsDynamicResponse && !renderOpts.isDraftMode && !renderOpts.isServerAction;
const prerenderState = isStaticGeneration && renderOpts.experimental.ppr ? (0, _dynamicrendering.createPrerenderState)(renderOpts.isDebugPPRSkeleton) : null;
const store = {
isStaticGeneration,
urlPathname,
pagePath: renderOpts.originalPathname,
incrementalCache: // we fallback to a global incremental cache for edge-runtime locally
// so that it can access the fs cache without mocks
renderOpts.incrementalCache || globalThis.__incrementalCache,
isRevalidate: renderOpts.isRevalidate,
isPrerendering: renderOpts.nextExport,
fetchCache: renderOpts.fetchCache,
isOnDemandRevalidate: renderOpts.isOnDemandRevalidate,
isDraftMode: renderOpts.isDraftMode,
prerenderState,
requestEndedState
};
// TODO: remove this when we resolve accessing the store outside the execution context
renderOpts.store = store;
return storage.run(store, callback, store);
}
};
//# sourceMappingURL=static-generation-async-storage-wrapper.js.map

View File

@ -0,0 +1 @@
{"version":3,"sources":["../../../src/server/async-storage/static-generation-async-storage-wrapper.ts"],"names":["StaticGenerationAsyncStorageWrapper","wrap","storage","urlPathname","renderOpts","requestEndedState","callback","isStaticGeneration","supportsDynamicResponse","isDraftMode","isServerAction","prerenderState","experimental","ppr","createPrerenderState","isDebugPPRSkeleton","store","pagePath","originalPathname","incrementalCache","globalThis","__incrementalCache","isRevalidate","isPrerendering","nextExport","fetchCache","isOnDemandRevalidate","run"],"mappings":";;;;+BA+CaA;;;eAAAA;;;kCAzCwB;AAyC9B,MAAMA,sCAGT;IACFC,MACEC,OAAiD,EACjD,EAAEC,WAAW,EAAEC,UAAU,EAAEC,iBAAiB,EAA2B,EACvEC,QAAkD;QAElD;;;;;;;;;;;;;;;;KAgBC,GACD,MAAMC,qBACJ,CAACH,WAAWI,uBAAuB,IACnC,CAACJ,WAAWK,WAAW,IACvB,CAACL,WAAWM,cAAc;QAE5B,MAAMC,iBACJJ,sBAAsBH,WAAWQ,YAAY,CAACC,GAAG,GAC7CC,IAAAA,sCAAoB,EAACV,WAAWW,kBAAkB,IAClD;QAEN,MAAMC,QAA+B;YACnCT;YACAJ;YACAc,UAAUb,WAAWc,gBAAgB;YACrCC,kBACE,qEAAqE;YACrE,mDAAmD;YACnDf,WAAWe,gBAAgB,IAAI,AAACC,WAAmBC,kBAAkB;YACvEC,cAAclB,WAAWkB,YAAY;YACrCC,gBAAgBnB,WAAWoB,UAAU;YACrCC,YAAYrB,WAAWqB,UAAU;YACjCC,sBAAsBtB,WAAWsB,oBAAoB;YAErDjB,aAAaL,WAAWK,WAAW;YAEnCE;YACAN;QACF;QAEA,sFAAsF;QACtFD,WAAWY,KAAK,GAAGA;QAEnB,OAAOd,QAAQyB,GAAG,CAACX,OAAOV,UAAUU;IACtC;AACF"}