From 5dbe8af4c866d94dc633009d26be143b079474d1 Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Wed, 10 Jul 2024 10:51:22 +0200 Subject: [PATCH] feat: add support for checksum verification (#5) --- src/commands/apps/bundles/create.ts | 10 +++++++++- src/utils/buffer.ts | 17 +++++++++++++++++ src/utils/hash.ts | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/utils/buffer.ts create mode 100644 src/utils/hash.ts diff --git a/src/commands/apps/bundles/create.ts b/src/commands/apps/bundles/create.ts index ad0807f..50971ab 100644 --- a/src/commands/apps/bundles/create.ts +++ b/src/commands/apps/bundles/create.ts @@ -8,6 +8,8 @@ import authorizationService from '../../../services/authorization-service'; import appsService from '../../../services/apps'; import appBundlesService from '../../../services/app-bundles'; import { getMessageFromUnknownError } from '../../../utils/error'; +import { createHash } from '../../../utils/hash'; +import { createBuffer } from '../../../utils/buffer'; export default defineCommand({ meta: { @@ -95,11 +97,17 @@ export default defineCommand({ const formData = new FormData(); if (path) { if (zip.isZipped(path)) { - formData.append('file', createReadStream(path)); + const readStream = createReadStream(path); + const buffer = await createBuffer(readStream); + const hash = await createHash(buffer); + formData.append('file', buffer, { filename: 'bundle.zip' }); + formData.append('checksum', hash); } else { consola.start('Zipping folder...'); const zipBuffer = await zip.zipFolder(path); + const hash = await createHash(zipBuffer); formData.append('file', zipBuffer, { filename: 'bundle.zip' }); + formData.append('checksum', hash); } } if (url) { diff --git a/src/utils/buffer.ts b/src/utils/buffer.ts new file mode 100644 index 0000000..b296d81 --- /dev/null +++ b/src/utils/buffer.ts @@ -0,0 +1,17 @@ +import { ReadStream } from 'fs'; + +export const createBuffer = async (data: ReadStream): Promise => { + const chunks: Buffer[] = []; + return new Promise((resolve, reject) => { + data.on('readable', () => { + let chunk; + while ((chunk = data.read())) { + chunks.push(chunk); + } + }); + data.on('end', () => { + resolve(Buffer.concat(chunks)); + }); + data.on('error', reject); + }); +}; diff --git a/src/utils/hash.ts b/src/utils/hash.ts new file mode 100644 index 0000000..0ed536c --- /dev/null +++ b/src/utils/hash.ts @@ -0,0 +1,4 @@ +export const createHash = async (data: Buffer): Promise => { + const crypto = await import('crypto'); + return crypto.createHash('sha256').update(data).digest('hex'); +};