@3sln/emon 0.0.0-stage → 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +113 -2
- package/THIRD_PARTY_NOTICES.md +4759 -0
- package/emon.wasm +0 -0
- package/package.json +34 -4
- package/src/index.d.ts +193 -0
- package/src/index.js +577 -0
- package/src/ocr.js +106 -0
- package/src/pdfium.d.ts +12 -0
- package/src/pdfium.js +287 -0
- package/src/render.js +55 -0
- package/src/sources.js +149 -0
- package/src/tesseract.d.ts +14 -0
- package/src/tesseract.js +114 -0
- package/src/wasm.js +111 -0
package/emon.wasm
ADDED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@3sln/emon",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "emon's JavaScript SDK: PDFs and images to semantic HTML in the browser, over the WASM binding (DEVLOG P9.3). The library has no dependencies; the devDependencies are the browser test harness and the tesseract.js engine the OCR adapter drives.",
|
|
6
|
+
"keywords": ["pdf", "html", "semantic-html", "ocr", "document-conversion", "scraping", "wasm", "webassembly", "tables"],
|
|
7
|
+
"author": "raystubbs (https://github.com/raystubbs)",
|
|
8
|
+
"homepage": "https://github.com/3sln/emon#readme",
|
|
9
|
+
"repository": { "type": "git", "url": "git+https://github.com/3sln/emon.git", "directory": "host/js" },
|
|
10
|
+
"bugs": { "url": "https://github.com/3sln/emon/issues" },
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "src/index.js",
|
|
13
|
+
"types": "src/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": "./src/index.js",
|
|
16
|
+
"./tesseract": "./src/tesseract.js",
|
|
17
|
+
"./pdfium": "./src/pdfium.js",
|
|
18
|
+
"./emon.wasm": "./emon.wasm",
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"files": ["src", "emon.wasm", "LICENSE", "THIRD_PARTY_NOTICES.md", "README.md"],
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"publishConfig": { "access": "public" },
|
|
24
|
+
"engines": { "node": ">=22" },
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@3sln/pdfium-wasm": "^0.1.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependenciesMeta": {
|
|
29
|
+
"@3sln/pdfium-wasm": { "optional": true }
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"puppeteer-core": "25.12.0",
|
|
33
|
+
"tesseract.js": "7.0.0",
|
|
34
|
+
"@tesseract.js-data/eng": "1.0.0"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// Type declarations of `@3sln/emon` (P11.4, D542). They are written by hand beside `index.js` and describe what `api/surface.edn` freezes: the names
|
|
2
|
+
// below are the package's exports, `bb package --check` fails when `index.js` exports a name this file does not declare (and the other way
|
|
3
|
+
// round). The WASM instance, a Model's handle and the constructors of `Emon`, `Model` and `Asset` are not API and are not declared.
|
|
4
|
+
|
|
5
|
+
/** A byte source: the engine reads only what it needs of it, a chunk at a time. `bytes` is there when the document is already in memory. */
|
|
6
|
+
export interface ByteSource {
|
|
7
|
+
readonly size: number;
|
|
8
|
+
read(offset: number, length: number): Promise<Uint8Array | ArrayBuffer | ArrayBufferView>;
|
|
9
|
+
bytes?: Uint8Array;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Anything `toSource` takes: an ArrayBuffer or view, a Blob or File, a URL (a string with a scheme, or a `URL`), a `ReadableStream`, or a `ByteSource`. */
|
|
13
|
+
export type Input = ArrayBuffer | ArrayBufferView | Blob | URL | string | ReadableStream<Uint8Array> | ByteSource;
|
|
14
|
+
|
|
15
|
+
/** An input with the password of an encrypted document. */
|
|
16
|
+
export interface InputWithPassword {
|
|
17
|
+
source: Input;
|
|
18
|
+
password?: string | Uint8Array;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class SourceChangedError extends Error {
|
|
22
|
+
constructor(message: string);
|
|
23
|
+
readonly code: 'source-changed';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A named emon failure: `code` is the stable name a crawler routes on (`password-required`, `input-bytes`, ...). */
|
|
27
|
+
export class EmonError extends Error {
|
|
28
|
+
constructor(code: string, message?: string);
|
|
29
|
+
readonly code: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function bytesSource(buf: ArrayBuffer | ArrayBufferView, options?: { lazy?: boolean }): ByteSource;
|
|
33
|
+
export function blobSource(blob: Blob): ByteSource;
|
|
34
|
+
export function streamSource(stream: ReadableStream<Uint8Array>, options?: { max?: number }): Promise<ByteSource>;
|
|
35
|
+
export function urlSource(
|
|
36
|
+
url: string | URL,
|
|
37
|
+
options?: { fetch?: typeof fetch; headers?: Record<string, string>; signal?: AbortSignal; fetchOptions?: RequestInit },
|
|
38
|
+
): Promise<ByteSource>;
|
|
39
|
+
export function toSource(input: Input, options?: { lazy?: boolean; max?: number }): Promise<ByteSource>;
|
|
40
|
+
|
|
41
|
+
/** A request for the OCR port (`ports.ocr`): see `@3sln/emon/tesseract`. */
|
|
42
|
+
export interface OcrRequest {
|
|
43
|
+
id: number;
|
|
44
|
+
psm: number;
|
|
45
|
+
flags: number;
|
|
46
|
+
dpi: number;
|
|
47
|
+
rect: { x: number; y: number; w: number; h: number } | null;
|
|
48
|
+
langs: string[];
|
|
49
|
+
symbols: boolean;
|
|
50
|
+
osd: boolean;
|
|
51
|
+
bitmap(): Uint8Array;
|
|
52
|
+
[k: string]: unknown;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** An OCR port: `null` or `undefined` answers `unsupported`; a rejection answers `failed`. */
|
|
56
|
+
export type OcrPort = (request: OcrRequest) => Promise<object | null | undefined>;
|
|
57
|
+
|
|
58
|
+
/** One page of a renderer's document. */
|
|
59
|
+
export interface Rendered {
|
|
60
|
+
width: number;
|
|
61
|
+
height: number;
|
|
62
|
+
/** RGB, 3 bytes a pixel, row-major. */
|
|
63
|
+
rgb: Uint8Array;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface RendererDocument {
|
|
67
|
+
render(
|
|
68
|
+
request: { page: number; dpi: number; layer: number },
|
|
69
|
+
sink?: (bytes: number) => Uint8Array,
|
|
70
|
+
): Promise<Rendered | { status: 'failed' | 'unsupported'; reason?: string }>;
|
|
71
|
+
close(): void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A renderer (`ports.render`): see `@3sln/emon/pdfium`. */
|
|
75
|
+
export interface Renderer {
|
|
76
|
+
/** the layers it can separate, a bit set (bit n = layer n) */
|
|
77
|
+
layers?: number;
|
|
78
|
+
producer?: Record<string, string>;
|
|
79
|
+
open(
|
|
80
|
+
doc: { size: number; bytes?: Uint8Array; read(offset: number, length: number): Promise<Uint8Array>; password?: Uint8Array },
|
|
81
|
+
limits: { pixels: number },
|
|
82
|
+
): Promise<RendererDocument>;
|
|
83
|
+
terminate?(): Promise<void>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** SPEC 13's settings by their names. */
|
|
87
|
+
export interface ConvertOptions {
|
|
88
|
+
locale?: string;
|
|
89
|
+
ocr?: 'always' | 'auto' | 'never' | string;
|
|
90
|
+
furniture?: 'policy' | 'keep' | 'drop' | string;
|
|
91
|
+
assets?: 'none' | 'images' | 'figures' | string;
|
|
92
|
+
assetDpi?: number;
|
|
93
|
+
assetPixels?: number;
|
|
94
|
+
assetBytes?: number;
|
|
95
|
+
assetsBytes?: number;
|
|
96
|
+
assetSignatures?: boolean | 'on' | 'off';
|
|
97
|
+
ocrScope?: 'full' | 'witness' | string;
|
|
98
|
+
keepEvidence?: boolean;
|
|
99
|
+
maxRounds?: number;
|
|
100
|
+
fetchRounds?: number;
|
|
101
|
+
/** kin's limits by name (`{ 'input-bytes': n, ... }`) */
|
|
102
|
+
limits?: Record<string, number>;
|
|
103
|
+
producer?: Record<string, string>;
|
|
104
|
+
/** the byte source's chunk size (default 64 KiB) */
|
|
105
|
+
chunk?: number;
|
|
106
|
+
/** read even an in-memory input by range */
|
|
107
|
+
lazy?: boolean;
|
|
108
|
+
ports?: { ocr?: OcrPort; render?: Renderer };
|
|
109
|
+
/** replay: a Map (or entries, or an object) of transcript file name to bytes; no port is called */
|
|
110
|
+
transcript?: Map<string, Uint8Array | ArrayBuffer> | Array<[string, Uint8Array | ArrayBuffer]> | Record<string, Uint8Array | ArrayBuffer>;
|
|
111
|
+
/** the Model then has `transcript()` */
|
|
112
|
+
record?: boolean;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface HtmlOptions {
|
|
116
|
+
ids?: boolean;
|
|
117
|
+
geometry?: boolean;
|
|
118
|
+
/** 0..1000 */
|
|
119
|
+
confidenceBelow?: number;
|
|
120
|
+
ledger?: boolean;
|
|
121
|
+
pretty?: boolean;
|
|
122
|
+
furnitureDrop?: boolean;
|
|
123
|
+
milestones?: boolean;
|
|
124
|
+
initialsPlain?: boolean;
|
|
125
|
+
alignStyle?: boolean;
|
|
126
|
+
assets?: 'inline' | 'external' | 'omit';
|
|
127
|
+
assetPrefix?: string;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface ConvertStats {
|
|
131
|
+
attempts: number;
|
|
132
|
+
fetchRounds: number;
|
|
133
|
+
bytesRequests: number;
|
|
134
|
+
requests: number;
|
|
135
|
+
sourceReads: number;
|
|
136
|
+
bytesFetched: number;
|
|
137
|
+
inputBytes: number;
|
|
138
|
+
reads: number;
|
|
139
|
+
bytesRead: number;
|
|
140
|
+
notes: string[];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface IntakeResult {
|
|
144
|
+
format: string;
|
|
145
|
+
version: [number, number];
|
|
146
|
+
pages: number;
|
|
147
|
+
encrypted: boolean;
|
|
148
|
+
bytes: number;
|
|
149
|
+
line: string;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export class Asset {
|
|
153
|
+
/** the position in `model.assets` */
|
|
154
|
+
readonly index: number;
|
|
155
|
+
/** the file name, `a<id>.jpg` or `a<id>.png` */
|
|
156
|
+
readonly name: string;
|
|
157
|
+
/** `image/jpeg` or `image/png` */
|
|
158
|
+
readonly type: string;
|
|
159
|
+
/** the encoded size in bytes */
|
|
160
|
+
readonly size: number;
|
|
161
|
+
/** The encoded bytes (a copy). */
|
|
162
|
+
bytes(): Uint8Array;
|
|
163
|
+
blob(): Blob;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export class Model {
|
|
167
|
+
readonly stats: ConvertStats;
|
|
168
|
+
/** SPEC 4: emon JSON, as text. */
|
|
169
|
+
json(options?: { pretty?: boolean }): string;
|
|
170
|
+
/** The JSON, parsed. */
|
|
171
|
+
object(): any;
|
|
172
|
+
/** SPEC 5: HTML. */
|
|
173
|
+
html(options?: HtmlOptions): string;
|
|
174
|
+
htmlWithBlobURLs(options?: HtmlOptions): { html: string; urls: Map<string, string>; revoke(): void };
|
|
175
|
+
evidence(): string;
|
|
176
|
+
readonly assets: Asset[];
|
|
177
|
+
transcript(): Map<string, Uint8Array>;
|
|
178
|
+
/** Frees the Model's WASM memory. */
|
|
179
|
+
dispose(): void;
|
|
180
|
+
[Symbol.dispose](): void;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export class Emon {
|
|
184
|
+
/** @param wasm the module (`emon.wasm`, shipped in this package as `@3sln/emon/emon.wasm`): bytes, a compiled module, a Response or a URL */
|
|
185
|
+
static load(
|
|
186
|
+
wasm: ArrayBuffer | Uint8Array | WebAssembly.Module | Response | Promise<Response> | string | URL,
|
|
187
|
+
defaults?: { chunk?: number },
|
|
188
|
+
): Promise<Emon>;
|
|
189
|
+
readonly defaults: { chunk?: number };
|
|
190
|
+
convert(input: Input | InputWithPassword, options?: ConvertOptions): Promise<Model>;
|
|
191
|
+
/** Stage 0 of each input, cheap. */
|
|
192
|
+
intake(inputs: Input | InputWithPassword | Array<Input | InputWithPassword>, options?: ConvertOptions): Promise<IntakeResult[]>;
|
|
193
|
+
}
|