@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.
Files changed (177) hide show
  1. package/README.md +4 -3
  2. package/package.json +3 -2
  3. package/src/config/command.ts +46 -4
  4. package/src/config/config.ts +86 -11
  5. package/src/search/search.ts +17 -7
  6. package/src/subagent/answer.ts +41 -16
  7. package/src/subagent/fetch/cmaps/78-EUC-H.bcmap +0 -0
  8. package/src/subagent/fetch/cmaps/78-EUC-V.bcmap +0 -0
  9. package/src/subagent/fetch/cmaps/78-H.bcmap +0 -0
  10. package/src/subagent/fetch/cmaps/78-RKSJ-H.bcmap +0 -0
  11. package/src/subagent/fetch/cmaps/78-RKSJ-V.bcmap +0 -0
  12. package/src/subagent/fetch/cmaps/78-V.bcmap +0 -0
  13. package/src/subagent/fetch/cmaps/78ms-RKSJ-H.bcmap +0 -0
  14. package/src/subagent/fetch/cmaps/78ms-RKSJ-V.bcmap +0 -0
  15. package/src/subagent/fetch/cmaps/83pv-RKSJ-H.bcmap +0 -0
  16. package/src/subagent/fetch/cmaps/90ms-RKSJ-H.bcmap +0 -0
  17. package/src/subagent/fetch/cmaps/90ms-RKSJ-V.bcmap +0 -0
  18. package/src/subagent/fetch/cmaps/90msp-RKSJ-H.bcmap +0 -0
  19. package/src/subagent/fetch/cmaps/90msp-RKSJ-V.bcmap +0 -0
  20. package/src/subagent/fetch/cmaps/90pv-RKSJ-H.bcmap +0 -0
  21. package/src/subagent/fetch/cmaps/90pv-RKSJ-V.bcmap +0 -0
  22. package/src/subagent/fetch/cmaps/Add-H.bcmap +0 -0
  23. package/src/subagent/fetch/cmaps/Add-RKSJ-H.bcmap +0 -0
  24. package/src/subagent/fetch/cmaps/Add-RKSJ-V.bcmap +0 -0
  25. package/src/subagent/fetch/cmaps/Add-V.bcmap +0 -0
  26. package/src/subagent/fetch/cmaps/Adobe-CNS1-0.bcmap +0 -0
  27. package/src/subagent/fetch/cmaps/Adobe-CNS1-1.bcmap +0 -0
  28. package/src/subagent/fetch/cmaps/Adobe-CNS1-2.bcmap +0 -0
  29. package/src/subagent/fetch/cmaps/Adobe-CNS1-3.bcmap +0 -0
  30. package/src/subagent/fetch/cmaps/Adobe-CNS1-4.bcmap +0 -0
  31. package/src/subagent/fetch/cmaps/Adobe-CNS1-5.bcmap +0 -0
  32. package/src/subagent/fetch/cmaps/Adobe-CNS1-6.bcmap +0 -0
  33. package/src/subagent/fetch/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
  34. package/src/subagent/fetch/cmaps/Adobe-GB1-0.bcmap +0 -0
  35. package/src/subagent/fetch/cmaps/Adobe-GB1-1.bcmap +0 -0
  36. package/src/subagent/fetch/cmaps/Adobe-GB1-2.bcmap +0 -0
  37. package/src/subagent/fetch/cmaps/Adobe-GB1-3.bcmap +0 -0
  38. package/src/subagent/fetch/cmaps/Adobe-GB1-4.bcmap +0 -0
  39. package/src/subagent/fetch/cmaps/Adobe-GB1-5.bcmap +0 -0
  40. package/src/subagent/fetch/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
  41. package/src/subagent/fetch/cmaps/Adobe-Japan1-0.bcmap +0 -0
  42. package/src/subagent/fetch/cmaps/Adobe-Japan1-1.bcmap +0 -0
  43. package/src/subagent/fetch/cmaps/Adobe-Japan1-2.bcmap +0 -0
  44. package/src/subagent/fetch/cmaps/Adobe-Japan1-3.bcmap +0 -0
  45. package/src/subagent/fetch/cmaps/Adobe-Japan1-4.bcmap +0 -0
  46. package/src/subagent/fetch/cmaps/Adobe-Japan1-5.bcmap +0 -0
  47. package/src/subagent/fetch/cmaps/Adobe-Japan1-6.bcmap +0 -0
  48. package/src/subagent/fetch/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
  49. package/src/subagent/fetch/cmaps/Adobe-Korea1-0.bcmap +0 -0
  50. package/src/subagent/fetch/cmaps/Adobe-Korea1-1.bcmap +0 -0
  51. package/src/subagent/fetch/cmaps/Adobe-Korea1-2.bcmap +0 -0
  52. package/src/subagent/fetch/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
  53. package/src/subagent/fetch/cmaps/B5-H.bcmap +0 -0
  54. package/src/subagent/fetch/cmaps/B5-V.bcmap +0 -0
  55. package/src/subagent/fetch/cmaps/B5pc-H.bcmap +0 -0
  56. package/src/subagent/fetch/cmaps/B5pc-V.bcmap +0 -0
  57. package/src/subagent/fetch/cmaps/CNS-EUC-H.bcmap +0 -0
  58. package/src/subagent/fetch/cmaps/CNS-EUC-V.bcmap +0 -0
  59. package/src/subagent/fetch/cmaps/CNS1-H.bcmap +0 -0
  60. package/src/subagent/fetch/cmaps/CNS1-V.bcmap +0 -0
  61. package/src/subagent/fetch/cmaps/CNS2-H.bcmap +0 -0
  62. package/src/subagent/fetch/cmaps/CNS2-V.bcmap +3 -0
  63. package/src/subagent/fetch/cmaps/ETHK-B5-H.bcmap +0 -0
  64. package/src/subagent/fetch/cmaps/ETHK-B5-V.bcmap +0 -0
  65. package/src/subagent/fetch/cmaps/ETen-B5-H.bcmap +0 -0
  66. package/src/subagent/fetch/cmaps/ETen-B5-V.bcmap +0 -0
  67. package/src/subagent/fetch/cmaps/ETenms-B5-H.bcmap +3 -0
  68. package/src/subagent/fetch/cmaps/ETenms-B5-V.bcmap +0 -0
  69. package/src/subagent/fetch/cmaps/EUC-H.bcmap +0 -0
  70. package/src/subagent/fetch/cmaps/EUC-V.bcmap +0 -0
  71. package/src/subagent/fetch/cmaps/Ext-H.bcmap +0 -0
  72. package/src/subagent/fetch/cmaps/Ext-RKSJ-H.bcmap +0 -0
  73. package/src/subagent/fetch/cmaps/Ext-RKSJ-V.bcmap +0 -0
  74. package/src/subagent/fetch/cmaps/Ext-V.bcmap +0 -0
  75. package/src/subagent/fetch/cmaps/GB-EUC-H.bcmap +0 -0
  76. package/src/subagent/fetch/cmaps/GB-EUC-V.bcmap +0 -0
  77. package/src/subagent/fetch/cmaps/GB-H.bcmap +6 -0
  78. package/src/subagent/fetch/cmaps/GB-V.bcmap +0 -0
  79. package/src/subagent/fetch/cmaps/GBK-EUC-H.bcmap +0 -0
  80. package/src/subagent/fetch/cmaps/GBK-EUC-V.bcmap +0 -0
  81. package/src/subagent/fetch/cmaps/GBK2K-H.bcmap +0 -0
  82. package/src/subagent/fetch/cmaps/GBK2K-V.bcmap +0 -0
  83. package/src/subagent/fetch/cmaps/GBKp-EUC-H.bcmap +0 -0
  84. package/src/subagent/fetch/cmaps/GBKp-EUC-V.bcmap +0 -0
  85. package/src/subagent/fetch/cmaps/GBT-EUC-H.bcmap +0 -0
  86. package/src/subagent/fetch/cmaps/GBT-EUC-V.bcmap +0 -0
  87. package/src/subagent/fetch/cmaps/GBT-H.bcmap +0 -0
  88. package/src/subagent/fetch/cmaps/GBT-V.bcmap +0 -0
  89. package/src/subagent/fetch/cmaps/GBTpc-EUC-H.bcmap +0 -0
  90. package/src/subagent/fetch/cmaps/GBTpc-EUC-V.bcmap +0 -0
  91. package/src/subagent/fetch/cmaps/GBpc-EUC-H.bcmap +0 -0
  92. package/src/subagent/fetch/cmaps/GBpc-EUC-V.bcmap +0 -0
  93. package/src/subagent/fetch/cmaps/H.bcmap +0 -0
  94. package/src/subagent/fetch/cmaps/HKdla-B5-H.bcmap +0 -0
  95. package/src/subagent/fetch/cmaps/HKdla-B5-V.bcmap +0 -0
  96. package/src/subagent/fetch/cmaps/HKdlb-B5-H.bcmap +0 -0
  97. package/src/subagent/fetch/cmaps/HKdlb-B5-V.bcmap +0 -0
  98. package/src/subagent/fetch/cmaps/HKgccs-B5-H.bcmap +0 -0
  99. package/src/subagent/fetch/cmaps/HKgccs-B5-V.bcmap +0 -0
  100. package/src/subagent/fetch/cmaps/HKm314-B5-H.bcmap +0 -0
  101. package/src/subagent/fetch/cmaps/HKm314-B5-V.bcmap +0 -0
  102. package/src/subagent/fetch/cmaps/HKm471-B5-H.bcmap +0 -0
  103. package/src/subagent/fetch/cmaps/HKm471-B5-V.bcmap +0 -0
  104. package/src/subagent/fetch/cmaps/HKscs-B5-H.bcmap +0 -0
  105. package/src/subagent/fetch/cmaps/HKscs-B5-V.bcmap +0 -0
  106. package/src/subagent/fetch/cmaps/Hankaku.bcmap +0 -0
  107. package/src/subagent/fetch/cmaps/Hiragana.bcmap +0 -0
  108. package/src/subagent/fetch/cmaps/KSC-EUC-H.bcmap +0 -0
  109. package/src/subagent/fetch/cmaps/KSC-EUC-V.bcmap +0 -0
  110. package/src/subagent/fetch/cmaps/KSC-H.bcmap +0 -0
  111. package/src/subagent/fetch/cmaps/KSC-Johab-H.bcmap +0 -0
  112. package/src/subagent/fetch/cmaps/KSC-Johab-V.bcmap +0 -0
  113. package/src/subagent/fetch/cmaps/KSC-V.bcmap +0 -0
  114. package/src/subagent/fetch/cmaps/KSCms-UHC-H.bcmap +0 -0
  115. package/src/subagent/fetch/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
  116. package/src/subagent/fetch/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
  117. package/src/subagent/fetch/cmaps/KSCms-UHC-V.bcmap +0 -0
  118. package/src/subagent/fetch/cmaps/KSCpc-EUC-H.bcmap +0 -0
  119. package/src/subagent/fetch/cmaps/KSCpc-EUC-V.bcmap +0 -0
  120. package/src/subagent/fetch/cmaps/Katakana.bcmap +0 -0
  121. package/src/subagent/fetch/cmaps/LICENSE +36 -0
  122. package/src/subagent/fetch/cmaps/NWP-H.bcmap +0 -0
  123. package/src/subagent/fetch/cmaps/NWP-V.bcmap +0 -0
  124. package/src/subagent/fetch/cmaps/RKSJ-H.bcmap +0 -0
  125. package/src/subagent/fetch/cmaps/RKSJ-V.bcmap +0 -0
  126. package/src/subagent/fetch/cmaps/Roman.bcmap +0 -0
  127. package/src/subagent/fetch/cmaps/UniCNS-UCS2-H.bcmap +0 -0
  128. package/src/subagent/fetch/cmaps/UniCNS-UCS2-V.bcmap +0 -0
  129. package/src/subagent/fetch/cmaps/UniCNS-UTF16-H.bcmap +0 -0
  130. package/src/subagent/fetch/cmaps/UniCNS-UTF16-V.bcmap +0 -0
  131. package/src/subagent/fetch/cmaps/UniCNS-UTF32-H.bcmap +0 -0
  132. package/src/subagent/fetch/cmaps/UniCNS-UTF32-V.bcmap +0 -0
  133. package/src/subagent/fetch/cmaps/UniCNS-UTF8-H.bcmap +0 -0
  134. package/src/subagent/fetch/cmaps/UniCNS-UTF8-V.bcmap +0 -0
  135. package/src/subagent/fetch/cmaps/UniGB-UCS2-H.bcmap +0 -0
  136. package/src/subagent/fetch/cmaps/UniGB-UCS2-V.bcmap +0 -0
  137. package/src/subagent/fetch/cmaps/UniGB-UTF16-H.bcmap +0 -0
  138. package/src/subagent/fetch/cmaps/UniGB-UTF16-V.bcmap +0 -0
  139. package/src/subagent/fetch/cmaps/UniGB-UTF32-H.bcmap +0 -0
  140. package/src/subagent/fetch/cmaps/UniGB-UTF32-V.bcmap +0 -0
  141. package/src/subagent/fetch/cmaps/UniGB-UTF8-H.bcmap +0 -0
  142. package/src/subagent/fetch/cmaps/UniGB-UTF8-V.bcmap +0 -0
  143. package/src/subagent/fetch/cmaps/UniJIS-UCS2-H.bcmap +0 -0
  144. package/src/subagent/fetch/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
  145. package/src/subagent/fetch/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
  146. package/src/subagent/fetch/cmaps/UniJIS-UCS2-V.bcmap +0 -0
  147. package/src/subagent/fetch/cmaps/UniJIS-UTF16-H.bcmap +0 -0
  148. package/src/subagent/fetch/cmaps/UniJIS-UTF16-V.bcmap +0 -0
  149. package/src/subagent/fetch/cmaps/UniJIS-UTF32-H.bcmap +0 -0
  150. package/src/subagent/fetch/cmaps/UniJIS-UTF32-V.bcmap +0 -0
  151. package/src/subagent/fetch/cmaps/UniJIS-UTF8-H.bcmap +0 -0
  152. package/src/subagent/fetch/cmaps/UniJIS-UTF8-V.bcmap +0 -0
  153. package/src/subagent/fetch/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
  154. package/src/subagent/fetch/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
  155. package/src/subagent/fetch/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
  156. package/src/subagent/fetch/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
  157. package/src/subagent/fetch/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
  158. package/src/subagent/fetch/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
  159. package/src/subagent/fetch/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
  160. package/src/subagent/fetch/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
  161. package/src/subagent/fetch/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
  162. package/src/subagent/fetch/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
  163. package/src/subagent/fetch/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
  164. package/src/subagent/fetch/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
  165. package/src/subagent/fetch/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
  166. package/src/subagent/fetch/cmaps/UniKS-UCS2-H.bcmap +0 -0
  167. package/src/subagent/fetch/cmaps/UniKS-UCS2-V.bcmap +0 -0
  168. package/src/subagent/fetch/cmaps/UniKS-UTF16-H.bcmap +0 -0
  169. package/src/subagent/fetch/cmaps/UniKS-UTF16-V.bcmap +0 -0
  170. package/src/subagent/fetch/cmaps/UniKS-UTF32-H.bcmap +0 -0
  171. package/src/subagent/fetch/cmaps/UniKS-UTF32-V.bcmap +0 -0
  172. package/src/subagent/fetch/cmaps/UniKS-UTF8-H.bcmap +0 -0
  173. package/src/subagent/fetch/cmaps/UniKS-UTF8-V.bcmap +0 -0
  174. package/src/subagent/fetch/cmaps/V.bcmap +0 -0
  175. package/src/subagent/fetch/cmaps/WP-Symbol.bcmap +0 -0
  176. package/src/subagent/fetch/errors.ts +0 -2
  177. package/src/subagent/fetch/pdf.ts +74 -72
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
@@ -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 {spawn} from 'node:child_process'
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 {createLineReader} from '../guards/lines'
6
- import {onPath} from '../platform'
8
+ import {describe} from '../describe'
7
9
  import {FetchErrors} from './errors'
8
10
 
9
- // `soft` keeps a hyphen that ends a line: the default joins `VX-` / `4471` into `VX4471`.
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
- // Not execFile: its maxBuffer caps stderr too, and a damaged PDF can warn past the page cap.
32
- const convert = (bytes: Uint8Array, maxBuffer: number, signal?: AbortSignal): Promise<string> =>
33
- new Promise<string>((resolve, reject) => {
34
- // A plain spawn runs only an .exe or .com; a .cmd needs cmd.exe.
35
- const child = spawn(onPath('pdftotext', '.EXE;.COM'), args, {signal})
36
- const chunks: Buffer[] = []
37
- const lines = createLineReader()
38
- let size = 0
39
- let said = ''
40
- // Usage text answers a bad command line. With these fixed options only an old pdftotext prints it.
41
- let usage = false
42
-
43
- const hear = (text: string) => {
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 pageBreak = '\f'
74
- const softHyphen = '\u00AD'
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 clean = (text: string): string =>
80
- foldLigatures(text.replaceAll(pageBreak, '').replaceAll(softHyphen, '')).trim()
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
- const text = clean(await convert(bytes, maxBuffer, signal))
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