Skip to content

Commit 70bef81

Browse files
committed
Do nanoid and svgdom as async imports
1 parent 5eed233 commit 70bef81

File tree

3 files changed

+52
-52
lines changed

3 files changed

+52
-52
lines changed

package-lock.json

Lines changed: 33 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@
4040
"i18next": "^22.4.15",
4141
"ion-js": "^5.2.0",
4242
"lodash": "^4.17.21",
43-
"nanoid": "^3.3.11",
43+
"nanoid": "^5.1.6",
4444
"svgdom": "^0.1.14",
4545
"web-push": "^3.6.3"
4646
},
4747
"devDependencies": {
4848
"@aws-sdk/types": "^3.310.0",
4949
"@types/aws-lambda": "^8.10.115",
5050
"@types/node": "^18.16.3",
51+
"@types/svgdom": "^0.1.2",
5152
"@types/uuid": "^9.0.1",
5253
"@types/web-push": "^3.3.2",
5354
"@typescript-eslint/eslint-plugin": "^5.59.1",

utils/thumbnails.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import i18n from 'i18next';
1313
import enBack from "../locales/en/apback.json";
1414
import { registerWindow, SVG, Svg } from "@svgdotjs/svg.js";
1515
import { APRenderRep, type IRenderOptions, addPrefix, render } from "@abstractplay/renderer";
16-
import { customAlphabet } from "nanoid";
17-
const nanoid = customAlphabet("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 5);
1816

1917
const REGION = "us-east-1";
2018
const s3 = new S3Client({region: REGION});
@@ -53,10 +51,17 @@ type SamplerEntry = {
5351
completed: ReservoirSampler<GameRec>;
5452
}
5553

56-
const randomInt = (max: number, min = 1): number => {
57-
min = Math.ceil(min);
58-
max = Math.floor(max);
59-
return Math.floor(Math.random() * (max - min + 1)) + min;
54+
async function makeIdGenerator() {
55+
// Dynamically import nanoid
56+
const { customAlphabet } = await import('nanoid');
57+
58+
// Create a generator with your custom alphabet
59+
const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
60+
const length = 5;
61+
62+
const generate = customAlphabet(alphabet, length);
63+
64+
return generate;
6065
}
6166

6267
export const handler: Handler = async (event: any, context?: any) => {
@@ -283,6 +288,7 @@ export const handler: Handler = async (event: any, context?: any) => {
283288
console.log("Thumbnails stored");
284289

285290
// pre-render light and dark mode versions of the SVGs
291+
console.log("Attempting to pre-render light/dark SVGs");
286292
const contextLight = {
287293
background: "#fff",
288294
strokes: "#000",
@@ -303,15 +309,16 @@ export const handler: Handler = async (event: any, context?: any) => {
303309
["light", contextLight],
304310
["dark", contextDark],
305311
]);
306-
// eslint-disable-next-line @typescript-eslint/no-var-requires
307-
const { createSVGWindow } = require("svgdom");
312+
const generateId = await makeIdGenerator();
313+
const svgdom = await import('svgdom');
314+
const { createSVGWindow } = svgdom;
308315
const window = createSVGWindow();
309316
const document = window.document;
310317

311318
// register window and document
312319
registerWindow(window, document);
313320
for (const [meta, json] of allRecs.entries()) {
314-
const prefix = nanoid();
321+
const prefix = generateId();
315322
for (const [name, context] of contexts.entries()) {
316323
const canvas = SVG(document.documentElement) as Svg;
317324
const opts: IRenderOptions = {prefix, target: canvas, colourContext: context};
@@ -328,6 +335,7 @@ export const handler: Handler = async (event: any, context?: any) => {
328335
}
329336
}
330337
}
338+
console.log("Pre-rendering complete")
331339

332340
// invalidate CloudFront distribution
333341
const cfParams: CreateInvalidationCommandInput = {

0 commit comments

Comments
 (0)