@veryfront/ext-document-kreuzberg 0.1.986 → 0.1.988

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,17 @@
9
9
  import "../../../_dnt.polyfills.js";
10
10
  import type { ExtensionFactory } from "veryfront/extensions";
11
11
  import type { DocumentExtractor, KreuzbergExtractor } from "veryfront/extensions/compat";
12
+ /** Maximum time to wait for fallback worker extraction before aborting. */
13
+ export declare const EXTRACTION_TIMEOUT_MS = 120000;
14
+ declare function extractInWorkerDeno(buffer: ArrayBuffer, mimeType: string): Promise<string>;
15
+ export interface KreuzbergDocumentExtractorDeps {
16
+ isDenoRuntime?: boolean;
17
+ loadNativeKreuzberg?: () => Promise<KreuzbergExtractor>;
18
+ extractInWorkerDeno?: typeof extractInWorkerDeno;
19
+ }
12
20
  export declare class KreuzbergDocumentExtractor implements DocumentExtractor {
21
+ private readonly deps;
22
+ constructor(deps?: KreuzbergDocumentExtractorDeps);
13
23
  importKreuzberg(): Promise<KreuzbergExtractor>;
14
24
  extractInWorker(buffer: ArrayBuffer, mimeType: string): Promise<string>;
15
25
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/extensions/ext-document-kreuzberg/src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,4BAA4B,CAAC;AAKpC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,KAAK,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAsDrG,qBAAa,0BAA2B,YAAW,iBAAiB;IAClE,eAAe,IAAI,OAAO,CAAC,kBAAkB,CAAC;IAIxC,eAAe,CAAC,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAW9E;AAED,QAAA,MAAM,oBAAoB,EAAE,gBAkB3B,CAAC;AAEF,eAAe,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/extensions/ext-document-kreuzberg/src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,4BAA4B,CAAC;AAKpC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,KAAK,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAIrG,2EAA2E;AAC3E,eAAO,MAAM,qBAAqB,SAAU,CAAC;AAE7C,iBAAS,mBAAmB,CAC1B,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,MAAM,CAAC,CA0CjB;AAED,MAAM,WAAW,8BAA8B;IAC7C,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,mBAAmB,CAAC,EAAE,MAAM,OAAO,CAAC,kBAAkB,CAAC,CAAC;IACxD,mBAAmB,CAAC,EAAE,OAAO,mBAAmB,CAAC;CAClD;AAgBD,qBAAa,0BAA2B,YAAW,iBAAiB;IACtD,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAAJ,IAAI,GAAE,8BAAmC;IAEtE,eAAe,IAAI,OAAO,CAAC,kBAAkB,CAAC;IAIxC,eAAe,CAAC,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CA2B9E;AAED,QAAA,MAAM,oBAAoB,EAAE,gBAkB3B,CAAC;AAEF,eAAe,oBAAoB,CAAC"}
@@ -8,10 +8,10 @@
8
8
  */
9
9
  import "../../../_dnt.polyfills.js";
10
10
  import * as dntShim from "../../../_dnt.shims.js";
11
- import { loadKreuzberg } from "./kreuzberg.js";
11
+ import { isMissingPackageError, loadKreuzberg, loadKreuzbergNative } from "./kreuzberg.js";
12
12
  import { isDeno } from "./runtime.js";
13
- /** Maximum time to wait for document text extraction before aborting. */
14
- const EXTRACTION_TIMEOUT_MS = 30_000;
13
+ /** Maximum time to wait for fallback worker extraction before aborting. */
14
+ export const EXTRACTION_TIMEOUT_MS = 120_000;
15
15
  function extractInWorkerDeno(buffer, mimeType) {
16
16
  return new Promise((resolve, reject) => {
17
17
  // The worker ships as raw TypeScript in the compiled binary and from source
@@ -46,20 +46,43 @@ function extractInWorkerDeno(buffer, mimeType) {
46
46
  worker.postMessage({ buffer, mimeType }, [buffer]);
47
47
  });
48
48
  }
49
+ function isPdfMimeType(mimeType) {
50
+ return mimeType.toLowerCase().split(";")[0]?.trim() === "application/pdf";
51
+ }
52
+ async function extractWithNativeKreuzberg(buffer, mimeType, loadNative) {
53
+ const { extractBytes } = await loadNative();
54
+ const result = await extractBytes(new Uint8Array(buffer), mimeType);
55
+ return result.content;
56
+ }
49
57
  export class KreuzbergDocumentExtractor {
58
+ deps;
59
+ constructor(deps = {}) {
60
+ this.deps = deps;
61
+ }
50
62
  importKreuzberg() {
51
63
  return loadKreuzberg();
52
64
  }
53
65
  async extractInWorker(buffer, mimeType) {
54
- // Only a real Deno runtime gets the isolated Worker; Node/Bun extract
55
- // in-process via @kreuzberg/node. See ./runtime.ts for why a bare `Deno`
56
- // check is unreliable in the dnt npm build.
57
- if (!isDeno) {
66
+ const isDenoRuntime = this.deps.isDenoRuntime ?? isDeno;
67
+ const extractWithWorker = this.deps.extractInWorkerDeno ?? extractInWorkerDeno;
68
+ // Node/Bun extract in-process via @kreuzberg/node. Deno keeps the isolated
69
+ // Worker fallback, but PDFs first try the native extractor because the WASM
70
+ // PDF path can hang on valid large manuals.
71
+ if (!isDenoRuntime) {
58
72
  const { extractBytes } = await loadKreuzberg();
59
73
  const result = await extractBytes(new Uint8Array(buffer), mimeType);
60
74
  return result.content;
61
75
  }
62
- return extractInWorkerDeno(buffer, mimeType);
76
+ if (isPdfMimeType(mimeType)) {
77
+ try {
78
+ return await extractWithNativeKreuzberg(buffer, mimeType, this.deps.loadNativeKreuzberg ?? loadKreuzbergNative);
79
+ }
80
+ catch (error) {
81
+ if (!isMissingPackageError(error))
82
+ throw error;
83
+ }
84
+ }
85
+ return extractWithWorker(buffer, mimeType);
63
86
  }
64
87
  }
65
88
  const extDocumentKreuzberg = () => {
@@ -1,3 +1,5 @@
1
1
  import type { KreuzbergExtractor } from "veryfront/extensions/compat";
2
+ export declare function loadKreuzbergNative(): Promise<KreuzbergExtractor>;
2
3
  export declare function loadKreuzberg(): Promise<KreuzbergExtractor>;
4
+ export declare function isMissingPackageError(error: unknown): boolean;
3
5
  //# sourceMappingURL=kreuzberg.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"kreuzberg.d.ts","sourceRoot":"","sources":["../../../../src/extensions/ext-document-kreuzberg/src/kreuzberg.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAwBlF,wBAAsB,aAAa,IAAI,OAAO,CAAC,kBAAkB,CAAC,CA2BjE"}
1
+ {"version":3,"file":"kreuzberg.d.ts","sourceRoot":"","sources":["../../../../src/extensions/ext-document-kreuzberg/src/kreuzberg.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAWlF,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAWvE;AAED,wBAAsB,aAAa,IAAI,OAAO,CAAC,kBAAkB,CAAC,CA2BjE;AAeD,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAqB7D"}
@@ -10,23 +10,22 @@
10
10
  */
11
11
  import * as dntShim from "../../../_dnt.shims.js";
12
12
  import { isDeno } from "./runtime.js";
13
- // deno-lint-ignore no-explicit-any
14
- async function loadKreuzbergNode() {
13
+ export async function loadKreuzbergNative() {
15
14
  try {
16
15
  return await import("@kreuzberg/node");
17
16
  }
18
17
  catch (error) {
19
18
  if (!isMissingPackageError(error))
20
19
  throw error;
21
- throw new Error('Document extraction on Node requires the optional package "@kreuzberg/node". ' +
22
- "Install @kreuzberg/node@^4.4.2 or disable document extraction.");
20
+ throw new Error('Native document extraction requires the optional package "@kreuzberg/node". ' +
21
+ "Install @kreuzberg/node@^4.4.2 or disable document extraction.", { cause: error });
23
22
  }
24
23
  }
25
24
  export async function loadKreuzberg() {
26
25
  // Node/Bun load the native @kreuzberg/node; only a real Deno runtime uses the
27
26
  // WASM build. See ./runtime.ts for why a bare `Deno` check is unreliable here.
28
27
  if (!isDeno) {
29
- return loadKreuzbergNode();
28
+ return loadKreuzbergNative();
30
29
  }
31
30
  const mod = await importKreuzbergWasm();
32
31
  const mainModule = typeof dntShim.Deno.mainModule === "string"
@@ -55,13 +54,23 @@ async function importKreuzbergWasm() {
55
54
  if (!isMissingPackageError(error))
56
55
  throw error;
57
56
  throw new Error('Document extraction on Deno requires the optional package "@kreuzberg/wasm". ' +
58
- "Install @kreuzberg/wasm@4.5.2 or disable document extraction.");
57
+ "Install @kreuzberg/wasm@4.5.2 or disable document extraction.", { cause: error });
59
58
  }
60
59
  }
61
- function isMissingPackageError(error) {
62
- const message = error instanceof Error ? error.message : String(error);
63
- return message.includes("Cannot find package") ||
64
- message.includes("Cannot find module") ||
65
- message.includes("ERR_MODULE_NOT_FOUND") ||
66
- message.includes("Module not found");
60
+ export function isMissingPackageError(error) {
61
+ const seen = new Set();
62
+ let current = error;
63
+ while (current !== undefined && current !== null && !seen.has(current)) {
64
+ seen.add(current);
65
+ const message = current instanceof Error ? current.message : String(current);
66
+ if (message.includes("Cannot find package") ||
67
+ message.includes("Cannot find module") ||
68
+ message.includes("Cannot find native binding") ||
69
+ message.includes("ERR_MODULE_NOT_FOUND") ||
70
+ message.includes("Module not found")) {
71
+ return true;
72
+ }
73
+ current = current instanceof Error ? current.cause : undefined;
74
+ }
75
+ return false;
67
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veryfront/ext-document-kreuzberg",
3
- "version": "0.1.986",
3
+ "version": "0.1.988",
4
4
  "description": "Veryfront first-party extension package for ext-document-kreuzberg",
5
5
  "keywords": [
6
6
  "veryfront",
@@ -53,7 +53,7 @@
53
53
  "@kreuzberg/wasm": "4.5.2"
54
54
  },
55
55
  "peerDependencies": {
56
- "veryfront": "^0.1.986"
56
+ "veryfront": "^0.1.988"
57
57
  },
58
58
  "type": "module",
59
59
  "types": "./esm/extensions/ext-document-kreuzberg/src/index.d.ts",
@@ -63,4 +63,4 @@
63
63
  "NOTICE",
64
64
  "README.md"
65
65
  ]
66
- }
66
+ }