Skip to content

Commit

Permalink
NodeResolver: Refactor the recursion to an async-await loop
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
thgreasi committed Nov 10, 2023
1 parent 82f90b2 commit 7638f63
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions lib/resolve/resolvers/nodeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,35 @@ 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<string[]> => {
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<string[]> => {
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;
Expand Down

0 comments on commit 7638f63

Please sign in to comment.