@sachfolio/cas-reader 0.0.0-stage → 0.2.0-beta.1
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/CHANGELOG.md +19 -0
- package/LICENSE +71 -0
- package/NOTICE +41 -0
- package/README.md +198 -2
- package/dist/analytics/contract.d.ts +110 -0
- package/dist/analytics/contract.js +79 -0
- package/dist/analytics/eligibility.d.ts +66 -0
- package/dist/analytics/eligibility.js +93 -0
- package/dist/analytics/gains-schema.d.ts +898 -0
- package/dist/analytics/gains-schema.js +170 -0
- package/dist/analytics/gains.d.ts +319 -0
- package/dist/analytics/gains.js +649 -0
- package/dist/analytics/modules.d.ts +33 -0
- package/dist/analytics/modules.js +74 -0
- package/dist/analytics/returns-schema.d.ts +931 -0
- package/dist/analytics/returns-schema.js +152 -0
- package/dist/analytics/returns.d.ts +126 -0
- package/dist/analytics/returns.js +453 -0
- package/dist/analytics/schedule112a.d.ts +77 -0
- package/dist/analytics/schedule112a.js +134 -0
- package/dist/analytics/xirr.d.ts +75 -0
- package/dist/analytics/xirr.js +314 -0
- package/dist/bin/cas.d.ts +2 -0
- package/dist/bin/cas.js +137 -0
- package/dist/checks.d.ts +49 -0
- package/dist/checks.js +227 -0
- package/dist/compat/casparser.d.ts +121 -0
- package/dist/compat/casparser.js +193 -0
- package/dist/detect.d.ts +10 -0
- package/dist/detect.js +69 -0
- package/dist/errors.d.ts +26 -0
- package/dist/errors.js +95 -0
- package/dist/export/csv.d.ts +21 -0
- package/dist/export/csv.js +49 -0
- package/dist/extract/browser.d.ts +17 -0
- package/dist/extract/browser.js +34 -0
- package/dist/extract/budget.d.ts +46 -0
- package/dist/extract/budget.js +88 -0
- package/dist/extract/node.d.ts +11 -0
- package/dist/extract/node.js +31 -0
- package/dist/extract/pdfjs.d.ts +71 -0
- package/dist/extract/pdfjs.js +244 -0
- package/dist/extract/types.d.ts +106 -0
- package/dist/extract/types.js +11 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +26 -0
- package/dist/layout/lines.d.ts +51 -0
- package/dist/layout/lines.js +115 -0
- package/dist/licence/app-licence.d.ts +53 -0
- package/dist/licence/app-licence.js +139 -0
- package/dist/licence/ed25519.d.ts +2 -0
- package/dist/licence/ed25519.js +103 -0
- package/dist/model.d.ts +148 -0
- package/dist/model.js +1 -0
- package/dist/num.d.ts +19 -0
- package/dist/num.js +27 -0
- package/dist/parse.d.ts +4 -0
- package/dist/parse.js +13 -0
- package/dist/parsers/cams/classify.d.ts +25 -0
- package/dist/parsers/cams/classify.js +103 -0
- package/dist/parsers/cams/detailed.d.ts +15 -0
- package/dist/parsers/cams/detailed.js +343 -0
- package/dist/parsers/cams/header.d.ts +47 -0
- package/dist/parsers/cams/header.js +170 -0
- package/dist/parsers/cams/investor.d.ts +28 -0
- package/dist/parsers/cams/investor.js +139 -0
- package/dist/parsers/cams/summary.d.ts +29 -0
- package/dist/parsers/cams/summary.js +413 -0
- package/dist/parsers/cams/table.d.ts +55 -0
- package/dist/parsers/cams/table.js +144 -0
- package/dist/parsers/cams/total.d.ts +17 -0
- package/dist/parsers/cams/total.js +105 -0
- package/dist/parsers/depository/mf.d.ts +22 -0
- package/dist/parsers/depository/mf.js +438 -0
- package/dist/privacy.d.ts +243 -0
- package/dist/privacy.js +256 -0
- package/dist/schema.d.ts +779 -0
- package/dist/schema.js +214 -0
- package/examples/nextjs/app/page.jsx +34 -0
- package/examples/node/server.mjs +45 -0
- package/examples/python/read_cas.py +67 -0
- package/examples/vite/index.html +14 -0
- package/examples/vite/main.js +22 -0
- package/package.json +54 -4
package/dist/errors.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
export class CasError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
constructor(code, message, options) {
|
|
4
|
+
super(message, options);
|
|
5
|
+
this.name = "CasError";
|
|
6
|
+
this.code = code;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function isCasError(e) {
|
|
10
|
+
return e instanceof CasError;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* One developer-facing line per code: what happened, what the caller (or its
|
|
14
|
+
* user) can do next, and whether trying the same file again can help. A host app
|
|
15
|
+
* can show `message` and `nextStep` to its users. Meaning follows the Sachfolio
|
|
16
|
+
* app's own error copy (apps/web/src/copy.ts). The hosted API sends the same
|
|
17
|
+
* text as `error.message` / `error.next_step`.
|
|
18
|
+
*/
|
|
19
|
+
export const CAS_ERRORS = {
|
|
20
|
+
NEED_PASSWORD: {
|
|
21
|
+
message: "The PDF is password-protected.",
|
|
22
|
+
nextStep: "Ask for the password and read again. CAMS and KFintech use the one set on the request form; NSDL and CDSL use the first holder's PAN in capitals.",
|
|
23
|
+
retry: true,
|
|
24
|
+
},
|
|
25
|
+
INCORRECT_PASSWORD: {
|
|
26
|
+
message: "The password doesn't open this PDF.",
|
|
27
|
+
nextStep: "Passwords are case-sensitive. Ask again and retry with the same file.",
|
|
28
|
+
retry: true,
|
|
29
|
+
},
|
|
30
|
+
DAMAGED_FILE: {
|
|
31
|
+
message: "The PDF is damaged or incomplete.",
|
|
32
|
+
nextStep: "Download the attachment again from the original email and read the new copy.",
|
|
33
|
+
retry: false,
|
|
34
|
+
},
|
|
35
|
+
UNSUPPORTED_ENCRYPTION: {
|
|
36
|
+
message: "The PDF uses protection the reader can't open.",
|
|
37
|
+
nextStep: "Use the original attachment from the CAMS, KFintech, NSDL or CDSL email.",
|
|
38
|
+
retry: false,
|
|
39
|
+
},
|
|
40
|
+
FILE_TOO_LARGE: {
|
|
41
|
+
message: "The file is larger than the reader's 25 MB limit.",
|
|
42
|
+
nextStep: "A CAS is usually far smaller, so this may be a different file. Use the PDF attached to the registrar's email.",
|
|
43
|
+
retry: false,
|
|
44
|
+
},
|
|
45
|
+
TOO_MANY_PAGES: {
|
|
46
|
+
message: "The PDF has more than 300 pages.",
|
|
47
|
+
nextStep: "Request a shorter period, or a statement per PAN.",
|
|
48
|
+
retry: false,
|
|
49
|
+
},
|
|
50
|
+
TOO_MUCH_TEXT: {
|
|
51
|
+
message: "The PDF holds more text than the reader will process safely.",
|
|
52
|
+
nextStep: "Request a shorter period. In a browser, try a computer rather than a phone.",
|
|
53
|
+
retry: false,
|
|
54
|
+
},
|
|
55
|
+
TIMEOUT: {
|
|
56
|
+
message: "Reading took longer than the time limit.",
|
|
57
|
+
nextStep: "Try again. If it keeps happening, request a shorter period or read on a faster device.",
|
|
58
|
+
retry: true,
|
|
59
|
+
},
|
|
60
|
+
CANCELLED: {
|
|
61
|
+
message: "The read was cancelled.",
|
|
62
|
+
nextStep: "Nothing was kept. Start the read again when ready.",
|
|
63
|
+
retry: true,
|
|
64
|
+
},
|
|
65
|
+
IMAGE_ONLY: {
|
|
66
|
+
message: "The PDF is scanned images with no text.",
|
|
67
|
+
nextStep: "Use the original PDF from the registrar's email, which has real text in it.",
|
|
68
|
+
retry: false,
|
|
69
|
+
},
|
|
70
|
+
REPRINTED_PDF: {
|
|
71
|
+
message: "This looks like a re-saved or printed copy, not the original statement.",
|
|
72
|
+
nextStep: "Printing or saving as PDF removes details the reader needs. Use the original attachment from the email.",
|
|
73
|
+
retry: false,
|
|
74
|
+
},
|
|
75
|
+
MFCENTRAL_UNSUPPORTED: {
|
|
76
|
+
message: "MF Central statements aren't read yet.",
|
|
77
|
+
nextStep: "Ask the user to request a Consolidated Account Statement from CAMS instead.",
|
|
78
|
+
retry: false,
|
|
79
|
+
},
|
|
80
|
+
DEPOSITORY_UNSUPPORTED_YET: {
|
|
81
|
+
message: "This NSDL or CDSL statement couldn't be read.",
|
|
82
|
+
nextStep: "No longer returned: NSDL and CDSL mutual fund sections are read. Kept so code that matches on it still compiles.",
|
|
83
|
+
retry: false,
|
|
84
|
+
},
|
|
85
|
+
UNKNOWN_TEMPLATE: {
|
|
86
|
+
message: "This isn't a statement layout the reader knows.",
|
|
87
|
+
nextStep: "The reader reads CAS from CAMS, KFintech, NSDL and CDSL. It doesn't guess at other layouts. Send a masked copy to developers@sachfolio.com if it should be one of these.",
|
|
88
|
+
retry: false,
|
|
89
|
+
},
|
|
90
|
+
EXTRACTION_FAILED: {
|
|
91
|
+
message: "The statement couldn't be read.",
|
|
92
|
+
nextStep: "This is the reader's fault, not the file's. Nothing was kept. Try again; if it repeats, email developers@sachfolio.com with the error code and engine version.",
|
|
93
|
+
retry: true,
|
|
94
|
+
},
|
|
95
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSV for spreadsheets, built from a CasDocument. Mirrors the investor app's
|
|
3
|
+
* "Save a copy" export (apps/web/src/lib/export.ts) with ISO dates only.
|
|
4
|
+
*
|
|
5
|
+
* - Numbers are the engine's exact decimal strings (`Num.value`): plain,
|
|
6
|
+
* ungrouped, ASCII minus, never a JS number. An absent value is an empty cell.
|
|
7
|
+
* - Dates are ISO (YYYY-MM-DD).
|
|
8
|
+
* - Every extracted string is untrusted: a text cell a spreadsheet could read
|
|
9
|
+
* as a formula (first character = + - @, a tab or a carriage return) gets a
|
|
10
|
+
* leading '.
|
|
11
|
+
* - RFC 4180: cells with a comma, quote or line break are quoted; rows end in
|
|
12
|
+
* CRLF. No byte-order mark: prefix "" yourself if Excel must show ₹ or
|
|
13
|
+
* accented names correctly.
|
|
14
|
+
*/
|
|
15
|
+
import type { CasDocument } from "../model.js";
|
|
16
|
+
export declare const HOLDINGS_CSV_HEADER: readonly ["Fund house", "Folio", "Scheme", "ISIN", "Plan", "Option", "Closing units", "NAV", "NAV date", "Value", "Value date", "Page"];
|
|
17
|
+
export declare const TRANSACTIONS_CSV_HEADER: readonly ["Fund house", "Folio", "Scheme", "ISIN", "Date", "Type", "Description", "Amount", "Units", "Price", "Unit balance", "Page"];
|
|
18
|
+
/** One row per holding, in statement order, with a header row. */
|
|
19
|
+
export declare function toHoldingsCsv(cas: CasDocument): string;
|
|
20
|
+
/** One row per transaction, in statement order, with a header row. */
|
|
21
|
+
export declare function toTransactionsCsv(cas: CasDocument): string;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const CRLF = "\r\n";
|
|
2
|
+
/** Leading characters a spreadsheet may treat as a formula (OWASP CSV injection, plus U+2212 minus). */
|
|
3
|
+
const FORMULA_START = /^[=+\-@−\t\r]/;
|
|
4
|
+
const PLAIN_DECIMAL = /^-?\d+(\.\d+)?$/;
|
|
5
|
+
function quote(text) {
|
|
6
|
+
return /[",\r\n]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
|
|
7
|
+
}
|
|
8
|
+
/** A text cell: formula-neutralised, then quoted. */
|
|
9
|
+
function text(v) {
|
|
10
|
+
if (v == null)
|
|
11
|
+
return "";
|
|
12
|
+
return quote(FORMULA_START.test(v) ? `'${v}` : v);
|
|
13
|
+
}
|
|
14
|
+
/** A numeric cell: a plain decimal as is; anything else is written as a neutralised text cell. */
|
|
15
|
+
function decimal(v) {
|
|
16
|
+
if (v == null)
|
|
17
|
+
return "";
|
|
18
|
+
return PLAIN_DECIMAL.test(v) ? v : text(v);
|
|
19
|
+
}
|
|
20
|
+
const num = (n) => decimal(n?.value);
|
|
21
|
+
const page = (n) => (n == null || !Number.isInteger(n) ? "" : String(n));
|
|
22
|
+
const amc = (name) => (name === "UNKNOWN" ? "" : name);
|
|
23
|
+
function csv(header, rows) {
|
|
24
|
+
return [header.map(text), ...rows].map((r) => r.join(",") + CRLF).join("");
|
|
25
|
+
}
|
|
26
|
+
export const HOLDINGS_CSV_HEADER = [
|
|
27
|
+
"Fund house", "Folio", "Scheme", "ISIN", "Plan", "Option",
|
|
28
|
+
"Closing units", "NAV", "NAV date", "Value", "Value date", "Page",
|
|
29
|
+
];
|
|
30
|
+
export const TRANSACTIONS_CSV_HEADER = [
|
|
31
|
+
"Fund house", "Folio", "Scheme", "ISIN",
|
|
32
|
+
"Date", "Type", "Description", "Amount", "Units", "Price", "Unit balance", "Page",
|
|
33
|
+
];
|
|
34
|
+
/** One row per holding, in statement order, with a header row. */
|
|
35
|
+
export function toHoldingsCsv(cas) {
|
|
36
|
+
return csv(HOLDINGS_CSV_HEADER, cas.folios.flatMap((folio) => folio.schemes.map((s) => [
|
|
37
|
+
text(amc(folio.amc)), text(folio.folio), text(s.name), text(s.isin), text(s.plan), text(s.option),
|
|
38
|
+
num(s.close), num(s.nav?.value), text(s.nav?.date), num(s.valuation?.value), text(s.valuation?.date),
|
|
39
|
+
page((s.footerSource ?? s.source).page),
|
|
40
|
+
])));
|
|
41
|
+
}
|
|
42
|
+
/** One row per transaction, in statement order, with a header row. */
|
|
43
|
+
export function toTransactionsCsv(cas) {
|
|
44
|
+
return csv(TRANSACTIONS_CSV_HEADER, cas.folios.flatMap((folio) => folio.schemes.flatMap((s) => s.transactions.map((t) => [
|
|
45
|
+
text(amc(folio.amc)), text(folio.folio), text(s.name), text(s.isin),
|
|
46
|
+
text(t.date), text(t.type), text(t.description),
|
|
47
|
+
num(t.amount), num(t.units), num(t.price), num(t.balance), page(t.source.page),
|
|
48
|
+
]))));
|
|
49
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CasDocument } from "../model.js";
|
|
2
|
+
import type { Extractor } from "./types.js";
|
|
3
|
+
export interface CasReaderOptions {
|
|
4
|
+
/** URL of the folder `copy-assets` filled, e.g. "/pdfjs/" (a trailing slash is added if missing). */
|
|
5
|
+
assetsUrl: string;
|
|
6
|
+
/** The pdf.js worker script; defaults to `pdf.worker.mjs` in `assetsUrl`. */
|
|
7
|
+
workerSrc?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface CasReader {
|
|
10
|
+
extractor: Extractor;
|
|
11
|
+
readCas(bytes: Uint8Array, options?: {
|
|
12
|
+
password?: string;
|
|
13
|
+
signal?: AbortSignal;
|
|
14
|
+
}): Promise<CasDocument>;
|
|
15
|
+
}
|
|
16
|
+
export declare function createCasReader(options: CasReaderOptions): CasReader;
|
|
17
|
+
export * from "../index.js";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser entry for the developer package (PLAN.md stage E): reads a
|
|
3
|
+
* statement on the user's device with pdf.js's legacy build, so the PDF and
|
|
4
|
+
* its password never reach any server, yours or ours.
|
|
5
|
+
*
|
|
6
|
+
* pdf.js's worker, fonts, cmaps and wasm decoders are served from your own
|
|
7
|
+
* site: copy them once with `npx sachfolio-cas copy-assets public/pdfjs`, then
|
|
8
|
+
*
|
|
9
|
+
* const reader = createCasReader({ assetsUrl: "/pdfjs/" });
|
|
10
|
+
* const cas = await reader.readCas(new Uint8Array(await file.arrayBuffer()), { password });
|
|
11
|
+
*
|
|
12
|
+
* Nothing is fetched from any other origin.
|
|
13
|
+
*/
|
|
14
|
+
import * as pdfjs from "pdfjs-dist/legacy/build/pdf.mjs";
|
|
15
|
+
import { parseCas } from "../parse.js";
|
|
16
|
+
import { createPdfjsExtractor } from "./pdfjs.js";
|
|
17
|
+
export function createCasReader(options) {
|
|
18
|
+
const base = new URL(options.assetsUrl.endsWith("/") ? options.assetsUrl : `${options.assetsUrl}/`, globalThis.location?.href).href;
|
|
19
|
+
pdfjs.GlobalWorkerOptions.workerSrc = options.workerSrc ?? `${base}pdf.worker.mjs`;
|
|
20
|
+
const extractor = createPdfjsExtractor(pdfjs, {
|
|
21
|
+
cMapUrl: `${base}cmaps/`,
|
|
22
|
+
standardFontDataUrl: `${base}standard_fonts/`,
|
|
23
|
+
wasmUrl: `${base}wasm/`,
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
extractor,
|
|
27
|
+
async readCas(bytes, o = {}) {
|
|
28
|
+
const doc = await extractor.extract(bytes, { ...(o.password !== undefined ? { password: o.password } : {}), ...(o.signal ? { signal: o.signal } : {}) });
|
|
29
|
+
return parseCas(doc);
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
// The rest of the API, so a browser app imports from one place.
|
|
34
|
+
export * from "../index.js";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** One reading of the host's JS heap, in bytes. */
|
|
2
|
+
export interface HeapReading {
|
|
3
|
+
used: number;
|
|
4
|
+
limit: number;
|
|
5
|
+
}
|
|
6
|
+
/** Returns the current heap reading, or null when the host can't tell. Must be cheap and synchronous. */
|
|
7
|
+
export type HeapProbe = () => HeapReading | null;
|
|
8
|
+
export interface TextBudgetLimits {
|
|
9
|
+
/** Total text items across the document. */
|
|
10
|
+
maxItems: number;
|
|
11
|
+
/** Text items on one page. */
|
|
12
|
+
maxPageItems: number;
|
|
13
|
+
/** Estimated bytes kept for one page's text (see estimateItemBytes). */
|
|
14
|
+
maxPageTextBytes: number;
|
|
15
|
+
/** Estimated bytes kept for the whole document's text. */
|
|
16
|
+
maxTextBytes: number;
|
|
17
|
+
/** Stop when the heap probe reports more than this share of its limit (0–1). */
|
|
18
|
+
maxHeapShare: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Bytes one extracted item is assumed to occupy: the RawItem object, its
|
|
22
|
+
* transform array and strings (V8 stores most text one byte per character,
|
|
23
|
+
* but two is the safe assumption).
|
|
24
|
+
*/
|
|
25
|
+
export declare const ITEM_OVERHEAD_BYTES = 320;
|
|
26
|
+
export declare function estimateItemBytes(str: unknown): number;
|
|
27
|
+
export declare class TextBudget {
|
|
28
|
+
private readonly limits;
|
|
29
|
+
private readonly probe?;
|
|
30
|
+
private items;
|
|
31
|
+
private bytes;
|
|
32
|
+
private pageItems;
|
|
33
|
+
private pageBytes;
|
|
34
|
+
private page;
|
|
35
|
+
constructor(limits: TextBudgetLimits, probe?: HeapProbe | undefined);
|
|
36
|
+
/** Starts counting a new page. */
|
|
37
|
+
startPage(n: number): void;
|
|
38
|
+
/** Counts one chunk of raw pdf.js text items; throws TOO_MUCH_TEXT when a limit is passed. */
|
|
39
|
+
add(chunk: ReadonlyArray<Record<string, unknown>>): void;
|
|
40
|
+
/** Checks the host's heap reading, if any. Also called between pages. */
|
|
41
|
+
checkHeap(): void;
|
|
42
|
+
get totals(): {
|
|
43
|
+
items: number;
|
|
44
|
+
bytes: number;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory guard for extraction (LAUNCH-CHECKLIST §3, ROADMAP M3). A phone tab
|
|
3
|
+
* that runs out of memory is killed without a word, so extraction stops
|
|
4
|
+
* itself first and reports TOO_MUCH_TEXT ("This protects your phone from
|
|
5
|
+
* running out of memory", DESIGN-BRIEF §7.9).
|
|
6
|
+
*
|
|
7
|
+
* Two kinds of limit, checked as each chunk of text arrives from pdf.js
|
|
8
|
+
* (pages are streamed, so a single huge page is stopped part-way):
|
|
9
|
+
* - a deterministic budget on text items and their estimated size, per page
|
|
10
|
+
* and in total, which behaves the same on every device;
|
|
11
|
+
* - an optional heap reading from the host (the browser's
|
|
12
|
+
* `performance.memory` where it exists), stopping when the JS heap is
|
|
13
|
+
* close to its limit.
|
|
14
|
+
* The estimate is deliberately simple; it bounds what the extractor keeps,
|
|
15
|
+
* not what the browser or pdf.js allocate around it.
|
|
16
|
+
*/
|
|
17
|
+
import { CasError } from "../errors.js";
|
|
18
|
+
/**
|
|
19
|
+
* Bytes one extracted item is assumed to occupy: the RawItem object, its
|
|
20
|
+
* transform array and strings (V8 stores most text one byte per character,
|
|
21
|
+
* but two is the safe assumption).
|
|
22
|
+
*/
|
|
23
|
+
export const ITEM_OVERHEAD_BYTES = 320;
|
|
24
|
+
export function estimateItemBytes(str) {
|
|
25
|
+
return ITEM_OVERHEAD_BYTES + (typeof str === "string" ? 2 * str.length : 0);
|
|
26
|
+
}
|
|
27
|
+
export class TextBudget {
|
|
28
|
+
limits;
|
|
29
|
+
probe;
|
|
30
|
+
items = 0;
|
|
31
|
+
bytes = 0;
|
|
32
|
+
pageItems = 0;
|
|
33
|
+
pageBytes = 0;
|
|
34
|
+
page = 0;
|
|
35
|
+
constructor(limits, probe) {
|
|
36
|
+
this.limits = limits;
|
|
37
|
+
this.probe = probe;
|
|
38
|
+
}
|
|
39
|
+
/** Starts counting a new page. */
|
|
40
|
+
startPage(n) {
|
|
41
|
+
this.page = n;
|
|
42
|
+
this.pageItems = 0;
|
|
43
|
+
this.pageBytes = 0;
|
|
44
|
+
}
|
|
45
|
+
/** Counts one chunk of raw pdf.js text items; throws TOO_MUCH_TEXT when a limit is passed. */
|
|
46
|
+
add(chunk) {
|
|
47
|
+
let bytes = 0;
|
|
48
|
+
for (const item of chunk)
|
|
49
|
+
bytes += estimateItemBytes(item["str"]);
|
|
50
|
+
this.items += chunk.length;
|
|
51
|
+
this.pageItems += chunk.length;
|
|
52
|
+
this.bytes += bytes;
|
|
53
|
+
this.pageBytes += bytes;
|
|
54
|
+
const l = this.limits;
|
|
55
|
+
if (this.items > l.maxItems)
|
|
56
|
+
throw tooMuch(`More than ${l.maxItems} text items.`);
|
|
57
|
+
if (this.pageItems > l.maxPageItems)
|
|
58
|
+
throw tooMuch(`Page ${this.page} has more than ${l.maxPageItems} text items.`);
|
|
59
|
+
if (this.pageBytes > l.maxPageTextBytes)
|
|
60
|
+
throw tooMuch(`Page ${this.page} has more than ${l.maxPageTextBytes} bytes of text (estimated).`);
|
|
61
|
+
if (this.bytes > l.maxTextBytes)
|
|
62
|
+
throw tooMuch(`More than ${l.maxTextBytes} bytes of text (estimated).`);
|
|
63
|
+
this.checkHeap();
|
|
64
|
+
}
|
|
65
|
+
/** Checks the host's heap reading, if any. Also called between pages. */
|
|
66
|
+
checkHeap() {
|
|
67
|
+
if (!this.probe)
|
|
68
|
+
return;
|
|
69
|
+
let r;
|
|
70
|
+
try {
|
|
71
|
+
r = this.probe();
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return; // A probe that fails tells us nothing; the item budget still holds.
|
|
75
|
+
}
|
|
76
|
+
if (!r || !(r.limit > 0) || !(r.used >= 0))
|
|
77
|
+
return;
|
|
78
|
+
if (r.used > this.limits.maxHeapShare * r.limit) {
|
|
79
|
+
throw tooMuch(`JS heap at ${Math.round((100 * r.used) / r.limit)}% of its limit.`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
get totals() {
|
|
83
|
+
return { items: this.items, bytes: this.bytes };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function tooMuch(message) {
|
|
87
|
+
return new CasError("TOO_MUCH_TEXT", message);
|
|
88
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CasDocument } from "../model.js";
|
|
2
|
+
import type { Extractor } from "./types.js";
|
|
3
|
+
export declare function createNodeExtractor(): Extractor;
|
|
4
|
+
/**
|
|
5
|
+
* Reads a statement in Node: PDF bytes (and its password) → CasDocument.
|
|
6
|
+
* Throws CasError (`isCasError`) for anything it can't read.
|
|
7
|
+
*/
|
|
8
|
+
export declare function readCas(bytes: Uint8Array, options?: {
|
|
9
|
+
password?: string;
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
}): Promise<CasDocument>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node entry for the pdf.js backend (CLI, corpus harness, tests).
|
|
3
|
+
* The browser worker gets its own entry with bundled resource URLs (M3).
|
|
4
|
+
*/
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { pathToFileURL } from "node:url";
|
|
8
|
+
import * as pdfjs from "pdfjs-dist/legacy/build/pdf.mjs";
|
|
9
|
+
import { createPdfjsExtractor } from "./pdfjs.js";
|
|
10
|
+
import { parseCas } from "../parse.js";
|
|
11
|
+
const require = createRequire(import.meta.url);
|
|
12
|
+
const pdfjsRoot = path.dirname(require.resolve("pdfjs-dist/package.json"));
|
|
13
|
+
// Node runs pdf.js's "fake worker" in-process; point it at the matching build.
|
|
14
|
+
pdfjs.GlobalWorkerOptions.workerSrc = pathToFileURL(path.join(pdfjsRoot, "legacy", "build", "pdf.worker.mjs")).href;
|
|
15
|
+
// pdf.js requires a trailing "/" and rejects Windows backslashes here.
|
|
16
|
+
const dir = (sub) => path.join(pdfjsRoot, sub).replaceAll("\\", "/") + "/";
|
|
17
|
+
export function createNodeExtractor() {
|
|
18
|
+
return createPdfjsExtractor(pdfjs, {
|
|
19
|
+
cMapUrl: dir("cmaps"),
|
|
20
|
+
standardFontDataUrl: dir("standard_fonts"),
|
|
21
|
+
wasmUrl: dir("wasm"),
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Reads a statement in Node: PDF bytes (and its password) → CasDocument.
|
|
26
|
+
* Throws CasError (`isCasError`) for anything it can't read.
|
|
27
|
+
*/
|
|
28
|
+
export async function readCas(bytes, options = {}) {
|
|
29
|
+
const doc = await createNodeExtractor().extract(bytes, { ...(options.password !== undefined ? { password: options.password } : {}), ...(options.signal ? { signal: options.signal } : {}) });
|
|
30
|
+
return parseCas(doc);
|
|
31
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type Extractor } from "./types.js";
|
|
2
|
+
/** The subset of the pdf.js module surface this backend relies on. */
|
|
3
|
+
export interface PdfjsModule {
|
|
4
|
+
version: string;
|
|
5
|
+
getDocument(params: Record<string, unknown>): PdfjsLoadingTask;
|
|
6
|
+
Util: {
|
|
7
|
+
transform(m1: number[], m2: number[]): number[];
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
interface PdfjsLoadingTask {
|
|
11
|
+
promise: Promise<PdfjsDocument>;
|
|
12
|
+
destroy(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
interface PdfjsDocument {
|
|
15
|
+
numPages: number;
|
|
16
|
+
getPage(n: number): Promise<PdfjsPage>;
|
|
17
|
+
getMetadata(): Promise<{
|
|
18
|
+
info: Record<string, unknown>;
|
|
19
|
+
}>;
|
|
20
|
+
}
|
|
21
|
+
interface PdfjsTextContent {
|
|
22
|
+
items: Array<Record<string, unknown>>;
|
|
23
|
+
styles: Record<string, {
|
|
24
|
+
fontFamily?: string;
|
|
25
|
+
}>;
|
|
26
|
+
}
|
|
27
|
+
interface PdfjsPage {
|
|
28
|
+
rotate: number;
|
|
29
|
+
getViewport(p: {
|
|
30
|
+
scale: number;
|
|
31
|
+
rotation?: number;
|
|
32
|
+
}): {
|
|
33
|
+
width: number;
|
|
34
|
+
height: number;
|
|
35
|
+
transform: number[];
|
|
36
|
+
};
|
|
37
|
+
getTextContent(p?: Record<string, unknown>): Promise<PdfjsTextContent>;
|
|
38
|
+
/** pdf.js streams a page's text in chunks (getTextContent collects them). Optional for test doubles. */
|
|
39
|
+
streamTextContent?(p?: Record<string, unknown>): ReadableStream<PdfjsTextContent>;
|
|
40
|
+
cleanup(): void;
|
|
41
|
+
}
|
|
42
|
+
export interface PdfjsResourceUrls {
|
|
43
|
+
/** Directory URL/path (trailing slash) with pdf.js bcmaps, for CID fonts. */
|
|
44
|
+
cMapUrl?: string;
|
|
45
|
+
/** Directory URL/path (trailing slash) with pdf.js standard font data. */
|
|
46
|
+
standardFontDataUrl?: string;
|
|
47
|
+
/** Directory URL/path (trailing slash) with pdf.js wasm decoders. */
|
|
48
|
+
wasmUrl?: string;
|
|
49
|
+
/**
|
|
50
|
+
* A reusable pdf.js PDFWorker (browser). Passed explicitly so destroying a
|
|
51
|
+
* document doesn't tear down the worker: one worker serves the session
|
|
52
|
+
* (architecture ROADMAP M3). Called per document, so a cleared worker can be
|
|
53
|
+
* replaced.
|
|
54
|
+
*/
|
|
55
|
+
worker?: () => unknown;
|
|
56
|
+
/**
|
|
57
|
+
* pdf.js `BinaryDataFactory` class (browser): what answers the worker's
|
|
58
|
+
* requests for CMaps, standard fonts and wasm. The app passes one that
|
|
59
|
+
* answers from memory, so no request made after a file is picked depends
|
|
60
|
+
* on the file (ROADMAP M3 "Input-independent requests").
|
|
61
|
+
*/
|
|
62
|
+
binaryDataFactory?: unknown;
|
|
63
|
+
/**
|
|
64
|
+
* pdf.js `maxImageSize` in pixels (-1: no limit). Text never needs image
|
|
65
|
+
* pixels, but pdf.js decodes images inside Type3 glyphs while loading a
|
|
66
|
+
* font; 0 skips every image, so no image decoder (wasm) is ever requested.
|
|
67
|
+
*/
|
|
68
|
+
maxImageSize?: number;
|
|
69
|
+
}
|
|
70
|
+
export declare function createPdfjsExtractor(pdfjs: PdfjsModule, urls?: PdfjsResourceUrls): Extractor;
|
|
71
|
+
export {};
|