Skip to content
This repository was archived by the owner on Mar 28, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9483430

Browse files
authoredMay 9, 2024
Update crypto and https imports for isomorphic support (#29)
* update crypto and https imports to support browser * version bump
1 parent 16d3636 commit 9483430

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed
 

‎package-lock.json

Lines changed: 34 additions & 8 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "getindexify",
3-
"version": "0.0.36",
3+
"version": "0.0.37",
44
"description": "This is the TypeScript client for interacting with the Indexify service.",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",
@@ -23,7 +23,9 @@
2323
},
2424
"homepage": "https://github.com/tensorlakeai/indexify-typescript-client#readme",
2525
"devDependencies": {
26+
"@types/crypto-js": "^4.2.2",
2627
"@types/jest": "^29.5.12",
28+
"@types/uuid": "^9.0.8",
2729
"jest": "^29.7.0",
2830
"ts-jest": "^29.1.2",
2931
"ts-node": "^10.9.2",
@@ -32,6 +34,7 @@
3234
},
3335
"dependencies": {
3436
"axios": "^1.6.7",
35-
"crypto": "^1.0.1"
37+
"crypto-js": "^4.2.0",
38+
"uuid": "^9.0.1"
3639
}
3740
}

‎src/client.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ import {
1616
IContent,
1717
IExtractResponse,
1818
} from "./types";
19-
import { Agent } from "https";
20-
import { randomUUID } from "crypto";
21-
import { createHash } from "crypto";
19+
import { v4 as uuidv4 } from "uuid";
20+
import CryptoJS from "crypto-js";
2221

2322
const DEFAULT_SERVICE_URL = "http://localhost:8900"; // Set your default service URL
2423

@@ -33,7 +32,7 @@ class IndexifyClient {
3332
namespace: string = "default",
3433
// optional mtls config
3534
extractionPolicies: IExtractionPolicy[],
36-
httpsAgent?: Agent
35+
httpsAgent?: any
3736
) {
3837
this.serviceUrl = serviceUrl;
3938
this.namespace = namespace;
@@ -128,7 +127,7 @@ class IndexifyClient {
128127
mtlsConfig,
129128
}: {
130129
mtlsConfig?: IMtlsConfig;
131-
}): Agent | undefined {
130+
}): any | undefined {
132131
let httpsAgent = undefined;
133132
if (mtlsConfig !== undefined) {
134133
if (typeof window !== "undefined") {
@@ -137,6 +136,7 @@ class IndexifyClient {
137136
);
138137
}
139138
const fs = require("fs");
139+
const { Agent } = require("https");
140140
httpsAgent = new Agent({
141141
cert: fs.readFileSync(mtlsConfig.certPath),
142142
key: fs.readFileSync(mtlsConfig.keyPath),
@@ -204,13 +204,13 @@ class IndexifyClient {
204204
name: string,
205205
query: string,
206206
topK: number,
207-
include_content:boolean = true
207+
include_content: boolean = true
208208
): Promise<ISearchIndexResponse[]> {
209209
const resp = await this.client.post("search", {
210210
index: name,
211211
query,
212212
k: topK,
213-
include_content
213+
include_content,
214214
});
215215
return resp.data["results"];
216216
}
@@ -397,13 +397,12 @@ class IndexifyClient {
397397
}
398398

399399
generateUniqueHexId(): string {
400-
return randomUUID().replace(/-/g, "").substring(0, 16);
400+
return uuidv4().replace(/-/g, "").substring(0, 16);
401401
}
402402

403403
generateHashFromString(inputString: string): string {
404-
const hashObject = createHash("sha256");
405-
hashObject.update(inputString);
406-
return hashObject.digest("hex").substring(0, 16);
404+
const hash = CryptoJS.SHA256(inputString);
405+
return hash.toString(CryptoJS.enc.Hex).substring(0, 16);
407406
}
408407
}
409408

‎tests/client.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,16 @@ test("Test Extract Method", async () => {
233233
expect(content.features?.[0].data.title).toBe("Stephen Curry");
234234
});
235235

236+
test("Test generateHashFromString", async () => {
237+
const client = await IndexifyClient.createClient();
238+
expect(client.generateHashFromString("test")).toBe("9f86d081884c7d65");
239+
});
240+
241+
test("Test generateUniqueHexId", async () => {
242+
const client = await IndexifyClient.createClient();
243+
expect(client.generateUniqueHexId()).toHaveLength(16);
244+
});
245+
236246
// test.only("MTLS", async () => {
237247
// const fs = require("fs")
238248
// const https = require("https")

0 commit comments

Comments
 (0)
This repository has been archived.