Skip to content

Commit 0e59111

Browse files
last comment
1 parent 3c5448f commit 0e59111

File tree

8 files changed

+32
-19
lines changed

8 files changed

+32
-19
lines changed

.eslintrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
"@typescript-eslint/no-require-imports": "off"
6565
},
6666
"overrides": [
67+
{
68+
"files": ["lib/*js"],
69+
"parserOptions": {
70+
"ecmaVersion": 2019,
71+
"sourceType": "commonjs"
72+
}
73+
},
6774
{
6875
"files": ["test/**/*ts"],
6976
"rules": {

addon/compression.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "compression.h"
44

5-
std::vector<uint8_t> Compression::compress(const std::vector<uint8_t>& data,
5+
std::vector<uint8_t> mongodb_zstd::compress(const std::vector<uint8_t>& data,
66
size_t compression_level) {
77
size_t output_buffer_size = ZSTD_compressBound(data.size());
88
std::vector<uint8_t> output(output_buffer_size);
@@ -19,7 +19,7 @@ std::vector<uint8_t> Compression::compress(const std::vector<uint8_t>& data,
1919
return output;
2020
}
2121

22-
std::vector<uint8_t> Compression::decompress(const std::vector<uint8_t>& compressed) {
22+
std::vector<uint8_t> mongodb_zstd::decompress(const std::vector<uint8_t>& compressed) {
2323
std::vector<uint8_t> decompressed;
2424

2525
using DCTX_Deleter = void (*)(ZSTD_DCtx*);
@@ -50,18 +50,20 @@ std::vector<uint8_t> Compression::decompress(const std::vector<uint8_t>& compres
5050
// the return value is a suggested next input size (just a hint
5151
// for better latency) that will never request more than the
5252
// remaining frame size.
53-
auto inputRemains = [](ZSTD_inBuffer& input) { return input.pos < input.size; };
54-
auto isOutputBufferFlushed = [](ZSTD_outBuffer& output) { return output.pos < output.size; };
53+
auto inputRemains = [](const ZSTD_inBuffer& input) { return input.pos < input.size; };
54+
auto isOutputBufferFlushed = [](const ZSTD_outBuffer& output) { return output.pos < output.size; };
5555

5656
while (inputRemains(input) || !isOutputBufferFlushed(output)) {
5757
size_t const ret = ZSTD_decompressStream(decompression_context.get(), &output, &input);
5858
if (ZSTD_isError(ret)) {
5959
throw std::runtime_error(ZSTD_getErrorName(ret));
6060
}
6161

62-
for (size_t i = 0; i < output.pos; ++i) {
63-
decompressed.push_back(output_buffer[i]);
64-
}
62+
size_t decompressed_size = decompressed.size();
63+
decompressed.resize(decompressed_size + output.pos);
64+
std::copy(output_buffer.data(),
65+
output_buffer.data() + output.pos,
66+
decompressed.data() + decompressed_size);
6567

6668
// move the position back go 0, to indicate that we are ready for more data
6769
output.pos = 0;

addon/compression.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#include "compression_worker.h"
88
#include "zstd.h"
99

10-
namespace Compression {
10+
namespace mongodb_zstd {
1111
std::vector<uint8_t> compress(const std::vector<uint8_t>& data, size_t compression_level);
1212
std::vector<uint8_t> decompress(const std::vector<uint8_t>& data);
13-
} // namespace Compression
13+
} // namespace mongodb_zstd
1414

1515
#endif

addon/compression_worker.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using namespace Napi;
99

10+
namespace mongodb_zstd {
1011
/**
1112
* @brief An asynchronous Napi::Worker that can be with any function that produces
1213
* CompressionResults.
@@ -17,11 +18,11 @@ class CompressionWorker final : public Napi::AsyncWorker {
1718
: Napi::AsyncWorker{callback, "compression worker"}, m_worker(worker), m_result{} {}
1819

1920
protected:
20-
void Execute() final {
21+
void Execute() {
2122
m_result = m_worker();
2223
}
2324

24-
void OnOK() final {
25+
void OnOK() {
2526
if (!m_result.has_value()) {
2627
Callback().Call({Napi::Error::New(Env(),
2728
"zstd runtime error - async worker finished without "
@@ -41,4 +42,5 @@ class CompressionWorker final : public Napi::AsyncWorker {
4142
std::optional<std::vector<uint8_t>> m_result;
4243
};
4344

45+
} // namespace mongodb_zstd
4446
#endif

addon/zstd.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
#include <napi.h>
44

5-
#include <string>
65
#include <vector>
76

87
#include "compression.h"
98
#include "compression_worker.h"
109

1110
using namespace Napi;
1211

12+
namespace mongodb_zstd {
1313
void Compress(const Napi::CallbackInfo& info) {
1414
// Argument handling happens in JS
1515
if (info.Length() != 3) {
16-
std::string error_message = "Expected three arguments.";
16+
const char* error_message = "Expected three arguments.";
1717
throw TypeError::New(info.Env(), error_message);
1818
}
1919

@@ -25,7 +25,7 @@ void Compress(const Napi::CallbackInfo& info) {
2525

2626
CompressionWorker* worker =
2727
new CompressionWorker(callback, [data = std::move(data), compression_level] {
28-
return Compression::compress(data, compression_level);
28+
return mongodb_zstd::compress(data, compression_level);
2929
});
3030

3131
worker->Queue();
@@ -34,7 +34,7 @@ void Compress(const Napi::CallbackInfo& info) {
3434
void Decompress(const CallbackInfo& info) {
3535
// Argument handling happens in JS
3636
if (info.Length() != 2) {
37-
std::string error_message = "Expected two argument.";
37+
const char* error_message = "Expected two argument.";
3838
throw TypeError::New(info.Env(), error_message);
3939
}
4040

@@ -44,7 +44,7 @@ void Decompress(const CallbackInfo& info) {
4444
const Napi::Function& callback = info[1].As<Function>();
4545

4646
CompressionWorker* worker = new CompressionWorker(
47-
callback, [data = std::move(data)] { return Compression::decompress(data); });
47+
callback, [data = std::move(data)] { return mongodb_zstd::decompress(data); });
4848

4949
worker->Queue();
5050
}
@@ -56,3 +56,6 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
5656
}
5757

5858
NODE_API_MODULE(zstd, Init)
59+
60+
} // namespace mongodb_zstd
61+

etc/install-zstd.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
#!/bin/sh
22
set -o xtrace
33

44
clean_deps() {

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12
const zstd = require('bindings')('zstd');
23
const { promisify } = require('util');
34

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
"node": ">= 16.20.1"
3030
},
3131
"scripts": {
32-
"prepare": "npm run compile",
3332
"compile": "node-gyp rebuild",
34-
3533
"test": "mocha test/index.test.js",
3634
"install-zstd": "bash etc/install-zstd.sh",
3735
"check:eslint": "ESLINT_USE_FLAT_CONFIG=false eslint *ts lib/ .*.json",

0 commit comments

Comments
 (0)