11cb0ef41Sopenharmony_ci#include <node.h> 21cb0ef41Sopenharmony_ci#include <node_buffer.h> 31cb0ef41Sopenharmony_ci#include <zlib.h> 41cb0ef41Sopenharmony_ci#include <assert.h> 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_cinamespace { 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciinline void CompressBytes(const v8::FunctionCallbackInfo<v8::Value>& info) { 91cb0ef41Sopenharmony_ci assert(info[0]->IsArrayBufferView()); 101cb0ef41Sopenharmony_ci auto view = info[0].As<v8::ArrayBufferView>(); 111cb0ef41Sopenharmony_ci auto byte_offset = view->ByteOffset(); 121cb0ef41Sopenharmony_ci auto byte_length = view->ByteLength(); 131cb0ef41Sopenharmony_ci assert(view->HasBuffer()); 141cb0ef41Sopenharmony_ci auto buffer = view->Buffer(); 151cb0ef41Sopenharmony_ci auto contents = buffer->GetBackingStore(); 161cb0ef41Sopenharmony_ci auto data = static_cast<unsigned char*>(contents->Data()) + byte_offset; 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci Bytef buf[1024]; 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci z_stream stream; 211cb0ef41Sopenharmony_ci stream.zalloc = nullptr; 221cb0ef41Sopenharmony_ci stream.zfree = nullptr; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci int err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 251cb0ef41Sopenharmony_ci -15, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); 261cb0ef41Sopenharmony_ci assert(err == Z_OK); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci stream.avail_in = byte_length; 291cb0ef41Sopenharmony_ci stream.next_in = data; 301cb0ef41Sopenharmony_ci stream.avail_out = sizeof(buf); 311cb0ef41Sopenharmony_ci stream.next_out = buf; 321cb0ef41Sopenharmony_ci err = deflate(&stream, Z_FINISH); 331cb0ef41Sopenharmony_ci assert(err == Z_STREAM_END); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci auto result = node::Buffer::Copy(info.GetIsolate(), 361cb0ef41Sopenharmony_ci reinterpret_cast<const char*>(buf), 371cb0ef41Sopenharmony_ci sizeof(buf) - stream.avail_out); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci deflateEnd(&stream); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci info.GetReturnValue().Set(result.ToLocalChecked()); 421cb0ef41Sopenharmony_ci} 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciinline void Initialize(v8::Local<v8::Object> exports, 451cb0ef41Sopenharmony_ci v8::Local<v8::Value> module, 461cb0ef41Sopenharmony_ci v8::Local<v8::Context> context) { 471cb0ef41Sopenharmony_ci auto isolate = context->GetIsolate(); 481cb0ef41Sopenharmony_ci auto key = v8::String::NewFromUtf8( 491cb0ef41Sopenharmony_ci isolate, "compressBytes").ToLocalChecked(); 501cb0ef41Sopenharmony_ci auto value = v8::FunctionTemplate::New(isolate, CompressBytes) 511cb0ef41Sopenharmony_ci ->GetFunction(context) 521cb0ef41Sopenharmony_ci .ToLocalChecked(); 531cb0ef41Sopenharmony_ci assert(exports->Set(context, key, value).IsJust()); 541cb0ef41Sopenharmony_ci} 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci} // anonymous namespace 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ciNODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize) 59