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'); +};