pdfium-native 0.5.3 → 0.5.5

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,29 @@ 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.5] - 2026-04-26
8
+
9
+ ### Changed
10
+
11
+ - Build TypeScript on `npm install` via `prepare` script (`husky && tsc`) so `dist/` is always rebuilt from `lib/` after install and on `npm pack`/publish, preventing stale `dist/` drift
12
+
13
+ ## [0.5.4] - 2026-04-11
14
+
15
+ ### Fixed
16
+
17
+ - Remove `%PDF-` header validation — PDFium already returns `FORMAT` error for invalid input; the pre-check was blocking PDF repair for files with damaged or missing headers
18
+ - Ref-counted PDFium initialization (`atomic<int>` refcount) for multi-worker-thread safety — init on first env, destroy on last
19
+ - Zero-copy `Buffer::New` with heap-allocated vector and release callback for render, image extraction, split, and merge operations (replaces `Buffer::Copy`)
20
+ - Fix `ReadU16` minimum length check from 4 to 2 bytes
21
+ - Fix `AddonData` memory leak in cleanup hook
22
+
23
+ ### Changed
24
+
25
+ - Extract shared BGRA→RGB/RGBA pixel conversion into `pixel_convert.h` inline helper
26
+ - Consolidate `GetPdfiumErrorMessage()` into `napi_helpers.h`, removing duplicates
27
+ - Switch compiler optimization from `-Os` to `-O2`; replace `<filesystem>` with POSIX `access()`
28
+ - Compact `pageAliveFlags` and optimize pixel conversion loops
29
+
7
30
  ## [0.5.3] - 2026-04-08
8
31
 
9
32
  ### Fixed
package/binding.gyp CHANGED
@@ -18,7 +18,7 @@
18
18
  "-fno-exceptions"
19
19
  ],
20
20
  "cflags": [
21
- "-Os",
21
+ "-O2",
22
22
  "-flto",
23
23
  "-ffunction-sections",
24
24
  "-fdata-sections"
@@ -41,7 +41,7 @@
41
41
  "DEAD_CODE_STRIPPING": "YES",
42
42
  "LLVM_LTO": "YES",
43
43
  "OTHER_CPLUSPLUSFLAGS": [
44
- "-Os",
44
+ "-O2",
45
45
  "-ffunction-sections",
46
46
  "-fdata-sections"
47
47
  ],
@@ -82,7 +82,7 @@
82
82
  "msvs_settings": {
83
83
  "VCCLCompilerTool": {
84
84
  "ExceptionHandling": 1,
85
- "Optimization": 1,
85
+ "Optimization": 2,
86
86
  "WholeProgramOptimization": "true",
87
87
  "AdditionalOptions": [
88
88
  "/std:c++17"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdfium-native",
3
- "version": "0.5.3",
3
+ "version": "0.5.5",
4
4
  "description": "Native Node.js bindings for PDFium",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -77,7 +77,7 @@
77
77
  "lint:check": "eslint .",
78
78
  "format": "prettier --write .",
79
79
  "format:check": "prettier --check .",
80
- "prepare": "husky"
80
+ "prepare": "husky && tsc"
81
81
  },
82
82
  "lint-staged": {
83
83
  "*.{ts,js,json,md,yaml,yml}": "prettier --write",
package/src/document.h CHANGED
@@ -3,6 +3,7 @@
3
3
  #include "bookmarks_worker.h"
4
4
  #include "page.h"
5
5
 
6
+ #include <algorithm>
6
7
  #include <atomic>
7
8
  #include <memory>
8
9
 
@@ -37,6 +38,11 @@ public:
37
38
 
38
39
  // register a page's alive flag so we can invalidate it on destroy
39
40
  void RegisterPageAlive(std::shared_ptr<std::atomic<bool>> flag) {
41
+ // compact: remove entries for pages already closed/GC'd
42
+ pageAliveFlags_.erase(
43
+ std::remove_if(pageAliveFlags_.begin(), pageAliveFlags_.end(),
44
+ [](const auto &f) { return !f->load(); }),
45
+ pageAliveFlags_.end());
40
46
  pageAliveFlags_.push_back(std::move(flag));
41
47
  }
42
48
 
@@ -9,7 +9,41 @@
9
9
  #include "fpdf_doc.h"
10
10
  #include "fpdfview.h"
11
11
 
12
- inline std::atomic<bool> g_initialized{false};
12
+ inline std::atomic<int> g_pdfium_refcount{0};
13
+
14
+ // format the last PDFium error as "CODE:message"
15
+ inline std::string GetPdfiumErrorMessage() {
16
+ unsigned long err = FPDF_GetLastError();
17
+ const char *code;
18
+ const char *msg;
19
+ switch (err) {
20
+ case FPDF_ERR_FILE:
21
+ code = "FILE";
22
+ msg = "File not found or could not be opened";
23
+ break;
24
+ case FPDF_ERR_FORMAT:
25
+ code = "FORMAT";
26
+ msg = "Not a valid PDF or corrupted";
27
+ break;
28
+ case FPDF_ERR_PASSWORD:
29
+ code = "PASSWORD";
30
+ msg = "Password required or incorrect";
31
+ break;
32
+ case FPDF_ERR_SECURITY:
33
+ code = "SECURITY";
34
+ msg = "Unsupported security scheme";
35
+ break;
36
+ case FPDF_ERR_PAGE:
37
+ code = "PAGE";
38
+ msg = "Page error";
39
+ break;
40
+ default:
41
+ code = "UNKNOWN";
42
+ msg = "Unknown error";
43
+ break;
44
+ }
45
+ return std::string(code) + ":" + msg;
46
+ }
13
47
 
14
48
  // PDFium is not thread-safe — serialize all calls through a global mutex.
15
49
  // This unblocks the Node.js event loop while waiting for the lock.
@@ -130,7 +164,7 @@ protected:
130
164
  template <typename GetLen, typename GetData>
131
165
  inline std::u16string ReadU16(GetLen getLen, GetData getData) {
132
166
  unsigned long len = getLen(static_cast<FPDF_WCHAR *>(nullptr), 0);
133
- if (len < 4 || len % 2 != 0)
167
+ if (len < 2 || len % 2 != 0)
134
168
  return {};
135
169
  std::vector<unsigned short> buf(len / sizeof(unsigned short));
136
170
  getData(reinterpret_cast<FPDF_WCHAR *>(buf.data()), len);
@@ -12,39 +12,6 @@
12
12
  // Module-level: loadDocument (async)
13
13
  // ---------------------------------------------------------------------------
14
14
 
15
- static std::string GetPdfiumErrorMessage() {
16
- unsigned long err = FPDF_GetLastError();
17
- const char *code;
18
- const char *msg;
19
- switch (err) {
20
- case FPDF_ERR_FILE:
21
- code = "FILE";
22
- msg = "File not found or could not be opened";
23
- break;
24
- case FPDF_ERR_FORMAT:
25
- code = "FORMAT";
26
- msg = "Not a valid PDF or corrupted";
27
- break;
28
- case FPDF_ERR_PASSWORD:
29
- code = "PASSWORD";
30
- msg = "Password required or incorrect";
31
- break;
32
- case FPDF_ERR_SECURITY:
33
- code = "SECURITY";
34
- msg = "Unsupported security scheme";
35
- break;
36
- case FPDF_ERR_PAGE:
37
- code = "PAGE";
38
- msg = "Page error";
39
- break;
40
- default:
41
- code = "UNKNOWN";
42
- msg = "Unknown error";
43
- break;
44
- }
45
- return std::string(code) + ":" + msg;
46
- }
47
-
48
15
  // ---------------------------------------------------------------------------
49
16
  // LoadDocumentWorker — async document loading
50
17
  // ---------------------------------------------------------------------------
@@ -75,11 +42,6 @@ protected:
75
42
  if (useFile_) {
76
43
  doc_ = FPDF_LoadDocument(filePath_.c_str(), pw);
77
44
  } 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
- }
83
45
  if (bufferData_.size() > static_cast<size_t>(INT_MAX)) {
84
46
  SetError("FORMAT:Buffer too large");
85
47
  return;
@@ -437,18 +399,21 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
437
399
  auto envAlive = addonData->envAlive;
438
400
 
439
401
  // Layer 2: cleanup hook — fires during RunCleanup() after uv drain
440
- env.AddCleanupHook([envAlive]() { envAlive->store(false); });
402
+ env.AddCleanupHook([addonData]() {
403
+ addonData->envAlive->store(false);
404
+ delete addonData;
405
+ });
441
406
 
442
- if (!g_initialized) {
407
+ if (g_pdfium_refcount.fetch_add(1) == 0) {
443
408
  FPDF_InitLibrary();
444
- g_initialized = true;
409
+ }
445
410
 
446
- // clean up PDFium on environment teardown
447
- env.AddCleanupHook([]() {
411
+ // decrement ref count on environment teardown; destroy when last env exits
412
+ env.AddCleanupHook([]() {
413
+ if (g_pdfium_refcount.fetch_sub(1) == 1) {
448
414
  FPDF_DestroyLibrary();
449
- g_initialized = false;
450
- });
451
- }
415
+ }
416
+ });
452
417
 
453
418
  // register classes — constructor refs are per-env (not static globals)
454
419
  // because each worker thread has its own V8 isolate
@@ -0,0 +1,29 @@
1
+ // shared BGRA → RGB/RGBA pixel conversion for render workers
2
+ #pragma once
3
+
4
+ #include <cstdint>
5
+
6
+ // convert one row of BGRA source pixels to RGB (dstChannels=3) or
7
+ // RGBA (dstChannels=4). srcBpp is the source bytes per pixel (3 or 4).
8
+ // hoisted conditional keeps inner loop branch-free for auto-vectorization.
9
+ inline void convertBgraRow(const uint8_t *src, uint8_t *dst, int width,
10
+ int srcBpp, int dstChannels) {
11
+ if (dstChannels == 4) {
12
+ for (int x = 0; x < width; x++) {
13
+ dst[0] = src[2]; // R ← B
14
+ dst[1] = src[1]; // G
15
+ dst[2] = src[0]; // B ← R
16
+ dst[3] = src[3]; // A
17
+ src += srcBpp;
18
+ dst += 4;
19
+ }
20
+ } else {
21
+ for (int x = 0; x < width; x++) {
22
+ dst[0] = src[2]; // R ← B
23
+ dst[1] = src[1]; // G
24
+ dst[2] = src[0]; // B ← R
25
+ src += srcBpp;
26
+ dst += 3;
27
+ }
28
+ }
29
+ }
@@ -5,10 +5,17 @@
5
5
 
6
6
  #include <atomic>
7
7
  #include <cstdio>
8
- #include <filesystem>
9
8
  #include <memory>
10
9
  #include <vector>
11
10
 
11
+ #ifdef _WIN32
12
+ #include <io.h>
13
+ #define F_OK 0
14
+ #define access _access
15
+ #else
16
+ #include <unistd.h>
17
+ #endif
18
+
12
19
  #include "fpdf_edit.h"
13
20
 
14
21
  // max total pixels for image bitmap allocation (~256 MP, ~1 GB at 4 channels)
@@ -35,8 +42,7 @@ public:
35
42
  std::shared_ptr<std::atomic<bool>> docAlive)
36
43
  : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
37
44
  page_(page), doc_(doc), objectIndex_(objectIndex), format_(format),
38
- quality_(quality),
39
- outputPath_(std::move(outputPath)), mode_(mode),
45
+ quality_(quality), outputPath_(std::move(outputPath)), mode_(mode),
40
46
  pageAlive_(std::move(pageAlive)), docAlive_(std::move(docAlive)) {}
41
47
 
42
48
  Napi::Promise Promise() { return deferred_.Promise(); }
@@ -85,8 +91,10 @@ protected:
85
91
  void OnOK() override {
86
92
  Napi::Env env = Env();
87
93
  if (outputPath_.empty()) {
88
- auto data = Napi::Buffer<uint8_t>::Copy(env, encodedData_.data(),
89
- encodedData_.size());
94
+ auto *vec = new std::vector<uint8_t>(std::move(encodedData_));
95
+ auto data = Napi::Buffer<uint8_t>::New(
96
+ env, vec->data(), vec->size(),
97
+ [](Napi::Env, uint8_t *, std::vector<uint8_t> *v) { delete v; }, vec);
90
98
  deferred_.Resolve(data);
91
99
  } else {
92
100
  deferred_.Resolve(env.Undefined());
@@ -196,22 +204,18 @@ private:
196
204
  std::vector<uint8_t> pixels(static_cast<size_t>(width) *
197
205
  static_cast<size_t>(height) * outChannels);
198
206
 
199
- for (int y = 0; y < height; y++) {
200
- auto *row = static_cast<uint8_t *>(bufferData) + y * stride;
201
- for (int x = 0; x < width; x++) {
202
- int dstIdx = (y * width + x) * outChannels;
203
-
204
- if (bitmapFormat == FPDFBitmap_Gray) {
205
- pixels[dstIdx] = row[x];
206
- } else {
207
- int srcIdx = x * srcBytesPerPixel;
208
- pixels[dstIdx + 0] = row[srcIdx + 2]; // R ← B
209
- pixels[dstIdx + 1] = row[srcIdx + 1]; // G
210
- pixels[dstIdx + 2] = row[srcIdx + 0]; // B ← R
211
- if (keepAlpha)
212
- pixels[dstIdx + 3] = row[srcIdx + 3]; // A
213
- }
207
+ if (bitmapFormat == FPDFBitmap_Gray) {
208
+ for (int y = 0; y < height; y++) {
209
+ auto *src = static_cast<uint8_t *>(bufferData) + y * stride;
210
+ auto *dst = pixels.data() + y * width;
211
+ for (int x = 0; x < width; x++)
212
+ dst[x] = src[x];
214
213
  }
214
+ } else {
215
+ for (int y = 0; y < height; y++)
216
+ convertBgraRow(static_cast<uint8_t *>(bufferData) + y * stride,
217
+ pixels.data() + y * width * outChannels, width,
218
+ srcBytesPerPixel, outChannels);
215
219
  }
216
220
 
217
221
  FPDFBitmap_Destroy(bitmap);
@@ -235,11 +239,14 @@ private:
235
239
  }
236
240
 
237
241
  bool WriteToFile() {
238
- std::filesystem::path outPath(outputPath_);
239
- auto parentDir = outPath.parent_path();
240
- if (!parentDir.empty() && !std::filesystem::is_directory(parentDir)) {
241
- SetError("Parent directory does not exist: " + parentDir.string());
242
- return false;
242
+ // check parent directory exists
243
+ auto slash = outputPath_.rfind('/');
244
+ if (slash != std::string::npos && slash > 0) {
245
+ std::string parentDir = outputPath_.substr(0, slash);
246
+ if (access(parentDir.c_str(), F_OK) != 0) {
247
+ SetError("Parent directory does not exist: " + parentDir);
248
+ return false;
249
+ }
243
250
  }
244
251
 
245
252
  FILE *f = fopen(outputPath_.c_str(), "wb");
@@ -1,12 +1,20 @@
1
1
  #pragma once
2
2
 
3
3
  #include "napi_helpers.h"
4
+ #include "pixel_convert.h"
4
5
 
5
6
  #include <atomic>
6
7
  #include <cstdio>
7
- #include <filesystem>
8
8
  #include <memory>
9
9
 
10
+ #ifdef _WIN32
11
+ #include <io.h>
12
+ #define F_OK 0
13
+ #define access _access
14
+ #else
15
+ #include <unistd.h>
16
+ #endif
17
+
10
18
  #include "stb_image_write.h"
11
19
 
12
20
  // stb write callback — appends bytes to a std::vector
@@ -30,10 +38,9 @@ public:
30
38
  std::shared_ptr<std::atomic<bool>> pageAlive)
31
39
  : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
32
40
  page_(page), renderWidth_(renderWidth), renderHeight_(renderHeight),
33
- format_(format), quality_(quality),
34
- outputPath_(std::move(outputPath)), rotation_(rotation),
35
- transparent_(transparent), renderFlags_(renderFlags),
36
- pageAlive_(std::move(pageAlive)) {}
41
+ format_(format), quality_(quality), outputPath_(std::move(outputPath)),
42
+ rotation_(rotation), transparent_(transparent),
43
+ renderFlags_(renderFlags), pageAlive_(std::move(pageAlive)) {}
37
44
 
38
45
  Napi::Promise Promise() { return deferred_.Promise(); }
39
46
 
@@ -74,18 +81,10 @@ protected:
74
81
  std::vector<uint8_t> pixels(static_cast<size_t>(renderWidth_) *
75
82
  static_cast<size_t>(renderHeight_) * channels);
76
83
 
77
- for (int y = 0; y < renderHeight_; y++) {
78
- auto *row = static_cast<uint8_t *>(bufferData) + y * stride;
79
- for (int x = 0; x < renderWidth_; x++) {
80
- int srcIdx = x * 4;
81
- int dstIdx = (y * renderWidth_ + x) * channels;
82
- pixels[dstIdx + 0] = row[srcIdx + 2]; // R ← BGRA[2]
83
- pixels[dstIdx + 1] = row[srcIdx + 1]; // G ← BGRA[1]
84
- pixels[dstIdx + 2] = row[srcIdx + 0]; // B ← BGRA[0]
85
- if (transparent_)
86
- pixels[dstIdx + 3] = row[srcIdx + 3]; // A ← BGRA[3]
87
- }
88
- }
84
+ for (int y = 0; y < renderHeight_; y++)
85
+ convertBgraRow(static_cast<uint8_t *>(bufferData) + y * stride,
86
+ pixels.data() + y * renderWidth_ * channels, renderWidth_,
87
+ 4, channels);
89
88
 
90
89
  FPDFBitmap_Destroy(bitmap);
91
90
 
@@ -109,11 +108,13 @@ protected:
109
108
  // write to file if output path was specified
110
109
  if (!outputPath_.empty()) {
111
110
  // verify parent directory exists
112
- std::filesystem::path outPath(outputPath_);
113
- auto parentDir = outPath.parent_path();
114
- if (!parentDir.empty() && !std::filesystem::is_directory(parentDir)) {
115
- SetError("Parent directory does not exist: " + parentDir.string());
116
- return;
111
+ auto slash = outputPath_.rfind('/');
112
+ if (slash != std::string::npos && slash > 0) {
113
+ std::string parentDir = outputPath_.substr(0, slash);
114
+ if (access(parentDir.c_str(), F_OK) != 0) {
115
+ SetError("Parent directory does not exist: " + parentDir);
116
+ return;
117
+ }
117
118
  }
118
119
 
119
120
  FILE *f = fopen(outputPath_.c_str(), "wb");
@@ -134,8 +135,10 @@ protected:
134
135
  void OnOK() override {
135
136
  Napi::Env env = Env();
136
137
  if (outputPath_.empty()) {
137
- auto data = Napi::Buffer<uint8_t>::Copy(env, encodedData_.data(),
138
- encodedData_.size());
138
+ auto *vec = new std::vector<uint8_t>(std::move(encodedData_));
139
+ auto data = Napi::Buffer<uint8_t>::New(
140
+ env, vec->data(), vec->size(),
141
+ [](Napi::Env, uint8_t *, std::vector<uint8_t> *v) { delete v; }, vec);
139
142
  deferred_.Resolve(data);
140
143
  } else {
141
144
  deferred_.Resolve(env.Undefined());
@@ -4,9 +4,16 @@
4
4
 
5
5
  #include <climits>
6
6
  #include <cstdio>
7
- #include <filesystem>
8
7
  #include <vector>
9
8
 
9
+ #ifdef _WIN32
10
+ #include <io.h>
11
+ #define F_OK 0
12
+ #define access _access
13
+ #else
14
+ #include <unistd.h>
15
+ #endif
16
+
10
17
  #include "fpdf_edit.h"
11
18
  #include "fpdf_ppo.h"
12
19
  #include "fpdf_save.h"
@@ -42,11 +49,13 @@ static inline bool SaveDocument(FPDF_DOCUMENT doc,
42
49
  }
43
50
 
44
51
  if (!outputPath.empty()) {
45
- std::filesystem::path outPath(outputPath);
46
- auto parentDir = outPath.parent_path();
47
- if (!parentDir.empty() && !std::filesystem::is_directory(parentDir)) {
48
- outError = "Parent directory does not exist: " + parentDir.string();
49
- return false;
52
+ auto slash = outputPath.rfind('/');
53
+ if (slash != std::string::npos && slash > 0) {
54
+ std::string parentDir = outputPath.substr(0, slash);
55
+ if (access(parentDir.c_str(), F_OK) != 0) {
56
+ outError = "Parent directory does not exist: " + parentDir;
57
+ return false;
58
+ }
50
59
  }
51
60
  FILE *f = fopen(outputPath.c_str(), "wb");
52
61
  if (!f) {
@@ -85,24 +94,7 @@ static inline FPDF_DOCUMENT LoadDoc(const std::vector<uint8_t> &bufferData,
85
94
  }
86
95
 
87
96
  if (!doc) {
88
- unsigned long err = FPDF_GetLastError();
89
- switch (err) {
90
- case FPDF_ERR_FILE:
91
- outError = "FILE:File not found or could not be opened";
92
- break;
93
- case FPDF_ERR_FORMAT:
94
- outError = "FORMAT:Not a valid PDF or corrupted";
95
- break;
96
- case FPDF_ERR_PASSWORD:
97
- outError = "PASSWORD:Password required or incorrect";
98
- break;
99
- case FPDF_ERR_SECURITY:
100
- outError = "SECURITY:Unsupported security scheme";
101
- break;
102
- default:
103
- outError = "UNKNOWN:Unknown error";
104
- break;
105
- }
97
+ outError = GetPdfiumErrorMessage();
106
98
  }
107
99
  return doc;
108
100
  }
@@ -120,8 +112,8 @@ public:
120
112
  std::string password)
121
113
  : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
122
114
  bufferData_(std::move(data)), splitAt_(std::move(splitAt)),
123
- outputPaths_(std::move(outputPaths)),
124
- password_(std::move(password)), useFile_(false) {}
115
+ outputPaths_(std::move(outputPaths)), password_(std::move(password)),
116
+ useFile_(false) {}
125
117
 
126
118
  // file path variant
127
119
  SplitDocumentWorker(Napi::Env env, std::string filePath,
@@ -130,8 +122,8 @@ public:
130
122
  std::string password)
131
123
  : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
132
124
  filePath_(std::move(filePath)), splitAt_(std::move(splitAt)),
133
- outputPaths_(std::move(outputPaths)),
134
- password_(std::move(password)), useFile_(true) {}
125
+ outputPaths_(std::move(outputPaths)), password_(std::move(password)),
126
+ useFile_(true) {}
135
127
 
136
128
  Napi::Promise Promise() { return deferred_.Promise(); }
137
129
 
@@ -219,9 +211,13 @@ protected:
219
211
  } else {
220
212
  Napi::Array arr = Napi::Array::New(env, resultParts_.size());
221
213
  for (size_t i = 0; i < resultParts_.size(); i++) {
222
- arr.Set(static_cast<uint32_t>(i),
223
- Napi::Buffer<uint8_t>::Copy(env, resultParts_[i].data(),
224
- resultParts_[i].size()));
214
+ auto *vec = new std::vector<uint8_t>(std::move(resultParts_[i]));
215
+ arr.Set(
216
+ static_cast<uint32_t>(i),
217
+ Napi::Buffer<uint8_t>::New(
218
+ env, vec->data(), vec->size(),
219
+ [](Napi::Env, uint8_t *, std::vector<uint8_t> *v) { delete v; },
220
+ vec));
225
221
  }
226
222
  deferred_.Resolve(arr);
227
223
  }
@@ -324,8 +320,11 @@ protected:
324
320
  void OnOK() override {
325
321
  Napi::Env env = Env();
326
322
  if (outputPath_.empty()) {
327
- deferred_.Resolve(Napi::Buffer<uint8_t>::Copy(env, resultData_.data(),
328
- resultData_.size()));
323
+ auto *vec = new std::vector<uint8_t>(std::move(resultData_));
324
+ deferred_.Resolve(Napi::Buffer<uint8_t>::New(
325
+ env, vec->data(), vec->size(),
326
+ [](Napi::Env, uint8_t *, std::vector<uint8_t> *v) { delete v; },
327
+ vec));
329
328
  } else {
330
329
  deferred_.Resolve(env.Undefined());
331
330
  }