|
1 | | -import { |
2 | | - Base64Input, BufferInput, BytesInput, InputSource, |
3 | | - PathInput, StreamInput, UrlInput, |
4 | | -} from "./input"; |
| 1 | +import { InputSource } from "./input"; |
5 | 2 | import { errorHandler } from "./errors/handler"; |
6 | 3 | import { LOG_LEVELS, logger } from "./logger"; |
7 | 4 |
|
8 | 5 | import { setTimeout } from "node:timers/promises"; |
9 | 6 | import { ErrorResponse, InferenceResponse, JobResponse } from "./parsing/v2"; |
10 | 7 | import { MindeeApiV2 } from "./http/mindeeApiV2"; |
11 | 8 | import { MindeeHttpErrorV2 } from "./errors/mindeeError"; |
12 | | -import { Readable } from "stream"; |
13 | 9 |
|
14 | 10 | /** |
15 | 11 | * Parameters for the internal polling loop in {@link ClientV2.enqueueAndGetInference | enqueueAndGetInference()} . |
@@ -160,6 +156,9 @@ export class ClientV2 { |
160 | 156 | if (inputSource === undefined) { |
161 | 157 | throw new Error("The 'enqueue' function requires an input document."); |
162 | 158 | } |
| 159 | + if (!inputSource.isInitialized()) { |
| 160 | + await inputSource.init(); |
| 161 | + } |
163 | 162 | return await this.mindeeApi.reqPostInferenceEnqueue(inputSource, params); |
164 | 163 | } |
165 | 164 |
|
@@ -232,19 +231,19 @@ export class ClientV2 { |
232 | 231 | * Send a document to an endpoint and poll the server until the result is sent or |
233 | 232 | * until the maximum number of tries is reached. |
234 | 233 | * |
235 | | - * @param inputDoc document to parse. |
| 234 | + * @param inputSource file or URL to parse. |
236 | 235 | * @param params parameters relating to prediction options. |
237 | 236 | * |
238 | 237 | * @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`. |
239 | 238 | * @category Synchronous |
240 | 239 | * @returns a `Promise` containing parsing results. |
241 | 240 | */ |
242 | 241 | async enqueueAndGetInference( |
243 | | - inputDoc: InputSource, |
| 242 | + inputSource: InputSource, |
244 | 243 | params: InferenceParameters |
245 | 244 | ): Promise<InferenceResponse> { |
246 | 245 | const validatedAsyncParams = this.#setAsyncParams(params.pollingOptions); |
247 | | - const enqueueResponse: JobResponse = await this.enqueueInference(inputDoc, params); |
| 246 | + const enqueueResponse: JobResponse = await this.enqueueInference(inputSource, params); |
248 | 247 | if (enqueueResponse.job.id === undefined || enqueueResponse.job.id.length === 0) { |
249 | 248 | logger.error(`Failed enqueueing:\n${enqueueResponse.getRawHttp()}`); |
250 | 249 | throw Error("Enqueueing of the document failed."); |
@@ -283,72 +282,4 @@ Job status: ${pollResults.job.status}.` |
283 | 282 | " seconds" |
284 | 283 | ); |
285 | 284 | } |
286 | | - |
287 | | - /** |
288 | | - * Load an input source from a local path. |
289 | | - * @param inputPath |
290 | | - */ |
291 | | - sourceFromPath(inputPath: string): PathInput { |
292 | | - return new PathInput({ |
293 | | - inputPath: inputPath, |
294 | | - }); |
295 | | - } |
296 | | - |
297 | | - /** |
298 | | - * Load an input source from a base64 encoded string. |
299 | | - * @param inputString input content, as a string. |
300 | | - * @param filename file name. |
301 | | - */ |
302 | | - sourceFromBase64(inputString: string, filename: string): Base64Input { |
303 | | - return new Base64Input({ |
304 | | - inputString: inputString, |
305 | | - filename: filename, |
306 | | - }); |
307 | | - } |
308 | | - |
309 | | - /** |
310 | | - * Load an input source from a `stream.Readable` object. |
311 | | - * @param inputStream input content, as a readable stream. |
312 | | - * @param filename file name. |
313 | | - */ |
314 | | - sourceFromStream(inputStream: Readable, filename: string): StreamInput { |
315 | | - return new StreamInput({ |
316 | | - inputStream: inputStream, |
317 | | - filename: filename, |
318 | | - }); |
319 | | - } |
320 | | - |
321 | | - /** |
322 | | - * Load an input source from bytes. |
323 | | - * @param inputBytes input content, as a Uint8Array or Buffer. |
324 | | - * @param filename file name. |
325 | | - */ |
326 | | - sourceFromBytes(inputBytes: Uint8Array, filename: string): BytesInput { |
327 | | - return new BytesInput({ |
328 | | - inputBytes: inputBytes, |
329 | | - filename: filename, |
330 | | - }); |
331 | | - } |
332 | | - |
333 | | - /** |
334 | | - * Load an input source from a Buffer. |
335 | | - * @param buffer input content, as a buffer. |
336 | | - * @param filename file name. |
337 | | - */ |
338 | | - sourceFromBuffer(buffer: Buffer, filename: string): BufferInput { |
339 | | - return new BufferInput({ |
340 | | - buffer: buffer, |
341 | | - filename: filename, |
342 | | - }); |
343 | | - } |
344 | | - |
345 | | - /** |
346 | | - * Load an input source from a URL. |
347 | | - * @param url input url. Must be HTTPS. |
348 | | - */ |
349 | | - sourceFromUrl(url: string): UrlInput { |
350 | | - return new UrlInput({ |
351 | | - url: url, |
352 | | - }); |
353 | | - } |
354 | 285 | } |
0 commit comments