Skip to content

Commit ca233ef

Browse files
committed
refactor: move non-CacheHandler related types and utils from cache-types to blob-types module
1 parent d2a7fac commit ca233ef

File tree

7 files changed

+66
-66
lines changed

7 files changed

+66
-66
lines changed

src/build/content/static.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { trace } from '@opentelemetry/api'
66
import { wrapTracer } from '@opentelemetry/api/experimental'
77
import glob from 'fast-glob'
88

9+
import type { HtmlBlob } from '../../shared/blob-types.cjs'
910
import { encodeBlobKey } from '../../shared/blobkey.js'
10-
import type { HtmlBlob } from '../../shared/cache-types.cjs'
1111
import { PluginContext } from '../plugin-context.js'
1212
import { verifyNetlifyForms } from '../verification.js'
1313

src/run/handlers/cache.cts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { PrerenderManifest } from 'next/dist/build/index.js'
1111
import { NEXT_CACHE_TAGS_HEADER } from 'next/dist/lib/constants.js'
1212

1313
import { name as nextRuntimePkgName, version as nextRuntimePkgVersion } from '../../../package.json'
14+
import { type TagManifest } from '../../shared/blob-types.cjs'
1415
import {
1516
type CacheHandlerContext,
1617
type CacheHandlerForMultipleVersions,
@@ -20,7 +21,6 @@ import {
2021
type NetlifyCachedRouteValue,
2122
type NetlifyCacheHandlerValue,
2223
type NetlifyIncrementalCacheValue,
23-
type TagManifest,
2424
} from '../../shared/cache-types.cjs'
2525
import {
2626
getMemoizedKeyValueStoreBackedByRegionalBlobStore,

src/run/next.cts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { relative, resolve } from 'path'
44
// @ts-expect-error no types installed
55
import { patchFs } from 'fs-monkey'
66

7-
import { HtmlBlob } from '../shared/cache-types.cjs'
7+
import { HtmlBlob } from '../shared/blob-types.cjs'
88

99
import { getRequestContext } from './handlers/request-context.cjs'
1010
import { getTracer } from './handlers/tracer.cjs'

src/run/storage/request-scoped-in-memory-cache.cts

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { isPromise } from 'node:util/types'
2+
13
import { LRUCache } from 'lru-cache'
24

3-
import { type BlobType, estimateBlobSize } from '../../shared/cache-types.cjs'
5+
import { type BlobType, isHtmlBlob, isTagManifest } from '../../shared/blob-types.cjs'
46
import { getRequestContext } from '../handlers/request-context.cjs'
7+
import { recordWarning } from '../handlers/tracer.cjs'
58

69
// lru-cache types don't like using `null` for values, so we use a symbol to represent it and do conversion
710
// so it doesn't leak outside
@@ -22,6 +25,46 @@ export function setInMemoryCacheMaxSizeFromNextConfig(size: unknown) {
2225
}
2326
}
2427

28+
const estimateBlobSize = (valueToStore: BlobType | null | Promise<unknown>): number => {
29+
// very approximate size calculation to avoid expensive exact size calculation
30+
// inspired by https://github.com/vercel/next.js/blob/ed10f7ed0246fcc763194197eb9beebcbd063162/packages/next/src/server/lib/incremental-cache/file-system-cache.ts#L60-L79
31+
if (valueToStore === null || isPromise(valueToStore) || isTagManifest(valueToStore)) {
32+
return 25
33+
}
34+
if (isHtmlBlob(valueToStore)) {
35+
return valueToStore.html.length
36+
}
37+
let knownKindFailed = false
38+
try {
39+
if (valueToStore.value?.kind === 'FETCH') {
40+
return valueToStore.value.data.body.length
41+
}
42+
if (valueToStore.value?.kind === 'APP_PAGE') {
43+
return valueToStore.value.html.length + (valueToStore.value.rscData?.length ?? 0)
44+
}
45+
if (valueToStore.value?.kind === 'PAGE' || valueToStore.value?.kind === 'PAGES') {
46+
return valueToStore.value.html.length + JSON.stringify(valueToStore.value.pageData).length
47+
}
48+
if (valueToStore.value?.kind === 'ROUTE' || valueToStore.value?.kind === 'APP_ROUTE') {
49+
return valueToStore.value.body.length
50+
}
51+
} catch {
52+
// size calculation rely on the shape of the value, so if it's not what we expect, we fallback to JSON.stringify
53+
knownKindFailed = true
54+
}
55+
56+
// fallback for not known kinds or known kinds that did fail to calculate size
57+
// we should also monitor cases when fallback is used because it's not the most efficient way to calculate/estimate size
58+
// and might indicate need to make adjustments or additions to the size calculation
59+
recordWarning(
60+
new Error(
61+
`Blob size calculation did fallback to JSON.stringify. Kind: KnownKindFailed: ${knownKindFailed}, ${valueToStore.value?.kind ?? 'undefined'}`,
62+
),
63+
)
64+
65+
return JSON.stringify(valueToStore).length
66+
}
67+
2568
function getInMemoryLRUCache() {
2669
if (typeof extendedGlobalThis[IN_MEMORY_LRU_CACHE] === 'undefined') {
2770
const maxSize =

src/run/storage/storage.cts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// and should not be used directly outside of this directory.
44
// There is eslint `no-restricted-imports` rule to enforce this.
55

6-
import { type BlobType } from '../../shared/cache-types.cjs'
6+
import { type BlobType } from '../../shared/blob-types.cjs'
77
import { getTracer } from '../handlers/tracer.cjs'
88

99
import { getRegionalBlobStore } from './regional-blob-store.cjs'

src/shared/blob-types.cts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { type NetlifyCacheHandlerValue } from './cache-types.cjs'
2+
3+
export type TagManifest = { revalidatedAt: number }
4+
5+
export type HtmlBlob = {
6+
html: string
7+
isFallback: boolean
8+
}
9+
10+
export type BlobType = NetlifyCacheHandlerValue | TagManifest | HtmlBlob
11+
12+
export const isTagManifest = (value: BlobType): value is TagManifest => {
13+
return false
14+
}
15+
16+
export const isHtmlBlob = (value: BlobType): value is HtmlBlob => {
17+
return false
18+
}

src/shared/cache-types.cts

-61
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { isPromise } from 'node:util/types'
2-
31
import type {
42
CacheHandler,
53
CacheHandlerValue,
@@ -12,8 +10,6 @@ import type {
1210
IncrementalCacheValue,
1311
} from 'next/dist/server/response-cache/types.js'
1412

15-
import { recordWarning } from '../run/handlers/tracer.cjs'
16-
1713
export type { CacheHandlerContext } from 'next/dist/server/lib/incremental-cache/index.js'
1814

1915
type CacheControl = {
@@ -158,60 +154,3 @@ export type CacheHandlerForMultipleVersions = BaseCacheHandlerForMultipleVersion
158154
context: CacheHandlerSetContextForMultipleVersions,
159155
) => ReturnType<BaseCacheHandlerForMultipleVersions['set']>
160156
}
161-
162-
export type TagManifest = { revalidatedAt: number }
163-
164-
export type HtmlBlob = {
165-
html: string
166-
isFallback: boolean
167-
}
168-
169-
export type BlobType = NetlifyCacheHandlerValue | TagManifest | HtmlBlob
170-
171-
const isTagManifest = (value: BlobType): value is TagManifest => {
172-
return false
173-
}
174-
175-
const isHtmlBlob = (value: BlobType): value is HtmlBlob => {
176-
return false
177-
}
178-
179-
export const estimateBlobSize = (valueToStore: BlobType | null | Promise<unknown>): number => {
180-
// very approximate size calculation to avoid expensive exact size calculation
181-
// inspired by https://github.com/vercel/next.js/blob/ed10f7ed0246fcc763194197eb9beebcbd063162/packages/next/src/server/lib/incremental-cache/file-system-cache.ts#L60-L79
182-
if (valueToStore === null || isPromise(valueToStore) || isTagManifest(valueToStore)) {
183-
return 25
184-
}
185-
if (isHtmlBlob(valueToStore)) {
186-
return valueToStore.html.length
187-
}
188-
let knownKindFailed = false
189-
try {
190-
if (valueToStore.value?.kind === 'FETCH') {
191-
return valueToStore.value.data.body.length
192-
}
193-
if (valueToStore.value?.kind === 'APP_PAGE') {
194-
return valueToStore.value.html.length + (valueToStore.value.rscData?.length ?? 0)
195-
}
196-
if (valueToStore.value?.kind === 'PAGE' || valueToStore.value?.kind === 'PAGES') {
197-
return valueToStore.value.html.length + JSON.stringify(valueToStore.value.pageData).length
198-
}
199-
if (valueToStore.value?.kind === 'ROUTE' || valueToStore.value?.kind === 'APP_ROUTE') {
200-
return valueToStore.value.body.length
201-
}
202-
} catch {
203-
// size calculation rely on the shape of the value, so if it's not what we expect, we fallback to JSON.stringify
204-
knownKindFailed = true
205-
}
206-
207-
// fallback for not known kinds or known kinds that did fail to calculate size
208-
// we should also monitor cases when fallback is used because it's not the most efficient way to calculate/estimate size
209-
// and might indicate need to make adjustments or additions to the size calculation
210-
recordWarning(
211-
new Error(
212-
`Blob size calculation did fallback to JSON.stringify. Kind: KnownKindFailed: ${knownKindFailed}, ${valueToStore.value?.kind ?? 'undefined'}`,
213-
),
214-
)
215-
216-
return JSON.stringify(valueToStore).length
217-
}

0 commit comments

Comments
 (0)