Normalize baseURL once for trigger and stream endpoints
Co-authored-by: Eric Allam <eric@trigger.dev>
This commit is contained in:
@@ -273,6 +273,68 @@ describe("TriggerChatTransport", function () {
|
||||
expect(observedStreamPath).toBe("/realtime/v1/streams/run_trailing_baseurl/chat-stream");
|
||||
});
|
||||
|
||||
it("normalizes repeated trailing slashes in baseURL for stream URLs", async function () {
|
||||
let observedStreamPath: string | undefined;
|
||||
|
||||
const server = await startServer(function (req, res) {
|
||||
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {
|
||||
res.writeHead(200, {
|
||||
"content-type": "application/json",
|
||||
"x-trigger-jwt": "pk_run_multi_trailing_baseurl",
|
||||
});
|
||||
res.end(JSON.stringify({ id: "run_multi_trailing_baseurl" }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === "GET") {
|
||||
observedStreamPath = req.url ?? "";
|
||||
}
|
||||
|
||||
if (
|
||||
req.method === "GET" &&
|
||||
req.url === "/realtime/v1/streams/run_multi_trailing_baseurl/chat-stream"
|
||||
) {
|
||||
res.writeHead(200, {
|
||||
"content-type": "text/event-stream",
|
||||
});
|
||||
writeSSE(
|
||||
res,
|
||||
"1-0",
|
||||
JSON.stringify({ type: "text-start", id: "multi_trailing_1" })
|
||||
);
|
||||
writeSSE(
|
||||
res,
|
||||
"2-0",
|
||||
JSON.stringify({ type: "text-end", id: "multi_trailing_1" })
|
||||
);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(404);
|
||||
res.end();
|
||||
});
|
||||
|
||||
const transport = new TriggerChatTransport({
|
||||
task: "chat-task",
|
||||
accessToken: "pk_trigger",
|
||||
baseURL: `${server.url}///`,
|
||||
stream: "chat-stream",
|
||||
});
|
||||
|
||||
const stream = await transport.sendMessages({
|
||||
trigger: "submit-message",
|
||||
chatId: "chat-multi-trailing-baseurl",
|
||||
messageId: undefined,
|
||||
messages: [],
|
||||
abortSignal: undefined,
|
||||
});
|
||||
|
||||
const chunks = await readChunks(stream);
|
||||
expect(chunks).toHaveLength(2);
|
||||
expect(observedStreamPath).toBe("/realtime/v1/streams/run_multi_trailing_baseurl/chat-stream");
|
||||
});
|
||||
|
||||
it("supports baseURL path prefixes for trigger and stream routes", async function () {
|
||||
let observedTriggerPath: string | undefined;
|
||||
let observedStreamPath: string | undefined;
|
||||
|
||||
@@ -145,7 +145,7 @@ export class TriggerChatTransport<
|
||||
this.payloadMapper = resolvePayloadMapper<UI_MESSAGE, PAYLOAD>(options.payloadMapper);
|
||||
this.triggerOptions = options.triggerOptions;
|
||||
this.runStore = options.runStore ?? new InMemoryTriggerChatRunStore();
|
||||
this.baseURL = options.baseURL ?? "https://api.trigger.dev";
|
||||
this.baseURL = normalizeBaseUrl(options.baseURL ?? "https://api.trigger.dev");
|
||||
this.previewBranch = options.previewBranch;
|
||||
this.requestOptions = options.requestOptions;
|
||||
this.triggerClient = new ApiClient(
|
||||
@@ -374,11 +374,10 @@ export class TriggerChatTransport<
|
||||
}
|
||||
|
||||
private createStreamUrl(runId: string, streamKey: string): string {
|
||||
const normalizedBaseUrl = this.baseURL.replace(/\/$/, "");
|
||||
const encodedRunId = encodeURIComponent(runId);
|
||||
const encodedStreamKey = encodeURIComponent(streamKey);
|
||||
|
||||
return `${normalizedBaseUrl}/realtime/v1/streams/${encodedRunId}/${encodedStreamKey}`;
|
||||
return `${this.baseURL}/realtime/v1/streams/${encodedRunId}/${encodedStreamKey}`;
|
||||
}
|
||||
|
||||
private async markRunInactiveAndDelete(runState: TriggerChatRunState) {
|
||||
@@ -460,6 +459,10 @@ function resolvePayloadMapper<
|
||||
return createDefaultPayload as TriggerChatPayloadMapper<UI_MESSAGE, PAYLOAD>;
|
||||
}
|
||||
|
||||
function normalizeBaseUrl(baseURL: string) {
|
||||
return baseURL.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
function createTransportRequest<UI_MESSAGE extends UIMessage>(
|
||||
options: TriggerChatSendMessagesOptions<UI_MESSAGE>
|
||||
): TriggerChatTransportRequest<UI_MESSAGE> {
|
||||
|
||||
Reference in New Issue
Block a user