Skip to content

Commit 06e772a

Browse files
authored
Merge pull request #7216 from liranmauda/liran-move-md5-functions-into-utils
Adding crypto_utils
2 parents c36e6c8 + 6cd33b2 commit 06e772a

File tree

4 files changed

+51
-51
lines changed

4 files changed

+51
-51
lines changed

Diff for: src/test/utils/s3ops.js

+5-18
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ const util = require('util');
77
const https = require('https');
88
const stream = require('stream');
99
const crypto = require('crypto');
10+
const querystring = require('querystring');
1011
const P = require('../../util/promise');
1112
const RandStream = require('../../util/rand_stream');
12-
const querystring = require('querystring');
13+
const crypto_utils = require('../../util/crypto_utils');
1314

1415
require('../../util/dotenv').load();
1516

@@ -78,7 +79,7 @@ class S3OPS {
7879
data_size = data_size || 50;
7980

8081
const actual_size = data_size * multiplier;
81-
const md5_stream = new_md5_stream();
82+
const md5_stream = crypto_utils.new_md5_stream();
8283
const input_stream = new RandStream(actual_size, {
8384
highWaterMark: 1024 * 1024,
8485
});
@@ -175,7 +176,7 @@ class S3OPS {
175176
VersionId: versionid ? versionid : undefined,
176177
});
177178
const read_stream = req.createReadStream();
178-
const md5_stream = new_md5_stream();
179+
const md5_stream = crypto_utils.new_md5_stream();
179180
read_stream.pipe(md5_stream).pipe(target_stream);
180181
await new Promise((resolve, reject) => {
181182
read_stream.once('error', reject);
@@ -774,7 +775,7 @@ class S3OPS {
774775
async _multipart_upload_internal(bucket, file_name, data, part_size, overlook_error) {
775776
try {
776777
bucket = bucket || 'first.bucket';
777-
const md5_stream = new_md5_stream();
778+
const md5_stream = crypto_utils.new_md5_stream();
778779
const start_ts = Date.now();
779780

780781
let body;
@@ -822,18 +823,4 @@ class S3OPS {
822823

823824
}
824825

825-
function new_md5_stream() {
826-
const md5_stream = new stream.Transform({
827-
transform(buf, encoding, next) {
828-
this.md5.update(buf);
829-
this.size += buf.length;
830-
this.push(buf);
831-
next();
832-
}
833-
});
834-
md5_stream.md5 = crypto.createHash('md5');
835-
md5_stream.size = 0;
836-
return md5_stream;
837-
}
838-
839826
exports.S3OPS = S3OPS;

Diff for: src/util/crypto_utils.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Copyright (C) 2023 NooBaa */
2+
'use strict';
3+
4+
const crypto = require('crypto');
5+
const stream = require('stream');
6+
7+
function new_md5_stream() {
8+
const md5_stream = new stream.Transform({
9+
transform(buf, encoding, next) {
10+
this.md5.update(buf);
11+
this.size += buf.length;
12+
next(null, buf);
13+
}
14+
});
15+
16+
md5_stream.md5 = crypto.createHash('md5');
17+
md5_stream.size = 0;
18+
md5_stream.md5_buf = null;
19+
20+
return md5_stream;
21+
}
22+
23+
function calc_body_md5(stream_file) {
24+
const md5_stream = new_md5_stream();
25+
const new_stream = stream_file.pipe(md5_stream);
26+
return {
27+
new_stream,
28+
md5_buf: () => {
29+
if (md5_stream.md5_buf) return md5_stream.md5_buf;
30+
31+
const final_md5 = md5_stream.md5.digest('hex');
32+
md5_stream.md5_buf = Buffer.from(final_md5, 'hex');
33+
34+
return md5_stream.md5_buf;
35+
}
36+
};
37+
}
38+
39+
exports.new_md5_stream = new_md5_stream;
40+
exports.calc_body_md5 = calc_body_md5;

Diff for: src/util/google_storage_wrap.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const { Util } = require('@google-cloud/storage/build/src/nodejs-common/util');
55
const pkg = require('../../package.json');
66
const DEV_MODE = (process.env.DEV_MODE === 'true');
7+
const crypto_utils = require('./crypto_utils');
78

89
let stage_or_prod = 'production';
910
if (DEV_MODE) {
@@ -12,4 +13,7 @@ if (DEV_MODE) {
1213
Util.prototype.getUserAgentFromPackageJson = () => `NooBaa/${pkg.version} (GPN:noobaa.com; ${stage_or_prod}) NooBaa/${pkg.version}`;
1314

1415
const { Storage } = require('@google-cloud/storage');
16+
17+
Storage.calc_body_md5 = stream_file => crypto_utils.calc_body_md5(stream_file);
18+
1519
module.exports = Storage;

Diff for: src/util/new_azure_storage_wrap.js

+2-33
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,15 @@
11
/* Copyright (C) 2016 NooBaa */
22
'use strict';
3-
const crypto = require('crypto');
4-
const stream = require('stream');
3+
const crypto_utils = require('./crypto_utils');
54
const azure_storage = require('@azure/storage-blob');
65
// needed only for enerateBlockIdPrefix() and get_block_id() functions
76
const old_azure_storage = require('azure-storage');
87

9-
function new_md5_stream() {
10-
const md5_stream = new stream.Transform({
11-
transform(buf, encoding, next) {
12-
this.md5.update(buf);
13-
this.size += buf.length;
14-
next(null, buf);
15-
}
16-
});
17-
18-
md5_stream.md5 = crypto.createHash('md5');
19-
md5_stream.size = 0;
20-
md5_stream.md5_buf = null;
21-
22-
return md5_stream;
23-
}
24-
258
azure_storage.get_container_client = (blob_service, container) => blob_service.getContainerClient(container);
269

2710
azure_storage.get_blob_client = (container_client, blob) => container_client.getBlobClient(blob).getBlockBlobClient();
2811

29-
azure_storage.calc_body_md5 = stream_file => {
30-
const md5_stream = new_md5_stream();
31-
const new_stream = stream_file.pipe(md5_stream);
32-
return {
33-
new_stream,
34-
md5_buf: () => {
35-
if (md5_stream.md5_buf) return md5_stream.md5_buf;
36-
37-
const final_md5 = md5_stream.md5.digest('hex');
38-
md5_stream.md5_buf = Buffer.from(final_md5, 'hex');
39-
40-
return md5_stream.md5_buf;
41-
}
42-
};
43-
};
12+
azure_storage.calc_body_md5 = stream_file => crypto_utils.calc_body_md5(stream_file);
4413

4514
// create old lib blob service - needed for functions that do not exist in the new lib
4615
// needed only for generateBlockIdPrefix() and get_block_id() functions

0 commit comments

Comments
 (0)