Skip to content

Commit

Permalink
support node12
Browse files Browse the repository at this point in the history
  • Loading branch information
pierreinglebert committed Oct 19, 2019
1 parent 7d52e2c commit e4c7cf0
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 195 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ branches:
env:
matrix:
- NODE_VERSION="6"
- NODE_VERSION="7"
- NODE_VERSION="8"
- NODE_VERSION="9"
- NODE_VERSION="10"
- NODE_VERSION="11"
- NODE_VERSION="12"
global:
- secure: IxORreMUlF3CYU6129/JAQ00ZMR+yxrD8x+nAka8T+qQ8MI3OBkONwYeOXknE0PDgWvD315gPK9KQXSplruEuz8mmxjUJNOzvAikUMF7hVUIl79hSwLDkFZ1MC0rV90dbb7hOlxD1EjqASqTbEJPNarDzIcDxf/G6cubPgnxNbE=
- secure: eGf5iU0ixKUhp4z+/o8/6A7lvL33nZFCS510gJzytgh0qpACHSYz+2KzNT+ddYRE2Mobj3WPqxXRq8mtMAlyuXogwQhmnYOBtskSkPUK+u5DhX+s1IQHu7DyGfx+h44jlOX2D3peKL0HOLl+SPDkzAt4Xec/W3VNPHolQDy9Tjg=
Expand Down
29 changes: 14 additions & 15 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,27 @@ environment:
node_pre_gyp_secretAccessKey:
secure: aqsNf2cyJ0hpprZZvwJ3bLeUeFjZ0OhbJSulfs9VLA4rqmADnnbU//aL/NBoXQQJ
matrix:
- nodejs_version: "6"
platform: x64
- nodejs_version: "7"
platform: x64
- nodejs_version: "8"
platform: x64
- nodejs_version: "9"
platform: x64
- nodejs_version: "10"
platform: x64
- nodejs_version: "11"
platform: x64
- nodejs_version: "8"
nodejs_arch: "x86"
- nodejs_version: "8"
nodejs_arch: "x64"
- nodejs_version: "10"
nodejs_arch: "x86"
- nodejs_version: "10"
nodejs_arch: "x64"
- nodejs_version: "12"
nodejs_arch: "x86"
- nodejs_version: "12"
nodejs_arch: "x64"

matrix:
fast_finish: true

install:
# Get zopfli submodule
- git submodule update --init --recursive
- ps: Install-Product node $env:nodejs_version $env:platform
# Install newer npm
- npm install -g npm@3
- ps: Get-WmiObject Win32_Processor
- ps: Install-Product node $env:nodejs_version $env:nodejs_arch
- ps: $env:path = $env:appdata + "\npm;" + $env:path

# work around an issue with node-gyp v3.3.1 and node 4x
Expand Down
9 changes: 5 additions & 4 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
'configurations': {
'Debug': {
'cflags': ['-g3', '-O0'],
'cflags': ['-g3', '-O0', '-fno-exceptions'],
'msvs_settings': {
'VCCLCompilerTool': {
'BasicRuntimeChecks': 3, # /RTC1
Expand Down Expand Up @@ -48,11 +48,12 @@
},

"target_name": "<(module_name)",
'lflags': ['-lm'],
"lflags": ["-lm"],
"defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
"include_dirs": [
"zopfli/src/zopfli",
"zopfli/src/zopflipng",
"<!(node -e \"require('nan')\")"
"<!@(node -p \"require('node-addon-api').include\")"
],
"sources": [
"src/zopfli-binding.cc",
Expand Down Expand Up @@ -81,7 +82,7 @@
{
"target_name": "action_after_build",
"type": "none",
"dependencies": ["<(module_name)"],
"dependencies": ["<!(node -p \"require('node-addon-api').gyp\")", "<(module_name)"],
"copies": [
{
"files": ["<(PRODUCT_DIR)/<(module_name).node"],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"dependencies": {
"commander": "^2.20.0",
"defaults": "^1.0.3",
"nan": "^2.13.2",
"node-addon-api": "*",
"node-pre-gyp": "^0.13.0"
},
"devDependencies": {
Expand Down
132 changes: 62 additions & 70 deletions src/png/zopflipng.cc
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
#include <node.h>
#include <v8.h>
#include "nan.h"
#include <napi.h>

#include "lodepng/lodepng.h"
#include "zopflipng_lib.h"

using namespace v8;
using namespace Napi;

void parseOptions(const Local<Object>& options, ZopfliPNGOptions& png_options) {
Local<String> option_name;
Local<Value> option_value;
void parseOptions(const Napi::Object& options, ZopfliPNGOptions& png_options) {
Napi::Value option_value;
const Napi::Env env = options.Env();

if(!options.IsEmpty()) {

// Allow altering hidden colors of fully transparent pixels
option_name = Nan::New<String>("lossy_transparent").ToLocalChecked();
if (Nan::Has(options, option_name).FromJust()) {
option_value = Nan::Get(options, option_name).ToLocalChecked();
if(!option_value->IsNumber()) {
Nan::ThrowTypeError("Wrong type for option 'lossy_transparent'");
if (options.Has("lossy_transparent")) {
option_value = options.Get("lossy_transparent");
if(!option_value.IsNumber()) {
Napi::TypeError::New(env, "Wrong type for option 'lossy_transparent'").ThrowAsJavaScriptException();

}
png_options.lossy_transparent = option_value->Int32Value();
png_options.lossy_transparent = option_value.As<Napi::Number>().Int32Value();
}

// Convert 16-bit per channel images to 8-bit per channel
option_name = Nan::New<String>("lossy_8bit").ToLocalChecked();
if (Nan::Has(options, option_name).FromJust()) {
option_value = Nan::Get(options, option_name).ToLocalChecked();
if(!option_value->IsNumber()) {
Nan::ThrowTypeError("Wrong type for option 'lossy_8bit'");
if (options.Has("lossy_8bit")) {
option_value = options.Get("lossy_8bit");
if(!option_value.IsNumber()) {
Napi::TypeError::New(env, "Wrong type for option 'lossy_8bit'").ThrowAsJavaScriptException();

}
png_options.lossy_8bit = option_value->Int32Value();
png_options.lossy_8bit = option_value.As<Napi::Number>().Int32Value();
}

// Filter strategies to try
//"zero", "one", "two", "three", "four", "minimum", "entropy", "predefined", "brute"
option_name = Nan::New<String>("filter_strategies").ToLocalChecked();
Local<Value> fieldValue = Nan::Get(options, option_name).ToLocalChecked();
if(!fieldValue->IsUndefined() && !fieldValue->IsNull()) {
if(fieldValue->IsArray()) {
Handle<Array> filter_strategies = Handle<Array>::Cast(fieldValue);
for (uint32_t i = 0; i < filter_strategies->Length(); i++) {
std::string strStrategy(*Nan::Utf8String(filter_strategies->Get(i)->ToString()));
Napi::Value fieldValue = options.Get("filter_strategies");
if(!fieldValue.IsUndefined() && !fieldValue.IsNull()) {
if(fieldValue.IsArray()) {
Array filter_strategies = fieldValue.As<Array>();
for (uint32_t i = 0; i < filter_strategies.Length(); i++) {
std::string strStrategy(filter_strategies.Get(i).ToString());
ZopfliPNGFilterStrategy strategy = kStrategyZero;
if(strStrategy.compare("zero") == 0) { strategy = kStrategyZero; }
else if(strStrategy.compare("one") == 0) { strategy = kStrategyOne; }
Expand All @@ -53,92 +49,88 @@ void parseOptions(const Local<Object>& options, ZopfliPNGOptions& png_options) {
else if(strStrategy.compare("predefined") == 0) { strategy = kStrategyPredefined; }
else if(strStrategy.compare("bruteforce") == 0) { strategy = kStrategyBruteForce; }
else {
Nan::ThrowTypeError((std::string("Wrong strategy : ") + strStrategy).c_str());
Napi::TypeError::New(options.Env(), (std::string("Wrong strategy : ") + strStrategy).c_str()).ThrowAsJavaScriptException();
}
png_options.filter_strategies.push_back(strategy);
}
} else {
//Wrong
Nan::ThrowTypeError("Wrong type for option 'filter_strategies'");
Napi::TypeError::New(env, "Wrong type for option 'filter_strategies'").ThrowAsJavaScriptException();
}
}

// Automatically choose filter strategy using less good compression
option_name = Nan::New<String>("auto_filter_strategy").ToLocalChecked();
if (Nan::Has(options, option_name).FromJust()) {
option_value = Nan::Get(options, option_name).ToLocalChecked();
if(!option_value->IsNumber()) {
Nan::ThrowTypeError("Wrong type for option 'auto_filter_strategy'");
if (options.Has("auto_filter_strategy")) {
option_value = options.Get("auto_filter_strategy");
if(!option_value.IsNumber()) {
Napi::TypeError::New(env, "Wrong type for option 'auto_filter_strategy'").ThrowAsJavaScriptException();
}
png_options.auto_filter_strategy = option_value->Int32Value();
png_options.auto_filter_strategy = option_value.As<Napi::Number>().Int32Value();
}

// PNG chunks to keep
// chunks to literally copy over from the original PNG to the resulting one
option_name = Nan::New<String>("use_zopfli").ToLocalChecked();
if (Nan::Has(options, option_name).FromJust()) {
option_value = Nan::Get(options, option_name).ToLocalChecked();
if(!option_value->IsNumber()) {
Nan::ThrowTypeError("Wrong type for option 'use_zopfli'");
if (options.Has("use_zopfli")) {
option_value = options.Get("use_zopfli");
if(!option_value.IsNumber()) {
Napi::TypeError::New(env, "Wrong type for option 'use_zopfli'").ThrowAsJavaScriptException();
}
png_options.use_zopfli = option_value->Int32Value();
png_options.use_zopfli = option_value.As<Napi::Number>().Int32Value();
}

// Zopfli number of iterations
option_name = Nan::New<String>("num_iterations").ToLocalChecked();
if (Nan::Has(options, option_name).FromJust()) {
option_value = Nan::Get(options, option_name).ToLocalChecked();
if(!option_value->IsNumber()) {
Nan::ThrowTypeError("Wrong type for option 'num_iterations'");
if (options.Has("num_iterations")) {
option_value = options.Get("num_iterations");
if(!option_value.IsNumber()) {
Napi::TypeError::New(env, "Wrong type for option 'num_iterations'").ThrowAsJavaScriptException();
}
png_options.num_iterations = option_value->Int32Value();
png_options.num_iterations = option_value.As<Napi::Number>().Int32Value();
}

// Zopfli number of iterations on images > 200 KiB
option_name = Nan::New<String>("num_iterations_large").ToLocalChecked();
if (Nan::Has(options, option_name).FromJust()) {
option_value = Nan::Get(options, option_name).ToLocalChecked();
if(!option_value->IsNumber()) {
Nan::ThrowTypeError("Wrong type for option 'num_iterations_large'");
if (options.Has("num_iterations_large")) {
option_value = options.Get("num_iterations_large");
if(!option_value.IsNumber()) {
Napi::TypeError::New(env, "Wrong type for option 'num_iterations_large'").ThrowAsJavaScriptException();
}
png_options.num_iterations_large = option_value->Int32Value();
png_options.num_iterations_large = option_value.As<Napi::Number>().Int32Value();
}

// Split chunk strategy none, first, last, both
std::string strStrategy;
option_name = Nan::New<String>("block_split_strategy").ToLocalChecked();
if (Nan::Has(options, option_name).FromJust()) {
option_value = Nan::Get(options, option_name).ToLocalChecked();
if(!option_value->IsString()) {
Nan::ThrowTypeError("Wrong type for option 'block_split_strategy'");
if (options.Has("block_split_strategy")) {
option_value = options.Get("block_split_strategy");
if(!option_value.IsString()) {
Napi::TypeError::New(env, "Wrong type for option 'block_split_strategy'").ThrowAsJavaScriptException();
}
if(strStrategy.compare("none") == 0) { png_options.block_split_strategy = 0; }
else if(strStrategy.compare("first") == 0) { png_options.block_split_strategy = 1; }
else if(strStrategy.compare("last") == 0) { png_options.block_split_strategy = 2; }
else if(strStrategy.compare("both") == 0) { png_options.block_split_strategy = 3; }
else {
Nan::ThrowTypeError("Wrong value for option 'block_split_strategy'");
Napi::TypeError::New(env, "Wrong value for option 'block_split_strategy'").ThrowAsJavaScriptException();
}
}
}
}


NAN_METHOD(PNGDeflate) {
if(info.Length() < 1 || !info[0]->IsString()) {
Nan::ThrowTypeError("First argument must be a string");
Napi::Value PNGDeflate(const Napi::CallbackInfo& info) {
const Napi::Env env = info.Env();
if(info.Length() < 1 || !info[0].IsString()) {
Napi::TypeError::New(env, "First argument must be a string").ThrowAsJavaScriptException();
}
std::string imageName(*Nan::Utf8String(info[0]->ToString()));
std::string imageName(info[0].As<Napi::String>().Utf8Value().c_str());

if(info.Length() < 2 || !info[1]->IsString()) {
Nan::ThrowTypeError("First argument must be a string");
if(info.Length() < 2 || !info[1].IsString()) {
Napi::TypeError::New(env, "First argument must be a string").ThrowAsJavaScriptException();
}
std::string out_filename(*Nan::Utf8String(info[1]->ToString()));
std::string out_filename(info[1].As<Napi::String>().Utf8Value().c_str());

ZopfliPNGOptions png_options;

if(info.Length() >= 2 && info[2]->IsObject()) {
Local<Object> options = info[2]->ToObject();
if(info.Length() >= 2 && info[2].IsObject()) {
Napi::Object options = info[2].ToObject();
parseOptions(options, png_options);
}

Expand All @@ -165,5 +157,5 @@ NAN_METHOD(PNGDeflate) {
lodepng::save_file(resultpng, out_filename);
}
}
info.GetReturnValue().Set(Nan::New<v8::Integer>(error));
}
return Napi::Number::New(env, error);
}
6 changes: 3 additions & 3 deletions src/png/zopflipng.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "nan.h"
#include "napi.h"

#ifndef NODE_ZOPFLI_PNG_H_
#define NODE_ZOPFLI_PNG_H_

NAN_METHOD(PNGDeflate);
Napi::Value PNGDeflate(const Napi::CallbackInfo& info);

#endif
#endif

0 comments on commit e4c7cf0

Please sign in to comment.