create project

This commit is contained in:
ismailsosic
2022-12-27 12:05:56 +01:00
parent 2a33a2d3de
commit cd2143287c
16035 changed files with 2489703 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './';
export default class FetchCache implements CacheHandler {
private headers;
private cacheEndpoint;
private debug;
constructor(ctx: CacheHandlerContext);
get(key: string, fetchCache?: boolean): Promise<CacheHandlerValue | null>;
set(key: string, data: CacheHandlerValue['value'], fetchCache?: boolean): Promise<void>;
}

View File

@@ -0,0 +1,134 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _lruCache = _interopRequireDefault(require("next/dist/compiled/lru-cache"));
var _appRouterHeaders = require("../../../client/components/app-router-headers");
class FetchCache {
constructor(ctx){
if (ctx.maxMemoryCacheSize && !memoryCache) {
memoryCache = new _lruCache.default({
max: ctx.maxMemoryCacheSize,
length ({ value }) {
var ref;
if (!value) {
return 25;
} else if (value.kind === "REDIRECT") {
return JSON.stringify(value.props).length;
} else if (value.kind === "IMAGE") {
throw new Error("invariant image should not be incremental-cache");
} else if (value.kind === "FETCH") {
return JSON.stringify(value.data || "").length;
}
// rough estimate of size of cache value
return value.html.length + (((ref = JSON.stringify(value.pageData)) == null ? void 0 : ref.length) || 0);
}
});
}
this.debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE;
this.headers = {};
this.headers["Content-Type"] = "application/json";
if (_appRouterHeaders.FETCH_CACHE_HEADER in ctx._requestHeaders) {
const newHeaders = JSON.parse(ctx._requestHeaders[_appRouterHeaders.FETCH_CACHE_HEADER]);
for(const k in newHeaders){
this.headers[k] = newHeaders[k];
}
}
this.cacheEndpoint = `https://${ctx._requestHeaders["x-vercel-sc-host"]}${ctx._requestHeaders["x-vercel-sc-basepath"] || ""}`;
if (this.debug) {
console.log("using cache endpoint", this.cacheEndpoint);
}
}
async get(key, fetchCache) {
if (!fetchCache) return null;
let data = memoryCache == null ? void 0 : memoryCache.get(key);
// get data from fetch cache
if (!data) {
try {
const start = Date.now();
const res = await fetch(`${this.cacheEndpoint}/v1/suspense-cache/getItems`, {
method: "POST",
body: JSON.stringify([
key
]),
headers: this.headers
});
if (!res.ok) {
console.error(await res.text());
throw new Error(`invalid response from cache ${res.status}`);
}
const items = await res.json();
const item = items[key];
if (!item || !item.value) {
console.log({
item
});
throw new Error(`invalid item returned`);
}
const cached = JSON.parse(item.value);
if (!cached || cached.kind !== "FETCH") {
this.debug && console.log({
cached
});
throw new Error(`invalid cache value`);
}
data = {
lastModified: Date.now() - item.age * 1000,
value: cached
};
if (this.debug) {
console.log("got fetch cache entry duration:", Date.now() - start, data);
}
if (data) {
memoryCache == null ? void 0 : memoryCache.set(key, data);
}
} catch (err) {
// unable to get data from fetch-cache
console.error(`Failed to get from fetch-cache`, err);
}
}
return data || null;
}
async set(key, data, fetchCache) {
if (!fetchCache) return;
memoryCache == null ? void 0 : memoryCache.set(key, {
value: data,
lastModified: Date.now()
});
try {
const start = Date.now();
const body = JSON.stringify([
{
id: key,
value: JSON.stringify(data)
},
]);
const res = await fetch(`${this.cacheEndpoint}/v1/suspense-cache/setItems`, {
method: "POST",
headers: this.headers,
body: body
});
if (!res.ok) {
this.debug && console.log(await res.text());
throw new Error(`invalid response ${res.status}`);
}
if (this.debug) {
console.log("successfully set to fetch-cache duration:", Date.now() - start, body);
}
} catch (err) {
// unable to set to fetch-cache
console.error(`Failed to update fetch cache`, err);
}
return;
}
}
exports.default = FetchCache;
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
let memoryCache;
//# sourceMappingURL=fetch-cache.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,11 @@
import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './';
export default class FileSystemCache implements CacheHandler {
private fs;
private flushToDisk?;
private serverDistDir;
private appDir;
constructor(ctx: CacheHandlerContext);
get(key: string, fetchCache?: boolean): Promise<CacheHandlerValue | null>;
set(key: string, data: CacheHandlerValue['value']): Promise<void>;
private getFsPath;
}

View File

@@ -0,0 +1,141 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _lruCache = _interopRequireDefault(require("next/dist/compiled/lru-cache"));
var _path = _interopRequireDefault(require("../../../shared/lib/isomorphic/path"));
class FileSystemCache {
constructor(ctx){
this.fs = ctx.fs;
this.flushToDisk = ctx.flushToDisk;
this.serverDistDir = ctx.serverDistDir;
this.appDir = !!ctx._appDir;
if (ctx.maxMemoryCacheSize && !memoryCache) {
memoryCache = new _lruCache.default({
max: ctx.maxMemoryCacheSize,
length ({ value }) {
var ref;
if (!value) {
return 25;
} else if (value.kind === "REDIRECT") {
return JSON.stringify(value.props).length;
} else if (value.kind === "IMAGE") {
throw new Error("invariant image should not be incremental-cache");
} else if (value.kind === "FETCH") {
return JSON.stringify(value.data || "").length;
}
// rough estimate of size of cache value
return value.html.length + (((ref = JSON.stringify(value.pageData)) == null ? void 0 : ref.length) || 0);
}
});
}
}
async get(key, fetchCache) {
let data = memoryCache == null ? void 0 : memoryCache.get(key);
// let's check the disk for seed data
if (!data) {
try {
const { filePath , isAppPath } = await this.getFsPath({
pathname: fetchCache ? key : `${key}.html`,
fetchCache
});
const fileData = await this.fs.readFile(filePath);
const { mtime } = await this.fs.stat(filePath);
if (fetchCache) {
const lastModified = mtime.getTime();
data = {
lastModified,
value: JSON.parse(fileData)
};
} else {
const pageData = isAppPath ? await this.fs.readFile((await this.getFsPath({
pathname: `${key}.rsc`,
appDir: true
})).filePath) : JSON.parse(await this.fs.readFile(await (await this.getFsPath({
pathname: `${key}.json`,
appDir: false
})).filePath));
data = {
lastModified: mtime.getTime(),
value: {
kind: "PAGE",
html: fileData,
pageData
}
};
}
if (data) {
memoryCache == null ? void 0 : memoryCache.set(key, data);
}
} catch (_) {
// unable to get data from disk
}
}
return data || null;
}
async set(key, data) {
memoryCache == null ? void 0 : memoryCache.set(key, {
value: data,
lastModified: Date.now()
});
if (!this.flushToDisk) return;
if ((data == null ? void 0 : data.kind) === "PAGE") {
const isAppPath = typeof data.pageData === "string";
const { filePath: htmlPath } = await this.getFsPath({
pathname: `${key}.html`,
appDir: isAppPath
});
await this.fs.mkdir(_path.default.dirname(htmlPath));
await this.fs.writeFile(htmlPath, data.html);
await this.fs.writeFile((await this.getFsPath({
pathname: `${key}.${isAppPath ? "rsc" : "json"}`,
appDir: isAppPath
})).filePath, isAppPath ? data.pageData : JSON.stringify(data.pageData));
} else if ((data == null ? void 0 : data.kind) === "FETCH") {
const { filePath } = await this.getFsPath({
pathname: key,
fetchCache: true
});
await this.fs.mkdir(_path.default.dirname(filePath));
await this.fs.writeFile(filePath, JSON.stringify(data));
}
}
async getFsPath({ pathname , appDir , fetchCache }) {
if (fetchCache) {
// we store in .next/cache/fetch-cache so it can be persisted
// across deploys
return {
filePath: _path.default.join(this.serverDistDir, "..", "cache", "fetch-cache", pathname),
isAppPath: false
};
}
let isAppPath = false;
let filePath = _path.default.join(this.serverDistDir, "pages", pathname);
if (!this.appDir || appDir === false) return {
filePath,
isAppPath
};
try {
await this.fs.readFile(filePath);
return {
filePath,
isAppPath
};
} catch (err) {
return {
filePath: _path.default.join(this.serverDistDir, "app", pathname),
isAppPath: true
};
}
}
}
exports.default = FileSystemCache;
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
let memoryCache;
//# sourceMappingURL=file-system-cache.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,46 @@
import type { CacheFs } from '../../../shared/lib/utils';
import { PrerenderManifest } from '../../../build';
import { IncrementalCacheValue, IncrementalCacheEntry } from '../../response-cache';
export interface CacheHandlerContext {
fs: CacheFs;
dev?: boolean;
flushToDisk?: boolean;
serverDistDir: string;
maxMemoryCacheSize?: number;
_appDir: boolean;
_requestHeaders: IncrementalCache['requestHeaders'];
}
export interface CacheHandlerValue {
lastModified?: number;
value: IncrementalCacheValue | null;
}
export declare class CacheHandler {
constructor(_ctx: CacheHandlerContext);
get(_key: string, _fetchCache?: boolean): Promise<CacheHandlerValue | null>;
set(_key: string, _data: IncrementalCacheValue | null, _fetchCache?: boolean): Promise<void>;
}
export declare class IncrementalCache {
dev?: boolean;
cacheHandler: CacheHandler;
prerenderManifest: PrerenderManifest;
requestHeaders: Record<string, undefined | string | string[]>;
minimalMode?: boolean;
constructor({ fs, dev, appDir, flushToDisk, fetchCache, minimalMode, serverDistDir, requestHeaders, maxMemoryCacheSize, getPrerenderManifest, incrementalCacheHandlerPath, }: {
fs: CacheFs;
dev: boolean;
appDir?: boolean;
fetchCache?: boolean;
minimalMode?: boolean;
serverDistDir: string;
flushToDisk?: boolean;
requestHeaders: IncrementalCache['requestHeaders'];
maxMemoryCacheSize?: number;
incrementalCacheHandlerPath?: string;
getPrerenderManifest: () => PrerenderManifest;
});
private calculateRevalidate;
_getPathname(pathname: string, fetchCache?: boolean): string;
fetchCacheKey(url: string, init?: RequestInit): Promise<string>;
get(pathname: string, fetchCache?: boolean): Promise<IncrementalCacheEntry | null>;
set(pathname: string, data: IncrementalCacheValue | null, revalidateSeconds?: number | false, fetchCache?: boolean): Promise<void>;
}

View File

@@ -0,0 +1,172 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _fileSystemCache = _interopRequireDefault(require("./file-system-cache"));
var _path = _interopRequireDefault(require("../../../shared/lib/isomorphic/path"));
var _normalizePagePath = require("../../../shared/lib/page-path/normalize-page-path");
var _fetchCache = _interopRequireDefault(require("./fetch-cache"));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function toRoute(pathname) {
return pathname.replace(/\/$/, "").replace(/\/index$/, "") || "/";
}
class CacheHandler {
// eslint-disable-next-line
constructor(_ctx){}
async get(_key, _fetchCache) {
return {};
}
async set(_key, _data, _fetchCache) {}
}
exports.CacheHandler = CacheHandler;
class IncrementalCache {
constructor({ fs , dev , appDir , flushToDisk , fetchCache , minimalMode , serverDistDir , requestHeaders , maxMemoryCacheSize , getPrerenderManifest , incrementalCacheHandlerPath }){
let cacheHandlerMod = _fileSystemCache.default;
if (process.env.NEXT_RUNTIME !== "edge" && incrementalCacheHandlerPath) {
cacheHandlerMod = require(incrementalCacheHandlerPath);
cacheHandlerMod = cacheHandlerMod.default || cacheHandlerMod;
}
if (minimalMode && fetchCache) {
cacheHandlerMod = _fetchCache.default;
}
if (process.env.__NEXT_TEST_MAX_ISR_CACHE) {
// Allow cache size to be overridden for testing purposes
maxMemoryCacheSize = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10);
}
this.dev = dev;
this.minimalMode = minimalMode;
this.requestHeaders = requestHeaders;
this.prerenderManifest = getPrerenderManifest();
this.cacheHandler = new cacheHandlerMod({
dev,
fs,
flushToDisk,
serverDistDir,
maxMemoryCacheSize,
_appDir: !!appDir,
_requestHeaders: requestHeaders
});
}
calculateRevalidate(pathname, fromTime) {
// in development we don't have a prerender-manifest
// and default to always revalidating to allow easier debugging
if (this.dev) return new Date().getTime() - 1000;
// if an entry isn't present in routes we fallback to a default
// of revalidating after 1 second
const { initialRevalidateSeconds } = this.prerenderManifest.routes[toRoute(pathname)] || {
initialRevalidateSeconds: 1
};
const revalidateAfter = typeof initialRevalidateSeconds === "number" ? initialRevalidateSeconds * 1000 + fromTime : initialRevalidateSeconds;
return revalidateAfter;
}
_getPathname(pathname, fetchCache) {
return fetchCache ? pathname : (0, _normalizePagePath).normalizePagePath(pathname);
}
// x-ref: https://github.com/facebook/react/blob/2655c9354d8e1c54ba888444220f63e836925caa/packages/react/src/ReactFetch.js#L23
async fetchCacheKey(url, init = {}) {
const cacheString = JSON.stringify([
url,
init.method,
init.headers,
init.mode,
init.redirect,
init.credentials,
init.referrer,
init.referrerPolicy,
init.integrity,
init.next,
init.cache,
]);
let cacheKey;
if (process.env.NEXT_RUNTIME === "edge") {
function bufferToHex(buffer) {
return Array.prototype.map.call(new Uint8Array(buffer), (b)=>b.toString(16).padStart(2, "0")).join("");
}
const buffer1 = new TextEncoder().encode(cacheString);
cacheKey = bufferToHex(await crypto.subtle.digest("SHA-256", buffer1));
} else {
const crypto = require("crypto");
cacheKey = crypto.createHash("sha256").update(cacheString).digest("hex");
}
return cacheKey;
}
// get data from cache if available
async get(pathname, fetchCache) {
var ref, ref1;
// we don't leverage the prerender cache in dev mode
// so that getStaticProps is always called for easier debugging
if (this.dev) return null;
pathname = this._getPathname(pathname, fetchCache);
let entry = null;
const cacheData = await this.cacheHandler.get(pathname, fetchCache);
if ((cacheData == null ? void 0 : (ref = cacheData.value) == null ? void 0 : ref.kind) === "FETCH") {
const data = cacheData.value.data;
const age = Math.round((Date.now() - (cacheData.lastModified || 0)) / 1000);
const revalidate = cacheData.value.revalidate;
return {
isStale: age > revalidate,
value: {
kind: "FETCH",
data,
age,
revalidate,
isStale: age > revalidate
},
revalidateAfter: (cacheData.lastModified || Date.now()) + revalidate * 1000
};
}
const curRevalidate = (ref1 = this.prerenderManifest.routes[toRoute(pathname)]) == null ? void 0 : ref1.initialRevalidateSeconds;
const revalidateAfter = this.calculateRevalidate(pathname, (cacheData == null ? void 0 : cacheData.lastModified) || Date.now());
const isStale = revalidateAfter !== false && revalidateAfter < Date.now() ? true : undefined;
if (cacheData) {
entry = {
isStale,
curRevalidate,
revalidateAfter,
value: cacheData.value
};
}
if (!cacheData && this.prerenderManifest.notFoundRoutes.includes(pathname)) {
// for the first hit after starting the server the cache
// may not have a way to save notFound: true so if
// the prerender-manifest marks this as notFound then we
// return that entry and trigger a cache set to give it a
// chance to update in-memory entries
entry = {
isStale,
value: null,
curRevalidate,
revalidateAfter
};
this.set(pathname, entry.value, curRevalidate, fetchCache);
}
return entry;
}
// populate the incremental cache with new data
async set(pathname, data, revalidateSeconds, fetchCache) {
if (this.dev) return;
pathname = this._getPathname(pathname, fetchCache);
try {
// we use the prerender manifest memory instance
// to store revalidate timings for calculating
// revalidateAfter values so we update this on set
if (typeof revalidateSeconds !== "undefined" && !fetchCache) {
this.prerenderManifest.routes[pathname] = {
dataRoute: _path.default.posix.join("/_next/data", `${(0, _normalizePagePath).normalizePagePath(pathname)}.json`),
srcRoute: null,
initialRevalidateSeconds: revalidateSeconds
};
}
await this.cacheHandler.set(pathname, data, fetchCache);
} catch (error) {
console.warn("Failed to update prerender cache for", pathname, error);
}
}
}
exports.IncrementalCache = IncrementalCache;
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long