This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
stream_decode.cc
68 lines (55 loc) · 2.15 KB
/
stream_decode.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "stream_decode.h"
#include "stream_decode_worker.h"
using namespace v8;
StreamDecode::StreamDecode(bool isAsync) : isAsync(isAsync), next_in(NULL), available_in(0) {
state = BrotliDecoderCreateInstance(Allocator::Alloc, Allocator::Free, &alloc);
alloc.ReportMemoryToV8();
}
StreamDecode::~StreamDecode() {
BrotliDecoderDestroyInstance(state);
}
void StreamDecode::Init(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("StreamDecode").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "transform", Transform);
Nan::SetPrototypeMethod(tpl, "flush", Flush);
constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
Nan::Set(target, Nan::New("StreamDecode").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
}
NAN_METHOD(StreamDecode::New) {
StreamDecode* obj = new StreamDecode(Nan::To<bool>(info[0]).FromJust());
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
NAN_METHOD(StreamDecode::Transform) {
StreamDecode* obj = ObjectWrap::Unwrap<StreamDecode>(info.Holder());
Local<Object> buffer = Nan::To<Object>(info[0]).ToLocalChecked();
obj->next_in = (const uint8_t*) node::Buffer::Data(buffer);
obj->available_in = node::Buffer::Length(buffer);
Nan::Callback *callback = new Nan::Callback(info[1].As<Function>());
StreamDecodeWorker *worker = new StreamDecodeWorker(callback, obj);
if (Nan::To<bool>(info[2]).FromJust()) {
Nan::AsyncQueueWorker(worker);
} else {
worker->Execute();
worker->WorkComplete();
worker->Destroy();
}
}
NAN_METHOD(StreamDecode::Flush) {
StreamDecode* obj = ObjectWrap::Unwrap<StreamDecode>(info.Holder());
Nan::Callback *callback = new Nan::Callback(info[0].As<Function>());
obj->next_in = nullptr;
obj->available_in = 0;
StreamDecodeWorker *worker = new StreamDecodeWorker(callback, obj);
if (Nan::To<bool>(info[1]).FromJust()) {
Nan::AsyncQueueWorker(worker);
} else {
worker->Execute();
worker->WorkComplete();
worker->Destroy();
}
}
Nan::Persistent<Function> StreamDecode::constructor;