added fonts
This commit is contained in:
1 parent
4134e3a234
commit
142d2986b6
9079 files changed
+1498421
-2
No files matched your search
+64
@@ -0,0 +1,64 @@
|
||||
import { arrayBufferToString, stringToUint8Array } from '../app-render/encryption-utils';
|
||||
/**
|
||||
* Parses serialized cache entries into a UseCacheCacheStore
|
||||
* @param entries - The serialized entries to parse
|
||||
* @returns A new UseCacheCacheStore containing the parsed entries
|
||||
*/ export function parseUseCacheCacheStore(entries) {
|
||||
const store = new Map();
|
||||
for (const [key, { value, tags, stale, timestamp, expire, revalidate }] of entries){
|
||||
store.set(key, Promise.resolve({
|
||||
// Create a ReadableStream from the Uint8Array
|
||||
value: new ReadableStream({
|
||||
start (controller) {
|
||||
// Enqueue the Uint8Array to the stream
|
||||
controller.enqueue(stringToUint8Array(atob(value)));
|
||||
// Close the stream
|
||||
controller.close();
|
||||
}
|
||||
}),
|
||||
tags,
|
||||
stale,
|
||||
timestamp,
|
||||
expire,
|
||||
revalidate
|
||||
}));
|
||||
}
|
||||
return store;
|
||||
}
|
||||
/**
|
||||
* Serializes UseCacheCacheStore entries into an array of key-value pairs
|
||||
* @param entries - The store entries to stringify
|
||||
* @returns A promise that resolves to an array of key-value pairs with serialized values
|
||||
*/ export async function serializeUseCacheCacheStore(entries) {
|
||||
return Promise.all(Array.from(entries).map(([key, value])=>{
|
||||
return value.then(async (entry)=>{
|
||||
const [left, right] = entry.value.tee();
|
||||
entry.value = right;
|
||||
let binaryString = '';
|
||||
// We want to encode the value as a string, but we aren't sure if the
|
||||
// value is a a stream of UTF-8 bytes or not, so let's just encode it
|
||||
// as a string using base64.
|
||||
for await (const chunk of left){
|
||||
binaryString += arrayBufferToString(chunk);
|
||||
}
|
||||
return [
|
||||
key,
|
||||
{
|
||||
// Encode the value as a base64 string.
|
||||
value: btoa(binaryString),
|
||||
tags: entry.tags,
|
||||
stale: entry.stale,
|
||||
timestamp: entry.timestamp,
|
||||
expire: entry.expire,
|
||||
revalidate: entry.revalidate
|
||||
}
|
||||
];
|
||||
}).catch(()=>{
|
||||
// Any failed cache writes should be ignored as to not discard the
|
||||
// entire cache.
|
||||
return null;
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
//# sourceMappingURL=cache-store.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../src/server/resume-data-cache/cache-store.ts"],"sourcesContent":["import {\n arrayBufferToString,\n stringToUint8Array,\n} from '../app-render/encryption-utils'\nimport type { CacheEntry } from '../lib/cache-handlers/types'\nimport type { CachedFetchValue } from '../response-cache/types'\n\n/**\n * A generic cache store type that provides a subset of Map functionality\n */\ntype CacheStore<T> = Pick<\n Map<string, T>,\n 'entries' | 'keys' | 'size' | 'get' | 'set'\n>\n\n/**\n * A cache store specifically for fetch cache values\n */\nexport type FetchCacheStore = CacheStore<CachedFetchValue>\n\n/**\n * A cache store for encrypted bound args of inline server functions.\n */\nexport type EncryptedBoundArgsCacheStore = CacheStore<string>\n\n/**\n * An in-memory-only cache store for decrypted bound args of inline server\n * functions.\n */\nexport type DecryptedBoundArgsCacheStore = CacheStore<string>\n\n/**\n * Serialized format for \"use cache\" entries\n */\nexport interface UseCacheCacheStoreSerialized {\n value: string\n tags: string[]\n stale: number\n timestamp: number\n expire: number\n revalidate: number\n}\n\n/**\n * A cache store specifically for \"use cache\" values that stores promises of\n * cache entries.\n */\nexport type UseCacheCacheStore = CacheStore<Promise<CacheEntry>>\n\n/**\n * Parses serialized cache entries into a UseCacheCacheStore\n * @param entries - The serialized entries to parse\n * @returns A new UseCacheCacheStore containing the parsed entries\n */\nexport function parseUseCacheCacheStore(\n entries: Iterable<[string, UseCacheCacheStoreSerialized]>\n): UseCacheCacheStore {\n const store = new Map<string, Promise<CacheEntry>>()\n\n for (const [\n key,\n { value, tags, stale, timestamp, expire, revalidate },\n ] of entries) {\n store.set(\n key,\n Promise.resolve({\n // Create a ReadableStream from the Uint8Array\n value: new ReadableStream<Uint8Array>({\n start(controller) {\n // Enqueue the Uint8Array to the stream\n controller.enqueue(stringToUint8Array(atob(value)))\n\n // Close the stream\n controller.close()\n },\n }),\n tags,\n stale,\n timestamp,\n expire,\n revalidate,\n })\n )\n }\n\n return store\n}\n\n/**\n * Serializes UseCacheCacheStore entries into an array of key-value pairs\n * @param entries - The store entries to stringify\n * @returns A promise that resolves to an array of key-value pairs with serialized values\n */\nexport async function serializeUseCacheCacheStore(\n entries: IterableIterator<[string, Promise<CacheEntry>]>\n): Promise<Array<[string, UseCacheCacheStoreSerialized] | null>> {\n return Promise.all(\n Array.from(entries).map(([key, value]) => {\n return value\n .then(async (entry) => {\n const [left, right] = entry.value.tee()\n entry.value = right\n\n let binaryString: string = ''\n\n // We want to encode the value as a string, but we aren't sure if the\n // value is a a stream of UTF-8 bytes or not, so let's just encode it\n // as a string using base64.\n for await (const chunk of left) {\n binaryString += arrayBufferToString(chunk)\n }\n\n return [\n key,\n {\n // Encode the value as a base64 string.\n value: btoa(binaryString),\n tags: entry.tags,\n stale: entry.stale,\n timestamp: entry.timestamp,\n expire: entry.expire,\n revalidate: entry.revalidate,\n },\n ] satisfies [string, UseCacheCacheStoreSerialized]\n })\n .catch(() => {\n // Any failed cache writes should be ignored as to not discard the\n // entire cache.\n return null\n })\n })\n )\n}\n"],"names":["arrayBufferToString","stringToUint8Array","parseUseCacheCacheStore","entries","store","Map","key","value","tags","stale","timestamp","expire","revalidate","set","Promise","resolve","ReadableStream","start","controller","enqueue","atob","close","serializeUseCacheCacheStore","all","Array","from","map","then","entry","left","right","tee","binaryString","chunk","btoa","catch"],"mappings":"AAAA,SACEA,mBAAmB,EACnBC,kBAAkB,QACb,iCAAgC;AA8CvC;;;;CAIC,GACD,OAAO,SAASC,wBACdC,OAAyD;IAEzD,MAAMC,QAAQ,IAAIC;IAElB,KAAK,MAAM,CACTC,KACA,EAAEC,KAAK,EAAEC,IAAI,EAAEC,KAAK,EAAEC,SAAS,EAAEC,MAAM,EAAEC,UAAU,EAAE,CACtD,IAAIT,QAAS;QACZC,MAAMS,GAAG,CACPP,KACAQ,QAAQC,OAAO,CAAC;YACd,8CAA8C;YAC9CR,OAAO,IAAIS,eAA2B;gBACpCC,OAAMC,UAAU;oBACd,uCAAuC;oBACvCA,WAAWC,OAAO,CAAClB,mBAAmBmB,KAAKb;oBAE3C,mBAAmB;oBACnBW,WAAWG,KAAK;gBAClB;YACF;YACAb;YACAC;YACAC;YACAC;YACAC;QACF;IAEJ;IAEA,OAAOR;AACT;AAEA;;;;CAIC,GACD,OAAO,eAAekB,4BACpBnB,OAAwD;IAExD,OAAOW,QAAQS,GAAG,CAChBC,MAAMC,IAAI,CAACtB,SAASuB,GAAG,CAAC,CAAC,CAACpB,KAAKC,MAAM;QACnC,OAAOA,MACJoB,IALine truncated
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
import { InvariantError } from '../../shared/lib/invariant-error';
|
||||
import { serializeUseCacheCacheStore, parseUseCacheCacheStore } from './cache-store';
|
||||
/**
|
||||
* Serializes a resume data cache into a JSON string for storage or
|
||||
* transmission. Handles 'use cache' values, fetch responses, and encrypted
|
||||
* bound args for inline server functions.
|
||||
*
|
||||
* @param resumeDataCache - The immutable cache to serialize
|
||||
* @returns A Promise that resolves to the serialized cache as a JSON string, or
|
||||
* 'null' if empty
|
||||
*/ export async function stringifyResumeDataCache(resumeDataCache) {
|
||||
if (process.env.NEXT_RUNTIME === 'edge') {
|
||||
throw Object.defineProperty(new InvariantError('`stringifyResumeDataCache` should not be called in edge runtime.'), "__NEXT_ERROR_CODE", {
|
||||
value: "E602",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
} else {
|
||||
if (resumeDataCache.fetch.size === 0 && resumeDataCache.cache.size === 0) {
|
||||
return 'null';
|
||||
}
|
||||
const json = {
|
||||
store: {
|
||||
fetch: Object.fromEntries(Array.from(resumeDataCache.fetch.entries())),
|
||||
cache: Object.fromEntries((await serializeUseCacheCacheStore(resumeDataCache.cache.entries())).filter((entry)=>entry !== null)),
|
||||
encryptedBoundArgs: Object.fromEntries(Array.from(resumeDataCache.encryptedBoundArgs.entries()))
|
||||
}
|
||||
};
|
||||
// Compress the JSON string using zlib. As the data we already want to
|
||||
// decompress is in memory, we use the synchronous deflateSync function.
|
||||
const { deflateSync } = require('node:zlib');
|
||||
return deflateSync(JSON.stringify(json)).toString('base64');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Creates a new empty mutable resume data cache for pre-rendering.
|
||||
* Initializes fresh Map instances for both the 'use cache' and fetch caches.
|
||||
* Used at the start of pre-rendering to begin collecting cached values.
|
||||
*
|
||||
* @returns A new empty PrerenderResumeDataCache instance
|
||||
*/ export function createPrerenderResumeDataCache() {
|
||||
return {
|
||||
cache: new Map(),
|
||||
fetch: new Map(),
|
||||
encryptedBoundArgs: new Map(),
|
||||
decryptedBoundArgs: new Map()
|
||||
};
|
||||
}
|
||||
export function createRenderResumeDataCache(prerenderResumeDataCacheOrPersistedCache) {
|
||||
if (process.env.NEXT_RUNTIME === 'edge') {
|
||||
throw Object.defineProperty(new InvariantError('`createRenderResumeDataCache` should not be called in edge runtime.'), "__NEXT_ERROR_CODE", {
|
||||
value: "E556",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
} else {
|
||||
if (typeof prerenderResumeDataCacheOrPersistedCache !== 'string') {
|
||||
// If the cache is already a prerender cache, we can return it directly,
|
||||
// we're just performing a type change.
|
||||
return prerenderResumeDataCacheOrPersistedCache;
|
||||
}
|
||||
if (prerenderResumeDataCacheOrPersistedCache === 'null') {
|
||||
return {
|
||||
cache: new Map(),
|
||||
fetch: new Map(),
|
||||
encryptedBoundArgs: new Map(),
|
||||
decryptedBoundArgs: new Map()
|
||||
};
|
||||
}
|
||||
// This should be a compressed string. Let's decompress it using zlib.
|
||||
// As the data we already want to decompress is in memory, we use the
|
||||
// synchronous inflateSync function.
|
||||
const { inflateSync } = require('node:zlib');
|
||||
const json = JSON.parse(inflateSync(Buffer.from(prerenderResumeDataCacheOrPersistedCache, 'base64')).toString('utf-8'));
|
||||
return {
|
||||
cache: parseUseCacheCacheStore(Object.entries(json.store.cache)),
|
||||
fetch: new Map(Object.entries(json.store.fetch)),
|
||||
encryptedBoundArgs: new Map(Object.entries(json.store.encryptedBoundArgs)),
|
||||
decryptedBoundArgs: new Map()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
//# sourceMappingURL=resume-data-cache.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../src/server/resume-data-cache/resume-data-cache.ts"],"sourcesContent":["import { InvariantError } from '../../shared/lib/invariant-error'\nimport {\n type UseCacheCacheStore,\n type FetchCacheStore,\n type EncryptedBoundArgsCacheStore,\n serializeUseCacheCacheStore,\n parseUseCacheCacheStore,\n type DecryptedBoundArgsCacheStore,\n type UseCacheCacheStoreSerialized,\n} from './cache-store'\n\n/**\n * An immutable version of the resume data cache used during rendering.\n * This cache is read-only and cannot be modified once created.\n */\nexport interface RenderResumeDataCache {\n /**\n * A read-only Map store for values cached by the 'use cache' React hook.\n * The 'set' operation is omitted to enforce immutability.\n */\n readonly cache: Omit<UseCacheCacheStore, 'set'>\n\n /**\n * A read-only Map store for cached fetch responses.\n * The 'set' operation is omitted to enforce immutability.\n */\n readonly fetch: Omit<FetchCacheStore, 'set'>\n\n /**\n * A read-only Map store for encrypted bound args of inline server functions.\n * The 'set' operation is omitted to enforce immutability.\n */\n readonly encryptedBoundArgs: Omit<EncryptedBoundArgsCacheStore, 'set'>\n\n /**\n * A read-only Map store for decrypted bound args of inline server functions.\n * This is only intended for in-memory usage during pre-rendering, and must\n * not be persisted in the resume store. The 'set' operation is omitted to\n * enforce immutability.\n */\n readonly decryptedBoundArgs: Omit<DecryptedBoundArgsCacheStore, 'set'>\n}\n\n/**\n * A mutable version of the resume data cache used during pre-rendering.\n * This cache allows both reading and writing of cached values.\n */\nexport interface PrerenderResumeDataCache {\n /**\n * A mutable Map store for values cached by the 'use cache' React hook.\n * Supports both 'get' and 'set' operations to build the cache during\n * pre-rendering.\n */\n readonly cache: UseCacheCacheStore\n\n /**\n * A mutable Map store for cached fetch responses.\n * Supports both 'get' and 'set' operations to build the cache during\n * pre-rendering.\n */\n readonly fetch: FetchCacheStore\n\n /**\n * A mutable Map store for encrypted bound args of inline server functions.\n * Supports both 'get' and 'set' operations to build the cache during\n * pre-rendering.\n */\n readonly encryptedBoundArgs: EncryptedBoundArgsCacheStore\n\n /**\n * A mutable Map store for decrypted bound args of inline server functions.\n * This is only intended for in-memory usage during pre-rendering, and must\n * not be persisted in the resume store. Supports both 'get' and 'set'\n * operations to build the cache during pre-rendering.\n */\n readonly decryptedBoundArgs: DecryptedBoundArgsCacheStore\n}\n\ntype ResumeStoreSerialized = {\n store: {\n cache: {\n [key: string]: any\n }\n fetch: {\n [key: string]: any\n }\n encryptedBoundArgs: {\n [key: string]: string\n }\n }\n}\n\n/**\n * Serializes a resume data cache into a JSON string for storage or\n * transmission. Handles 'use cache' values, fetch responses, and encrypted\n * bound args for inline server functions.\n *\n * @param resumeDataCache - The immutable cache to serialize\n * @returns A Promise that resolves to the serialized cache as a JSON string, or\n * 'null' if empty\n */\nexport async function stringifyResumeDataCache(\n resumeDataCache: RenderResumeDataCache | PrerenderResumeDataCache\n): Promise<string> {\n if (process.env.NEXT_RUNTIME === 'edge') {\n throw new InvariantError(\n '`stringifyResumeDataCache` should not be called in edge runtime.'\n )\n } else {\n if (resumeDataCache.fetch.size === 0 && resumeDataCache.cache.size === 0) {\n return 'null'\n }\n\n const json: ResumeStoreSerialized = {\n store: {\n fetch: Object.fromEntries(Array.from(resumeDataCache.fetch.entries())),\n cache: Object.fromEntries(\n (\n await serializeUseCacheCacheStore(resumeDataCache.cache.entries())\n ).filter(\n (entry): entry is [string, UseCacheCacheStoreSerialized] =>\n entry !== null\n )\n ),\n encryptedBoundArgs: Object.fromEntries(\n Array.from(resumeDataCache.encryptedBoundArgs.entries())\n ),\n },\n }\n\n // Compress the JSON string using zlib. As the data we already want to\n // decompress is in memory, we use the synchronous deflateSync function.\n const { deflateSync } = require('node:zlib') as typeof import('node:zlib')\n\n return deflateSync(JSON.stringify(json)).toString('base64')\n }\n}\n\n/**\n * Creates a new empty mutable resume data cache for pre-rendering.\n * Initializes fresh Map instances for both the 'use cache' and fetch caches.\n * Used at the start of pre-rendering to begin collecting cached values.\n *\n * @returns A new empty PrerenderResumeDataCache iLine truncated
|
||||
Reference in new issue
Block a user