Skip to content

Commit

Permalink
Merge branch 'main' into worker-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
nicktrn committed Dec 12, 2023
2 parents 8edd2c4 + 6d4676f commit f130fbd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 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 @@ -126,6 +126,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"
}
}
}
2 changes: 1 addition & 1 deletion docs/documentation/quickstarts/hono.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Our `@trigger.dev/hono` package provides two different ways of configuring the n

### Cloudflare Workers

Becase environment variables in Cloudflare Workers aren't available in the global scope, but are instead available only inside the fetch handler, we need to use the `addMiddleware` function to add the necessary middleware to your Hono app.
Because environment variables in Cloudflare Workers aren't available in the global scope, but are instead available only inside the fetch handler, we need to use the `addMiddleware` function to add the necessary middleware to your Hono app.

```ts
import { Hono } from "hono";
Expand Down

0 comments on commit f130fbd

Please sign in to comment.