Comparing version 1.0.5 to 1.0.6
@@ -30,8 +30,9 @@ // Require base | ||
// Get bucket | ||
this._setBucket(); | ||
} | ||
async _setBucket() { | ||
this.bucket = new Promise(async (res) => { | ||
// authorize | ||
await this.store.authorize(); | ||
// resolve got bucket | ||
res(await this.store.getBucket({ | ||
@@ -41,4 +42,20 @@ bucketName : config.get('b2.bucket'), | ||
}); | ||
// so re-calls to this method can know the process has finished | ||
await this.bucket; | ||
} | ||
async _safeExec(cb, times = 0) { | ||
try { | ||
return await cb(); | ||
} catch (err) { | ||
if (err.response.status === 401 && times < (config.get('b2.authRetries') || 5)) { | ||
await this._setBucket(); | ||
return await this._safeExec(cb, times + 1); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} | ||
/** | ||
@@ -75,5 +92,7 @@ * Gets asset url | ||
// get upload url | ||
const uploadUrl = await this.store.getUploadUrl({ | ||
bucketId : bucket.data.buckets[0].bucketId, | ||
}); | ||
const uploadUrl = await this._safeExec(async () => { | ||
return await this.store.getUploadUrl({ | ||
bucketId : bucket.data.buckets[0].bucketId, | ||
}); | ||
}) | ||
@@ -87,14 +106,15 @@ // Augment date | ||
// await | ||
const { data } = (await this.store.uploadFile({ | ||
data : await fs.readFile(tmp), // this is expecting a Buffer, not an encoded string | ||
fileName : `${asset.get('path')}/${label ? `${label}.${asset.get(`thumbs.${label}.ext`)}` : `full.${asset.get('ext')}`}`, | ||
uploadUrl : uploadUrl.data.uploadUrl, | ||
uploadAuthToken : uploadUrl.data.authorizationToken, | ||
info : { | ||
str : date, | ||
hash : asset.get('hash'), | ||
}, | ||
onUploadProgress : () => {}, // progress monitoring | ||
// ...common arguments (optional) | ||
})); | ||
const { data } = await this._safeExec(async () => { | ||
return await this.store.uploadFile({ | ||
data : await fs.readFile(tmp), // this is expecting a Buffer, not an encoded string | ||
fileName : `${asset.get('path')}/${label ? `${label}.${asset.get(`thumbs.${label}.ext`)}` : `full.${asset.get('ext')}`}`, | ||
uploadUrl : uploadUrl.data.uploadUrl, | ||
uploadAuthToken : uploadUrl.data.authorizationToken, | ||
info : { | ||
str : date, | ||
hash : asset.get('hash'), | ||
}, | ||
onUploadProgress : () => {}, | ||
}); | ||
}); | ||
@@ -138,5 +158,7 @@ // set b2 info | ||
// delete file version | ||
await this.store.deleteFileVersion({ | ||
fileId : asset.get(`${label ? `thumbs.${label}.` : ''}b2.fileId`), | ||
fileName : `${asset.get('path')}/${label ? `${label}.${asset.get(`thumbs.${label}.ext`)}` : `full.${asset.get('ext')}`}`, | ||
await this._safeExec(async () => { | ||
return await this.store.deleteFileVersion({ | ||
fileId : asset.get(`${label ? `thumbs.${label}.` : ''}b2.fileId`), | ||
fileName : `${asset.get('path')}/${label ? `${label}.${asset.get(`thumbs.${label}.ext`)}` : `full.${asset.get('ext')}`}`, | ||
}); | ||
}); | ||
@@ -143,0 +165,0 @@ } |
{ | ||
"name": "@edenjs/b2", | ||
"description": "nothing yet", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"dependencies": { | ||
@@ -6,0 +6,0 @@ "backblaze-b2": "^1.3.1" |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
8392
178
0