Skip to content

Commit

Permalink
Refactor route deduplication logic
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyphreak committed Mar 2, 2024
1 parent 3728dfb commit 3f48e4b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ async function listWorkerDomainRoutes(
token,
`/zones/${domain}/workers/routes`
);
const routes = routeQuery.result;
const routes = unique(routeQuery.result);
debug(`Found '${routes.length}' matching routes`);
return routes;
}
Expand All @@ -343,7 +343,7 @@ async function listWorkerRoutes(token: string, account: string): Promise<any> {
routes.flat().map((x) => {
debug(`Found route ${x.pattern}`);
});
return routes.flat();
return unique(routes.flat());
} else {
debug('No routes found');
return [];
Expand Down
10 changes: 6 additions & 4 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ const attrDifference = (x: any[], y: any[], property: string) => {
};

const unique = (xs: any[], property = 'id'): any[] => {
return Object.values(xs.reduce((acc, obj) => ({ ...acc, [obj[property]]: obj })));
}
return Object.values(
xs.reduce((acc, obj) => ({ ...acc, [obj[property]]: obj }))
);
};

async function wrangler(config: (any) => any, fn: () => void) {
const configSaved = fs.readFileSync(`${CWD}/wrangler.toml`).toString();
Expand Down Expand Up @@ -92,8 +94,8 @@ async function deploy(
async () => {
const configRoutes = (config.routes || []) as Route[];
const publishRoutes = [...routeData, ...configRoutes];
const allRoutes = unique((await listWorkerRoutes(token, accountId)).flat());
const currentRoutes = unique(allRoutes.filter((r) => r.script == name));
const allRoutes = (await listWorkerRoutes(token, accountId)).flat();
const currentRoutes = allRoutes.filter((r) => r.script == name);
const addRoutes = attrDifference(publishRoutes, currentRoutes, 'pattern');
const delRoutes = attrDifference(currentRoutes, publishRoutes, 'pattern');
debug(`Account routes: ${JSON.stringify(allRoutes)}`);
Expand Down

0 comments on commit 3f48e4b

Please sign in to comment.