Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Server/Client Manifests from Singleton in encryption-utils #70485

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/next/src/server/app-render/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
stringToUint8Array,
} from './encryption-utils'

import type { ManifestNode } from '../../build/webpack/plugins/flight-manifest-plugin'

const textEncoder = new TextEncoder()
const textDecoder = new TextDecoder()

Expand Down Expand Up @@ -97,6 +99,13 @@ export async function decryptActionBoundArgs(
// Decrypt the serialized string with the action id as the salt.
const decryped = await decodeActionBoundArg(actionId, await encrypted)

// TODO: We can't use the client reference manifest to resolve the modules
// on the server side - instead they need to be recovered as the module
// references (proxies) again.
// For now, we'll just use an empty module map.
const ssrModuleMap: {
[moduleExport: string]: ManifestNode
} = {}
// Using Flight to deserialize the args from the string.
const deserialized = await createFromReadableStream(
new ReadableStream({
Expand All @@ -107,12 +116,11 @@ export async function decryptActionBoundArgs(
}),
{
ssrManifest: {
// TODO: We can't use the client reference manifest to resolve the modules
// on the server side - instead they need to be recovered as the module
// references (proxies) again.
// For now, we'll just use an empty module map.
moduleLoading: {},
moduleMap: {},
// moduleLoading must be null because we don't want to trigger preloads of ClientReferences
// to be added to the current execution. Instead, we'll wait for any ClientReference
// to be emitted which themselves will handle the preloading.
moduleLoading: null,
moduleMap: ssrModuleMap,
},
}
)
Expand Down
71 changes: 49 additions & 22 deletions packages/next/src/server/use-cache/use-cache-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import {
import type { StaticGenerationStore } from '../../client/components/static-generation-async-storage.external'
import { staticGenerationAsyncStorage } from '../../client/components/static-generation-async-storage.external'

import {
getClientReferenceManifestSingleton,
getServerModuleMap,
} from '../app-render/encryption-utils'

import type { ManifestNode } from '../../build/webpack/plugins/flight-manifest-plugin'

type CacheEntry = {
value: ReadableStream
stale: boolean
Expand Down Expand Up @@ -63,13 +70,6 @@ cacheHandlerMap.set('default', {
shouldRevalidateStale: false,
})

const serverManifest: any = null // TODO
const clientManifest: any = null // TODO
const ssrManifest: any = {
moduleMap: {},
moduleLoading: null,
} // TODO

// TODO: Consider moving this another module that is guaranteed to be required in a safe scope.
const runInCleanSnapshot = createSnapshot()

Expand All @@ -81,28 +81,39 @@ async function generateCacheEntry(
fn: any
): Promise<ReadableStream> {
const temporaryReferences = createServerTemporaryReferenceSet()
const [, args] = await decodeReply<any[]>(encodedArguments, serverManifest, {
temporaryReferences,
})

const [, args] = await decodeReply<any[]>(
encodedArguments,
getServerModuleMap(),
{
temporaryReferences,
}
)

// Invoke the inner function to load a new result.
const result = fn.apply(null, args)

let didError = false
let firstError: any = null

const stream = renderToReadableStream(result, clientManifest, {
environmentName: 'Cache',
temporaryReferences,
onError(error: any) {
// Report the error.
console.error(error)
if (!didError) {
didError = true
firstError = error
}
},
})
const clientReferenceManifestSingleton = getClientReferenceManifestSingleton()
Copy link
Member

@ijjk ijjk Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this singleton reliable? It looks like we are sharing it across requests although we could have concurrent requests in serverful environment/dev

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I think it's not. That's why it's sketchy but it's not net-new broken. We should fix it for encryption too at the same time if we need to fix it. Unrelated to this pr because caches can be called in the same scopes as encrypted closures.


const stream = renderToReadableStream(
result,
clientReferenceManifestSingleton.clientModules,
{
environmentName: 'Cache',
temporaryReferences,
onError(error: any) {
// Report the error.
console.error(error)
if (!didError) {
didError = true
firstError = error
}
},
}
)

const [returnStream, savedStream] = stream.tee()

Expand Down Expand Up @@ -235,6 +246,22 @@ export function cache(kind: string, id: string, fn: any) {
// server terminal. Once while generating the cache entry and once when replaying it on
// the server, which is required to pick it up for replaying again on the client.
const replayConsoleLogs = true

// TODO: We can't use the client reference manifest to resolve the modules
// on the server side - instead they need to be recovered as the module
// references (proxies) again.
// For now, we'll just use an empty module map.
const ssrModuleMap: {
[moduleExport: string]: ManifestNode
} = {}

const ssrManifest = {
// moduleLoading must be null because we don't want to trigger preloads of ClientReferences
// to be added to the consumer. Instead, we'll wait for any ClientReference to be emitted
// which themselves will handle the preloading.
moduleLoading: null,
moduleMap: ssrModuleMap,
}
return createFromReadableStream(stream, {
ssrManifest,
temporaryReferences,
Expand Down
Loading