in src/backend/inventory/package-versions-table-widget-function.lambda.ts [14:85]
export async function handler({ key, description, widgetContext }: Event): Promise<string | { markdown: string }> {
console.log(`Event: ${JSON.stringify({ key, description, widgetContext }, null, 2)}`);
try {
const bucketName = requireEnv('BUCKET_NAME');
let { Body, ContentEncoding, LastModified } = await s3().getObject({
Bucket: bucketName,
Key: key,
}).promise();
// If it was compressed, de-compress it now...
if (ContentEncoding === 'gzip') {
Body = gunzipSync(Buffer.from(Body! as any));
}
const list = Array.from((JSON.parse(Body!.toString('utf-8')) as string[])
.reduce(
(map, entry) => {
// Split on the @ that is not at the beginning of the string
const [name, version] = entry.split(/(?!^)@/);
if (!map.has(name)) {
map.set(name, []);
}
map.get(name)!.push(version);
return map;
},
new Map<string, string[]>(),
)
.entries())
.sort(([l], [r]) => l.localeCompare(r));
// Trying to ensure we don't cause the dashboard to hang due to large DOM.
const maxCount = 100;
const objectUrl = `${widgetContext.domain}/s3/object/${bucketName}?prefix=${key}`;
return {
markdown: [
description,
...(list.length > maxCount
? [
`Showing only the first ${maxCount} packages.`,
`The complete list can be obtained [from S3](${objectUrl}).`,
'',
]
: []),
'Id | Package Name | Count | Versions',
'--:|--------------|-------|---------',
...list.slice(0, maxCount).map(([name, versions], index) => {
versions = semverSort(versions).reverse();
return `${index + 1} | \`${name}\` | ${versions.length} | ${versions.map((v) => `[\`${v}\`](${s3ConsoleUrl(bucketName, name, v)})`).join(', ')}`;
}),
'',
`Last updated: \`${LastModified?.toISOString() ?? 'N/A'}\``,
].join('\n'),
};
} catch (error) {
if (error instanceof Error) {
return {
markdown: [
'**⚠️ An error occurred**',
`- **name:** \`${error.name}\``,
`- **message:** ${error.message}`,
'- **stack:**',
' ```',
error.stack?.replace(/^/g, ' '),
' ```',
].join('\n'),
};
};
throw error;
}
}