Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
50bead1b
Unverified
Commit
50bead1b
authored
Dec 05, 2024
by
Hendrik Liebau
Committed by
GitHub
Dec 05, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cache the bound args encryption result for inline server functions (#73521)
parent
550f4a8c
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
147 additions
and
51 deletions
+147
-51
encryption.ts
packages/next/src/server/app-render/encryption.ts
+35
-4
postponed-state.test.ts
packages/next/src/server/app-render/postponed-state.test.ts
+1
-0
cache-store.ts
packages/next/src/server/resume-data-cache/cache-store.ts
+11
-31
resume-data-cache.test.ts
...xt/src/server/resume-data-cache/resume-data-cache.test.ts
+1
-1
resume-data-cache.ts
...es/next/src/server/resume-data-cache/resume-data-cache.ts
+38
-13
form.tsx
.../e2e/app-dir/dynamic-io/app/server-action-inline/form.tsx
+17
-0
page.tsx
.../e2e/app-dir/dynamic-io/app/server-action-inline/page.tsx
+14
-0
dynamic-io.server-action.test.ts
test/e2e/app-dir/dynamic-io/dynamic-io.server-action.test.ts
+30
-2
No files found.
packages/next/src/server/app-render/encryption.ts
View file @
50bead1b
...
@@ -16,7 +16,11 @@ import {
...
@@ -16,7 +16,11 @@ import {
getServerModuleMap
,
getServerModuleMap
,
stringToUint8Array
,
stringToUint8Array
,
}
from
'./encryption-utils'
}
from
'./encryption-utils'
import
{
workUnitAsyncStorage
}
from
'./work-unit-async-storage.external'
import
{
getPrerenderResumeDataCache
,
getRenderResumeDataCache
,
workUnitAsyncStorage
,
}
from
'./work-unit-async-storage.external'
const
isEdgeRuntime
=
process
.
env
.
NEXT_RUNTIME
===
'edge'
const
isEdgeRuntime
=
process
.
env
.
NEXT_RUNTIME
===
'edge'
...
@@ -47,6 +51,10 @@ async function decodeActionBoundArg(actionId: string, arg: string) {
...
@@ -47,6 +51,10 @@ async function decodeActionBoundArg(actionId: string, arg: string) {
return
decrypted
.
slice
(
actionId
.
length
)
return
decrypted
.
slice
(
actionId
.
length
)
}
}
/**
* Encrypt the serialized string with the action id as the salt. Add a prefix to
* later ensure that the payload is correctly decrypted, similar to a checksum.
*/
async
function
encodeActionBoundArg
(
actionId
:
string
,
arg
:
string
)
{
async
function
encodeActionBoundArg
(
actionId
:
string
,
arg
:
string
)
{
const
key
=
await
getActionEncryptionKey
()
const
key
=
await
getActionEncryptionKey
()
if
(
key
===
undefined
)
{
if
(
key
===
undefined
)
{
...
@@ -109,11 +117,34 @@ export async function encryptActionBoundArgs(actionId: string, args: any[]) {
...
@@ -109,11 +117,34 @@ export async function encryptActionBoundArgs(actionId: string, args: any[]) {
throw
error
throw
error
}
}
// Encrypt the serialized string with the action id as the salt.
const
workUnitStore
=
workUnitAsyncStorage
.
getStore
()
// Add a prefix to later ensure that the payload is correctly decrypted, similar
// to a checksum.
if
(
!
workUnitStore
)
{
return
encodeActionBoundArg
(
actionId
,
serialized
)
}
const
prerenderResumeDataCache
=
getPrerenderResumeDataCache
(
workUnitStore
)
const
renderResumeDataCache
=
getRenderResumeDataCache
(
workUnitStore
)
const
cacheKey
=
actionId
+
serialized
const
cachedEncrypted
=
prerenderResumeDataCache
?.
encryptedBoundArgs
.
get
(
cacheKey
)
??
renderResumeDataCache
?.
encryptedBoundArgs
.
get
(
cacheKey
)
if
(
cachedEncrypted
)
{
return
cachedEncrypted
}
const
cacheSignal
=
workUnitStore
.
type
===
'prerender'
?
workUnitStore
.
cacheSignal
:
undefined
cacheSignal
?.
beginRead
()
const
encrypted
=
await
encodeActionBoundArg
(
actionId
,
serialized
)
const
encrypted
=
await
encodeActionBoundArg
(
actionId
,
serialized
)
cacheSignal
?.
endRead
()
prerenderResumeDataCache
?.
encryptedBoundArgs
.
set
(
cacheKey
,
encrypted
)
return
encrypted
return
encrypted
}
}
...
...
packages/next/src/server/app-render/postponed-state.test.ts
View file @
50bead1b
...
@@ -47,6 +47,7 @@ describe('getDynamicHTMLPostponedState', () => {
...
@@ -47,6 +47,7 @@ describe('getDynamicHTMLPostponedState', () => {
"cache": Map {
"cache": Map {
"1" => Promise {},
"1" => Promise {},
},
},
"encryptedBoundArgs": Map {},
"fetch": Map {},
"fetch": Map {},
},
},
"type": 2,
"type": 2,
...
...
packages/next/src/server/resume-data-cache/cache-store.ts
View file @
50bead1b
...
@@ -16,31 +16,14 @@ type CacheStore<T> = Pick<Map<string, T>, 'entries' | 'size' | 'get' | 'set'>
...
@@ -16,31 +16,14 @@ type CacheStore<T> = Pick<Map<string, T>, 'entries' | 'size' | 'get' | 'set'>
export
type
FetchCacheStore
=
CacheStore
<
CachedFetchValue
>
export
type
FetchCacheStore
=
CacheStore
<
CachedFetchValue
>
/**
/**
* Parses fetch cache entries into a FetchCacheStore
* A cache store for encrypted bound args of inline server functions.
* @param entries - The entries to parse into the store
* @returns A new FetchCacheStore containing the entries
*/
*/
export
function
parseFetchCacheStore
(
export
type
EncryptedBoundArgsCacheStore
=
CacheStore
<
string
>
entries
:
Iterable
<
[
string
,
CachedFetchValue
]
>
):
FetchCacheStore
{
return
new
Map
(
entries
)
}
/**
* Stringifies a FetchCacheStore into an array of key-value pairs
* @param store - The store to stringify
* @returns A promise that resolves to an array of key-value pairs
*/
export
function
stringifyFetchCacheStore
(
entries
:
IterableIterator
<
[
string
,
CachedFetchValue
]
>
):
[
string
,
CachedFetchValue
][]
{
return
Array
.
from
(
entries
)
}
/**
/**
* Serialized format for
cache
entries
* Serialized format for
"use cache"
entries
*/
*/
interface
CacheCacheStoreSerialized
{
interface
Use
CacheCacheStoreSerialized
{
value
:
string
value
:
string
tags
:
string
[]
tags
:
string
[]
stale
:
number
stale
:
number
...
@@ -53,10 +36,7 @@ interface CacheCacheStoreSerialized {
...
@@ -53,10 +36,7 @@ interface CacheCacheStoreSerialized {
* A cache store specifically for "use cache" values that stores promises of
* A cache store specifically for "use cache" values that stores promises of
* cache entries.
* cache entries.
*/
*/
export
type
UseCacheCacheStore
=
Pick
<
export
type
UseCacheCacheStore
=
CacheStore
<
Promise
<
CacheEntry
>>
Map
<
string
,
Promise
<
CacheEntry
>>
,
'entries'
|
'size'
|
'get'
|
'set'
>
/**
/**
* Parses serialized cache entries into a UseCacheCacheStore
* Parses serialized cache entries into a UseCacheCacheStore
...
@@ -64,7 +44,7 @@ export type UseCacheCacheStore = Pick<
...
@@ -64,7 +44,7 @@ export type UseCacheCacheStore = Pick<
* @returns A new UseCacheCacheStore containing the parsed entries
* @returns A new UseCacheCacheStore containing the parsed entries
*/
*/
export
function
parseUseCacheCacheStore
(
export
function
parseUseCacheCacheStore
(
entries
:
Iterable
<
[
string
,
CacheCacheStoreSerialized
]
>
entries
:
Iterable
<
[
string
,
Use
CacheCacheStoreSerialized
]
>
):
UseCacheCacheStore
{
):
UseCacheCacheStore
{
const
store
=
new
Map
<
string
,
Promise
<
CacheEntry
>>
()
const
store
=
new
Map
<
string
,
Promise
<
CacheEntry
>>
()
...
@@ -98,13 +78,13 @@ export function parseUseCacheCacheStore(
...
@@ -98,13 +78,13 @@ export function parseUseCacheCacheStore(
}
}
/**
/**
* S
tringifies a UseCacheCacheStore
into an array of key-value pairs
* S
erializes UseCacheCacheStore entries
into an array of key-value pairs
* @param
store - The store
to stringify
* @param
entries - The store entries
to stringify
* @returns A promise that resolves to an array of key-value pairs with serialized values
* @returns A promise that resolves to an array of key-value pairs with serialized values
*/
*/
export
async
function
s
tringify
UseCacheCacheStore
(
export
async
function
s
erialize
UseCacheCacheStore
(
entries
:
IterableIterator
<
[
string
,
Promise
<
CacheEntry
>
]
>
entries
:
IterableIterator
<
[
string
,
Promise
<
CacheEntry
>
]
>
):
Promise
<
[
string
,
CacheCacheStoreSerialized
][]
>
{
):
Promise
<
[
string
,
Use
CacheCacheStoreSerialized
][]
>
{
return
Promise
.
all
(
return
Promise
.
all
(
Array
.
from
(
entries
).
map
(([
key
,
value
])
=>
{
Array
.
from
(
entries
).
map
(([
key
,
value
])
=>
{
return
value
.
then
(
async
(
entry
)
=>
{
return
value
.
then
(
async
(
entry
)
=>
{
...
@@ -131,7 +111,7 @@ export async function stringifyUseCacheCacheStore(
...
@@ -131,7 +111,7 @@ export async function stringifyUseCacheCacheStore(
expire
:
entry
.
expire
,
expire
:
entry
.
expire
,
revalidate
:
entry
.
revalidate
,
revalidate
:
entry
.
revalidate
,
},
},
]
as
[
string
,
CacheCacheStoreSerialized
]
]
satisfies
[
string
,
Use
CacheCacheStoreSerialized
]
})
})
})
})
)
)
...
...
packages/next/src/server/resume-data-cache/resume-data-cache.test.ts
View file @
50bead1b
...
@@ -41,7 +41,7 @@ describe('stringifyResumeDataCache', () => {
...
@@ -41,7 +41,7 @@ describe('stringifyResumeDataCache', () => {
).
toString
(
'utf-8'
)
).
toString
(
'utf-8'
)
expect
(
decompressed
).
toMatchInlineSnapshot
(
expect
(
decompressed
).
toMatchInlineSnapshot
(
`"{"store":{"fetch":{},"cache":{"key":{"value":"dmFsdWU=","tags":[],"stale":0,"timestamp":0,"expire":0,"revalidate":0}}}}"`
`"{"store":{"fetch":{},"cache":{"key":{"value":"dmFsdWU=","tags":[],"stale":0,"timestamp":0,"expire":0,"revalidate":0}}
,"encryptedBoundArgs":{}
}}"`
)
)
})
})
})
})
...
...
packages/next/src/server/resume-data-cache/resume-data-cache.ts
View file @
50bead1b
...
@@ -2,10 +2,9 @@ import { InvariantError } from '../../shared/lib/invariant-error'
...
@@ -2,10 +2,9 @@ import { InvariantError } from '../../shared/lib/invariant-error'
import
{
import
{
type
UseCacheCacheStore
,
type
UseCacheCacheStore
,
type
FetchCacheStore
,
type
FetchCacheStore
,
stringifyFetch
CacheStore
,
type
EncryptedBoundArgs
CacheStore
,
s
tringify
UseCacheCacheStore
,
s
erialize
UseCacheCacheStore
,
parseUseCacheCacheStore
,
parseUseCacheCacheStore
,
parseFetchCacheStore
,
}
from
'./cache-store'
}
from
'./cache-store'
/**
/**
...
@@ -24,6 +23,12 @@ export interface RenderResumeDataCache {
...
@@ -24,6 +23,12 @@ export interface RenderResumeDataCache {
* The 'set' operation is omitted to enforce immutability.
* The 'set' operation is omitted to enforce immutability.
*/
*/
readonly
fetch
:
Omit
<
FetchCacheStore
,
'set'
>
readonly
fetch
:
Omit
<
FetchCacheStore
,
'set'
>
/**
* A read-only Map store for encrypted bound args of inline server functions.
* The 'set' operation is omitted to enforce immutability.
*/
readonly
encryptedBoundArgs
:
Omit
<
EncryptedBoundArgsCacheStore
,
'set'
>
}
}
/**
/**
...
@@ -33,15 +38,24 @@ export interface RenderResumeDataCache {
...
@@ -33,15 +38,24 @@ export interface RenderResumeDataCache {
export
interface
PrerenderResumeDataCache
{
export
interface
PrerenderResumeDataCache
{
/**
/**
* A mutable Map store for values cached by the 'use cache' React hook.
* A mutable Map store for values cached by the 'use cache' React hook.
* Supports both get and set operations to build the cache during pre-rendering.
* Supports both 'get' and 'set' operations to build the cache during
* pre-rendering.
*/
*/
readonly
cache
:
UseCacheCacheStore
readonly
cache
:
UseCacheCacheStore
/**
/**
* A mutable Map store for cached fetch responses.
* A mutable Map store for cached fetch responses.
* Supports both get and set operations to build the cache during pre-rendering.
* Supports both 'get' and 'set' operations to build the cache during
* pre-rendering.
*/
*/
readonly
fetch
:
FetchCacheStore
readonly
fetch
:
FetchCacheStore
/**
* A mutable Map store for encrypted bound args of inline server functions.
* Supports both 'get' and 'set' operations to build the cache during
* pre-rendering.
*/
readonly
encryptedBoundArgs
:
EncryptedBoundArgsCacheStore
}
}
type
ResumeStoreSerialized
=
{
type
ResumeStoreSerialized
=
{
...
@@ -52,15 +66,20 @@ type ResumeStoreSerialized = {
...
@@ -52,15 +66,20 @@ type ResumeStoreSerialized = {
fetch
:
{
fetch
:
{
[
key
:
string
]:
any
[
key
:
string
]:
any
}
}
encryptedBoundArgs
:
{
[
key
:
string
]:
string
}
}
}
}
}
/**
/**
* Serializes a resume data cache into a JSON string for storage or transmission.
* Serializes a resume data cache into a JSON string for storage or
* Handles both 'use cache' values and fetch responses.
* transmission. Handles 'use cache' values, fetch responses, and encrypted
* bound args for inline server functions.
*
*
* @param resumeDataCache - The immutable cache to serialize
* @param resumeDataCache - The immutable cache to serialize
* @returns A Promise that resolves to the serialized cache as a JSON string, or 'null' if empty
* @returns A Promise that resolves to the serialized cache as a JSON string, or
* 'null' if empty
*/
*/
export
async
function
stringifyResumeDataCache
(
export
async
function
stringifyResumeDataCache
(
resumeDataCache
:
RenderResumeDataCache
|
PrerenderResumeDataCache
resumeDataCache
:
RenderResumeDataCache
|
PrerenderResumeDataCache
...
@@ -76,11 +95,12 @@ export async function stringifyResumeDataCache(
...
@@ -76,11 +95,12 @@ export async function stringifyResumeDataCache(
const
json
:
ResumeStoreSerialized
=
{
const
json
:
ResumeStoreSerialized
=
{
store
:
{
store
:
{
fetch
:
Object
.
fromEntries
(
fetch
:
Object
.
fromEntries
(
Array
.
from
(
resumeDataCache
.
fetch
.
entries
())),
stringifyFetchCacheStore
(
resumeDataCache
.
fetch
.
entries
())
),
cache
:
Object
.
fromEntries
(
cache
:
Object
.
fromEntries
(
await
stringifyUseCacheCacheStore
(
resumeDataCache
.
cache
.
entries
())
await
serializeUseCacheCacheStore
(
resumeDataCache
.
cache
.
entries
())
),
encryptedBoundArgs
:
Object
.
fromEntries
(
Array
.
from
(
resumeDataCache
.
encryptedBoundArgs
.
entries
())
),
),
},
},
}
}
...
@@ -104,6 +124,7 @@ export function createPrerenderResumeDataCache(): PrerenderResumeDataCache {
...
@@ -104,6 +124,7 @@ export function createPrerenderResumeDataCache(): PrerenderResumeDataCache {
return
{
return
{
cache
:
new
Map
(),
cache
:
new
Map
(),
fetch
:
new
Map
(),
fetch
:
new
Map
(),
encryptedBoundArgs
:
new
Map
(),
}
}
}
}
...
@@ -140,6 +161,7 @@ export function createRenderResumeDataCache(
...
@@ -140,6 +161,7 @@ export function createRenderResumeDataCache(
return
{
return
{
cache
:
new
Map
(),
cache
:
new
Map
(),
fetch
:
new
Map
(),
fetch
:
new
Map
(),
encryptedBoundArgs
:
new
Map
(),
}
}
}
}
...
@@ -156,7 +178,10 @@ export function createRenderResumeDataCache(
...
@@ -156,7 +178,10 @@ export function createRenderResumeDataCache(
return
{
return
{
cache
:
parseUseCacheCacheStore
(
Object
.
entries
(
json
.
store
.
cache
)),
cache
:
parseUseCacheCacheStore
(
Object
.
entries
(
json
.
store
.
cache
)),
fetch
:
parseFetchCacheStore
(
Object
.
entries
(
json
.
store
.
fetch
)),
fetch
:
new
Map
(
Object
.
entries
(
json
.
store
.
fetch
)),
encryptedBoundArgs
:
new
Map
(
Object
.
entries
(
json
.
store
.
encryptedBoundArgs
)
),
}
}
}
}
}
}
test/e2e/app-dir/dynamic-io/app/server-action-inline/form.tsx
0 → 100644
View file @
50bead1b
'use client'
import
{
useActionState
}
from
'react'
import
{
getSentinelValue
}
from
'../getSentinelValue'
export
function
Form
({
action
})
{
const
[
result
,
formAction
]
=
useActionState
(
action
,
'initial'
)
return
(
<
form
action=
{
formAction
}
>
<
h1
>
Inline Server Action with Dynamic IO
</
h1
>
<
button
>
Submit
</
button
>
<
p
>
{
result
}
</
p
>
<
div
id=
"page"
>
{
getSentinelValue
()
}
</
div
>
</
form
>
)
}
test/e2e/app-dir/dynamic-io/app/server-action-inline/page.tsx
0 → 100644
View file @
50bead1b
import
{
Form
}
from
'./form'
export
default
function
Page
()
{
const
value
=
'result'
return
(
<
Form
action=
{
async
()
=>
{
'use server'
return
value
}
}
/>
)
}
test/e2e/app-dir/dynamic-io/dynamic-io.server-action.test.ts
View file @
50bead1b
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
retry
}
from
'next-test-utils'
import
{
assertNoRedbox
,
retry
}
from
'next-test-utils'
describe
(
'dynamic-io'
,
()
=>
{
describe
(
'dynamic-io'
,
()
=>
{
const
{
next
}
=
nextTestSetup
({
const
{
next
,
isNextDev
}
=
nextTestSetup
({
files
:
__dirname
,
files
:
__dirname
,
})
})
...
@@ -15,4 +15,32 @@ describe('dynamic-io', () => {
...
@@ -15,4 +15,32 @@ describe('dynamic-io', () => {
expect
(
await
browser
.
elementByCss
(
'p'
).
text
()).
toBe
(
'result'
)
expect
(
await
browser
.
elementByCss
(
'p'
).
text
()).
toBe
(
'result'
)
})
})
})
})
it
(
'should not have dynamic IO errors when encoding bound args for inline server actions'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/server-action-inline'
)
expect
(
await
browser
.
elementByCss
(
'p'
).
text
()).
toBe
(
'initial'
)
await
browser
.
elementByCss
(
'button'
).
click
()
await
retry
(
async
()
=>
{
expect
(
await
browser
.
elementByCss
(
'p'
).
text
()).
toBe
(
'result'
)
})
expect
(
next
.
cliOutput
).
not
.
toMatch
(
'Error: Route "/server-action-inline"'
)
if
(
isNextDev
)
{
await
assertNoRedbox
(
browser
)
}
})
it
(
'should prerender pages with inline server actions'
,
async
()
=>
{
let
$
=
await
next
.
render$
(
'/server-action-inline'
,
{})
if
(
isNextDev
)
{
expect
(
$
(
'#layout'
).
text
()).
toBe
(
'at runtime'
)
expect
(
$
(
'#page'
).
text
()).
toBe
(
'at runtime'
)
}
else
{
expect
(
$
(
'#layout'
).
text
()).
toBe
(
'at buildtime'
)
expect
(
$
(
'#page'
).
text
()).
toBe
(
'at buildtime'
)
}
})
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment