From bde40f4430bc26a058598a64eeeedbb5ab35eb57 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Fri, 10 Nov 2023 16:57:48 +0200 Subject: [PATCH] NodeResolver: Refactor the recursion to an async-await loop Change-type: patch --- lib/resolve/resolvers/nodeResolver.ts | 52 ++++++++++++++------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/lib/resolve/resolvers/nodeResolver.ts b/lib/resolve/resolvers/nodeResolver.ts index cd40d09..58db06b 100644 --- a/lib/resolve/resolvers/nodeResolver.ts +++ b/lib/resolve/resolvers/nodeResolver.ts @@ -27,35 +27,37 @@ import { Bundle, FileInfo, Resolver } from '../resolver'; import { ParsedPathPlus } from '../utils'; const versionTest = RegExp.prototype.test.bind(/^[0-9]+\.[0-9]+\.[0-9]+$/); -const getDeviceTypeVersions = memoize((deviceType: string) => { - const get = async (prev: string[], url: string): Promise => { - const res = (await getAsync({ - url, - json: true, - })).body as { results: Array<{ name: string }>; next?: string }; - // explicit casting here, as typescript interprets the following statement as {}[] - const curr: string[] = res.results - .map(({ name }) => name) - .filter(versionTest); - const tags = prev.concat(curr); - - if (res.next != null) { - return get(tags, res.next); - } else { - return tags; +const getDeviceTypeVersions = memoize( + async (deviceType: string): Promise => { + const tags: string[] = []; + // 100 is the max page size + let nextUrl: + | string + | undefined = `https://hub.docker.com/v2/repositories/resin/${deviceType}-node/tags/?page_size=100`; + while (nextUrl != null) { + const res = ( + await getAsync({ + url: nextUrl, + json: true, + }) + ).body as { results: Array<{ name: string }>; next?: string }; + + const curr: string[] = res.results + .map(({ name }) => name) + .filter(versionTest); + + tags.push(...curr); + nextUrl = res.next; } - }; - - // 100 is the max page size - return get( - [], - `https://hub.docker.com/v2/repositories/resin/${deviceType}-node/tags/?page_size=100`, - ); -}, { + + return tags; + }, + { promise: true, primitive: true, maxAge: 3600 * 1000, // 1 hour -}); + }, +); export class NodeResolver implements Resolver { public priority = 0;