@@ -39,6 +39,7 @@ const extractExtension = (url) => {
39
39
*/
40
40
async function fetchImage ( ctx , imageUrl ) {
41
41
const { log } = ctx ;
42
+ log . debug ( 'fetching image: ' , imageUrl ) ;
42
43
const resp = await fetch ( imageUrl ) ;
43
44
if ( ! resp . ok ) {
44
45
throw errorWithResponse ( 502 , `Failed to fetch image: ${ imageUrl } (${ resp . status } )` ) ;
@@ -50,8 +51,6 @@ async function fetchImage(ctx, imageUrl) {
50
51
. map ( ( byte ) => byte . toString ( 16 ) . padStart ( 2 , '0' ) )
51
52
. join ( '' ) ;
52
53
53
- log . debug ( 'got hash: ' , imageUrl , hash ) ;
54
-
55
54
return {
56
55
data,
57
56
sourceUrl : imageUrl ,
@@ -134,19 +133,29 @@ export async function extractAndReplaceImages(ctx, product) {
134
133
return newUrl ;
135
134
} ;
136
135
137
- await Promise . all ( [
138
- processQueue ( [ ...product . images ?? [ ] ] , async ( image ) => {
139
- const newUrl = await processImage ( image . url ) ;
140
- if ( newUrl ) {
136
+ /**
137
+ * @type {[img: string, setImage: (value: string) => void][] }
138
+ */
139
+ const arr = [
140
+ ...( product . images ?? [ ] ) . map ( ( image ) => [
141
+ image . url ,
142
+ ( newUrl ) => {
141
143
image . url = newUrl ;
142
- }
143
- } ) ,
144
- processQueue ( [ ...product . variants ?? [ ] ] , async ( variant ) => {
145
- const newUrl = await processImage ( variant . image ) ;
146
- if ( newUrl ) {
144
+ } ,
145
+ ] ) ,
146
+ ...( product . variants ?? [ ] ) . map ( ( variant ) => [
147
+ variant . image ,
148
+ ( newUrl ) => {
147
149
variant . image = newUrl ;
148
- }
149
- } ) ,
150
- ] ) ;
150
+ } ,
151
+ ] ) ,
152
+ ] ;
153
+
154
+ await processQueue ( arr , async ( [ imageUrl , setImage ] ) => {
155
+ const newUrl = await processImage ( imageUrl ) ;
156
+ if ( newUrl ) {
157
+ setImage ( newUrl ) ;
158
+ }
159
+ } ) ;
151
160
return product ;
152
161
}
0 commit comments