Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d401e55

Browse filesBrowse files
addaleaxevanlucas
authored andcommitted
test: add an zlib binding addon test
Add a test addon that makes use of the zlib implementation bundled with node, checking that a compression/decompression round-trip works. This is largely based on the already-existing OpenSSL addon. Fixes: #7535 PR-URL: #8039 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 1f9fbad commit d401e55
Copy full SHA for d401e55

File tree

Expand file treeCollapse file tree

3 files changed

+75
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+75
-0
lines changed
Open diff view settings
Collapse file
+55Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <node.h>
2+
#include <node_buffer.h>
3+
#include <assert.h>
4+
#include <zlib.h>
5+
6+
namespace {
7+
8+
inline void CompressBytes(const v8::FunctionCallbackInfo<v8::Value>& info) {
9+
assert(info[0]->IsArrayBufferView());
10+
auto view = info[0].As<v8::ArrayBufferView>();
11+
auto byte_offset = view->ByteOffset();
12+
auto byte_length = view->ByteLength();
13+
assert(view->HasBuffer());
14+
auto buffer = view->Buffer();
15+
auto contents = buffer->GetContents();
16+
auto data = static_cast<unsigned char*>(contents.Data()) + byte_offset;
17+
18+
Bytef buf[1024];
19+
20+
z_stream stream;
21+
stream.zalloc = nullptr;
22+
stream.zfree = nullptr;
23+
24+
int err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
25+
-15, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
26+
assert(err == Z_OK);
27+
28+
stream.avail_in = byte_length;
29+
stream.next_in = data;
30+
stream.avail_out = sizeof(buf);
31+
stream.next_out = buf;
32+
err = deflate(&stream, Z_FINISH);
33+
assert(err == Z_STREAM_END);
34+
35+
auto result = node::Buffer::Copy(info.GetIsolate(),
36+
reinterpret_cast<const char*>(buf),
37+
sizeof(buf) - stream.avail_out);
38+
39+
deflateEnd(&stream);
40+
41+
info.GetReturnValue().Set(result.ToLocalChecked());
42+
}
43+
44+
inline void Initialize(v8::Local<v8::Object> exports,
45+
v8::Local<v8::Value> module,
46+
v8::Local<v8::Context> context) {
47+
auto isolate = context->GetIsolate();
48+
auto key = v8::String::NewFromUtf8(isolate, "compressBytes");
49+
auto value = v8::FunctionTemplate::New(isolate, CompressBytes)->GetFunction();
50+
assert(exports->Set(context, key, value).IsJust());
51+
}
52+
53+
} // anonymous namespace
54+
55+
NODE_MODULE_CONTEXT_AWARE(binding, Initialize)
Collapse file
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'sources': ['binding.cc'],
6+
'include_dirs': ['../../../deps/zlib'],
7+
},
8+
]
9+
}
Collapse file

‎test/addons/zlib-binding/test.js‎

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
require('../../common');
4+
const assert = require('assert');
5+
const zlib = require('zlib');
6+
const binding = require('./build/Release/binding');
7+
8+
const input = Buffer.from('Hello, World!');
9+
10+
const output = zlib.inflateRawSync(binding.compressBytes(input));
11+
assert.deepStrictEqual(input, output);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.