Skip to content

Commit dadb17f

Browse files
feat: enhance computeHashesFromStream to support encoding options and update usage in ClassifierSdk
1 parent 94fa7f6 commit dadb17f

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

src/hashing.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { Readable } from "stream";
22
import crypto from 'crypto';
33
import sharp from 'sharp'
4+
import { RequestEncoding } from "./main";
5+
import brotli from 'brotli'
46

57
/**
68
* Computes MD5 and SHA1 hashes from a readable stream and resizes any image data.
79
* @param stream Node.js readable stream (e.g., fs.createReadStream)
10+
* @param encoding Encoding type for the image data (default is UNCOMPRESSED)
811
* @returns {Promise<{md5: string, sha1: string, data: Buffer}>} Object containing MD5 hash, SHA1 hash, and resized image buffer
912
*/
10-
export async function computeHashesFromStream(stream: Readable): Promise<{ md5: string; sha1: string; data: Buffer }> {
11-
13+
export async function computeHashesFromStream(stream: Readable, encoding: RequestEncoding = RequestEncoding.UNCOMPRESSED): Promise<{ md5: string; sha1: string; data: Buffer }> {
1214
const md5 = crypto.createHash('md5');
1315
const sha1 = crypto.createHash('sha1');
1416
const resizer = sharp()
@@ -19,7 +21,11 @@ export async function computeHashesFromStream(stream: Readable): Promise<{ md5:
1921
stream.pipe(sha1);
2022
stream.pipe(resizer);
2123

22-
const data = await resizer.toBuffer();
24+
let data = await resizer.toBuffer();
25+
26+
if (encoding === RequestEncoding.BROTLI) {
27+
data = Buffer.from(await brotli.compress(data));
28+
}
2329

2430
return {
2531
md5: md5.digest('hex'),

src/main.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { Empty } from './athena/google/protobuf/empty';
88
import TypedEmitter from "typed-emitter"
99
import { AuthenticationOptions, AuthenticationManager } from './authenticationManager';
1010
import { computeHashesFromStream } from './hashing';
11-
import brotli from 'brotli'
1211

1312
/**
1413
* Options for classifyImage
@@ -139,22 +138,15 @@ export class ClassifierSdk extends (EventEmitter as new () => TypedEmitter<Class
139138
if (!this.classifierGrpcCall) {
140139
throw new Error('gRPC stream is not open. Call open() first.');
141140
}
142-
let {
141+
const {
143142
affiliate = this.options.affiliate,
144143
correlationId = randomUUID(),
145144
imageStream,
146-
encoding = RequestEncoding.UNSPECIFIED,
145+
encoding = RequestEncoding.UNCOMPRESSED,
147146
format = ImageFormat.UNSPECIFIED,
148147
} = options;
149148

150-
let { md5, sha1, data } = await computeHashesFromStream(imageStream);
151-
152-
if (encoding == RequestEncoding.UNCOMPRESSED || encoding == RequestEncoding.UNSPECIFIED) {
153-
// Handle uncompressed and compressed cases
154-
const compressed = await brotli.compress(data);
155-
data = Buffer.from(compressed);
156-
encoding = RequestEncoding.BROTLI;
157-
}
149+
const { md5, sha1, data } = await computeHashesFromStream(imageStream, encoding);
158150

159151
const hashes: ImageHash[] = [
160152
{ value: md5, type: HashType.MD5 },

0 commit comments

Comments
 (0)