From 3f48e4bfaf4fb020af457e10cf0466ea64ed38c9 Mon Sep 17 00:00:00 2001 From: Ted Cook Date: Sat, 2 Mar 2024 05:35:14 -0600 Subject: [PATCH] Refactor route deduplication logic --- src/cloudflare.ts | 4 ++-- src/deploy.ts | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/cloudflare.ts b/src/cloudflare.ts index 41cd975..c420a58 100644 --- a/src/cloudflare.ts +++ b/src/cloudflare.ts @@ -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; } @@ -343,7 +343,7 @@ async function listWorkerRoutes(token: string, account: string): Promise { routes.flat().map((x) => { debug(`Found route ${x.pattern}`); }); - return routes.flat(); + return unique(routes.flat()); } else { debug('No routes found'); return []; diff --git a/src/deploy.ts b/src/deploy.ts index 801f8f9..278ccdf 100644 --- a/src/deploy.ts +++ b/src/deploy.ts @@ -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(); @@ -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)}`);