Skip to content

Commit

Permalink
Auto update endpoint URL on redirect (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericallam authored Dec 12, 2023
1 parent 0a51ef3 commit 6d4676f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions apps/webapp/app/services/endpointApi.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class EndpointApi {
"x-trigger-api-key": this.apiKey,
"x-trigger-action": "INDEX_ENDPOINT",
},
redirect: "manual",
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { fromZodError } from "zod-validation-error";
import { IndexEndpointStats } from "@trigger.dev/core";
import { RegisterHttpEndpointService } from "../triggers/registerHttpEndpoint.server";
import { RegisterWebhookService } from "../triggers/registerWebhook.server";
import { EndpointIndex } from "@trigger.dev/database";

export class PerformEndpointIndexService {
#prismaClient: PrismaClient;
Expand All @@ -29,7 +30,7 @@ export class PerformEndpointIndexService {
this.#prismaClient = prismaClient;
}

public async call(id: string) {
public async call(id: string, redirectCount = 0): Promise<EndpointIndex> {
const endpointIndex = await this.#prismaClient.endpointIndex.update({
where: {
id,
Expand Down Expand Up @@ -66,6 +67,39 @@ export class PerformEndpointIndexService {
});
}

if (isRedirect(response.status)) {
// Update the endpoint URL with the response.headers.location
logger.debug("Endpoint is redirecting", {
headers: Object.fromEntries(response.headers.entries()),
});

const location = response.headers.get("location");

if (!location) {
return updateEndpointIndexWithError(this.#prismaClient, id, {
message: `Endpoint ${endpointIndex.endpoint.url} is redirecting but no location header is present`,
});
}

if (redirectCount > 5) {
return updateEndpointIndexWithError(this.#prismaClient, id, {
message: `Endpoint ${endpointIndex.endpoint.url} is redirecting too many times`,
});
}

await this.#prismaClient.endpoint.update({
where: {
id: endpointIndex.endpoint.id,
},
data: {
url: location,
},
});

// Re-run the endpoint index
return await this.call(id, redirectCount + 1);
}

if (response.status === 401) {
const body = await safeBodyFromResponse(response, errorParser);

Expand Down Expand Up @@ -378,3 +412,10 @@ async function updateEndpointIndexWithError(
},
});
}

const redirectStatus = [301, 302, 303, 307, 308];
const redirectStatusSet = new Set(redirectStatus);

function isRedirect(status: number) {
return redirectStatusSet.has(status);
}
3 changes: 2 additions & 1 deletion apps/webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build:remix": "remix build",
"build:server": "esbuild --platform=node --format=cjs ./server.ts --outdir=build",
"dev": "cross-env PORT=3030 remix dev",
"dev:manual": "cross-env PORT=3030 remix dev -c \"node ./build/server.js\"",
"format": "prettier --write .",
"lint": "eslint --cache --cache-location ./node_modules/.cache/eslint .",
"start": "cross-env NODE_ENV=production node --max-old-space-size=8192 ./build/server.js",
Expand Down Expand Up @@ -188,4 +189,4 @@
"engines": {
"node": ">=16.0.0"
}
}
}

0 comments on commit 6d4676f

Please sign in to comment.