Skip to content

Commit

Permalink
docs: add caching to ROA signing
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Nov 25, 2024
1 parent 55ee44a commit 4ec7868
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions website/roa-loader/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
const { inspect } = require('util');
const { createHash } = require('node:crypto');
const { inspect } = require('node:util');

const { urlToRequest } = require('loader-utils');

const signingUrl = new URL('https://api.apify.com/v2/tools/encode-and-sign');
signingUrl.searchParams.set('token', process.env.APIFY_SIGNING_TOKEN);
const queue = [];
const cache = {};
let working = false;

function hash(source) {
return createHash('sha1').update(source).digest('hex');
}

async function getHash(source) {
const cacheKey = hash(source);

if (cache[cacheKey]) {
return cache[cacheKey];
}

const memory = source.match(/playwright|puppeteer/i) ? 4096 : 1024;
const res = await (await fetch(signingUrl, {
method: 'POST',
Expand All @@ -32,13 +44,14 @@ async function getHash(source) {

const body = await res.json();

await new Promise((resolve) => setTimeout(resolve, 100));

if (!body.data || !body.data.encoded) {
console.error(`Signing failed:' ${inspect(body.error) || 'Unknown error'}`, body);
return 'invalid-token';
}

cache[cacheKey] = body.data.encoded;
await new Promise((resolve) => setTimeout(resolve, 100));

return body.data.encoded;
}

Expand Down

0 comments on commit 4ec7868

Please sign in to comment.