1- const { inspect } = require ( 'util' ) ;
1+ const { createHash } = require ( 'node:crypto' ) ;
2+ const { inspect } = require ( 'node:util' ) ;
23
34const { urlToRequest } = require ( 'loader-utils' ) ;
45
56const signingUrl = new URL ( 'https://api.apify.com/v2/tools/encode-and-sign' ) ;
67signingUrl . searchParams . set ( 'token' , process . env . APIFY_SIGNING_TOKEN ) ;
78const queue = [ ] ;
9+ const cache = { } ;
810let working = false ;
911
12+ function hash ( source ) {
13+ return createHash ( 'sha1' ) . update ( source ) . digest ( 'hex' ) ;
14+ }
15+
1016async function getHash ( source ) {
17+ const cacheKey = hash ( source ) ;
18+
19+ if ( cache [ cacheKey ] ) {
20+ return cache [ cacheKey ] ;
21+ }
22+
1123 const memory = source . match ( / p l a y w r i g h t | p u p p e t e e r / i) ? 4096 : 1024 ;
1224 const res = await ( await fetch ( signingUrl , {
1325 method : 'POST' ,
@@ -25,14 +37,15 @@ async function getHash(source) {
2537 } ,
2638 } ) ) . json ( ) ;
2739
28- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
29-
3040 if ( ! res . data || ! res . data . encoded ) {
3141 // eslint-disable-next-line no-console
3242 console . error ( `Signing failed:' ${ inspect ( res . error ) || 'Unknown error' } ` , res ) ;
3343 return 'invalid-token' ;
3444 }
3545
46+ cache [ cacheKey ] = res . data . encoded ;
47+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
48+
3649 return res . data . encoded ;
3750}
3851
@@ -68,7 +81,7 @@ module.exports = async function (code) {
6881
6982 // eslint-disable-next-line no-console
7083 console . log ( `Signing ${ urlToRequest ( this . resourcePath ) } ...` , { working, queue : queue . length } ) ;
71- const hash = await encodeAndSign ( code ) ;
84+ const codeHash = await encodeAndSign ( code ) ;
7285
73- return { code, hash } ;
86+ return { code, hash : codeHash } ;
7487} ;
0 commit comments