Skip to content

Commit

Permalink
feat: add support for checksum verification (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Jul 10, 2024
1 parent a200c8d commit 5dbe8af
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/commands/apps/bundles/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions src/utils/buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ReadStream } from 'fs';

export const createBuffer = async (data: ReadStream): Promise<Buffer> => {
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);
});
};
4 changes: 4 additions & 0 deletions src/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const createHash = async (data: Buffer): Promise<string> => {
const crypto = await import('crypto');
return crypto.createHash('sha256').update(data).digest('hex');
};

0 comments on commit 5dbe8af

Please sign in to comment.