pdfium-native 0.5.2 → 0.5.3
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 +7 -0
- package/package.json +1 -1
- package/src/annotations_worker.h +4 -7
- package/src/bookmarks_worker.h +3 -7
- package/src/document.h +6 -11
- package/src/form_fields_worker.h +4 -7
- package/src/links_worker.h +4 -7
- package/src/napi_helpers.h +74 -9
- package/src/objects_worker.h +4 -7
- package/src/pdfium_addon.cc +31 -21
- package/src/render_image_worker.h +4 -7
- package/src/render_worker.h +4 -7
- package/src/search_worker.h +4 -7
- package/src/split_merge_worker.h +10 -17
- package/src/text_worker.h +4 -7
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/), and this project adheres to [Semantic Versioning](https://semver.org/).
|
|
6
6
|
|
|
7
|
+
## [0.5.3] - 2026-04-08
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
|
|
11
|
+
- Store document and page constructor references per-env instead of as static globals — fixes cross-isolate V8 crash when multiple worker threads load the addon concurrently
|
|
12
|
+
- Add `SafeAsyncWorker` base class with `OnWorkComplete` guard (env-alive check, `napi_open_handle_scope` probe, try-catch) — all 13 async worker classes now inherit from it
|
|
13
|
+
|
|
7
14
|
## [0.5.2] - 2026-04-08
|
|
8
15
|
|
|
9
16
|
### Added
|
package/package.json
CHANGED
package/src/annotations_worker.h
CHANGED
|
@@ -35,14 +35,14 @@ struct AnnotationData {
|
|
|
35
35
|
std::vector<QuadPoints> quadPoints;
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
-
class GetAnnotationsWorker : public
|
|
38
|
+
class GetAnnotationsWorker : public SafeAsyncWorker {
|
|
39
39
|
public:
|
|
40
40
|
GetAnnotationsWorker(Napi::Env env, FPDF_PAGE page,
|
|
41
41
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
42
42
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
43
|
-
:
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
44
|
+
page_(page), pageAlive_(std::move(pageAlive)),
|
|
45
|
+
docAlive_(std::move(docAlive)) {}
|
|
46
46
|
|
|
47
47
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
48
48
|
|
|
@@ -208,7 +208,6 @@ protected:
|
|
|
208
208
|
}
|
|
209
209
|
|
|
210
210
|
void OnOK() override {
|
|
211
|
-
CHECK_ENV();
|
|
212
211
|
Napi::Env env = Env();
|
|
213
212
|
Napi::Array arr = Napi::Array::New(env, annotations_.size());
|
|
214
213
|
for (uint32_t i = 0; i < annotations_.size(); i++) {
|
|
@@ -263,13 +262,11 @@ protected:
|
|
|
263
262
|
}
|
|
264
263
|
|
|
265
264
|
void OnError(const Napi::Error &err) override {
|
|
266
|
-
CHECK_ENV();
|
|
267
265
|
deferred_.Reject(err.Value());
|
|
268
266
|
}
|
|
269
267
|
|
|
270
268
|
private:
|
|
271
269
|
Napi::Promise::Deferred deferred_;
|
|
272
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
273
270
|
FPDF_PAGE page_;
|
|
274
271
|
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
275
272
|
std::shared_ptr<std::atomic<bool>> docAlive_;
|
package/src/bookmarks_worker.h
CHANGED
|
@@ -25,13 +25,12 @@ struct BookmarkData {
|
|
|
25
25
|
std::vector<BookmarkData> children;
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
class GetBookmarksWorker : public
|
|
28
|
+
class GetBookmarksWorker : public SafeAsyncWorker {
|
|
29
29
|
public:
|
|
30
30
|
GetBookmarksWorker(Napi::Env env, FPDF_DOCUMENT doc,
|
|
31
31
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
32
|
-
:
|
|
33
|
-
|
|
34
|
-
}
|
|
32
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
33
|
+
doc_(doc), docAlive_(std::move(docAlive)) {}
|
|
35
34
|
|
|
36
35
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
37
36
|
|
|
@@ -46,18 +45,15 @@ protected:
|
|
|
46
45
|
}
|
|
47
46
|
|
|
48
47
|
void OnOK() override {
|
|
49
|
-
CHECK_ENV();
|
|
50
48
|
deferred_.Resolve(BuildArray(Env(), bookmarks_));
|
|
51
49
|
}
|
|
52
50
|
|
|
53
51
|
void OnError(const Napi::Error &err) override {
|
|
54
|
-
CHECK_ENV();
|
|
55
52
|
deferred_.Reject(err.Value());
|
|
56
53
|
}
|
|
57
54
|
|
|
58
55
|
private:
|
|
59
56
|
Napi::Promise::Deferred deferred_;
|
|
60
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
61
57
|
FPDF_DOCUMENT doc_;
|
|
62
58
|
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
63
59
|
std::vector<BookmarkData> bookmarks_;
|
package/src/document.h
CHANGED
|
@@ -43,9 +43,6 @@ public:
|
|
|
43
43
|
// returns the document alive flag (shared with pages)
|
|
44
44
|
std::shared_ptr<std::atomic<bool>> GetDocAlive() const { return docAlive_; }
|
|
45
45
|
|
|
46
|
-
// store the page constructor so we can create instances
|
|
47
|
-
static Napi::FunctionReference pageConstructor;
|
|
48
|
-
|
|
49
46
|
private:
|
|
50
47
|
FPDF_DOCUMENT doc_ = nullptr;
|
|
51
48
|
std::vector<uint8_t> ownedBuffer_;
|
|
@@ -110,14 +107,14 @@ private:
|
|
|
110
107
|
// GetPageWorker — async page loading
|
|
111
108
|
// ---------------------------------------------------------------------------
|
|
112
109
|
|
|
113
|
-
class GetPageWorker : public
|
|
110
|
+
class GetPageWorker : public SafeAsyncWorker {
|
|
114
111
|
public:
|
|
115
112
|
GetPageWorker(Napi::Env env, FPDF_DOCUMENT doc, int pageIndex,
|
|
116
113
|
PDFiumDocument *docWrapper,
|
|
117
114
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
118
|
-
:
|
|
119
|
-
|
|
120
|
-
|
|
115
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
116
|
+
doc_(doc), pageIndex_(pageIndex), docWrapper_(docWrapper),
|
|
117
|
+
docAlive_(std::move(docAlive)) {}
|
|
121
118
|
|
|
122
119
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
123
120
|
|
|
@@ -177,9 +174,9 @@ protected:
|
|
|
177
174
|
}
|
|
178
175
|
|
|
179
176
|
void OnOK() override {
|
|
180
|
-
CHECK_ENV();
|
|
181
177
|
Napi::Env env = Env();
|
|
182
|
-
|
|
178
|
+
auto *addon = env.GetInstanceData<AddonData>();
|
|
179
|
+
Napi::Object pageObj = addon->pageConstructor.New({});
|
|
183
180
|
PDFiumPage *pageWrapper = PDFiumPage::Unwrap(pageObj);
|
|
184
181
|
pageWrapper->SetPage(page_, doc_, pageIndex_, width_, height_);
|
|
185
182
|
pageWrapper->SetDocAlive(docAlive_);
|
|
@@ -205,13 +202,11 @@ protected:
|
|
|
205
202
|
}
|
|
206
203
|
|
|
207
204
|
void OnError(const Napi::Error &err) override {
|
|
208
|
-
CHECK_ENV();
|
|
209
205
|
deferred_.Reject(err.Value());
|
|
210
206
|
}
|
|
211
207
|
|
|
212
208
|
private:
|
|
213
209
|
Napi::Promise::Deferred deferred_;
|
|
214
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
215
210
|
FPDF_DOCUMENT doc_;
|
|
216
211
|
int pageIndex_;
|
|
217
212
|
PDFiumDocument *docWrapper_;
|
package/src/form_fields_worker.h
CHANGED
|
@@ -32,14 +32,14 @@ struct FormFieldData {
|
|
|
32
32
|
std::vector<FormFieldOptionData> options;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
class GetFormFieldsWorker : public
|
|
35
|
+
class GetFormFieldsWorker : public SafeAsyncWorker {
|
|
36
36
|
public:
|
|
37
37
|
GetFormFieldsWorker(Napi::Env env, FPDF_PAGE page, FPDF_DOCUMENT doc,
|
|
38
38
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
39
39
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
40
|
-
:
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
41
|
+
page_(page), doc_(doc), pageAlive_(std::move(pageAlive)),
|
|
42
|
+
docAlive_(std::move(docAlive)) {}
|
|
43
43
|
|
|
44
44
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
45
45
|
|
|
@@ -201,7 +201,6 @@ protected:
|
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
void OnOK() override {
|
|
204
|
-
CHECK_ENV();
|
|
205
204
|
Napi::Env env = Env();
|
|
206
205
|
Napi::Array arr = Napi::Array::New(env, fields_.size());
|
|
207
206
|
for (uint32_t i = 0; i < fields_.size(); i++) {
|
|
@@ -238,13 +237,11 @@ protected:
|
|
|
238
237
|
}
|
|
239
238
|
|
|
240
239
|
void OnError(const Napi::Error &err) override {
|
|
241
|
-
CHECK_ENV();
|
|
242
240
|
deferred_.Reject(err.Value());
|
|
243
241
|
}
|
|
244
242
|
|
|
245
243
|
private:
|
|
246
244
|
Napi::Promise::Deferred deferred_;
|
|
247
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
248
245
|
FPDF_PAGE page_;
|
|
249
246
|
FPDF_DOCUMENT doc_;
|
|
250
247
|
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
package/src/links_worker.h
CHANGED
|
@@ -26,14 +26,14 @@ struct LinkData {
|
|
|
26
26
|
std::string filePath;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
class GetLinksWorker : public
|
|
29
|
+
class GetLinksWorker : public SafeAsyncWorker {
|
|
30
30
|
public:
|
|
31
31
|
GetLinksWorker(Napi::Env env, FPDF_PAGE page, FPDF_DOCUMENT doc,
|
|
32
32
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
33
33
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
34
|
-
:
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
35
|
+
page_(page), doc_(doc), pageAlive_(std::move(pageAlive)),
|
|
36
|
+
docAlive_(std::move(docAlive)) {}
|
|
37
37
|
|
|
38
38
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
39
39
|
|
|
@@ -117,7 +117,6 @@ protected:
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
void OnOK() override {
|
|
120
|
-
CHECK_ENV();
|
|
121
120
|
Napi::Env env = Env();
|
|
122
121
|
Napi::Array arr = Napi::Array::New(env, links_.size());
|
|
123
122
|
for (uint32_t i = 0; i < links_.size(); i++) {
|
|
@@ -146,13 +145,11 @@ protected:
|
|
|
146
145
|
}
|
|
147
146
|
|
|
148
147
|
void OnError(const Napi::Error &err) override {
|
|
149
|
-
CHECK_ENV();
|
|
150
148
|
deferred_.Reject(err.Value());
|
|
151
149
|
}
|
|
152
150
|
|
|
153
151
|
private:
|
|
154
152
|
Napi::Promise::Deferred deferred_;
|
|
155
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
156
153
|
FPDF_PAGE page_;
|
|
157
154
|
FPDF_DOCUMENT doc_;
|
|
158
155
|
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
package/src/napi_helpers.h
CHANGED
|
@@ -34,15 +34,38 @@ constexpr int MAX_BOOKMARK_DEPTH = 64;
|
|
|
34
34
|
// ---------------------------------------------------------------------------
|
|
35
35
|
// Per-environment alive flag for worker thread safety
|
|
36
36
|
// ---------------------------------------------------------------------------
|
|
37
|
-
// When a Node.js worker thread is terminated (
|
|
38
|
-
//
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
//
|
|
37
|
+
// When a Node.js worker thread is terminated (worker.terminate()), it calls
|
|
38
|
+
// TerminateExecution() + set_can_call_into_js(false) and then drains the libuv
|
|
39
|
+
// event loop. Async work completions fire during this drain. The base
|
|
40
|
+
// AsyncWorker::OnWorkComplete creates a HandleScope which crashes if V8 is
|
|
41
|
+
// shutting down ("Cannot create a handle without a HandleScope" or "Entering
|
|
42
|
+
// the V8 API without proper locking").
|
|
43
|
+
//
|
|
44
|
+
// We use three layers of defense:
|
|
45
|
+
//
|
|
46
|
+
// Layer 1 — C-level env probe: napi_throw(env, nullptr) uses NAPI_PREAMBLE
|
|
47
|
+
// which checks can_call_into_js(). worker.terminate() sets this to false
|
|
48
|
+
// BEFORE draining the loop, so completions see it immediately. Returns
|
|
49
|
+
// napi_invalid_arg when alive (null value rejected), napi_cannot_run_js
|
|
50
|
+
// when shutting down. No V8 calls in either path.
|
|
51
|
+
//
|
|
52
|
+
// Layer 2 — envAlive_ atomic + cleanup hook: the cleanup hook fires during
|
|
53
|
+
// RunCleanup() after the uv drain, catching any completions that arrive
|
|
54
|
+
// during or after the cleanup phase.
|
|
55
|
+
//
|
|
56
|
+
// Layer 3 — prepareShutdown() JS export: called from the worker thread JS
|
|
57
|
+
// code before it signals readiness for termination. Sets envAlive_ to
|
|
58
|
+
// false while V8 is fully alive (belt-and-suspenders).
|
|
42
59
|
|
|
43
60
|
struct AddonData {
|
|
44
61
|
std::shared_ptr<std::atomic<bool>> envAlive =
|
|
45
62
|
std::make_shared<std::atomic<bool>>(true);
|
|
63
|
+
|
|
64
|
+
// per-env constructor references — must NOT be static globals because each
|
|
65
|
+
// worker thread has its own V8 isolate and env. A static global would be
|
|
66
|
+
// overwritten by the last thread to call Init(), causing cross-isolate use.
|
|
67
|
+
Napi::FunctionReference docConstructor;
|
|
68
|
+
Napi::FunctionReference pageConstructor;
|
|
46
69
|
};
|
|
47
70
|
|
|
48
71
|
inline std::shared_ptr<std::atomic<bool>> GetEnvAlive(Napi::Env env) {
|
|
@@ -50,10 +73,52 @@ inline std::shared_ptr<std::atomic<bool>> GetEnvAlive(Napi::Env env) {
|
|
|
50
73
|
return data ? data->envAlive : nullptr;
|
|
51
74
|
}
|
|
52
75
|
|
|
53
|
-
//
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// SafeAsyncWorker — guards OnWorkComplete against env teardown
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// All async workers MUST inherit from this class instead of Napi::AsyncWorker
|
|
80
|
+
// to be safe for use in worker threads that may be terminated at any time.
|
|
81
|
+
//
|
|
82
|
+
// OnWorkComplete is overridden to skip ALL V8 API calls when the environment
|
|
83
|
+
// is no longer alive. The JS promise will never settle, but the worker thread
|
|
84
|
+
// is being destroyed anyway — there is no listener for the result.
|
|
85
|
+
|
|
86
|
+
class SafeAsyncWorker : public Napi::AsyncWorker {
|
|
87
|
+
public:
|
|
88
|
+
void OnWorkComplete(Napi::Env env, napi_status status) override {
|
|
89
|
+
// Layer 2: cleanup hook already fired — env is being torn down
|
|
90
|
+
if (envAlive_ && !envAlive_->load())
|
|
91
|
+
return;
|
|
92
|
+
|
|
93
|
+
// Layer 1: probe whether V8 is still accessible by attempting the exact
|
|
94
|
+
// operation that crashes in the base class: opening a handle scope.
|
|
95
|
+
// napi_open_handle_scope is a raw C call — no C++ exception on failure.
|
|
96
|
+
// worker.terminate() races with this: the parent sets can_call_into_js
|
|
97
|
+
// to false and calls TerminateExecution, but with relaxed atomics the
|
|
98
|
+
// worker thread may not see the flag yet. If the scope open fails here,
|
|
99
|
+
// we know the env is dead and bail out immediately.
|
|
100
|
+
napi_handle_scope scope = nullptr;
|
|
101
|
+
if (napi_open_handle_scope(env, &scope) != napi_ok)
|
|
102
|
+
return;
|
|
103
|
+
napi_close_handle_scope(env, scope);
|
|
104
|
+
|
|
105
|
+
// safety net: catch Napi::Error in case the env becomes unusable between
|
|
106
|
+
// our probe and the base class's HandleScope / OnOK / OnError calls.
|
|
107
|
+
// The base OnWorkComplete creates a HandleScope OUTSIDE WrapCallback's
|
|
108
|
+
// try-catch, so a failure there throws an uncaught Napi::Error.
|
|
109
|
+
try {
|
|
110
|
+
Napi::AsyncWorker::OnWorkComplete(env, status);
|
|
111
|
+
} catch (const Napi::Error &) {
|
|
112
|
+
// env tore down between our probe and the base class call — swallow
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
protected:
|
|
117
|
+
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
118
|
+
|
|
119
|
+
explicit SafeAsyncWorker(Napi::Env env)
|
|
120
|
+
: Napi::AsyncWorker(env), envAlive_(GetEnvAlive(env)) {}
|
|
121
|
+
};
|
|
57
122
|
|
|
58
123
|
// ---------------------------------------------------------------------------
|
|
59
124
|
// PDFium UTF-16 string helpers
|
package/src/objects_worker.h
CHANGED
|
@@ -41,15 +41,15 @@ struct PageObjectData {
|
|
|
41
41
|
bool hasImageMetadata = false;
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
class GetObjectWorker : public
|
|
44
|
+
class GetObjectWorker : public SafeAsyncWorker {
|
|
45
45
|
public:
|
|
46
46
|
GetObjectWorker(Napi::Env env, FPDF_PAGE page, int index,
|
|
47
47
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
48
48
|
std::shared_ptr<std::atomic<bool>> docAlive,
|
|
49
49
|
Napi::Object pageObj)
|
|
50
|
-
:
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
51
|
+
page_(page), index_(index), pageAlive_(std::move(pageAlive)),
|
|
52
|
+
docAlive_(std::move(docAlive)),
|
|
53
53
|
pageRef_(Napi::Persistent(pageObj)) {}
|
|
54
54
|
|
|
55
55
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
@@ -248,7 +248,6 @@ protected:
|
|
|
248
248
|
}
|
|
249
249
|
|
|
250
250
|
void OnOK() override {
|
|
251
|
-
CHECK_ENV();
|
|
252
251
|
Napi::Env env = Env();
|
|
253
252
|
Napi::Object result = Napi::Object::New(env);
|
|
254
253
|
result.Set("type", Napi::String::New(env, data_.type));
|
|
@@ -323,13 +322,11 @@ protected:
|
|
|
323
322
|
}
|
|
324
323
|
|
|
325
324
|
void OnError(const Napi::Error &err) override {
|
|
326
|
-
CHECK_ENV();
|
|
327
325
|
deferred_.Reject(err.Value());
|
|
328
326
|
}
|
|
329
327
|
|
|
330
328
|
private:
|
|
331
329
|
Napi::Promise::Deferred deferred_;
|
|
332
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
333
330
|
FPDF_PAGE page_;
|
|
334
331
|
int index_;
|
|
335
332
|
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
package/src/pdfium_addon.cc
CHANGED
|
@@ -8,14 +8,10 @@
|
|
|
8
8
|
#include "fpdf_signature.h"
|
|
9
9
|
#include "split_merge_worker.h"
|
|
10
10
|
|
|
11
|
-
Napi::FunctionReference PDFiumDocument::pageConstructor;
|
|
12
|
-
|
|
13
11
|
// ---------------------------------------------------------------------------
|
|
14
12
|
// Module-level: loadDocument (async)
|
|
15
13
|
// ---------------------------------------------------------------------------
|
|
16
14
|
|
|
17
|
-
static Napi::FunctionReference g_docConstructor;
|
|
18
|
-
|
|
19
15
|
static std::string GetPdfiumErrorMessage() {
|
|
20
16
|
unsigned long err = FPDF_GetLastError();
|
|
21
17
|
const char *code;
|
|
@@ -53,20 +49,20 @@ static std::string GetPdfiumErrorMessage() {
|
|
|
53
49
|
// LoadDocumentWorker — async document loading
|
|
54
50
|
// ---------------------------------------------------------------------------
|
|
55
51
|
|
|
56
|
-
class LoadDocumentWorker : public
|
|
52
|
+
class LoadDocumentWorker : public SafeAsyncWorker {
|
|
57
53
|
public:
|
|
58
54
|
// buffer variant — copies data since buffer may be GC'd
|
|
59
55
|
LoadDocumentWorker(Napi::Env env, std::vector<uint8_t> data,
|
|
60
56
|
std::string password)
|
|
61
|
-
:
|
|
62
|
-
|
|
63
|
-
|
|
57
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
58
|
+
bufferData_(std::move(data)), password_(std::move(password)),
|
|
59
|
+
useFile_(false) {}
|
|
64
60
|
|
|
65
61
|
// file path variant
|
|
66
62
|
LoadDocumentWorker(Napi::Env env, std::string path, std::string password)
|
|
67
|
-
:
|
|
68
|
-
|
|
69
|
-
|
|
63
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
64
|
+
filePath_(std::move(path)), password_(std::move(password)),
|
|
65
|
+
useFile_(true) {}
|
|
70
66
|
|
|
71
67
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
72
68
|
|
|
@@ -153,9 +149,9 @@ protected:
|
|
|
153
149
|
}
|
|
154
150
|
|
|
155
151
|
void OnOK() override {
|
|
156
|
-
CHECK_ENV();
|
|
157
152
|
Napi::Env env = Env();
|
|
158
|
-
|
|
153
|
+
auto *addon = Env().GetInstanceData<AddonData>();
|
|
154
|
+
Napi::Object docObj = addon->docConstructor.New({});
|
|
159
155
|
PDFiumDocument *docWrapper = PDFiumDocument::Unwrap(docObj);
|
|
160
156
|
docWrapper->SetDocument(doc_);
|
|
161
157
|
|
|
@@ -211,13 +207,11 @@ protected:
|
|
|
211
207
|
}
|
|
212
208
|
|
|
213
209
|
void OnError(const Napi::Error &err) override {
|
|
214
|
-
CHECK_ENV();
|
|
215
210
|
deferred_.Reject(err.Value());
|
|
216
211
|
}
|
|
217
212
|
|
|
218
213
|
private:
|
|
219
214
|
Napi::Promise::Deferred deferred_;
|
|
220
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
221
215
|
std::vector<uint8_t> bufferData_;
|
|
222
216
|
std::string filePath_;
|
|
223
217
|
std::string password_;
|
|
@@ -424,11 +418,25 @@ Napi::Value MergeDocuments(const Napi::CallbackInfo &info) {
|
|
|
424
418
|
// Module init
|
|
425
419
|
// ---------------------------------------------------------------------------
|
|
426
420
|
|
|
421
|
+
/**
|
|
422
|
+
* Marks the environment as shutting down so in-flight async workers bail
|
|
423
|
+
* out of OnWorkComplete instead of creating a HandleScope on a dead isolate.
|
|
424
|
+
* Called from worker thread JS before signaling readiness for termination.
|
|
425
|
+
*/
|
|
426
|
+
Napi::Value PrepareShutdown(const Napi::CallbackInfo &info) {
|
|
427
|
+
auto envAlive = GetEnvAlive(info.Env());
|
|
428
|
+
if (envAlive)
|
|
429
|
+
envAlive->store(false);
|
|
430
|
+
return info.Env().Undefined();
|
|
431
|
+
}
|
|
432
|
+
|
|
427
433
|
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
428
|
-
//
|
|
434
|
+
// per-environment alive flag for async worker safety (Layer 2+3)
|
|
429
435
|
auto *addonData = new AddonData();
|
|
430
436
|
env.SetInstanceData(addonData);
|
|
431
437
|
auto envAlive = addonData->envAlive;
|
|
438
|
+
|
|
439
|
+
// Layer 2: cleanup hook — fires during RunCleanup() after uv drain
|
|
432
440
|
env.AddCleanupHook([envAlive]() { envAlive->store(false); });
|
|
433
441
|
|
|
434
442
|
if (!g_initialized) {
|
|
@@ -442,20 +450,22 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
|
442
450
|
});
|
|
443
451
|
}
|
|
444
452
|
|
|
445
|
-
// register classes
|
|
453
|
+
// register classes — constructor refs are per-env (not static globals)
|
|
454
|
+
// because each worker thread has its own V8 isolate
|
|
446
455
|
Napi::Function pageCtor = PDFiumPage::Init(env);
|
|
447
|
-
|
|
448
|
-
|
|
456
|
+
addonData->pageConstructor = Napi::Persistent(pageCtor);
|
|
457
|
+
addonData->pageConstructor.SuppressDestruct();
|
|
449
458
|
|
|
450
459
|
Napi::Function docCtor = PDFiumDocument::Init(env);
|
|
451
|
-
|
|
452
|
-
|
|
460
|
+
addonData->docConstructor = Napi::Persistent(docCtor);
|
|
461
|
+
addonData->docConstructor.SuppressDestruct();
|
|
453
462
|
|
|
454
463
|
exports.Set("PDFiumDocument", docCtor);
|
|
455
464
|
exports.Set("PDFiumPage", pageCtor);
|
|
456
465
|
exports.Set("loadDocument", Napi::Function::New(env, LoadDocument));
|
|
457
466
|
exports.Set("splitDocument", Napi::Function::New(env, SplitDocument));
|
|
458
467
|
exports.Set("mergeDocuments", Napi::Function::New(env, MergeDocuments));
|
|
468
|
+
exports.Set("prepareShutdown", Napi::Function::New(env, PrepareShutdown));
|
|
459
469
|
|
|
460
470
|
return exports;
|
|
461
471
|
}
|
|
@@ -26,16 +26,16 @@ enum RenderImageMode {
|
|
|
26
26
|
// RenderImageWorker — async image rendering from a page object
|
|
27
27
|
// ---------------------------------------------------------------------------
|
|
28
28
|
|
|
29
|
-
class RenderImageWorker : public
|
|
29
|
+
class RenderImageWorker : public SafeAsyncWorker {
|
|
30
30
|
public:
|
|
31
31
|
RenderImageWorker(Napi::Env env, FPDF_PAGE page, FPDF_DOCUMENT doc,
|
|
32
32
|
int objectIndex, int format, int quality,
|
|
33
33
|
std::string outputPath, int mode,
|
|
34
34
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
35
35
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
36
|
-
:
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
37
|
+
page_(page), doc_(doc), objectIndex_(objectIndex), format_(format),
|
|
38
|
+
quality_(quality),
|
|
39
39
|
outputPath_(std::move(outputPath)), mode_(mode),
|
|
40
40
|
pageAlive_(std::move(pageAlive)), docAlive_(std::move(docAlive)) {}
|
|
41
41
|
|
|
@@ -83,7 +83,6 @@ protected:
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
void OnOK() override {
|
|
86
|
-
CHECK_ENV();
|
|
87
86
|
Napi::Env env = Env();
|
|
88
87
|
if (outputPath_.empty()) {
|
|
89
88
|
auto data = Napi::Buffer<uint8_t>::Copy(env, encodedData_.data(),
|
|
@@ -95,13 +94,11 @@ protected:
|
|
|
95
94
|
}
|
|
96
95
|
|
|
97
96
|
void OnError(const Napi::Error &err) override {
|
|
98
|
-
CHECK_ENV();
|
|
99
97
|
deferred_.Reject(err.Value());
|
|
100
98
|
}
|
|
101
99
|
|
|
102
100
|
private:
|
|
103
101
|
Napi::Promise::Deferred deferred_;
|
|
104
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
105
102
|
FPDF_PAGE page_;
|
|
106
103
|
FPDF_DOCUMENT doc_;
|
|
107
104
|
int objectIndex_;
|
package/src/render_worker.h
CHANGED
|
@@ -22,15 +22,15 @@ inline void stb_write_callback(void *context, void *data, int size) {
|
|
|
22
22
|
// RenderWorker — async page rendering
|
|
23
23
|
// ---------------------------------------------------------------------------
|
|
24
24
|
|
|
25
|
-
class RenderWorker : public
|
|
25
|
+
class RenderWorker : public SafeAsyncWorker {
|
|
26
26
|
public:
|
|
27
27
|
RenderWorker(Napi::Env env, FPDF_PAGE page, int renderWidth, int renderHeight,
|
|
28
28
|
int format, int quality, std::string outputPath, int rotation,
|
|
29
29
|
bool transparent, int renderFlags,
|
|
30
30
|
std::shared_ptr<std::atomic<bool>> pageAlive)
|
|
31
|
-
:
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
32
|
+
page_(page), renderWidth_(renderWidth), renderHeight_(renderHeight),
|
|
33
|
+
format_(format), quality_(quality),
|
|
34
34
|
outputPath_(std::move(outputPath)), rotation_(rotation),
|
|
35
35
|
transparent_(transparent), renderFlags_(renderFlags),
|
|
36
36
|
pageAlive_(std::move(pageAlive)) {}
|
|
@@ -132,7 +132,6 @@ protected:
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
void OnOK() override {
|
|
135
|
-
CHECK_ENV();
|
|
136
135
|
Napi::Env env = Env();
|
|
137
136
|
if (outputPath_.empty()) {
|
|
138
137
|
auto data = Napi::Buffer<uint8_t>::Copy(env, encodedData_.data(),
|
|
@@ -144,13 +143,11 @@ protected:
|
|
|
144
143
|
}
|
|
145
144
|
|
|
146
145
|
void OnError(const Napi::Error &err) override {
|
|
147
|
-
CHECK_ENV();
|
|
148
146
|
deferred_.Reject(err.Value());
|
|
149
147
|
}
|
|
150
148
|
|
|
151
149
|
private:
|
|
152
150
|
Napi::Promise::Deferred deferred_;
|
|
153
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
154
151
|
FPDF_PAGE page_;
|
|
155
152
|
int renderWidth_;
|
|
156
153
|
int renderHeight_;
|
package/src/search_worker.h
CHANGED
|
@@ -24,15 +24,15 @@ struct SearchMatchData {
|
|
|
24
24
|
std::vector<Rect> rects;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
-
class SearchWorker : public
|
|
27
|
+
class SearchWorker : public SafeAsyncWorker {
|
|
28
28
|
public:
|
|
29
29
|
SearchWorker(Napi::Env env, FPDF_PAGE page, std::u16string needle,
|
|
30
30
|
unsigned long flags,
|
|
31
31
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
32
32
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
33
|
-
:
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
34
|
+
page_(page), needle_(std::move(needle)), flags_(flags),
|
|
35
|
+
pageAlive_(std::move(pageAlive)),
|
|
36
36
|
docAlive_(std::move(docAlive)) {}
|
|
37
37
|
|
|
38
38
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
@@ -87,7 +87,6 @@ protected:
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
void OnOK() override {
|
|
90
|
-
CHECK_ENV();
|
|
91
90
|
Napi::Env env = Env();
|
|
92
91
|
Napi::Array results = Napi::Array::New(env, matches_.size());
|
|
93
92
|
for (uint32_t i = 0; i < matches_.size(); i++) {
|
|
@@ -109,13 +108,11 @@ protected:
|
|
|
109
108
|
}
|
|
110
109
|
|
|
111
110
|
void OnError(const Napi::Error &err) override {
|
|
112
|
-
CHECK_ENV();
|
|
113
111
|
deferred_.Reject(err.Value());
|
|
114
112
|
}
|
|
115
113
|
|
|
116
114
|
private:
|
|
117
115
|
Napi::Promise::Deferred deferred_;
|
|
118
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
119
116
|
FPDF_PAGE page_;
|
|
120
117
|
std::u16string needle_;
|
|
121
118
|
unsigned long flags_;
|
package/src/split_merge_worker.h
CHANGED
|
@@ -111,16 +111,16 @@ static inline FPDF_DOCUMENT LoadDoc(const std::vector<uint8_t> &bufferData,
|
|
|
111
111
|
// SplitDocumentWorker — split a PDF into multiple documents at given indices
|
|
112
112
|
// ---------------------------------------------------------------------------
|
|
113
113
|
|
|
114
|
-
class SplitDocumentWorker : public
|
|
114
|
+
class SplitDocumentWorker : public SafeAsyncWorker {
|
|
115
115
|
public:
|
|
116
116
|
// buffer variant
|
|
117
117
|
SplitDocumentWorker(Napi::Env env, std::vector<uint8_t> data,
|
|
118
118
|
std::vector<int> splitAt,
|
|
119
119
|
std::vector<std::string> outputPaths,
|
|
120
120
|
std::string password)
|
|
121
|
-
:
|
|
122
|
-
|
|
123
|
-
|
|
121
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
122
|
+
bufferData_(std::move(data)), splitAt_(std::move(splitAt)),
|
|
123
|
+
outputPaths_(std::move(outputPaths)),
|
|
124
124
|
password_(std::move(password)), useFile_(false) {}
|
|
125
125
|
|
|
126
126
|
// file path variant
|
|
@@ -128,9 +128,9 @@ public:
|
|
|
128
128
|
std::vector<int> splitAt,
|
|
129
129
|
std::vector<std::string> outputPaths,
|
|
130
130
|
std::string password)
|
|
131
|
-
:
|
|
132
|
-
|
|
133
|
-
|
|
131
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
132
|
+
filePath_(std::move(filePath)), splitAt_(std::move(splitAt)),
|
|
133
|
+
outputPaths_(std::move(outputPaths)),
|
|
134
134
|
password_(std::move(password)), useFile_(true) {}
|
|
135
135
|
|
|
136
136
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
@@ -213,7 +213,6 @@ protected:
|
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
void OnOK() override {
|
|
216
|
-
CHECK_ENV();
|
|
217
216
|
Napi::Env env = Env();
|
|
218
217
|
if (!outputPaths_.empty()) {
|
|
219
218
|
deferred_.Resolve(env.Undefined());
|
|
@@ -229,13 +228,11 @@ protected:
|
|
|
229
228
|
}
|
|
230
229
|
|
|
231
230
|
void OnError(const Napi::Error &err) override {
|
|
232
|
-
CHECK_ENV();
|
|
233
231
|
deferred_.Reject(err.Value());
|
|
234
232
|
}
|
|
235
233
|
|
|
236
234
|
private:
|
|
237
235
|
Napi::Promise::Deferred deferred_;
|
|
238
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
239
236
|
std::vector<uint8_t> bufferData_;
|
|
240
237
|
std::string filePath_;
|
|
241
238
|
std::vector<int> splitAt_;
|
|
@@ -256,13 +253,12 @@ struct DocInput {
|
|
|
256
253
|
bool useFile;
|
|
257
254
|
};
|
|
258
255
|
|
|
259
|
-
class MergeDocumentsWorker : public
|
|
256
|
+
class MergeDocumentsWorker : public SafeAsyncWorker {
|
|
260
257
|
public:
|
|
261
258
|
MergeDocumentsWorker(Napi::Env env, std::vector<DocInput> inputs,
|
|
262
259
|
std::string outputPath)
|
|
263
|
-
:
|
|
264
|
-
|
|
265
|
-
outputPath_(std::move(outputPath)) {}
|
|
260
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
261
|
+
inputs_(std::move(inputs)), outputPath_(std::move(outputPath)) {}
|
|
266
262
|
|
|
267
263
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
268
264
|
|
|
@@ -326,7 +322,6 @@ protected:
|
|
|
326
322
|
}
|
|
327
323
|
|
|
328
324
|
void OnOK() override {
|
|
329
|
-
CHECK_ENV();
|
|
330
325
|
Napi::Env env = Env();
|
|
331
326
|
if (outputPath_.empty()) {
|
|
332
327
|
deferred_.Resolve(Napi::Buffer<uint8_t>::Copy(env, resultData_.data(),
|
|
@@ -337,13 +332,11 @@ protected:
|
|
|
337
332
|
}
|
|
338
333
|
|
|
339
334
|
void OnError(const Napi::Error &err) override {
|
|
340
|
-
CHECK_ENV();
|
|
341
335
|
deferred_.Reject(err.Value());
|
|
342
336
|
}
|
|
343
337
|
|
|
344
338
|
private:
|
|
345
339
|
Napi::Promise::Deferred deferred_;
|
|
346
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
347
340
|
std::vector<DocInput> inputs_;
|
|
348
341
|
std::string outputPath_;
|
|
349
342
|
std::vector<uint8_t> resultData_;
|
package/src/text_worker.h
CHANGED
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
// GetTextWorker — async text extraction
|
|
14
14
|
// ---------------------------------------------------------------------------
|
|
15
15
|
|
|
16
|
-
class GetTextWorker : public
|
|
16
|
+
class GetTextWorker : public SafeAsyncWorker {
|
|
17
17
|
public:
|
|
18
18
|
GetTextWorker(Napi::Env env, FPDF_PAGE page,
|
|
19
19
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
20
20
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
21
|
-
:
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
22
|
+
page_(page), pageAlive_(std::move(pageAlive)),
|
|
23
|
+
docAlive_(std::move(docAlive)) {}
|
|
24
24
|
|
|
25
25
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
26
26
|
|
|
@@ -46,7 +46,6 @@ protected:
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
void OnOK() override {
|
|
49
|
-
CHECK_ENV();
|
|
50
49
|
Napi::Env env = Env();
|
|
51
50
|
if (textLen_ == 0) {
|
|
52
51
|
deferred_.Resolve(Napi::String::New(env, ""));
|
|
@@ -57,13 +56,11 @@ protected:
|
|
|
57
56
|
}
|
|
58
57
|
|
|
59
58
|
void OnError(const Napi::Error &err) override {
|
|
60
|
-
CHECK_ENV();
|
|
61
59
|
deferred_.Reject(err.Value());
|
|
62
60
|
}
|
|
63
61
|
|
|
64
62
|
private:
|
|
65
63
|
Napi::Promise::Deferred deferred_;
|
|
66
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
67
64
|
FPDF_PAGE page_;
|
|
68
65
|
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
69
66
|
std::shared_ptr<std::atomic<bool>> docAlive_;
|