pdfium-native 0.5.1 → 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 CHANGED
@@ -4,6 +4,20 @@ 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
+
14
+ ## [0.5.2] - 2026-04-08
15
+
16
+ ### Added
17
+
18
+ - Validate `%PDF-` magic header on buffer input before passing to PDFium, returning a `FORMAT` error instead of risking an internal abort on garbage input
19
+ - Guard all async worker `OnOK`/`OnError` callbacks against V8 environment teardown — prevents fatal `HandleScope` crash when a Node.js worker thread is terminated mid-operation
20
+
7
21
  ## [0.5.1] - 2026-04-04
8
22
 
9
23
  ### Added
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdfium-native",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "Native Node.js bindings for PDFium",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -35,12 +35,12 @@ struct AnnotationData {
35
35
  std::vector<QuadPoints> quadPoints;
36
36
  };
37
37
 
38
- class GetAnnotationsWorker : public Napi::AsyncWorker {
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
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
43
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
44
44
  page_(page), pageAlive_(std::move(pageAlive)),
45
45
  docAlive_(std::move(docAlive)) {}
46
46
 
@@ -25,11 +25,11 @@ struct BookmarkData {
25
25
  std::vector<BookmarkData> children;
26
26
  };
27
27
 
28
- class GetBookmarksWorker : public Napi::AsyncWorker {
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
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
32
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
33
33
  doc_(doc), docAlive_(std::move(docAlive)) {}
34
34
 
35
35
  Napi::Promise Promise() { return deferred_.Promise(); }
@@ -44,7 +44,9 @@ protected:
44
44
  CollectBookmarks(nullptr, bookmarks_, 0);
45
45
  }
46
46
 
47
- void OnOK() override { deferred_.Resolve(BuildArray(Env(), bookmarks_)); }
47
+ void OnOK() override {
48
+ deferred_.Resolve(BuildArray(Env(), bookmarks_));
49
+ }
48
50
 
49
51
  void OnError(const Napi::Error &err) override {
50
52
  deferred_.Reject(err.Value());
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,12 +107,12 @@ private:
110
107
  // GetPageWorker — async page loading
111
108
  // ---------------------------------------------------------------------------
112
109
 
113
- class GetPageWorker : public Napi::AsyncWorker {
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
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
115
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
119
116
  doc_(doc), pageIndex_(pageIndex), docWrapper_(docWrapper),
120
117
  docAlive_(std::move(docAlive)) {}
121
118
 
@@ -178,7 +175,8 @@ protected:
178
175
 
179
176
  void OnOK() override {
180
177
  Napi::Env env = Env();
181
- Napi::Object pageObj = PDFiumDocument::pageConstructor.New({});
178
+ auto *addon = env.GetInstanceData<AddonData>();
179
+ Napi::Object pageObj = addon->pageConstructor.New({});
182
180
  PDFiumPage *pageWrapper = PDFiumPage::Unwrap(pageObj);
183
181
  pageWrapper->SetPage(page_, doc_, pageIndex_, width_, height_);
184
182
  pageWrapper->SetDocAlive(docAlive_);
@@ -32,12 +32,12 @@ struct FormFieldData {
32
32
  std::vector<FormFieldOptionData> options;
33
33
  };
34
34
 
35
- class GetFormFieldsWorker : public Napi::AsyncWorker {
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
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
40
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
41
41
  page_(page), doc_(doc), pageAlive_(std::move(pageAlive)),
42
42
  docAlive_(std::move(docAlive)) {}
43
43
 
@@ -26,12 +26,12 @@ struct LinkData {
26
26
  std::string filePath;
27
27
  };
28
28
 
29
- class GetLinksWorker : public Napi::AsyncWorker {
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
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
34
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
35
35
  page_(page), doc_(doc), pageAlive_(std::move(pageAlive)),
36
36
  docAlive_(std::move(docAlive)) {}
37
37
 
@@ -31,6 +31,95 @@ constexpr int MAX_BOOKMARK_DEPTH = 64;
31
31
  return; \
32
32
  }
33
33
 
34
+ // ---------------------------------------------------------------------------
35
+ // Per-environment alive flag for worker thread safety
36
+ // ---------------------------------------------------------------------------
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).
59
+
60
+ struct AddonData {
61
+ std::shared_ptr<std::atomic<bool>> envAlive =
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;
69
+ };
70
+
71
+ inline std::shared_ptr<std::atomic<bool>> GetEnvAlive(Napi::Env env) {
72
+ auto *data = env.GetInstanceData<AddonData>();
73
+ return data ? data->envAlive : nullptr;
74
+ }
75
+
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
+ };
122
+
34
123
  // ---------------------------------------------------------------------------
35
124
  // PDFium UTF-16 string helpers
36
125
  // ---------------------------------------------------------------------------
@@ -41,15 +41,16 @@ struct PageObjectData {
41
41
  bool hasImageMetadata = false;
42
42
  };
43
43
 
44
- class GetObjectWorker : public Napi::AsyncWorker {
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
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
50
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
51
51
  page_(page), index_(index), pageAlive_(std::move(pageAlive)),
52
- docAlive_(std::move(docAlive)), pageRef_(Napi::Persistent(pageObj)) {}
52
+ docAlive_(std::move(docAlive)),
53
+ pageRef_(Napi::Persistent(pageObj)) {}
53
54
 
54
55
  Napi::Promise Promise() { return deferred_.Promise(); }
55
56
 
@@ -1,20 +1,17 @@
1
1
  #include "document.h"
2
2
 
3
3
  #include <climits>
4
+ #include <cstring>
4
5
 
5
6
  #include "fpdf_attachment.h"
6
7
  #include "fpdf_catalog.h"
7
8
  #include "fpdf_signature.h"
8
9
  #include "split_merge_worker.h"
9
10
 
10
- Napi::FunctionReference PDFiumDocument::pageConstructor;
11
-
12
11
  // ---------------------------------------------------------------------------
13
12
  // Module-level: loadDocument (async)
14
13
  // ---------------------------------------------------------------------------
15
14
 
16
- static Napi::FunctionReference g_docConstructor;
17
-
18
15
  static std::string GetPdfiumErrorMessage() {
19
16
  unsigned long err = FPDF_GetLastError();
20
17
  const char *code;
@@ -52,18 +49,18 @@ static std::string GetPdfiumErrorMessage() {
52
49
  // LoadDocumentWorker — async document loading
53
50
  // ---------------------------------------------------------------------------
54
51
 
55
- class LoadDocumentWorker : public Napi::AsyncWorker {
52
+ class LoadDocumentWorker : public SafeAsyncWorker {
56
53
  public:
57
54
  // buffer variant — copies data since buffer may be GC'd
58
55
  LoadDocumentWorker(Napi::Env env, std::vector<uint8_t> data,
59
56
  std::string password)
60
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
57
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
61
58
  bufferData_(std::move(data)), password_(std::move(password)),
62
59
  useFile_(false) {}
63
60
 
64
61
  // file path variant
65
62
  LoadDocumentWorker(Napi::Env env, std::string path, std::string password)
66
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
63
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
67
64
  filePath_(std::move(path)), password_(std::move(password)),
68
65
  useFile_(true) {}
69
66
 
@@ -78,6 +75,11 @@ protected:
78
75
  if (useFile_) {
79
76
  doc_ = FPDF_LoadDocument(filePath_.c_str(), pw);
80
77
  } else {
78
+ if (bufferData_.size() < 5 ||
79
+ memcmp(bufferData_.data(), "%PDF-", 5) != 0) {
80
+ SetError("FORMAT:Input is not a valid PDF (missing %PDF- header)");
81
+ return;
82
+ }
81
83
  if (bufferData_.size() > static_cast<size_t>(INT_MAX)) {
82
84
  SetError("FORMAT:Buffer too large");
83
85
  return;
@@ -148,7 +150,8 @@ protected:
148
150
 
149
151
  void OnOK() override {
150
152
  Napi::Env env = Env();
151
- Napi::Object docObj = g_docConstructor.New({});
153
+ auto *addon = Env().GetInstanceData<AddonData>();
154
+ Napi::Object docObj = addon->docConstructor.New({});
152
155
  PDFiumDocument *docWrapper = PDFiumDocument::Unwrap(docObj);
153
156
  docWrapper->SetDocument(doc_);
154
157
 
@@ -415,7 +418,27 @@ Napi::Value MergeDocuments(const Napi::CallbackInfo &info) {
415
418
  // Module init
416
419
  // ---------------------------------------------------------------------------
417
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
+
418
433
  Napi::Object Init(Napi::Env env, Napi::Object exports) {
434
+ // per-environment alive flag for async worker safety (Layer 2+3)
435
+ auto *addonData = new AddonData();
436
+ env.SetInstanceData(addonData);
437
+ auto envAlive = addonData->envAlive;
438
+
439
+ // Layer 2: cleanup hook — fires during RunCleanup() after uv drain
440
+ env.AddCleanupHook([envAlive]() { envAlive->store(false); });
441
+
419
442
  if (!g_initialized) {
420
443
  FPDF_InitLibrary();
421
444
  g_initialized = true;
@@ -427,20 +450,22 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
427
450
  });
428
451
  }
429
452
 
430
- // register classes
453
+ // register classes — constructor refs are per-env (not static globals)
454
+ // because each worker thread has its own V8 isolate
431
455
  Napi::Function pageCtor = PDFiumPage::Init(env);
432
- PDFiumDocument::pageConstructor = Napi::Persistent(pageCtor);
433
- PDFiumDocument::pageConstructor.SuppressDestruct();
456
+ addonData->pageConstructor = Napi::Persistent(pageCtor);
457
+ addonData->pageConstructor.SuppressDestruct();
434
458
 
435
459
  Napi::Function docCtor = PDFiumDocument::Init(env);
436
- g_docConstructor = Napi::Persistent(docCtor);
437
- g_docConstructor.SuppressDestruct();
460
+ addonData->docConstructor = Napi::Persistent(docCtor);
461
+ addonData->docConstructor.SuppressDestruct();
438
462
 
439
463
  exports.Set("PDFiumDocument", docCtor);
440
464
  exports.Set("PDFiumPage", pageCtor);
441
465
  exports.Set("loadDocument", Napi::Function::New(env, LoadDocument));
442
466
  exports.Set("splitDocument", Napi::Function::New(env, SplitDocument));
443
467
  exports.Set("mergeDocuments", Napi::Function::New(env, MergeDocuments));
468
+ exports.Set("prepareShutdown", Napi::Function::New(env, PrepareShutdown));
444
469
 
445
470
  return exports;
446
471
  }
@@ -26,16 +26,17 @@ enum RenderImageMode {
26
26
  // RenderImageWorker — async image rendering from a page object
27
27
  // ---------------------------------------------------------------------------
28
28
 
29
- class RenderImageWorker : public Napi::AsyncWorker {
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
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
36
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
37
37
  page_(page), doc_(doc), objectIndex_(objectIndex), format_(format),
38
- quality_(quality), outputPath_(std::move(outputPath)), mode_(mode),
38
+ quality_(quality),
39
+ outputPath_(std::move(outputPath)), mode_(mode),
39
40
  pageAlive_(std::move(pageAlive)), docAlive_(std::move(docAlive)) {}
40
41
 
41
42
  Napi::Promise Promise() { return deferred_.Promise(); }
@@ -22,17 +22,18 @@ 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 Napi::AsyncWorker {
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
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
31
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
32
32
  page_(page), renderWidth_(renderWidth), renderHeight_(renderHeight),
33
- format_(format), quality_(quality), outputPath_(std::move(outputPath)),
34
- rotation_(rotation), transparent_(transparent),
35
- renderFlags_(renderFlags), pageAlive_(std::move(pageAlive)) {}
33
+ format_(format), quality_(quality),
34
+ outputPath_(std::move(outputPath)), rotation_(rotation),
35
+ transparent_(transparent), renderFlags_(renderFlags),
36
+ pageAlive_(std::move(pageAlive)) {}
36
37
 
37
38
  Napi::Promise Promise() { return deferred_.Promise(); }
38
39
 
@@ -24,15 +24,16 @@ struct SearchMatchData {
24
24
  std::vector<Rect> rects;
25
25
  };
26
26
 
27
- class SearchWorker : public Napi::AsyncWorker {
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
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
33
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
34
34
  page_(page), needle_(std::move(needle)), flags_(flags),
35
- pageAlive_(std::move(pageAlive)), docAlive_(std::move(docAlive)) {}
35
+ pageAlive_(std::move(pageAlive)),
36
+ docAlive_(std::move(docAlive)) {}
36
37
 
37
38
  Napi::Promise Promise() { return deferred_.Promise(); }
38
39
 
@@ -111,27 +111,27 @@ 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 Napi::AsyncWorker {
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
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
121
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
122
122
  bufferData_(std::move(data)), splitAt_(std::move(splitAt)),
123
- outputPaths_(std::move(outputPaths)), password_(std::move(password)),
124
- useFile_(false) {}
123
+ outputPaths_(std::move(outputPaths)),
124
+ password_(std::move(password)), useFile_(false) {}
125
125
 
126
126
  // file path variant
127
127
  SplitDocumentWorker(Napi::Env env, std::string filePath,
128
128
  std::vector<int> splitAt,
129
129
  std::vector<std::string> outputPaths,
130
130
  std::string password)
131
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
131
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
132
132
  filePath_(std::move(filePath)), splitAt_(std::move(splitAt)),
133
- outputPaths_(std::move(outputPaths)), password_(std::move(password)),
134
- useFile_(true) {}
133
+ outputPaths_(std::move(outputPaths)),
134
+ password_(std::move(password)), useFile_(true) {}
135
135
 
136
136
  Napi::Promise Promise() { return deferred_.Promise(); }
137
137
 
@@ -253,11 +253,11 @@ struct DocInput {
253
253
  bool useFile;
254
254
  };
255
255
 
256
- class MergeDocumentsWorker : public Napi::AsyncWorker {
256
+ class MergeDocumentsWorker : public SafeAsyncWorker {
257
257
  public:
258
258
  MergeDocumentsWorker(Napi::Env env, std::vector<DocInput> inputs,
259
259
  std::string outputPath)
260
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
260
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
261
261
  inputs_(std::move(inputs)), outputPath_(std::move(outputPath)) {}
262
262
 
263
263
  Napi::Promise Promise() { return deferred_.Promise(); }
package/src/text_worker.h CHANGED
@@ -13,12 +13,12 @@
13
13
  // GetTextWorker — async text extraction
14
14
  // ---------------------------------------------------------------------------
15
15
 
16
- class GetTextWorker : public Napi::AsyncWorker {
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
- : Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
21
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
22
22
  page_(page), pageAlive_(std::move(pageAlive)),
23
23
  docAlive_(std::move(docAlive)) {}
24
24