node/test/addons/zlib-binding/binding.cc
Daniel Bevenius d58a0c6285 test: fix NewFromUtf8 compiler warning
Currently there are a number of compiler warnings like the following:

../binding.cc:6:41:
warning: 'NewFromUtf8' is deprecated:
Use maybe version [-Wdeprecated-declarations]
  args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world"));
                                        ^
/node/deps/v8/include/v8.h:2883:10:
note: 'NewFromUtf8' has been explicitly marked deprecated here
  static V8_DEPRECATE_SOON(
         ^
/node/deps/v8/include/v8config.h:341:29:
note: expanded from macro 'V8_DEPRECATE_SOON'
  declarator __attribute__((deprecated(message)))
                            ^

This commit updates the code to use the maybe versions.

PR-URL: https://github.com/nodejs/node/pull/24216
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2018-11-09 21:26:31 -08:00

59 lines
1.8 KiB
C++

#include <node.h>
#include <node_buffer.h>
#include <assert.h>
#include <zlib.h>
namespace {
inline void CompressBytes(const v8::FunctionCallbackInfo<v8::Value>& info) {
assert(info[0]->IsArrayBufferView());
auto view = info[0].As<v8::ArrayBufferView>();
auto byte_offset = view->ByteOffset();
auto byte_length = view->ByteLength();
assert(view->HasBuffer());
auto buffer = view->Buffer();
auto contents = buffer->GetContents();
auto data = static_cast<unsigned char*>(contents.Data()) + byte_offset;
Bytef buf[1024];
z_stream stream;
stream.zalloc = nullptr;
stream.zfree = nullptr;
int err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
-15, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
assert(err == Z_OK);
stream.avail_in = byte_length;
stream.next_in = data;
stream.avail_out = sizeof(buf);
stream.next_out = buf;
err = deflate(&stream, Z_FINISH);
assert(err == Z_STREAM_END);
auto result = node::Buffer::Copy(info.GetIsolate(),
reinterpret_cast<const char*>(buf),
sizeof(buf) - stream.avail_out);
deflateEnd(&stream);
info.GetReturnValue().Set(result.ToLocalChecked());
}
inline void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> context) {
auto isolate = context->GetIsolate();
auto key = v8::String::NewFromUtf8(
isolate, "compressBytes", v8::NewStringType::kNormal).ToLocalChecked();
auto value = v8::FunctionTemplate::New(isolate, CompressBytes)
->GetFunction(context)
.ToLocalChecked();
assert(exports->Set(context, key, value).IsJust());
}
} // anonymous namespace
NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)