Skip to content

Commit

Permalink
support both serializable and non-serializable platform data
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Feb 1, 2025
1 parent 117dd0f commit e396405
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/waku/src/lib/builder/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { ReactNode } from 'react';
import type { Config } from '../../config.js';
import {
setAllEnvInternal,
iterateAllPlatformDataInternal,
iterateSerializablePlatformDataInternal,
unstable_getPlatformObject,
} from '../../server.js';
import type { EntriesPrd } from '../types.js';
Expand Down Expand Up @@ -771,7 +771,7 @@ export async function build(options: {
await mkdir(joinPath(rootDir, config.distDir, DIST_PLATFORM_DATA), {
recursive: true,
});
for (const [key, data] of iterateAllPlatformDataInternal()) {
for (const [key, data] of iterateSerializablePlatformDataInternal()) {
keys.add(key);
const destFile = joinPath(
rootDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { randomBytes } from 'node:crypto';
import type { Plugin } from 'vite';

import {
iterateAllPlatformDataInternal,
iterateSerializablePlatformDataInternal,
unstable_getPlatformObject,
} from '../../server.js';
import { SRC_ENTRIES } from '../constants.js';
Expand Down Expand Up @@ -215,7 +215,7 @@ export function deployCloudflarePlugin(opts: {
mkdirSync(path.join(workerDistDir, DIST_PLATFORM_DATA), {
recursive: true,
});
for (const [key, data] of iterateAllPlatformDataInternal()) {
for (const [key, data] of iterateSerializablePlatformDataInternal()) {
keys.add(key);
const destFile = path.join(
workerDistDir,
Expand Down
6 changes: 5 additions & 1 deletion packages/waku/src/router/define-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,11 @@ globalThis.__WAKU_ROUTER_PREFETCH__ = (path) => {
});
}

await unstable_setPlatformData('defineRouterPathConfigs', pathConfig);
await unstable_setPlatformData(
'defineRouterPathConfigs',
pathConfig,
true,
);
return tasks;
});

Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/router/fs-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function fsRouter(
}
// build only - skip in dev
if (platformObject.buildOptions?.unstable_phase) {
await unstable_setPlatformData('fsRouterFiles', files);
await unstable_setPlatformData('fsRouterFiles', files, true);
}
for (const file of files) {
const mod = await loadPage(file);
Expand Down
21 changes: 15 additions & 6 deletions packages/waku/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ export function getEnv(key: string): string | undefined {
/**
* This is an internal function and not for public use.
*/
export function iterateAllPlatformDataInternal(): Iterable<[string, unknown]> {
const platformData: Record<string, unknown> =
export function iterateSerializablePlatformDataInternal(): Iterable<
[string, unknown]
> {
const platformData: Record<string, [unknown, boolean]> =
(globalThis as any).__WAKU_SERVER_PLATFORM_DATA__ || {};
return Object.entries(platformData);
return Object.entries(platformData).map(([key, [data]]) => [key, data]);
}

/**
Expand All @@ -32,24 +34,31 @@ export function setPlatformDataLoaderInternal(
(globalThis as any).__WAKU_SERVER_PLATFORM_DATA_LOADER__ = loader;
}

// data must be JSON serializable
export async function unstable_setPlatformData<T>(
key: string,
data: T,
serializable: boolean,
): Promise<void> {
((globalThis as any).__WAKU_SERVER_PLATFORM_DATA__ ||= {})[key] = data;
const platformData: Record<string, [unknown, boolean]> =
(globalThis as any).__WAKU_SERVER_PLATFORM_DATA__ || {};
platformData[key] = [data, serializable];
}

export async function unstable_getPlatformData<T>(
key: string,
): Promise<T | undefined> {
const platformData: Record<string, [unknown, boolean]> =
(globalThis as any).__WAKU_SERVER_PLATFORM_DATA__ || {};
const item = platformData[key];
if (item) {
return item[0] as T;
}
const loader: ((key: string) => Promise<unknown>) | undefined = (
globalThis as any
).__WAKU_SERVER_PLATFORM_DATA_LOADER__;
if (loader) {
return loader(key) as T;
}
return (globalThis as any).__WAKU_SERVER_PLATFORM_DATA__?.[key];
}

export function unstable_getHeaders(): Readonly<Record<string, string>> {
Expand Down

0 comments on commit e396405

Please sign in to comment.