Skip to content

Commit 5962bc6

Browse files
fix: Address ESM compatibility issues
Update package.json and TypeScript configuration to properly support ES modules. Modify hashing and index modules to use ESM-compatible imports/exports.
1 parent f0c2e3b commit 5962bc6

5 files changed

Lines changed: 69 additions & 117 deletions

File tree

package-lock.json

Lines changed: 37 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@crispthinking/athena-classifier-sdk",
33
"version": "0.0.0",
4-
"main": "./dist/src/index.js",
4+
"main": "./dist/index.js",
55
"description": "A Node.js SDK for the Athena classifier that uses gRPC transport.",
66
"type": "module",
77
"engines": {
@@ -14,13 +14,13 @@
1414
],
1515
"exports": {
1616
".": {
17-
"import": "./dist/index.js",
18-
"require": "./dist/index.js"
17+
"require": "./dist/index.js",
18+
"import": "./dist/index.js"
1919
}
2020
},
2121
"devDependencies": {
22+
"@bufbuild/protoc-gen-es": "^2.8.0",
2223
"@eslint/js": "~9.35",
23-
"@protobuf-ts/plugin": "^2.11.1",
2424
"@types/brotli": "^1.3.4",
2525
"@types/eslint__js": "~9.14",
2626
"@types/node": "~20",
@@ -50,7 +50,7 @@
5050
"build": "tsc -p tsconfig.json",
5151
"build:watch": "tsc -w -p tsconfig.json",
5252
"build:release": "npm run clean && tsc -p tsconfig.release.json",
53-
"codegen": "mkdir -p src/generated && npx protoc --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts --ts_out=client_grpc1:./src/generated --proto_path=./athena-protobufs ./athena-protobufs/athena/**.proto",
53+
"codegen": "mkdir -p src/generated && npx protoc --plugin=protoc-gen-ts_proto=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=src/generated --ts_proto_opt=esModuleInterop=true,importSuffix=.js,useExactTypes=false,outputServices=grpc-js --proto_path=./athena-protobufs ./athena-protobufs/athena/**.proto",
5454
"lint": "eslint .",
5555
"lint:all": "npm run lint && npm run prettier:check && npx tsc --noEmit",
5656
"precommit:setup": "echo 'To install pre-commit hooks, run: uv tool install pre-commit && uvx pre-commit install'",
@@ -70,7 +70,6 @@
7070
"@bufbuild/protobuf": "^2.7.0",
7171
"@grpc/grpc-js": "^1.13.4",
7272
"@grpc/proto-loader": "^0.8.0",
73-
"@protobuf-ts/grpc-transport": "^2.11.1",
7473
"brotli": "^1.3.3",
7574
"jwt-decode": "^4.0.0",
7675
"openid-client": "^6.7.1",

src/hashing.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
HashType,
66
ImageFormat,
77
RequestEncoding,
8-
} from './generated/athena/models';
8+
} from './generated/athena/models.js';
99
import brotli from 'brotli';
1010
import { buffer } from 'stream/consumers';
1111

@@ -20,10 +20,10 @@ import { buffer } from 'stream/consumers';
2020
*/
2121
export async function computeHashesFromStream(
2222
data: Readable | Buffer<ArrayBufferLike>,
23-
encoding: RequestEncoding = RequestEncoding.UNCOMPRESSED,
24-
imageFormat: ImageFormat = ImageFormat.UNSPECIFIED,
23+
encoding: RequestEncoding = RequestEncoding.REQUEST_ENCODING_UNCOMPRESSED,
24+
imageFormat: ImageFormat = ImageFormat.IMAGE_FORMAT_UNSPECIFIED,
2525
resize: boolean = false,
26-
hashes: HashType[] = [HashType.MD5, HashType.SHA1],
26+
hashes: HashType[] = [HashType.HASH_TYPE_MD5, HashType.HASH_TYPE_SHA1],
2727
): Promise<{
2828
md5?: string | undefined;
2929
sha1?: string | undefined;
@@ -41,13 +41,13 @@ export async function computeHashesFromStream(
4141
stream = Readable.from(data);
4242
}
4343

44-
if (hashes.includes(HashType.MD5)) {
44+
if (hashes.includes(HashType.HASH_TYPE_MD5)) {
4545
{
4646
stream.pipe(md5);
4747
}
4848
}
4949

50-
if (hashes.includes(HashType.SHA1)) {
50+
if (hashes.includes(HashType.HASH_TYPE_SHA1)) {
5151
{
5252
stream.pipe(sha1);
5353
}
@@ -57,7 +57,7 @@ export async function computeHashesFromStream(
5757
const resizer = sharp().resize(448, 448).raw({ depth: 'char' });
5858
stream.pipe(resizer);
5959
data = await resizer.toBuffer();
60-
imageFormat = ImageFormat.RAW_UINT8;
60+
imageFormat = ImageFormat.IMAGE_FORMAT_RAW_UINT8;
6161
} else {
6262
data = await buffer(stream);
6363
// use sharp to validate the image dimensions
@@ -67,13 +67,13 @@ export async function computeHashesFromStream(
6767
}
6868
}
6969

70-
if (encoding === RequestEncoding.BROTLI) {
70+
if (encoding === RequestEncoding.REQUEST_ENCODING_BROTLI) {
7171
data = Buffer.from(brotli.compress(data));
7272
}
7373

7474
return {
75-
md5: hashes.includes(HashType.MD5) ? md5.digest('hex') : undefined,
76-
sha1: hashes.includes(HashType.SHA1) ? sha1.digest('hex') : undefined,
75+
md5: hashes.includes(HashType.HASH_TYPE_MD5) ? md5.digest('hex') : undefined,
76+
sha1: hashes.includes(HashType.HASH_TYPE_SHA1) ? sha1.digest('hex') : undefined,
7777
data,
7878
format: imageFormat,
7979
};

src/index.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import {
1010
Deployment,
1111
ImageFormat,
1212
ClassificationOutput,
13-
} from './generated/athena/models';
13+
} from './generated/athena/models.js';
1414
import * as grpc from '@grpc/grpc-js';
1515
import {
16-
type IClassifierServiceClient,
1716
ClassifierServiceClient,
18-
} from './generated/athena/athena.grpc-client';
17+
} from './generated/athena/athena.js';
1918
import { EventEmitter } from 'events';
2019
import { Empty } from './generated/google/protobuf/empty';
2120
import {
@@ -93,7 +92,7 @@ export const defaultGrpcAddress = 'trust.messages.crispthinking.com:443';
9392
*/
9493
export class ClassifierSdk extends (EventEmitter as new () => TypedEventEmitter<ClassifierEvents>) {
9594
private grpcAddress: string;
96-
private client: IClassifierServiceClient;
95+
private client: ClassifierServiceClient;
9796
private classifierGrpcCall: grpc.ClientDuplexStream<
9897
ClassifyRequest,
9998
ClassifyResponse
@@ -253,11 +252,11 @@ export class ClassifierSdk extends (EventEmitter as new () => TypedEventEmitter<
253252
affiliate = this.options.affiliate,
254253
correlationId = randomUUID().toString(),
255254
data: inputData,
256-
includeHashes = [HashType.MD5, HashType.SHA1],
257-
encoding = RequestEncoding.UNCOMPRESSED,
255+
includeHashes = [HashType.HASH_TYPE_MD5, HashType.HASH_TYPE_SHA1],
256+
encoding = RequestEncoding.REQUEST_ENCODING_UNCOMPRESSED,
258257
} = options;
259258

260-
let inputFormat: ImageFormat = ImageFormat.UNSPECIFIED;
259+
let inputFormat: ImageFormat = ImageFormat.IMAGE_FORMAT_UNSPECIFIED;
261260

262261
if ('resize' in options === false) {
263262
inputFormat = options.format;
@@ -274,11 +273,11 @@ export class ClassifierSdk extends (EventEmitter as new () => TypedEventEmitter<
274273
const hashes: ImageHash[] = [];
275274

276275
if (md5 && md5.trim() != '') {
277-
hashes.push({ value: md5, type: HashType.MD5 });
276+
hashes.push({ value: md5, type: HashType.HASH_TYPE_MD5 });
278277
}
279278

280279
if (sha1 && sha1.trim() != '') {
281-
hashes.push({ value: sha1, type: HashType.SHA1 });
280+
hashes.push({ value: sha1, type: HashType.HASH_TYPE_SHA1 });
282281
}
283282

284283
processedInputs.push({
@@ -314,11 +313,11 @@ export class ClassifierSdk extends (EventEmitter as new () => TypedEventEmitter<
314313
const options = {
315314
affiliate: this.options.affiliate,
316315
correlationId: randomUUID().toString(),
317-
includeHashes: [HashType.MD5, HashType.SHA1],
316+
includeHashes: [HashType.HASH_TYPE_MD5, HashType.HASH_TYPE_SHA1],
318317
encoding: request.encoding,
319318
};
320319

321-
let inputFormat: ImageFormat = ImageFormat.UNSPECIFIED;
320+
let inputFormat: ImageFormat = ImageFormat.IMAGE_FORMAT_UNSPECIFIED;
322321

323322
if ('resize' in request === false) {
324323
inputFormat = request.format;
@@ -335,11 +334,11 @@ export class ClassifierSdk extends (EventEmitter as new () => TypedEventEmitter<
335334
const hashes: ImageHash[] = [];
336335

337336
if (md5 && md5.trim() != '') {
338-
hashes.push({ value: md5, type: HashType.MD5 });
337+
hashes.push({ value: md5, type: HashType.HASH_TYPE_MD5 });
339338
}
340339

341340
if (sha1 && sha1.trim() != '') {
342-
hashes.push({ value: sha1, type: HashType.SHA1 });
341+
hashes.push({ value: sha1, type: HashType.HASH_TYPE_SHA1 });
343342
}
344343

345344
const input: ClassificationInput = {
@@ -399,7 +398,6 @@ export class ClassifierSdk extends (EventEmitter as new () => TypedEventEmitter<
399398
}
400399
}
401400

402-
export * from './generated/athena/models';
403-
export * from './generated/athena/athena';
404-
export * from './generated/athena/athena.grpc-client';
405-
export * from './hashing';
401+
export * from './generated/athena/models.js';
402+
export { ClassifierServiceClient } from './generated/athena/athena.js';
403+
export * from './hashing.js';

tsconfig.release.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"removeComments": true,
66
"rootDir": "src",
77
"module": "es2022",
8+
"moduleResolution": "node",
89
"target": "es2022",
910
"declaration": true,
11+
"verbatimModuleSyntax": false,
1012
"lib": ["ES2022"]
1113
},
1214
"include": ["src/**/*"],

0 commit comments

Comments
 (0)