|
|
import type { ActionManifest } from '../../build/webpack/plugins/flight-client-entry-plugin' |
|
|
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths' |
|
|
import { pathHasPrefix } from '../../shared/lib/router/utils/path-has-prefix' |
|
|
import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-prefix' |
|
|
import { workAsyncStorage } from './work-async-storage.external' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function createServerModuleMap({ |
|
|
serverActionsManifest, |
|
|
}: { |
|
|
serverActionsManifest: ActionManifest |
|
|
}) { |
|
|
return new Proxy( |
|
|
{}, |
|
|
{ |
|
|
get: (_, id: string) => { |
|
|
const workers = |
|
|
serverActionsManifest[ |
|
|
process.env.NEXT_RUNTIME === 'edge' ? 'edge' : 'node' |
|
|
]?.[id]?.workers |
|
|
|
|
|
if (!workers) { |
|
|
return undefined |
|
|
} |
|
|
|
|
|
const workStore = workAsyncStorage.getStore() |
|
|
|
|
|
let workerEntry: |
|
|
| { moduleId: string | number; async: boolean } |
|
|
| undefined |
|
|
|
|
|
if (workStore) { |
|
|
workerEntry = workers[normalizeWorkerPageName(workStore.page)] |
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
workerEntry = Object.values(workers).at(0) |
|
|
} |
|
|
|
|
|
if (!workerEntry) { |
|
|
return undefined |
|
|
} |
|
|
|
|
|
const { moduleId, async } = workerEntry |
|
|
|
|
|
return { id: moduleId, name: id, chunks: [], async } |
|
|
}, |
|
|
} |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function selectWorkerForForwarding( |
|
|
actionId: string, |
|
|
pageName: string, |
|
|
serverActionsManifest: ActionManifest |
|
|
) { |
|
|
const workers = |
|
|
serverActionsManifest[ |
|
|
process.env.NEXT_RUNTIME === 'edge' ? 'edge' : 'node' |
|
|
][actionId]?.workers |
|
|
const workerName = normalizeWorkerPageName(pageName) |
|
|
|
|
|
|
|
|
if (!workers) return |
|
|
|
|
|
|
|
|
if (workers[workerName]) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
return denormalizeWorkerPageName(Object.keys(workers)[0]) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function normalizeWorkerPageName(pageName: string) { |
|
|
if (pathHasPrefix(pageName, 'app')) { |
|
|
return pageName |
|
|
} |
|
|
|
|
|
return 'app' + pageName |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function denormalizeWorkerPageName(bundlePath: string) { |
|
|
return normalizeAppPath(removePathPrefix(bundlePath, 'app')) |
|
|
} |
|
|
|