Skip to content

Commit 51bf34c

Browse files
committed
🐛 make sure input sources are only initialized once
1 parent 2b44f3a commit 51bf34c

File tree

8 files changed

+75
-10
lines changed

8 files changed

+75
-10
lines changed

src/clientV2.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ export class ClientV2 {
163163
if (inputSource === undefined) {
164164
throw new Error("The 'enqueue' function requires an input document.");
165165
}
166-
if (!inputSource.isInitialized()) {
167-
await inputSource.init();
168-
}
166+
await inputSource.init();
169167
return await this.mindeeApi.reqPostInferenceEnqueue(inputSource, params);
170168
}
171169

src/input/sources/base64Input.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LocalInputSource } from "./localInputSource";
22
import { INPUT_TYPE_BASE64 } from "./inputSource";
3+
import { logger } from "../../logger";
34

45
interface Base64InputProps {
56
inputString: string;
@@ -19,6 +20,10 @@ export class Base64Input extends LocalInputSource {
1920
}
2021

2122
async init() {
23+
if (this.initialized) {
24+
return;
25+
}
26+
logger.debug("Loading from base64");
2227
this.fileObject = Buffer.from(this.inputString, "base64");
2328
this.mimeType = await this.checkMimetype();
2429
// clear out the string

src/input/sources/bufferInput.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LocalInputSource } from "./localInputSource";
22
import { INPUT_TYPE_BUFFER } from "./inputSource";
3+
import { logger } from "../../logger";
34

45
interface BufferInputProps {
56
buffer: Buffer;
@@ -16,6 +17,10 @@ export class BufferInput extends LocalInputSource {
1617
}
1718

1819
async init(): Promise<void> {
20+
if (this.initialized) {
21+
return;
22+
}
23+
logger.debug("Loading from buffer");
1924
this.mimeType = await this.checkMimetype();
2025
this.initialized = true;
2126
}

src/input/sources/bytesInput.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { INPUT_TYPE_BYTES } from "./inputSource";
22
import { LocalInputSource } from "./localInputSource";
3+
import { logger } from "../../logger";
34

45
interface BytesInputProps {
56
inputBytes: Uint8Array;
@@ -19,6 +20,10 @@ export class BytesInput extends LocalInputSource {
1920
}
2021

2122
async init() {
23+
if (this.initialized) {
24+
return;
25+
}
26+
logger.debug("Loading from bytes");
2227
this.fileObject = Buffer.from(this.inputBytes);
2328
this.mimeType = await this.checkMimetype();
2429
this.inputBytes = new Uint8Array(0);

src/input/sources/pathInput.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export class PathInput extends LocalInputSource {
2121
}
2222

2323
async init() {
24-
logger.debug(`Loading from: ${this.inputPath}`);
24+
if (this.initialized) {
25+
return;
26+
}
27+
logger.debug(`Loading from path: ${this.inputPath}`);
2528
this.fileObject = Buffer.from(await fs.readFile(this.inputPath));
2629
this.mimeType = await this.checkMimetype();
2730
this.initialized = true;

src/input/sources/streamInput.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Readable } from "stream";
22
import { LocalInputSource } from "./localInputSource";
33
import { INPUT_TYPE_STREAM } from "./inputSource";
4+
import { logger } from "../../logger";
45

56
interface StreamInputProps {
67
inputStream: Readable;
@@ -20,6 +21,10 @@ export class StreamInput extends LocalInputSource {
2021
}
2122

2223
async init() {
24+
if (this.initialized) {
25+
return;
26+
}
27+
logger.debug("Loading from stream");
2328
this.fileObject = await this.stream2buffer(this.inputStream);
2429
this.mimeType = await this.checkMimetype();
2530
this.initialized = true;

src/input/sources/urlInput.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { writeFile } from "fs/promises";
66
import { request as httpsRequest } from "https";
77
import { IncomingMessage } from "http";
88
import { BytesInput } from "./bytesInput";
9+
import { logger } from "../../logger";
910

1011
export class UrlInput extends InputSource {
1112
public readonly url: string;
@@ -16,6 +17,10 @@ export class UrlInput extends InputSource {
1617
}
1718

1819
async init() {
20+
if (this.initialized) {
21+
return;
22+
}
23+
logger.debug(`source URL: ${this.url}`);
1924
if (!this.url.toLowerCase().startsWith("https")) {
2025
throw new Error("URL must be HTTPS");
2126
}

tests/v2/clientV2.integration.ts

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { expect } from "chai";
22
import path from "node:path";
33

4-
import { ClientV2, InferenceParameters, PathInput, UrlInput } from "../../src";
4+
import { ClientV2, InferenceParameters, PathInput, UrlInput, Base64Input } from "../../src";
55
import { SimpleField } from "../../src/parsing/v2/field";
66
import { MindeeHttpErrorV2 } from "../../src/errors/mindeeError";
7+
import * as fs from "node:fs";
78

89
describe("MindeeClientV2 – integration tests (V2)", () => {
910
let client: ClientV2;
@@ -22,6 +23,11 @@ describe("MindeeClientV2 – integration tests (V2)", () => {
2223
"financial_document",
2324
"default_sample.jpg",
2425
);
26+
const sampleBase64Path = path.join(
27+
dataDir,
28+
"file_types",
29+
"receipt.txt",
30+
);
2531

2632
beforeEach(async () => {
2733
const apiKey = process.env["MINDEE_V2_API_KEY"] ?? "";
@@ -61,16 +67,16 @@ describe("MindeeClientV2 – integration tests (V2)", () => {
6167
expect(inference.activeOptions?.confidence).to.be.false;
6268
}).timeout(60000);
6369

64-
it("Filled, single-page image – enqueue & get inference must succeed", async () => {
70+
it("Filled, single-page image – PathInput - enqueueAndGetInference must succeed", async () => {
6571
const source = new PathInput({ inputPath: sampleImagePath });
6672
const params: InferenceParameters = {
6773
modelId,
6874
rag: false,
6975
rawText: true,
70-
polygon: false,
76+
polygon: true,
7177
confidence: false,
7278
webhookIds: [],
73-
alias: "ts_integration_filled_single"
79+
alias: "ts_integration_binary_filled_single"
7480
};
7581

7682
const response = await client.enqueueAndGetInference(source, params);
@@ -86,17 +92,50 @@ describe("MindeeClientV2 – integration tests (V2)", () => {
8692
expect(supplierField).to.be.instanceOf(SimpleField);
8793
expect(supplierField.value).to.equal("John Smith");
8894

89-
9095
expect(inference.result.rawText).to.exist;
9196
expect(inference.activeOptions).to.not.be.null;
9297
expect(inference.activeOptions?.rag).to.be.false;
9398
expect(inference.activeOptions?.rawText).to.be.true;
94-
expect(inference.activeOptions?.polygon).to.be.false;
99+
expect(inference.activeOptions?.polygon).to.be.true;
95100
expect(inference.activeOptions?.confidence).to.be.false;
96101

97102
expect(inference.result.rawText?.pages).to.have.lengthOf(1);
98103
}).timeout(120000);
99104

105+
it("Filled, single-page image – base64 - enqueueAndGetInference must succeed", async () => {
106+
const data = fs.readFileSync(sampleBase64Path, "utf8");
107+
const source = new Base64Input({ inputString: data, filename: "receipt.jpg" });
108+
const params: InferenceParameters = {
109+
modelId,
110+
rag: false,
111+
rawText: false,
112+
polygon: false,
113+
confidence: false,
114+
webhookIds: [],
115+
alias: "ts_integration_base64_filled_single"
116+
};
117+
118+
const response = await client.enqueueAndGetInference(source, params);
119+
120+
const inference = response.inference;
121+
expect(inference.file?.name).to.equal("receipt.txt");
122+
expect(inference.model?.id).to.equal(modelId);
123+
124+
expect(inference.result).to.exist;
125+
expect(inference.result.rawText).to.exist;
126+
127+
const supplierField = inference.result.fields.get("supplier_name") as SimpleField;
128+
expect(supplierField).to.be.instanceOf(SimpleField);
129+
expect(supplierField.value).to.equal("Clachan");
130+
131+
expect(inference.result.rawText).to.be.null;
132+
expect(inference.activeOptions).to.not.be.null;
133+
expect(inference.activeOptions?.rag).to.be.false;
134+
expect(inference.activeOptions?.rawText).to.be.false;
135+
expect(inference.activeOptions?.polygon).to.be.false;
136+
expect(inference.activeOptions?.confidence).to.be.false;
137+
}).timeout(120000);
138+
100139
it("Invalid model ID – enqueue must raise 422", async () => {
101140
const source = new PathInput({ inputPath: emptyPdfPath });
102141
const badParams: InferenceParameters = { modelId: "INVALID MODEL ID" };

0 commit comments

Comments
 (0)