From 567cd40b1a841e23062001c6f8a1ba24f8653f2f Mon Sep 17 00:00:00 2001 From: maruware Date: Thu, 26 Dec 2019 15:03:25 +0900 Subject: [PATCH] fix s3 content compare --- src/s3.ts | 15 +++++++++------ src/utils/md5.ts | 6 +++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index ef49d8c..e009a72 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -15,7 +15,7 @@ const resolveObjectKey = (cwd: string, base: string, filename: string) => { return relative(join(cwd, base), filename) } -type FileDef = { name: string; key: string } +type FileDef = { name: string; key: string; md5: string } const uploadFile = async (s3: S3, file: FileDef, params: DeployArgsParams) => { const body = createReadStream(file.name) @@ -26,6 +26,9 @@ const uploadFile = async (s3: S3, file: FileDef, params: DeployArgsParams) => { Key: file.key, Body: body, ContentType: contentType, + Metadata: { + md5: file.md5 + }, ...params }, service: s3 @@ -100,14 +103,14 @@ export const deployTask = async ({ const s3 = new S3({ ...config, computeChecksums: true }) const bucket = params.Bucket const filenames = await globAsync(pattern) - const files = filenames + const files = await Promise.all(filenames .filter(file => lstatSync(file).isFile()) - .map(file => ({ + .map(async file => ({ name: file, key: resolveObjectKey(cwd, base, file), - md5: calcMd5FromStream(file), + md5: await calcMd5FromStream(file), size: lstatSync(file).size - })) + }))) const tasks: ListrTask[] = files.map( (file): ListrTask => { return { @@ -119,7 +122,7 @@ export const deployTask = async ({ .promise() if (r.ETag === `"${file.md5}"`) return true // Large file can not be compare ETag. So compare file size. - if (r.ContentLength === file.size) return true + if (r.Metadata && r.Metadata.md5 === file.md5) return true return false } catch { return false diff --git a/src/utils/md5.ts b/src/utils/md5.ts index 8fe0f85..a086093 100644 --- a/src/utils/md5.ts +++ b/src/utils/md5.ts @@ -2,9 +2,9 @@ import crypto from 'crypto' import { createReadStream } from 'fs' export const calcMd5FromStream = (filePath: string) => { - return new Promise((resolve, reject) => { - var readStream = createReadStream(filePath) - var md5hash = crypto.createHash('md5') + return new Promise((resolve, reject) => { + const readStream = createReadStream(filePath) + const md5hash = crypto.createHash('md5') md5hash.setEncoding('base64') readStream.pipe(md5hash) readStream.on('end', () => {