@mjasnikovs/pi-nop 0.1.2 → 0.1.4
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/README.md +4 -3
- package/package.json +3 -2
- package/src/config/command.ts +46 -4
- package/src/config/config.ts +86 -11
- package/src/search/search.ts +17 -7
- package/src/subagent/answer.ts +41 -16
- package/src/subagent/fetch/cmaps/78-EUC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/78-EUC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/78-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/78-RKSJ-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/78-RKSJ-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/78-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/78ms-RKSJ-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/78ms-RKSJ-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/83pv-RKSJ-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/90ms-RKSJ-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/90ms-RKSJ-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/90msp-RKSJ-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/90msp-RKSJ-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/90pv-RKSJ-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/90pv-RKSJ-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Add-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Add-RKSJ-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Add-RKSJ-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Add-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-CNS1-0.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-CNS1-1.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-CNS1-2.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-CNS1-3.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-CNS1-4.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-CNS1-5.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-CNS1-6.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-GB1-0.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-GB1-1.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-GB1-2.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-GB1-3.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-GB1-4.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-GB1-5.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Japan1-0.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Japan1-1.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Japan1-2.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Japan1-3.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Japan1-4.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Japan1-5.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Japan1-6.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Korea1-0.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Korea1-1.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Korea1-2.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
- package/src/subagent/fetch/cmaps/B5-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/B5-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/B5pc-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/B5pc-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/CNS-EUC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/CNS-EUC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/CNS1-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/CNS1-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/CNS2-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/CNS2-V.bcmap +3 -0
- package/src/subagent/fetch/cmaps/ETHK-B5-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/ETHK-B5-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/ETen-B5-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/ETen-B5-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/ETenms-B5-H.bcmap +3 -0
- package/src/subagent/fetch/cmaps/ETenms-B5-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/EUC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/EUC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Ext-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Ext-RKSJ-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Ext-RKSJ-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Ext-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GB-EUC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GB-EUC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GB-H.bcmap +6 -0
- package/src/subagent/fetch/cmaps/GB-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBK-EUC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBK-EUC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBK2K-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBK2K-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBKp-EUC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBKp-EUC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBT-EUC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBT-EUC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBT-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBT-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBTpc-EUC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBTpc-EUC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBpc-EUC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/GBpc-EUC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKdla-B5-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKdla-B5-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKdlb-B5-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKdlb-B5-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKgccs-B5-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKgccs-B5-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKm314-B5-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKm314-B5-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKm471-B5-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKm471-B5-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKscs-B5-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/HKscs-B5-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Hankaku.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Hiragana.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSC-EUC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSC-EUC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSC-Johab-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSC-Johab-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSCms-UHC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSCms-UHC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSCpc-EUC-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/KSCpc-EUC-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Katakana.bcmap +0 -0
- package/src/subagent/fetch/cmaps/LICENSE +36 -0
- package/src/subagent/fetch/cmaps/NWP-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/NWP-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/RKSJ-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/RKSJ-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/Roman.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniCNS-UCS2-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniCNS-UCS2-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniCNS-UTF16-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniCNS-UTF16-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniCNS-UTF32-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniCNS-UTF32-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniCNS-UTF8-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniCNS-UTF8-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniGB-UCS2-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniGB-UCS2-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniGB-UTF16-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniGB-UTF16-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniGB-UTF32-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniGB-UTF32-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniGB-UTF8-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniGB-UTF8-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS-UCS2-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS-UCS2-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS-UTF16-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS-UTF16-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS-UTF32-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS-UTF32-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS-UTF8-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS-UTF8-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniKS-UCS2-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniKS-UCS2-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniKS-UTF16-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniKS-UTF16-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniKS-UTF32-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniKS-UTF32-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniKS-UTF8-H.bcmap +0 -0
- package/src/subagent/fetch/cmaps/UniKS-UTF8-V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/V.bcmap +0 -0
- package/src/subagent/fetch/cmaps/WP-Symbol.bcmap +0 -0
- package/src/subagent/fetch/errors.ts +0 -2
- package/src/subagent/fetch/pdf.ts +74 -72
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
%%Copyright: -----------------------------------------------------------
|
|
2
|
+
%%Copyright: Copyright 1990-2009 Adobe Systems Incorporated.
|
|
3
|
+
%%Copyright: All rights reserved.
|
|
4
|
+
%%Copyright:
|
|
5
|
+
%%Copyright: Redistribution and use in source and binary forms, with or
|
|
6
|
+
%%Copyright: without modification, are permitted provided that the
|
|
7
|
+
%%Copyright: following conditions are met:
|
|
8
|
+
%%Copyright:
|
|
9
|
+
%%Copyright: Redistributions of source code must retain the above
|
|
10
|
+
%%Copyright: copyright notice, this list of conditions and the following
|
|
11
|
+
%%Copyright: disclaimer.
|
|
12
|
+
%%Copyright:
|
|
13
|
+
%%Copyright: Redistributions in binary form must reproduce the above
|
|
14
|
+
%%Copyright: copyright notice, this list of conditions and the following
|
|
15
|
+
%%Copyright: disclaimer in the documentation and/or other materials
|
|
16
|
+
%%Copyright: provided with the distribution.
|
|
17
|
+
%%Copyright:
|
|
18
|
+
%%Copyright: Neither the name of Adobe Systems Incorporated nor the names
|
|
19
|
+
%%Copyright: of its contributors may be used to endorse or promote
|
|
20
|
+
%%Copyright: products derived from this software without specific prior
|
|
21
|
+
%%Copyright: written permission.
|
|
22
|
+
%%Copyright:
|
|
23
|
+
%%Copyright: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
24
|
+
%%Copyright: CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
25
|
+
%%Copyright: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
26
|
+
%%Copyright: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
27
|
+
%%Copyright: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
28
|
+
%%Copyright: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
29
|
+
%%Copyright: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
30
|
+
%%Copyright: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
31
|
+
%%Copyright: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
32
|
+
%%Copyright: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
33
|
+
%%Copyright: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
34
|
+
%%Copyright: OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
35
|
+
%%Copyright: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
36
|
+
%%Copyright: -----------------------------------------------------------
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
enum FetchErrors {
|
|
2
2
|
Failed = 'Fetch failed: ',
|
|
3
3
|
PageTooBig = 'Fetch failed: page too big for the subagent model context window. Tell the user; no query and no retry can read it',
|
|
4
|
-
NoPdfReader = 'Fetch failed: PDF needs pdftotext (poppler) on PATH',
|
|
5
|
-
OldPdfReader = 'Fetch failed: pdftotext is too old, it needs the -remove-hyphens option of a newer poppler',
|
|
6
4
|
NoTextLayer = 'Fetch failed: PDF has no text layer',
|
|
7
5
|
Locked = 'Fetch failed: PDF is password protected'
|
|
8
6
|
}
|
|
@@ -1,90 +1,92 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {fileURLToPath} from 'node:url'
|
|
2
|
+
|
|
3
|
+
import {getResolvedPDFJS} from 'unpdf'
|
|
4
|
+
import type {PDFPageProxy} from 'unpdf/pdfjs'
|
|
2
5
|
|
|
3
6
|
import {normalize} from '../answer'
|
|
4
7
|
import type {Source} from '../answer'
|
|
5
|
-
import {
|
|
6
|
-
import {onPath} from '../platform'
|
|
8
|
+
import {describe} from '../describe'
|
|
7
9
|
import {FetchErrors} from './errors'
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
// pdftotext on Windows ends lines with \r\n, which the split patterns would miss.
|
|
11
|
-
const args = ['-remove-hyphens', 'soft', '-eol', 'unix', '-', '-']
|
|
12
|
-
|
|
13
|
-
const startFailure = (error: Error & {code?: string}): Error =>
|
|
14
|
-
error.code === 'ENOENT' ?
|
|
15
|
-
new Error(FetchErrors.NoPdfReader, {cause: error})
|
|
16
|
-
: new Error(FetchErrors.Failed + error.message, {cause: error})
|
|
17
|
-
|
|
18
|
-
const exitFailure = (
|
|
19
|
-
usage: boolean,
|
|
20
|
-
said: string,
|
|
21
|
-
code: number | null,
|
|
22
|
-
signal: string | null
|
|
23
|
-
): Error => {
|
|
24
|
-
if (usage) return new Error(FetchErrors.OldPdfReader)
|
|
25
|
-
// A crash ends mid-run, so the last line is only a warning.
|
|
26
|
-
if (signal !== null) return new Error(FetchErrors.Failed + `pdftotext stopped on ${signal}`)
|
|
27
|
-
if (said.endsWith('Incorrect password')) return new Error(FetchErrors.Locked)
|
|
28
|
-
return new Error(FetchErrors.Failed + (said || `pdftotext exited with ${code}`))
|
|
29
|
-
}
|
|
11
|
+
type TextContent = Awaited<ReturnType<PDFPageProxy['getTextContent']>>
|
|
30
12
|
|
|
31
|
-
//
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
for (const line of lines.take(text)) {
|
|
45
|
-
usage ||= line.startsWith('Usage:')
|
|
46
|
-
said = line
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
child.stdout.on('data', (chunk: Buffer) => {
|
|
50
|
-
size += chunk.byteLength
|
|
51
|
-
chunks.push(chunk)
|
|
52
|
-
|
|
53
|
-
if (size <= maxBuffer) return
|
|
54
|
-
child.kill()
|
|
55
|
-
reject(new Error(FetchErrors.PageTooBig))
|
|
56
|
-
})
|
|
57
|
-
child.stderr.setEncoding('utf8')
|
|
58
|
-
child.stderr.on('data', hear)
|
|
59
|
-
child.on('error', reject)
|
|
60
|
-
child.on('close', (code, killedBy) => {
|
|
61
|
-
if (code === 0) return resolve(Buffer.concat(chunks).toString())
|
|
62
|
-
hear('\n')
|
|
63
|
-
reject(exitFailure(usage, said, code, killedBy))
|
|
64
|
-
})
|
|
65
|
-
// A child that exits or is aborted before reading its input closes stdin; the EPIPE kills pi.
|
|
66
|
-
child.stdin.on('error', () => undefined)
|
|
67
|
-
child.stdin.end(bytes)
|
|
68
|
-
}).catch((error: Error & {code?: string}) => {
|
|
69
|
-
// Our own failures carry no code; a failed spawn, or onPath finding nothing, does.
|
|
70
|
-
throw error.code === undefined ? error : startFailure(error)
|
|
13
|
+
// pdf.js wants a trailing slash, and on Windows fileURLToPath ends a folder in a backslash.
|
|
14
|
+
const cMapUrl = fileURLToPath(new URL('cmaps', import.meta.url)) + '/'
|
|
15
|
+
// pdf.js runs on microtasks only, so without this a long read freezes pi and an abort never lands.
|
|
16
|
+
const nextTurn = (): Promise<void> => new Promise(resolve => setImmediate(resolve))
|
|
17
|
+
|
|
18
|
+
const readFailure = (error: unknown): Error =>
|
|
19
|
+
error instanceof Error && error.name === 'PasswordException' ?
|
|
20
|
+
new Error(FetchErrors.Locked, {cause: error})
|
|
21
|
+
: new Error(FetchErrors.Failed + describe(error), {cause: error})
|
|
22
|
+
|
|
23
|
+
const fromPdfjs = <T>(work: Promise<T>): Promise<T> =>
|
|
24
|
+
work.catch((error: unknown) => {
|
|
25
|
+
throw readFailure(error)
|
|
71
26
|
})
|
|
72
27
|
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
// Only the Latin ligatures: full NFKC also turns m² into m2.
|
|
76
|
-
const foldLigatures = (text: string): string =>
|
|
77
|
-
text.replace(/[\uFB00-\uFB06]/g, ligature => ligature.normalize('NFKC'))
|
|
28
|
+
const chunkText = ({items}: TextContent): string =>
|
|
29
|
+
items.map(item => ('str' in item ? item.str + (item.hasEOL ? '\n' : '') : '')).join('')
|
|
78
30
|
|
|
79
|
-
const
|
|
80
|
-
|
|
31
|
+
const pageText = async (
|
|
32
|
+
page: PDFPageProxy,
|
|
33
|
+
allowance: number,
|
|
34
|
+
signal?: AbortSignal
|
|
35
|
+
): Promise<string> => {
|
|
36
|
+
const reader = page.streamTextContent().getReader()
|
|
37
|
+
let text = ''
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
for (
|
|
41
|
+
let chunk = await fromPdfjs(reader.read());
|
|
42
|
+
!chunk.done;
|
|
43
|
+
chunk = await fromPdfjs(reader.read())
|
|
44
|
+
) {
|
|
45
|
+
const part = chunkText(chunk.value as TextContent)
|
|
46
|
+
allowance -= Buffer.byteLength(part)
|
|
47
|
+
|
|
48
|
+
if (allowance < 0) throw new Error(FetchErrors.PageTooBig)
|
|
49
|
+
text += part
|
|
50
|
+
await nextTurn()
|
|
51
|
+
signal?.throwIfAborted()
|
|
52
|
+
}
|
|
53
|
+
} finally {
|
|
54
|
+
// With a full stream queue, the document's destroy() never settles.
|
|
55
|
+
await reader.cancel(new Error('PDF read stopped'))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return text
|
|
59
|
+
}
|
|
81
60
|
|
|
82
61
|
const pdfText = async (
|
|
83
62
|
bytes: Uint8Array,
|
|
84
63
|
maxBuffer: number,
|
|
85
64
|
signal?: AbortSignal
|
|
86
65
|
): Promise<string> => {
|
|
87
|
-
|
|
66
|
+
signal?.throwIfAborted()
|
|
67
|
+
const {getDocument} = await getResolvedPDFJS()
|
|
68
|
+
// A copy, since pdf.js refuses a Buffer and detaches the buffer it is given.
|
|
69
|
+
const task = getDocument({data: new Uint8Array(bytes), verbosity: 0, cMapUrl, cMapPacked: true})
|
|
70
|
+
const pages: string[] = []
|
|
71
|
+
let size = 0
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const pdf = await fromPdfjs(task.promise)
|
|
75
|
+
|
|
76
|
+
for (let number = 1; number <= pdf.numPages; number++) {
|
|
77
|
+
const page = await pageText(
|
|
78
|
+
await fromPdfjs(pdf.getPage(number)),
|
|
79
|
+
maxBuffer - size,
|
|
80
|
+
signal
|
|
81
|
+
)
|
|
82
|
+
size += Buffer.byteLength(page)
|
|
83
|
+
pages.push(page)
|
|
84
|
+
}
|
|
85
|
+
} finally {
|
|
86
|
+
await task.destroy()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const text = pages.join('\n\n').trim()
|
|
88
90
|
|
|
89
91
|
if (text === '') throw new Error(FetchErrors.NoTextLayer)
|
|
90
92
|
return text
|