From 3855127d563ef5452529beb634126aed08d18e9f Mon Sep 17 00:00:00 2001 From: Enngage Date: Wed, 21 Oct 2020 11:24:50 +0200 Subject: [PATCH] feat: adds ability to set artificial delay between asset download requests & wait until asset is downloaded before proceeding to next --- src/zip/zip.models.ts | 1 + src/zip/zip.service.ts | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/zip/zip.models.ts b/src/zip/zip.models.ts index bae53d9..3f333c2 100644 --- a/src/zip/zip.models.ts +++ b/src/zip/zip.models.ts @@ -3,4 +3,5 @@ export type ZipContext = 'node.js' | 'browser'; export interface IZipServiceConfig { enableLog: boolean; context: ZipContext + delayBetweenAssetDownloadRequestsMs?: number } diff --git a/src/zip/zip.service.ts b/src/zip/zip.service.ts index a91f2c8..2ce4c7b 100644 --- a/src/zip/zip.service.ts +++ b/src/zip/zip.service.ts @@ -8,6 +8,8 @@ import { IZipServiceConfig } from './zip.models'; export class ZipService { + private readonly delayBetweenAssetRequestsMs: number; + private readonly contentTypesName: string = 'contentTypes.json'; private readonly contentItemsName: string = 'contentItems.json'; private readonly taxonomiesName: string = 'taxonomies.json'; @@ -21,6 +23,7 @@ export class ZipService { private readonly validationName: string = 'validation.json'; constructor(private config: IZipServiceConfig) { + this.delayBetweenAssetRequestsMs = config?.delayBetweenAssetDownloadRequestsMs ?? 150; } public async extractZipAsync(zipFile: any): Promise { @@ -101,9 +104,12 @@ export class ZipService { } const assetFilename = asset.file_name; - assetIdFolder.file(assetFilename, this.getBinaryDataFromUrl(asset.url, this.config.enableLog), { + assetIdFolder.file(assetFilename, await this.getBinaryDataFromUrlAsync(asset.url, this.config.enableLog), { binary: true }); + + // create artificial delay between requests as to prevent errors on network + await this.sleepAsync(this.delayBetweenAssetRequestsMs); } if (this.config.enableLog) { @@ -119,6 +125,10 @@ export class ZipService { return content; } + private sleepAsync(ms: number): Promise { + return new Promise((resolve: any) => setTimeout(resolve, ms)); + } + private async extractBinaryFilesAsync( zip: JSZip, assets: AssetContracts.IAssetModelContract[] @@ -173,19 +183,19 @@ export class ZipService { return JSON.parse(text); } - private getBinaryDataFromUrl(url: string, enableLog: boolean): Promise { + private getBinaryDataFromUrlAsync(url: string, enableLog: boolean): Promise { // temp fix for Kontent Repository not validating url url = url.replace('#', '%23'); if (enableLog) { - console.log(`Downloading asset: ${url}`); + console.log(`Start asset download: ${url}`); } return axios.get(url, { responseType: 'arraybuffer', }).then( response => { if (enableLog) { - console.log(`Downloading asset completed: ${url}`); + console.log(`Completed asset download: ${url}`); } return response.data; }