Skip to content

Commit df44cd1

Browse files
committed
fix(zalo): reuse account-agnostic media route
1 parent be1e200 commit df44cd1

6 files changed

Lines changed: 92 additions & 17 deletions

File tree

‎docs/.generated/plugin-sdk-api-baseline.sha256‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ a94c9ff59cc361b04f8731a47d722c4bc88cfabf942221369a8cb67bdcda7249 module/tool-pl
143143
89846974257b7551d46a514902998d0592fc67b1d8ce036c6514d7b9cd1bc159 module/tool-results
144144
9d6ab352913a573b226e054e1dc8c6d088493aea9954950c65585923b5b6895a module/tool-send
145145
541df9dea799f25e83ea483d481ecebc5b91c016effab593c54d3efe3ee6517b module/web-media
146-
0d5ae2f16bb33c0fe4f85eea3da80283e89e2995a7406b98d24805feeee4f7ca module/webhook-ingress
146+
7c43c2cd0f09d72cf5f7cd7df582767e436acb697a81722dde47dbe171c66d2e module/webhook-ingress
147147
af8c5e1c84ec9d365a7aa0d15eb63f0dd9e7b50526023e6212bba020c229f224 module/webhook-request-guards
148148
1cc469eacda2818a116ab7b63847b9ddf86225e8d02e62fac48e618ba41a9852 module/widget-html
149149
9161b36ec0ab062ea41b363c894fcd672a7727f21cb726739f99f9c184fce69d module/zod

‎extensions/zalo/src/monitor.lifecycle.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,15 @@ describe("monitorZaloProvider lifecycle", () => {
359359
webhookUrl: "https://example.com/hooks/zalo",
360360
});
361361

362-
await expect(run).rejects.toThrow("route replacement denied");
362+
await expect(run).rejects.toThrow("route reuse denied");
363363

364364
expect(getWebhookInfoMock).not.toHaveBeenCalled();
365365
expect(getUpdatesMock).not.toHaveBeenCalled();
366366
expect(registry.httpRoutes).toEqual([existingRoute]);
367367
expect(statusSink).toHaveBeenCalledWith({
368368
connected: false,
369369
lifecycle: "recovering",
370-
lastError: expect.stringContaining("route replacement denied"),
370+
lastError: expect.stringContaining("route reuse denied"),
371371
});
372372
expect(statusSink).not.toHaveBeenCalledWith(expect.objectContaining({ lifecycle: "ready" }));
373373
expect(runtime.log).toHaveBeenCalledWith("[default] Zalo provider stopped mode=polling");

‎extensions/zalo/src/monitor.polling.media-reply.test-support.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ describe("Zalo polling media replies", () => {
555555
hostedMediaRoutes[0],
556556
"active Zalo hosted-media route",
557557
);
558+
expect(hostedMediaRoute).toBe(firstHostedMediaRoute);
558559

559560
await writeHostedZaloMediaFixture({
560561
id: "abc123abc123abc123abc123",

‎extensions/zalo/src/monitor.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,19 @@ const loadZaloWebhookModule = createLazyRuntimeModule(async () => ({
142142

143143
function registerSharedHostedMediaRoute(params: {
144144
path: string;
145-
accountId: string;
146145
log?: (message: string) => void;
147146
}): () => void {
148147
const routeKey = canonicalizeWebhookRouteKey(params.path);
149-
// Every account registers the account-agnostic handler so a swapped registry is populated.
150-
// Exact unregister handles stay leased until the final account releases the shared route.
148+
// Every account attempts the account-agnostic route so the first acquire after a registry swap
149+
// repopulates it; exact same-owner conflicts reuse the existing route without replacement.
151150
const unregister = registerPluginHttpRoute({
152151
auth: "plugin",
153152
match: "prefix",
154153
path: params.path,
155154
pluginId: "zalo",
156155
source: "zalo-hosted-media",
157-
accountId: params.accountId,
158156
log: params.log,
159-
replaceExisting: true,
157+
reuseExistingSameOwner: true,
160158
throwOnFailure: true,
161159
handler: async (req, res) => {
162160
const handled = await tryHandleHostedZaloMediaRequest(req, res);
@@ -931,7 +929,6 @@ export async function monitorZaloProvider(options: ZaloMonitorOptions): Promise<
931929
if (hostedMediaRoutePath) {
932930
const unregisterHostedMediaRoute = registerSharedHostedMediaRoute({
933931
path: hostedMediaRoutePath,
934-
accountId: account.accountId,
935932
log: runtime.log,
936933
});
937934
stopHandlers.push(unregisterHostedMediaRoute);

‎src/plugins/http-registry.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,65 @@ describe("registerPluginHttpRoute", () => {
238238
});
239239
});
240240

241+
it("reuses an exact same-owner route without replacing its handler", () => {
242+
const { registry, logs, register } = createLoggedRouteHarness();
243+
const firstHandler = vi.fn();
244+
const secondHandler = vi.fn();
245+
const unregisterFirst = register({
246+
path: "/plugins/shared",
247+
auth: "plugin",
248+
handler: firstHandler,
249+
pluginId: "demo",
250+
source: "shared-route",
251+
});
252+
253+
const unregisterSecond = register({
254+
path: "/PLUGINS//SHARED/",
255+
auth: "plugin",
256+
handler: secondHandler,
257+
pluginId: "demo",
258+
source: "shared-route",
259+
reuseExistingSameOwner: true,
260+
throwOnFailure: true,
261+
});
262+
263+
expect(registry.httpRoutes).toHaveLength(1);
264+
expect(registry.httpRoutes[0]?.handler).toBe(firstHandler);
265+
expect(logs.at(-1)).toContain("reusing existing webhook path");
266+
unregisterSecond();
267+
expect(registry.httpRoutes).toHaveLength(1);
268+
unregisterFirst();
269+
expect(registry.httpRoutes).toHaveLength(0);
270+
});
271+
272+
it.each([
273+
{ pluginId: "other", source: "shared-route" },
274+
{ pluginId: "demo", source: "other-route" },
275+
])("rejects route reuse by $pluginId/$source", ({ pluginId, source }) => {
276+
const { registry, register } = createLoggedRouteHarness();
277+
const firstHandler = vi.fn();
278+
register({
279+
path: "/plugins/shared",
280+
auth: "plugin",
281+
handler: firstHandler,
282+
pluginId: "demo",
283+
source: "shared-route",
284+
});
285+
286+
expect(() =>
287+
register({
288+
path: "/plugins/shared",
289+
auth: "plugin",
290+
pluginId,
291+
source,
292+
reuseExistingSameOwner: true,
293+
throwOnFailure: true,
294+
}),
295+
).toThrow("plugin: route reuse denied");
296+
expect(registry.httpRoutes).toHaveLength(1);
297+
expect(registry.httpRoutes[0]?.handler).toBe(firstHandler);
298+
});
299+
241300
it("finds a canonical exact alias behind an earlier prefix overlap", () => {
242301
const { registry, register } = createLoggedRouteHarness();
243302
register({

‎src/plugins/http-registry.ts‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export function registerPluginHttpRoute(params: {
2828
gatewayRuntimeScopeSurface?: PluginHttpRouteRegistration["gatewayRuntimeScopeSurface"];
2929
/** Replace an existing canonical route owned by the same plugin and compatible route source. */
3030
replaceExisting?: boolean;
31+
/** Reuse an existing canonical route only when its nonempty plugin and source owners match. */
32+
reuseExistingSameOwner?: boolean;
3133
/** Throw when the route cannot be registered instead of returning a no-op cleanup. */
3234
throwOnFailure?: boolean;
3335
pluginId?: string;
@@ -83,24 +85,40 @@ export function registerPluginHttpRoute(params: {
8385
`plugin: route conflict at ${normalizedPath} (${routeMatch})${suffix}`,
8486
);
8587
}
88+
const requestedOwner = normalizeOptionalString(params.pluginId);
89+
const requestedSource = normalizeOptionalString(params.source);
90+
const mismatchedOwner = canonicalMatches.find(
91+
(route) =>
92+
normalizeOptionalString(route.pluginId) !== requestedOwner ||
93+
normalizeOptionalString(route.source) !== requestedSource,
94+
);
95+
if (!params.replaceExisting && params.reuseExistingSameOwner) {
96+
if (requestedOwner !== undefined && requestedSource !== undefined && !mismatchedOwner) {
97+
params.log?.(
98+
`plugin: reusing existing webhook path ${normalizedPath} (${routeMatch}) (${requestedOwner}/${requestedSource})`,
99+
);
100+
return noopUnregister;
101+
}
102+
const conflictingOwner = mismatchedOwner ?? existing;
103+
return rejectRegistration(
104+
`plugin: route reuse denied for ${normalizedPath} (${routeMatch})${suffix}; owned by ${conflictingOwner.pluginId ?? "unknown-plugin"} (${conflictingOwner.source ?? "unknown-source"})`,
105+
);
106+
}
86107
if (!params.replaceExisting) {
87108
return rejectRegistration(
88109
`plugin: route conflict at ${normalizedPath} (${routeMatch})${suffix}; owned by ${existing.pluginId ?? "unknown-plugin"} (${existing.source ?? "unknown-source"})`,
89110
);
90111
}
91-
const replacementOwner = normalizeOptionalString(params.pluginId);
92-
const replacementSource = normalizeOptionalString(params.source);
93112
// Source-less same-plugin replacement shipped before route-source ownership.
94113
// Preserve it only when both sides omit source; otherwise require an exact source match.
95-
const mismatchedOwner = canonicalMatches.find(
114+
const incompatibleReplacement = canonicalMatches.find(
96115
(route) =>
97-
normalizeOptionalString(route.pluginId) !== replacementOwner ||
98-
(replacementOwner !== undefined &&
99-
normalizeOptionalString(route.source) !== replacementSource),
116+
normalizeOptionalString(route.pluginId) !== requestedOwner ||
117+
(requestedOwner !== undefined && normalizeOptionalString(route.source) !== requestedSource),
100118
);
101-
if (mismatchedOwner) {
119+
if (incompatibleReplacement) {
102120
return rejectRegistration(
103-
`plugin: route replacement denied for ${normalizedPath} (${routeMatch})${suffix}; owned by ${mismatchedOwner.pluginId ?? "unknown-plugin"} (${mismatchedOwner.source ?? "unknown-source"})`,
121+
`plugin: route replacement denied for ${normalizedPath} (${routeMatch})${suffix}; owned by ${incompatibleReplacement.pluginId ?? "unknown-plugin"} (${incompatibleReplacement.source ?? "unknown-source"})`,
104122
);
105123
}
106124
const pluginHint = params.pluginId ? ` (${params.pluginId})` : "";

0 commit comments

Comments
 (0)