Move base64/hex methods to PersistorHelper

Also add some null-safety checks
This commit is contained in:
Simon Detheridge 2020-02-13 16:55:01 +00:00
parent d9c9d74994
commit e58284aefe
3 changed files with 16 additions and 14 deletions

View file

@ -10,16 +10,12 @@ const PersistorHelper = require('./PersistorHelper')
const pipeline = promisify(Stream.pipeline)
function base64ToHex(base64) {
return Buffer.from(base64, 'base64').toString('hex')
}
// both of these settings will be null by default except for tests
// that's OK - GCS uses the locally-configured service account by default
const storage = new Storage(settings.filestore.gcs)
// workaround for broken uploads with custom endpoints:
// https://github.com/googleapis/nodejs-storage/issues/898
if (settings.filestore.gcs.apiEndpoint) {
if (settings.filestore.gcs && settings.filestore.gcs.apiEndpoint) {
storage.interceptors.push({
request: function(reqOpts) {
const url = new URL(reqOpts.uri)
@ -95,7 +91,7 @@ async function sendStream(bucket, key, readStream, sourceMd5) {
if (sourceMd5) {
writeOptions.validation = 'md5'
writeOptions.metadata = {
md5Hash: sourceMd5
md5Hash: PersistorHelper.hexToBase64(sourceMd5)
}
}
@ -123,7 +119,7 @@ async function sendStream(bucket, key, readStream, sourceMd5) {
}
}
async function getFileStream(bucket, key, opts) {
async function getFileStream(bucket, key, opts = {}) {
if (opts.end) {
// S3 (and http range headers) treat 'end' as inclusive, so increase this by 1
opts.end++
@ -174,7 +170,7 @@ async function getFileMd5Hash(bucket, key) {
.bucket(bucket)
.file(key)
.getMetadata()
return base64ToHex(metadata[0].md5Hash)
return PersistorHelper.base64ToHex(metadata[0].md5Hash)
} catch (err) {
throw PersistorHelper.wrapError(
err,

View file

@ -12,7 +12,9 @@ module.exports = {
verifyMd5,
getMeteredStream,
waitForStreamReady,
wrapError
wrapError,
hexToBase64,
base64ToHex
}
// returns a promise which resolves with the md5 hash of the stream
@ -103,3 +105,11 @@ function wrapError(error, message, params, ErrorType) {
}).withCause(error)
}
}
function base64ToHex(base64) {
return Buffer.from(base64, 'base64').toString('hex')
}
function hexToBase64(hex) {
return Buffer.from(hex, 'hex').toString('base64')
}

View file

@ -46,10 +46,6 @@ const S3Persistor = {
module.exports = S3Persistor
function hexToBase64(hex) {
return Buffer.from(hex, 'hex').toString('base64')
}
async function sendFile(bucketName, key, fsPath) {
let readStream
try {
@ -72,7 +68,7 @@ async function sendStream(bucketName, key, readStream, sourceMd5) {
let b64Hash
if (sourceMd5) {
b64Hash = hexToBase64(sourceMd5)
b64Hash = PersistorHelper.hexToBase64(sourceMd5)
} else {
hashPromise = PersistorHelper.calculateStreamMd5(readStream)
}