Skip to content
This repository was archived by the owner on Jun 6, 2021. It is now read-only.

napi #70

Merged
merged 16 commits into from
Jun 17, 2018
Merged

napi #70

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@
"src/common/stream_coder.cc",

"src/dec/stream_decode.cc",
"src/dec/stream_decode_worker.cc",
"src/dec/stream_decode_tasks.cc",

"src/enc/stream_encode.cc",
"src/enc/stream_encode_worker.cc",
"src/enc/stream_encode_tasks.cc",

"src/iltorb.cc"
],
"include_dirs": [
"<!(node -e \"require('nan')\")",
"brotli/c/include"
],
"defines": ["NOMINMAX"],
Expand Down
24 changes: 11 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ const { StreamEncode, StreamDecode } = require('./build/bindings/iltorb.node');
const { Transform } = require('stream');

class TransformStreamEncode extends Transform {
constructor(params={}, sync=false) {
constructor(params={}, async=true) {
super();
this.sync = sync;
this.encoding = false;
this.corked = false;
this.flushing = false;
this.encoder = new StreamEncode(params);
this.encoder = new StreamEncode(async, params);
}

_transform(chunk, encoding, next) {
Expand All @@ -32,7 +31,7 @@ class TransformStreamEncode extends Transform {
if (this.flushing) {
this.flush(true);
}
}, !this.sync);
});
}

_flush(done) {
Expand All @@ -42,7 +41,7 @@ class TransformStreamEncode extends Transform {
}
this._push(output);
done();
}, !this.sync);
});
}

_push(output) {
Expand Down Expand Up @@ -77,15 +76,14 @@ class TransformStreamEncode extends Transform {
this.corked = false;
this.flushing = false;
this.uncork();
}, true);
});
}
}

class TransformStreamDecode extends Transform {
constructor(sync=false) {
constructor(async=true) {
super();
this.sync = sync;
this.decoder = new StreamDecode();
this.decoder = new StreamDecode(async);
}

_transform(chunk, encoding, next) {
Expand All @@ -95,7 +93,7 @@ class TransformStreamDecode extends Transform {
}
this._push(output);
next();
}, !this.sync);
});
}

_flush(done) {
Expand All @@ -105,7 +103,7 @@ class TransformStreamDecode extends Transform {
}
this._push(output);
done();
}, !this.sync);
});
}

_push(output) {
Expand Down Expand Up @@ -214,7 +212,7 @@ function compressSync(input, params) {
params = {};
}
params = Object.assign({}, params, {size_hint: input.length});
const stream = new TransformStreamEncode(params, true);
const stream = new TransformStreamEncode(params, false);
const chunks = [];
let length = 0;
stream.on('error', function(e) {
Expand All @@ -232,7 +230,7 @@ function decompressSync(input) {
if (!Buffer.isBuffer(input)) {
throw new Error('Brotli input is not a buffer.');
}
const stream = new TransformStreamDecode(true);
const stream = new TransformStreamDecode(false);
const chunks = [];
let length = 0;
stream.on('error', function(e) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
],
"dependencies": {
"detect-libc": "^1.0.3",
"nan": "^2.10.0",
"npmlog": "^4.1.2",
"prebuild-install": "^4.0.0",
"which-pm-runs": "^1.0.0"
Expand Down
15 changes: 7 additions & 8 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const PREBUILD_TOKEN = process.env.PREBUILD_TOKEN;
const PUBLISH_BINARY = process.env.PUBLISH_BINARY || false;


function build(runtime, target) {
function build({target, runtime, abi}) {
try {
getTarget(target, runtime);
abi && getTarget(target, runtime);
} catch (err) {
return Promise.resolve();
}
Expand Down Expand Up @@ -42,19 +42,18 @@ function build(runtime, target) {


const builds = [
{ runtime: 'node', target: process.versions.modules }
{ runtime: 'node', target: process.versions.node, abi: false }
];

if (PUBLISH_BINARY) {
builds.push(
{ runtime: 'electron', target: '50' },
{ runtime: 'electron', target: '53' },
{ runtime: 'electron', target: process.versions.modules }
{ runtime: 'electron', target: '50', abi: true },
{ runtime: 'electron', target: '53', abi: true },
{ runtime: 'electron', target: process.versions.modules, abi: true }
);
}

builds
.reduce((promise, item) => {
return promise.then(() => build(item.runtime, item.target)).catch((code) => process.exit(code));
return promise.then(() => build(item)).catch((code) => process.exit(code));
}, Promise.resolve());

16 changes: 9 additions & 7 deletions src/common/allocator.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "allocator.h"
#include <nan.h>

void* Allocator::Alloc(void* opaque, size_t size) {
return static_cast<Allocator*>(opaque)->Alloc(size);
Expand All @@ -21,28 +20,31 @@ Allocator::AllocatedBuffer* Allocator::GetBufferInfo(void* address) {
return static_cast<AllocatedBuffer*>(address) - 1;
}

void Allocator::Free(void* opaque, void* address) {
void Allocator::Free(void* opaque, void* address, napi_env env) {
if (!address) {
return;
}

AllocatedBuffer* buf = GetBufferInfo(address);
int64_t size = buf->size + sizeof(*buf);

if (opaque) {
Allocator* alloc = static_cast<Allocator*>(opaque);
alloc->allocated_unreported_memory -= buf->size + sizeof(*buf);
alloc->allocated_unreported_memory -= size;
} else {
Nan::AdjustExternalMemory(-(buf->size + sizeof(*buf)));
int64_t result;
napi_adjust_external_memory(env, -size, &result);
}

free(buf);
}

void Allocator::Free(void* address) {
Free(this, address);
Free(this, address, NULL);
}

void Allocator::ReportMemoryToV8() {
Nan::AdjustExternalMemory(allocated_unreported_memory);
void Allocator::ReportMemoryToV8(napi_env env) {
int64_t result;
napi_adjust_external_memory(env, allocated_unreported_memory, &result);
allocated_unreported_memory = 0;
}
15 changes: 9 additions & 6 deletions src/common/allocator.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef ILTORB_ALLOCATOR_H
#define ILTORB_ALLOCATOR_H

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <node_api.h>

struct Allocator {
Allocator() : allocated_unreported_memory(0) {}
Expand All @@ -18,15 +18,18 @@ struct Allocator {
void Free(void* address);

static AllocatedBuffer* GetBufferInfo(void* address);
void ReportMemoryToV8();
void ReportMemoryToV8(napi_env env);

// Brotli-style parameter order.
static void* Alloc(void* opaque, size_t size);
static void Free(void* opaque, void* address);
static void Free(void* opaque, void* address) {
Free(opaque, address, NULL);
}
static void Free(void* opaque, void* address, napi_env env);

// Like Free, but in node::Buffer::FreeCallback style.
static void NodeFree(char* address, void* opaque) {
return Free(opaque, address);
static void NodeFree(napi_env env, void* address, void* opaque) {
Free(opaque, address, env);
}
};

Expand Down
27 changes: 12 additions & 15 deletions src/common/stream_coder.cc
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
#include "stream_coder.h"

StreamCoder::StreamCoder() {
}

StreamCoder::~StreamCoder() {
void StreamCoder::ClearPendingOutput(napi_env env) {
size_t n_chunks = pending_output.size();
for (size_t i = 0; i < n_chunks; i++) {
alloc.Free(pending_output[i]);
}

alloc.ReportMemoryToV8();
alloc.ReportMemoryToV8(env);
}

Local<Array> StreamCoder::PendingChunksAsArray() {
void StreamCoder::PendingChunksAsArray(napi_env env, napi_value* arr) {
size_t n_chunks = pending_output.size();
Local<Array> chunks = Nan::New<Array>(n_chunks);

napi_create_array_with_length(env, n_chunks, arr);

for (size_t i = 0; i < n_chunks; i++) {
uint8_t* current = pending_output[i];
Allocator::AllocatedBuffer* buf_info = Allocator::GetBufferInfo(current);
Nan::Set(chunks, i, Nan::NewBuffer(reinterpret_cast<char*>(current),
buf_info->size,
Allocator::NodeFree,
NULL).ToLocalChecked());
uint8_t* chunk = pending_output[i];
Allocator::AllocatedBuffer* buf_info = Allocator::GetBufferInfo(chunk);
napi_value buf;
napi_create_external_buffer(env, buf_info->size, chunk, Allocator::NodeFree, NULL, &buf);
napi_set_element(env, *arr, i, buf);
}
pending_output.clear();

return chunks;
pending_output.clear();
alloc.ReportMemoryToV8(env);
}
13 changes: 5 additions & 8 deletions src/common/stream_coder.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#ifndef STREAM_CODER_H
#define STREAM_CODER_H

#include <vector>
#include <node_api.h>
#include "allocator.h"
#include <nan.h>

using namespace v8;

class StreamCoder : public Nan::ObjectWrap {
class StreamCoder {
public:
Allocator alloc;
std::vector<uint8_t*> pending_output;

Local<Array> PendingChunksAsArray();
protected:
explicit StreamCoder();
~StreamCoder();
void ClearPendingOutput(napi_env env);
void PendingChunksAsArray(napi_env env, napi_value* arr);
};

#endif
Loading