imprimatur 0.8.0 → 0.9.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -10
- data/Cargo.lock +22 -5
- data/Cargo.toml +3 -2
- data/README.md +22 -0
- data/ext/imprimatur_native/cli/Cargo.toml +1 -1
- data/ext/imprimatur_native/core/Cargo.toml +1 -1
- data/ext/imprimatur_native/core/src/read/budget.rs +6 -0
- data/ext/imprimatur_native/core/src/read/markdown.rs +91 -5
- data/ext/imprimatur_native/core/src/read/pdf/isolation/protocol.rs +3 -1
- data/ext/imprimatur_native/core/src/read/pdf/isolation/recovery.rs +180 -0
- data/ext/imprimatur_native/core/src/read/pdf/isolation/stream.rs +17 -6
- data/ext/imprimatur_native/core/src/read/pdf/isolation.rs +10 -0
- data/ext/imprimatur_native/core/src/read/pdf/layout/blocks.rs +259 -0
- data/ext/imprimatur_native/core/src/read/pdf/layout.rs +2 -257
- data/ext/imprimatur_native/core/src/read.rs +12 -0
- data/ext/imprimatur_native/ffi/Cargo.toml +2 -1
- data/ext/imprimatur_native/ffi/src/barcode.rs +1 -1
- data/ext/imprimatur_native/ffi/src/classes.rs +52 -0
- data/ext/imprimatur_native/ffi/src/convert.rs +5 -20
- data/ext/imprimatur_native/ffi/src/document.rs +1 -1
- data/ext/imprimatur_native/ffi/src/lib.rs +26 -23
- data/ext/imprimatur_native/ffi/src/pdf.rs +48 -24
- data/ext/imprimatur_native/ffi/src/read.rs +2 -2
- data/lib/imprimatur/pdf_reader.rb +3 -2
- data/lib/imprimatur/version.rb +1 -1
- data/lib/imprimatur.rb +2 -0
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4efc9644ebf7bdc67f504b7b3c9f4ea046f3ac772b88656151f6a2179bcc4a56
|
|
4
|
+
data.tar.gz: e8096235512724a060c501c8c8019cc6dc256f53eee585aafd66435b10c48325
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2f374493b4da1a649b8bee42d6052733b62ff486e96eeaccbf9b85cc9e5a98cdfbc5e10b465807704a1775cd917887e93346333aa2cd9ffe7f94f419cccee0cd
|
|
7
|
+
data.tar.gz: a7731b05b0da31c47acdc2754bd8760d6f65547d918dd2e2d217be712eb8b4d3c09d2611c5d939761d912d29d9bdcfd73f91dae96dfbaba3ce02400845b75570
|
data/CHANGELOG.md
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
##
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
## [0.9.1](https://github.com/seuros/imprimatur/compare/v0.9.0...v0.9.1) (2026-11-19)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* **pdf:** refuse a recovery step after the pages were returned ([6723e3e](https://github.com/seuros/imprimatur/commit/6723e3ecd0009dc10b4136cfcb15b7bdd65022cb))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Performance Improvements
|
|
12
|
+
|
|
13
|
+
* **ruby:** resolve the binding's classes once at load ([6271c0e](https://github.com/seuros/imprimatur/commit/6271c0e5ef5d39dcc1047e818d4c6cccd7d3c2b4))
|
|
14
|
+
|
|
15
|
+
## [0.9.0](https://github.com/seuros/imprimatur/compare/v0.8.1...v0.9.0) (2026-11-18)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* **read:** to_markdown_outline reports each page's and sheet's lines ([#18](https://github.com/seuros/imprimatur/issues/18)) ([f0b262a](https://github.com/seuros/imprimatur/commit/f0b262acde3f248ca0b10c11ba85e6e1f39a6a3a))
|
|
21
|
+
|
|
22
|
+
## [0.8.1](https://github.com/seuros/imprimatur/compare/v0.8.0...v0.8.1) (2026-10-29)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Bug Fixes
|
|
26
|
+
|
|
27
|
+
* **pdf:** recover timeout neighbours within the overall deadline ([#16](https://github.com/seuros/imprimatur/issues/16)) ([e86fbbf](https://github.com/seuros/imprimatur/commit/e86fbbfef85f2c645090c43359c0744962e951a1))
|
|
28
|
+
* **pdf:** step timeout recovery so a Ruby wakeup cannot restart it ([75c3894](https://github.com/seuros/imprimatur/commit/75c3894c8a791c5ef4af562dc99de4e745770ad3))
|
|
13
29
|
|
|
14
30
|
## [0.8.0](https://github.com/seuros/imprimatur/compare/v0.7.0...v0.8.0) (2026-10-27)
|
|
15
31
|
|
data/Cargo.lock
CHANGED
|
@@ -135,6 +135,12 @@ version = "0.1.0"
|
|
|
135
135
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
136
136
|
checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
|
|
137
137
|
|
|
138
|
+
[[package]]
|
|
139
|
+
name = "bytes"
|
|
140
|
+
version = "1.12.2"
|
|
141
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
142
|
+
checksum = "776dfebd015e2d8732c684a2c71003674ccb49457b539b9e82151ba38e96dd7c"
|
|
143
|
+
|
|
138
144
|
[[package]]
|
|
139
145
|
name = "cexpr"
|
|
140
146
|
version = "0.6.0"
|
|
@@ -513,7 +519,7 @@ dependencies = [
|
|
|
513
519
|
|
|
514
520
|
[[package]]
|
|
515
521
|
name = "imprimatur"
|
|
516
|
-
version = "0.
|
|
522
|
+
version = "0.9.1"
|
|
517
523
|
dependencies = [
|
|
518
524
|
"aes",
|
|
519
525
|
"crc32fast",
|
|
@@ -536,7 +542,7 @@ dependencies = [
|
|
|
536
542
|
|
|
537
543
|
[[package]]
|
|
538
544
|
name = "imprimatur-cli"
|
|
539
|
-
version = "0.
|
|
545
|
+
version = "0.9.1"
|
|
540
546
|
dependencies = [
|
|
541
547
|
"imprimatur",
|
|
542
548
|
"regex",
|
|
@@ -548,10 +554,11 @@ dependencies = [
|
|
|
548
554
|
|
|
549
555
|
[[package]]
|
|
550
556
|
name = "imprimatur_native"
|
|
551
|
-
version = "0.
|
|
557
|
+
version = "0.9.1"
|
|
552
558
|
dependencies = [
|
|
553
559
|
"imprimatur",
|
|
554
560
|
"magnus",
|
|
561
|
+
"matryona-machines",
|
|
555
562
|
"rb-sys",
|
|
556
563
|
"serde_json",
|
|
557
564
|
]
|
|
@@ -666,10 +673,11 @@ checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6"
|
|
|
666
673
|
|
|
667
674
|
[[package]]
|
|
668
675
|
name = "magnus"
|
|
669
|
-
version = "0.9.
|
|
676
|
+
version = "0.9.4"
|
|
670
677
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
671
|
-
checksum = "
|
|
678
|
+
checksum = "3c5f2dd2017d172e2fde9969a35f10f1374a4dd79b37a17f1847f83201883de7"
|
|
672
679
|
dependencies = [
|
|
680
|
+
"bytes",
|
|
673
681
|
"magnus-macros",
|
|
674
682
|
"rb-sys",
|
|
675
683
|
"rb-sys-env",
|
|
@@ -687,6 +695,15 @@ dependencies = [
|
|
|
687
695
|
"syn 2.0.119",
|
|
688
696
|
]
|
|
689
697
|
|
|
698
|
+
[[package]]
|
|
699
|
+
name = "matryona-machines"
|
|
700
|
+
version = "0.6.0"
|
|
701
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
702
|
+
checksum = "cf8a06ab02efd767270815a1f515a40bbf3bfc77038e9360676ebe8d47833b07"
|
|
703
|
+
dependencies = [
|
|
704
|
+
"magnus",
|
|
705
|
+
]
|
|
706
|
+
|
|
690
707
|
[[package]]
|
|
691
708
|
name = "md-5"
|
|
692
709
|
version = "0.11.0"
|
data/Cargo.toml
CHANGED
|
@@ -15,9 +15,10 @@ crc32fast = "1.5.2"
|
|
|
15
15
|
datamatrix = "0.3.3"
|
|
16
16
|
font-test-data = "0.10.0"
|
|
17
17
|
hayro = { version = "0.8.0", default-features = false, features = ["embed-fonts", "embed-cmaps"] }
|
|
18
|
-
imprimatur = { path = "ext/imprimatur_native/core", version = "0.
|
|
18
|
+
imprimatur = { path = "ext/imprimatur_native/core", version = "0.9.1" }
|
|
19
19
|
libproc = "0.14.11"
|
|
20
|
-
magnus = { version = "0.9.
|
|
20
|
+
magnus = { version = "0.9.3", default-features = false }
|
|
21
|
+
matryona-machines = { version = ">=0.6", default-features = false }
|
|
21
22
|
md-5 = "0.11.0"
|
|
22
23
|
miniz_oxide = "0.9.1"
|
|
23
24
|
nix = { version = "0.31.3", features = ["resource", "process", "signal"] }
|
data/README.md
CHANGED
|
@@ -384,6 +384,28 @@ after metadata was read preserves completed pages and explicitly marks the
|
|
|
384
384
|
unreached pages; an expired overall deadline marks them `skipped`. Cancellation
|
|
385
385
|
still raises rather than returning a partial successful call.
|
|
386
386
|
|
|
387
|
+
For timeout-neighbour recovery, opt in to a shorter worker-attempt budget:
|
|
388
|
+
|
|
389
|
+
```ruby
|
|
390
|
+
Imprimatur.pdf_pages(bytes, timeout: 300, batch_timeout: 60)
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
Here `timeout:` is the **overall** deadline, including recovery; `batch_timeout:`
|
|
394
|
+
caps the initial worker and each singleton retry. Completed pages survive a
|
|
395
|
+
batch timeout. Unread later neighbours are tried once before the first missing
|
|
396
|
+
page (which may have stalled the worker). Recovery is best effort, never an
|
|
397
|
+
extension of the overall deadline. Output-limit skips are not retried, and all
|
|
398
|
+
attempts share `max_output_bytes:`. Input, depth, process-memory and per-page
|
|
399
|
+
limits still apply; `max_inflated:` remains per worker/recovery attempt, not a
|
|
400
|
+
cumulative decompression allowance.
|
|
401
|
+
|
|
402
|
+
With `batch_timeout:` enabled, an explicit finite `timeout:` replaces the
|
|
403
|
+
ordinary 60-second process ceiling for this call; `timeout: nil` retains that
|
|
404
|
+
60-second ceiling. Both budgets must be positive; zero overall time still fails
|
|
405
|
+
immediately. Omitting `batch_timeout:` preserves the existing behaviour. Rust
|
|
406
|
+
callers can use `pages_isolated_recovering` with an explicit attempt duration
|
|
407
|
+
and overall `Limits`/`ProcessLimits`.
|
|
408
|
+
|
|
387
409
|
This is resource containment, **not a filesystem/network security sandbox**.
|
|
388
410
|
The raw crate `info`, `pages` and `render_page` APIs remain in-process;
|
|
389
411
|
`render_page` cannot interrupt Hayro.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "imprimatur-cli"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.9.1"
|
|
4
4
|
description = "Read PDF, XLSX, XLS, ODS and DOCX documents as Markdown or JSON, search them, extract their images."
|
|
5
5
|
keywords = ["pdf", "xlsx", "docx", "markdown", "cli"]
|
|
6
6
|
categories = ["command-line-utilities", "text-processing"]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "imprimatur"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.9.1"
|
|
4
4
|
description = "XLSX, PDF and DOCX authoring, and PDF, XLSX, XLS, ODS and DOCX reading, with its own XML, zip and PDF code."
|
|
5
5
|
keywords = ["xlsx", "excel", "pdf", "docx", "spreadsheet"]
|
|
6
6
|
categories = ["encoding", "text-processing"]
|
|
@@ -45,6 +45,12 @@ impl Default for Limits {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
impl Limits {
|
|
48
|
+
/// The configured overall read timeout, if enabled.
|
|
49
|
+
#[must_use]
|
|
50
|
+
pub const fn timeout_duration(&self) -> Option<Duration> {
|
|
51
|
+
self.timeout
|
|
52
|
+
}
|
|
53
|
+
|
|
48
54
|
/// The largest document accepted, in bytes; 512 MiB by default.
|
|
49
55
|
#[must_use]
|
|
50
56
|
pub const fn max_input(mut self, bytes: usize) -> Self {
|
|
@@ -8,29 +8,94 @@
|
|
|
8
8
|
mod tests;
|
|
9
9
|
|
|
10
10
|
use std::fmt::Write as _;
|
|
11
|
+
use std::ops::RangeInclusive;
|
|
11
12
|
|
|
12
13
|
use super::content::{Block, Cell, Content, Grid, Page, Sheet, Text, push_reference};
|
|
13
14
|
|
|
14
15
|
/// The deepest Markdown heading.
|
|
15
16
|
const MAX_HEADING: u8 = 6;
|
|
16
17
|
|
|
18
|
+
/// A page or sheet of [`Content::to_markdown_outline`]'s Markdown, by line:
|
|
19
|
+
/// 1-based and inclusive. A page that writes nothing (the first, left blank)
|
|
20
|
+
/// has an empty range.
|
|
21
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
22
|
+
pub struct Section {
|
|
23
|
+
pub kind: SectionKind,
|
|
24
|
+
pub lines: RangeInclusive<usize>,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
28
|
+
pub enum SectionKind {
|
|
29
|
+
Page { number: u32, label: Option<String> },
|
|
30
|
+
Sheet { name: String, hidden: bool, table: Option<TableLines> },
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/// Where a sheet's table sits. Every row is one line, so data row `n` is
|
|
34
|
+
/// line `rows.start() + n - 1`; `header` is the header row and the
|
|
35
|
+
/// separator under it, what a slice of rows needs to stay a table.
|
|
36
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
37
|
+
pub struct TableLines {
|
|
38
|
+
pub header: RangeInclusive<usize>,
|
|
39
|
+
/// Empty when the table is a header alone.
|
|
40
|
+
pub rows: RangeInclusive<usize>,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
impl TableLines {
|
|
44
|
+
/// Data rows below the header.
|
|
45
|
+
#[must_use]
|
|
46
|
+
pub fn row_count(&self) -> usize {
|
|
47
|
+
self.rows.clone().count()
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
17
51
|
impl Content {
|
|
18
52
|
/// The content as Markdown: a PDF's pages (from the second, marked
|
|
19
53
|
/// `<!-- page N -->`), each sheet of a workbook under its name, a Word
|
|
20
54
|
/// document's blocks.
|
|
21
55
|
#[must_use]
|
|
22
56
|
pub fn to_markdown(&self) -> String {
|
|
57
|
+
self.to_markdown_outline().0
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// [`Content::to_markdown`], and where each page or sheet landed in it,
|
|
61
|
+
/// so a caller can hand out part of a long document without parsing the
|
|
62
|
+
/// Markdown back. A Word document has no sections.
|
|
63
|
+
#[must_use]
|
|
64
|
+
pub fn to_markdown_outline(&self) -> (String, Vec<Section>) {
|
|
23
65
|
let mut out = String::new();
|
|
66
|
+
let mut lines = Lines::default();
|
|
67
|
+
let mut sections = Vec::new();
|
|
24
68
|
match self {
|
|
25
|
-
Self::Pdf(pages) => pdf(&mut out, pages),
|
|
69
|
+
Self::Pdf(pages) => pdf(&mut out, pages, &mut lines, &mut sections),
|
|
26
70
|
Self::Xlsx(sheets) | Self::Ods(sheets) | Self::Xls(sheets) => {
|
|
27
71
|
for sheet in sheets {
|
|
28
|
-
write_sheet(&mut out, sheet);
|
|
72
|
+
sections.push(write_sheet(&mut out, sheet, &mut lines));
|
|
29
73
|
}
|
|
30
74
|
}
|
|
31
75
|
Self::Docx(blocks) => write_blocks(&mut out, blocks.iter(), Mode::Body),
|
|
32
76
|
}
|
|
33
|
-
out
|
|
77
|
+
(out, sections)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// Counts lines of the output as it grows, each byte once.
|
|
82
|
+
#[derive(Default)]
|
|
83
|
+
struct Lines {
|
|
84
|
+
counted: usize,
|
|
85
|
+
newlines: usize,
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
impl Lines {
|
|
89
|
+
/// The line the next write to `out` starts on.
|
|
90
|
+
fn next(&mut self, out: &str) -> usize {
|
|
91
|
+
self.newlines += out[self.counted..].matches('\n').count();
|
|
92
|
+
self.counted = out.len();
|
|
93
|
+
self.newlines + 1
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/// The last line written, when `out` ends with its newline.
|
|
97
|
+
fn last(&mut self, out: &str) -> usize {
|
|
98
|
+
self.next(out) - 1
|
|
34
99
|
}
|
|
35
100
|
}
|
|
36
101
|
|
|
@@ -43,13 +108,16 @@ pub enum Mode {
|
|
|
43
108
|
Cell,
|
|
44
109
|
}
|
|
45
110
|
|
|
46
|
-
fn pdf(out: &mut String, pages: &[Page]) {
|
|
111
|
+
fn pdf(out: &mut String, pages: &[Page], lines: &mut Lines, sections: &mut Vec<Section>) {
|
|
47
112
|
// Every page past the first gets its marker, blocks or none: an empty
|
|
48
113
|
// page (left bare by `--drop-repeated`, or genuinely blank) still needs
|
|
49
114
|
// one, so the markers stay a reliable page count and index.
|
|
50
115
|
for page in pages {
|
|
51
116
|
if page.number > 1 {
|
|
52
117
|
end_block(out);
|
|
118
|
+
}
|
|
119
|
+
let first = lines.next(out);
|
|
120
|
+
if page.number > 1 {
|
|
53
121
|
// Writing to a String cannot fail.
|
|
54
122
|
match &page.label {
|
|
55
123
|
Some(label) => {
|
|
@@ -61,21 +129,35 @@ fn pdf(out: &mut String, pages: &[Page]) {
|
|
|
61
129
|
}
|
|
62
130
|
}
|
|
63
131
|
write_blocks(out, page.blocks.iter().map(|placed| &placed.block), Mode::Body);
|
|
132
|
+
sections.push(Section {
|
|
133
|
+
kind: SectionKind::Page { number: page.number, label: page.label.clone() },
|
|
134
|
+
lines: first..=lines.last(out),
|
|
135
|
+
});
|
|
64
136
|
}
|
|
65
137
|
}
|
|
66
138
|
|
|
67
|
-
fn write_sheet(out: &mut String, sheet: &Sheet) {
|
|
139
|
+
fn write_sheet(out: &mut String, sheet: &Sheet, lines: &mut Lines) -> Section {
|
|
68
140
|
end_block(out);
|
|
141
|
+
let first = lines.next(out);
|
|
69
142
|
out.push_str("## ");
|
|
70
143
|
out.push_str(&sheet.name);
|
|
71
144
|
if sheet.hidden {
|
|
72
145
|
out.push_str(" (hidden)");
|
|
73
146
|
}
|
|
74
147
|
out.push_str("\n\n");
|
|
148
|
+
let mut table = None;
|
|
75
149
|
if sheet.cells.is_empty() {
|
|
76
150
|
out.push_str("_Empty._\n");
|
|
77
151
|
} else {
|
|
152
|
+
let before = out.len();
|
|
153
|
+
let header = lines.next(out);
|
|
78
154
|
write_table(out, &sheet.cells);
|
|
155
|
+
if out.len() > before {
|
|
156
|
+
table = Some(TableLines {
|
|
157
|
+
header: header..=header + 1,
|
|
158
|
+
rows: header + 2..=lines.last(out),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
79
161
|
}
|
|
80
162
|
for (index, note) in sheet.comments.iter().enumerate() {
|
|
81
163
|
if index == 0 {
|
|
@@ -98,6 +180,10 @@ fn write_sheet(out: &mut String, sheet: &Sheet) {
|
|
|
98
180
|
}
|
|
99
181
|
out.push('\n');
|
|
100
182
|
}
|
|
183
|
+
Section {
|
|
184
|
+
kind: SectionKind::Sheet { name: sheet.name.clone(), hidden: sheet.hidden, table },
|
|
185
|
+
lines: first..=lines.last(out),
|
|
186
|
+
}
|
|
101
187
|
}
|
|
102
188
|
|
|
103
189
|
/// Blocks in order: consecutive list items form one list; everything else
|
|
@@ -60,7 +60,7 @@ impl Request {
|
|
|
60
60
|
if process.memory_bytes == 0 || process.memory_bytes >= i64::MAX as u64 {
|
|
61
61
|
return Err(Error::InvalidPdfOption("memory limit must be positive and finite"));
|
|
62
62
|
}
|
|
63
|
-
let wall =
|
|
63
|
+
let wall = process.wall_time_for(limits);
|
|
64
64
|
let cpu_seconds = wall
|
|
65
65
|
.as_secs()
|
|
66
66
|
.checked_add(u64::from(wall.subsec_nanos() != 0))
|
|
@@ -165,6 +165,7 @@ pub(super) fn failure(error: &Error) -> (u8, String) {
|
|
|
165
165
|
let kind = match error {
|
|
166
166
|
Error::PasswordRequired => 1,
|
|
167
167
|
Error::UnsupportedDocument(_) => 2,
|
|
168
|
+
Error::LimitExceeded("time") => 6,
|
|
168
169
|
Error::LimitExceeded(_) | Error::Cancelled => 3,
|
|
169
170
|
Error::InvalidPdfOption(_) => 4,
|
|
170
171
|
_ => 5,
|
|
@@ -184,6 +185,7 @@ pub(super) fn error(kind: u8, message: String) -> Error {
|
|
|
184
185
|
2 => Error::UnsupportedDocument(message),
|
|
185
186
|
3 => Error::LimitExceeded("PDF worker limit"),
|
|
186
187
|
4 => Error::InvalidPdfOption("the PDF worker rejected the options"),
|
|
188
|
+
6 => Error::LimitExceeded("time"),
|
|
187
189
|
_ => Error::malformed("PDF worker", message),
|
|
188
190
|
}
|
|
189
191
|
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
//! Optional timeout-neighbour recovery under one overall supervisor deadline.
|
|
2
|
+
|
|
3
|
+
use std::iter;
|
|
4
|
+
use std::mem;
|
|
5
|
+
use std::path::Path;
|
|
6
|
+
use std::time::{Duration, Instant};
|
|
7
|
+
|
|
8
|
+
use super::{ProcessLimits, invoke, protocol, stream};
|
|
9
|
+
use crate::read::Limits;
|
|
10
|
+
use crate::read::pdf::{Pages, ReadOptions, Status, bounded};
|
|
11
|
+
use crate::{Error, Result};
|
|
12
|
+
|
|
13
|
+
#[cfg(test)]
|
|
14
|
+
mod tests;
|
|
15
|
+
|
|
16
|
+
/// Reads with a shorter worker-attempt timeout, recovering unread neighbours.
|
|
17
|
+
///
|
|
18
|
+
/// `limits.timeout` and `process.wall_time` bound the entire operation, not
|
|
19
|
+
/// each attempt. Completed records survive; timeout-unread pages are tried
|
|
20
|
+
/// once individually, later neighbours before the first missing page.
|
|
21
|
+
/// All workers retain the input, decompression, depth and process-memory caps.
|
|
22
|
+
/// Decompression is bounded per worker, as in ordinary page-local recovery;
|
|
23
|
+
/// retained text shares `options.max_output_bytes` across all attempts.
|
|
24
|
+
///
|
|
25
|
+
/// # Errors
|
|
26
|
+
/// As [`super::pages_isolated`], or a zero attempt timeout.
|
|
27
|
+
pub fn pages_isolated_recovering(
|
|
28
|
+
worker: &Path,
|
|
29
|
+
data: &[u8],
|
|
30
|
+
options: &ReadOptions,
|
|
31
|
+
limits: &Limits,
|
|
32
|
+
process: &ProcessLimits,
|
|
33
|
+
attempt_timeout: Duration,
|
|
34
|
+
) -> Result<Pages> {
|
|
35
|
+
let mut recovery = Recovery::new(options, attempt_timeout)?;
|
|
36
|
+
loop {
|
|
37
|
+
if let Some(pages) = recovery.step(worker, data, limits, process)? {
|
|
38
|
+
return Ok(pages);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// [`pages_isolated_recovering`], one worker attempt per [`Recovery::step`].
|
|
44
|
+
///
|
|
45
|
+
/// A step that fails keeps the progress before it, and the deadline set by
|
|
46
|
+
/// the first step: a caller woken spuriously can step again.
|
|
47
|
+
pub struct Recovery {
|
|
48
|
+
options: ReadOptions,
|
|
49
|
+
attempt_timeout: Duration,
|
|
50
|
+
deadline: Option<Instant>,
|
|
51
|
+
phase: Phase,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
enum Phase {
|
|
55
|
+
Batch,
|
|
56
|
+
/// `pending` pages are retried alone, last first.
|
|
57
|
+
Singles {
|
|
58
|
+
result: Pages,
|
|
59
|
+
pending: Vec<usize>,
|
|
60
|
+
remaining: usize,
|
|
61
|
+
},
|
|
62
|
+
Done,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
impl Recovery {
|
|
66
|
+
/// # Errors
|
|
67
|
+
/// A zero attempt timeout.
|
|
68
|
+
pub fn new(options: &ReadOptions, attempt_timeout: Duration) -> Result<Self> {
|
|
69
|
+
if attempt_timeout.is_zero() {
|
|
70
|
+
return Err(Error::InvalidPdfOption("batch timeout must be positive"));
|
|
71
|
+
}
|
|
72
|
+
Ok(Self { options: options.clone(), attempt_timeout, deadline: None, phase: Phase::Batch })
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// Runs the next worker attempt; the pages once recovery is over.
|
|
76
|
+
///
|
|
77
|
+
/// # Errors
|
|
78
|
+
/// As [`pages_isolated_recovering`], or a step after the pages were returned.
|
|
79
|
+
pub fn step(
|
|
80
|
+
&mut self,
|
|
81
|
+
worker: &Path,
|
|
82
|
+
data: &[u8],
|
|
83
|
+
limits: &Limits,
|
|
84
|
+
process: &ProcessLimits,
|
|
85
|
+
) -> Result<Option<Pages>> {
|
|
86
|
+
if matches!(self.phase, Phase::Done) {
|
|
87
|
+
return Err(Error::InvalidPdfOption("recovery has already returned its pages"));
|
|
88
|
+
}
|
|
89
|
+
let deadline = self.deadline(limits, process)?;
|
|
90
|
+
let attempt_timeout = self.attempt_timeout;
|
|
91
|
+
let attempt = |selection: &ReadOptions| {
|
|
92
|
+
let wall_time = deadline.saturating_duration_since(Instant::now()).min(attempt_timeout);
|
|
93
|
+
if wall_time.is_zero() {
|
|
94
|
+
return Err(Error::LimitExceeded("time"));
|
|
95
|
+
}
|
|
96
|
+
let process = ProcessLimits { wall_time, ..process.clone() };
|
|
97
|
+
let operation = protocol::Operation::Pages(selection.clone());
|
|
98
|
+
stream::decode_recoverable(
|
|
99
|
+
invoke(worker, data, operation, limits, &process)?,
|
|
100
|
+
selection,
|
|
101
|
+
)
|
|
102
|
+
};
|
|
103
|
+
let Phase::Singles { result, pending, remaining } = &mut self.phase else {
|
|
104
|
+
let (pages, first_missing) = attempt(&self.options)?;
|
|
105
|
+
let Some(first_missing) = first_missing else {
|
|
106
|
+
self.phase = Phase::Done;
|
|
107
|
+
return Ok(Some(pages));
|
|
108
|
+
};
|
|
109
|
+
let count = pages.pages.len();
|
|
110
|
+
let pending = (first_missing + 1..count)
|
|
111
|
+
.chain(iter::once(first_missing))
|
|
112
|
+
.filter(|&index| index < count)
|
|
113
|
+
.rev()
|
|
114
|
+
.collect();
|
|
115
|
+
let read: usize = pages.pages.iter().map(|page| page.text.len()).sum();
|
|
116
|
+
let remaining = self.options.max_output_bytes.saturating_sub(read);
|
|
117
|
+
self.phase = Phase::Singles { result: pages, pending, remaining };
|
|
118
|
+
return Ok(self.settle());
|
|
119
|
+
};
|
|
120
|
+
let Some(&index) = pending.last() else { return Ok(self.finish()) };
|
|
121
|
+
let number = result.pages[index].number;
|
|
122
|
+
let selection = ReadOptions {
|
|
123
|
+
first_page: number,
|
|
124
|
+
last_page: number,
|
|
125
|
+
max_pages: 1,
|
|
126
|
+
max_output_bytes: *remaining,
|
|
127
|
+
..self.options.clone()
|
|
128
|
+
};
|
|
129
|
+
match attempt(&selection) {
|
|
130
|
+
Ok((mut single, _)) => {
|
|
131
|
+
if single.info != result.info || single.pages.len() != 1 {
|
|
132
|
+
return Err(Error::malformed("PDF worker", "inconsistent recovery metadata"));
|
|
133
|
+
}
|
|
134
|
+
let page = single.pages.remove(0);
|
|
135
|
+
*remaining = remaining.saturating_sub(page.text.len());
|
|
136
|
+
result.pages[index] = page;
|
|
137
|
+
}
|
|
138
|
+
Err(error @ Error::LimitExceeded(reason)) if reason != "PDF worker output" => {
|
|
139
|
+
// A retry can expire while spooling, before metadata exists.
|
|
140
|
+
// Keep the prefix even then; no attempt renews the outer clock.
|
|
141
|
+
if Instant::now() >= deadline {
|
|
142
|
+
return Ok(self.finish());
|
|
143
|
+
}
|
|
144
|
+
let status = if stream::expired(&error) { Status::Skipped } else { Status::Failed };
|
|
145
|
+
result.pages[index] = bounded::unread(number, status, error.to_string());
|
|
146
|
+
}
|
|
147
|
+
// Valid worker failures with metadata already decode to page
|
|
148
|
+
// records. Infrastructure/protocol errors are not page damage.
|
|
149
|
+
Err(error) => return Err(error),
|
|
150
|
+
}
|
|
151
|
+
pending.pop();
|
|
152
|
+
Ok(self.settle())
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
fn deadline(&mut self, limits: &Limits, process: &ProcessLimits) -> Result<Instant> {
|
|
156
|
+
if let Some(deadline) = self.deadline {
|
|
157
|
+
return Ok(deadline);
|
|
158
|
+
}
|
|
159
|
+
let deadline = Instant::now()
|
|
160
|
+
.checked_add(process.wall_time_for(limits))
|
|
161
|
+
.ok_or(Error::InvalidPdfOption("worker wall time is too large"))?;
|
|
162
|
+
Ok(*self.deadline.insert(deadline))
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
fn settle(&mut self) -> Option<Pages> {
|
|
166
|
+
match &self.phase {
|
|
167
|
+
Phase::Singles { pending, remaining, .. } if pending.is_empty() || *remaining == 0 => {
|
|
168
|
+
self.finish()
|
|
169
|
+
}
|
|
170
|
+
_ => None,
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
fn finish(&mut self) -> Option<Pages> {
|
|
175
|
+
match mem::replace(&mut self.phase, Phase::Done) {
|
|
176
|
+
Phase::Singles { result, .. } => Some(result),
|
|
177
|
+
Phase::Batch | Phase::Done => None,
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -25,6 +25,15 @@ pub(super) fn write(out: &mut impl Write, frame: &Frame) -> Result<()> {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
pub(super) fn decode(output: Output, options: &ReadOptions) -> Result<Pages> {
|
|
28
|
+
decode_recoverable(output, options).map(|(pages, _)| pages)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The index comes from the supervisor/protocol failure, not human-readable
|
|
32
|
+
// page reasons: output-limit skips must never become recovery candidates.
|
|
33
|
+
pub(super) fn decode_recoverable(
|
|
34
|
+
output: Output,
|
|
35
|
+
options: &ReadOptions,
|
|
36
|
+
) -> Result<(Pages, Option<usize>)> {
|
|
28
37
|
let mut failure = output.failure;
|
|
29
38
|
if output.data.len() < protocol::HEADER.len() {
|
|
30
39
|
return Err(failure.unwrap_or_else(|| malformed("missing response")));
|
|
@@ -67,14 +76,12 @@ pub(super) fn decode(output: Output, options: &ReadOptions) -> Result<Pages> {
|
|
|
67
76
|
info.ok_or_else(|| failure.take().unwrap_or_else(|| malformed("missing metadata")))?;
|
|
68
77
|
let count = selected(info, options);
|
|
69
78
|
if pages.len() == count && ended {
|
|
70
|
-
return Ok(Pages { info, pages });
|
|
79
|
+
return Ok((Pages { info, pages }, None));
|
|
71
80
|
}
|
|
72
81
|
let error = failure.ok_or_else(|| malformed("incomplete response"))?;
|
|
73
82
|
let reason = error.to_string();
|
|
74
|
-
let
|
|
75
|
-
|
|
76
|
-
Error::LimitExceeded("time" | "render worker wall time" | "PDF worker limit")
|
|
77
|
-
);
|
|
83
|
+
let recoverable = expired(&error);
|
|
84
|
+
let timed_out = recoverable || matches!(error, Error::LimitExceeded("PDF worker limit"));
|
|
78
85
|
let first_missing = pages.len();
|
|
79
86
|
for index in first_missing..count {
|
|
80
87
|
let number =
|
|
@@ -83,7 +90,11 @@ pub(super) fn decode(output: Output, options: &ReadOptions) -> Result<Pages> {
|
|
|
83
90
|
if timed_out || index > first_missing { Status::Skipped } else { Status::Failed };
|
|
84
91
|
pages.push(bounded::unread(number, status, reason.clone()));
|
|
85
92
|
}
|
|
86
|
-
Ok(Pages { info, pages })
|
|
93
|
+
Ok((Pages { info, pages }, recoverable.then_some(first_missing)))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
pub(super) fn expired(error: &Error) -> bool {
|
|
97
|
+
matches!(error, Error::LimitExceeded("time" | "render worker wall time"))
|
|
87
98
|
}
|
|
88
99
|
|
|
89
100
|
fn selected(info: Info, options: &ReadOptions) -> usize {
|
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
//! occurs in the supervisor.
|
|
3
3
|
|
|
4
4
|
mod protocol;
|
|
5
|
+
mod recovery;
|
|
5
6
|
mod stream;
|
|
6
7
|
mod system;
|
|
7
8
|
#[cfg(test)]
|
|
9
|
+
mod test_support;
|
|
10
|
+
#[cfg(test)]
|
|
8
11
|
mod tests;
|
|
9
12
|
mod worker;
|
|
10
13
|
|
|
@@ -18,6 +21,7 @@ use std::time::{Duration, Instant};
|
|
|
18
21
|
use super::{Info, Pages, ReadOptions, RenderOptions};
|
|
19
22
|
use crate::read::{Budget, Image, Limits};
|
|
20
23
|
use crate::{Error, Result};
|
|
24
|
+
pub use recovery::{Recovery, pages_isolated_recovering};
|
|
21
25
|
pub use worker::worker_main;
|
|
22
26
|
|
|
23
27
|
/// Inspects a PDF in the trusted, resource-limited worker.
|
|
@@ -71,6 +75,12 @@ impl Default for ProcessLimits {
|
|
|
71
75
|
}
|
|
72
76
|
}
|
|
73
77
|
|
|
78
|
+
impl ProcessLimits {
|
|
79
|
+
fn wall_time_for(&self, limits: &Limits) -> Duration {
|
|
80
|
+
limits.timeout.unwrap_or(self.wall_time).min(self.wall_time)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
74
84
|
/// Renders through the explicitly selected, trusted worker executable.
|
|
75
85
|
///
|
|
76
86
|
/// The child is killed and reaped on timeout, cancellation, resource failure
|