Skip to content

Commit 916209c

Browse files
committed
refactor(rsc-mf): normalize raw action ids before bridge prefix
1 parent aa01c52 commit 916209c

2 files changed

Lines changed: 15 additions & 21 deletions

File tree

tests/integration/rsc-mf/remote/src/runtime/registerServerCallback.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,23 @@ import {
77

88
let registeredCallbackKey = '';
99
const ALIAS_TOKEN_PATTERN = /^[A-Za-z0-9_.-]+$/;
10+
const getNormalizedRawActionId = (rawActionId: string) => {
11+
const normalizedRawActionId = rawActionId.trim();
12+
if (!normalizedRawActionId || /\s/.test(normalizedRawActionId)) {
13+
throw new Error(
14+
`Remote action id must be a non-empty token without whitespace. Received: ${rawActionId}`,
15+
);
16+
}
17+
return normalizedRawActionId;
18+
};
1019
const getHostActionId = (rawActionId: string, remoteAlias: string) => {
11-
if (rawActionId.startsWith('remote:')) {
12-
return rawActionId;
20+
const normalizedRawActionId = getNormalizedRawActionId(rawActionId);
21+
if (normalizedRawActionId.startsWith('remote:')) {
22+
return normalizedRawActionId;
1323
}
1424

1525
// Align with RSC bridge action-id format expected by host runtime plugin.
16-
return `remote:${remoteAlias}:${rawActionId}`;
26+
return `remote:${remoteAlias}:${normalizedRawActionId}`;
1727
};
1828
const getNormalizedRemoteActionUrl = (remoteOrigin: string) => {
1929
const url = new URL(remoteOrigin);

tests/integration/rsc-mf/tests/index.test.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -618,27 +618,11 @@ function runTests({ mode }: TestConfig) {
618618
expect(runtimeRegisterSource).not.toContain('127.0.0.1:');
619619
expect(runtimeRegisterSource).not.toContain('window.location');
620620
expect(runtimeRegisterSource).toContain("remoteAlias = 'rscRemote'");
621-
expect(runtimeRegisterSource).toContain(
622-
'const ALIAS_TOKEN_PATTERN = /^[A-Za-z0-9_.-]+$/',
623-
);
624-
expect(runtimeRegisterSource).toContain(
625-
'const normalizedRemoteAlias = remoteAlias.trim()',
626-
);
627-
expect(runtimeRegisterSource).toContain(
628-
"normalizedRemoteAlias.includes(':')",
629-
);
630-
expect(runtimeRegisterSource).toContain(
631-
'!ALIAS_TOKEN_PATTERN.test(normalizedRemoteAlias)',
632-
);
633621
expect(runtimeRegisterSource).toContain(
634622
'Remote alias must be a non-empty token (letters, numbers, "-", "_", ".") without ":" delimiters',
635623
);
636-
expect(runtimeRegisterSource).toContain(
637-
"if (rawActionId.startsWith('remote:'))",
638-
);
639-
expect(runtimeRegisterSource).toContain(
640-
'return `remote:${remoteAlias}:${rawActionId}`',
641-
);
624+
expect(runtimeRegisterSource).toContain("startsWith('remote:')");
625+
expect(runtimeRegisterSource).toContain('return `remote:${remoteAlias}:');
642626
expect(runtimeRegisterSource).toContain("'x-rsc-action': hostActionId");
643627
expect(runtimeRegisterSource).toContain("method: 'POST'");
644628
expect(runtimeRegisterSource).toContain("Accept: 'text/x-component'");

0 commit comments

Comments
 (0)