<commit_before>#include <node.h>
#include <node_buffer.h>
#include <nan.h>
#include <uv.h>
#include "storj.h"

using namespace v8;
using namespace Nan;

void Timestamp(const v8::FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();

    uint64_t timestamp = storj_util_timestamp();
    Local<Number> timestamp_local = Number::New(isolate, timestamp);

    args.GetReturnValue().Set(timestamp_local);
}

void MnemonicCheck(const v8::FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();

    String::Utf8Value str(args[0]);
    const char *mnemonic = *str;

    bool mnemonic_check_result = storj_mnemonic_check(mnemonic);
    Local<Boolean> mnemonic_check_result_local = Boolean::New(isolate, mnemonic_check_result);

    args.GetReturnValue().Set(mnemonic_check_result_local);
}

void GetInfoCallback(uv_work_t *work_req, int status) {
    Nan::HandleScope scope;

    json_request_t *req = (json_request_t *) work_req->data;

    Nan::Callback *callback = (Nan::Callback*)req->handle;

    const char *result_str = json_object_to_json_string(req->response);

    Local<Value> argv[] = {
        Nan::Null(),
        v8::JSON::Parse(Nan::New(result_str).ToLocalChecked())
    };

    callback->Call(2, argv);
    free(req);
    free(work_req);
}

void GetInfo(const Nan::FunctionCallbackInfo<Value>& args) {
    if (args.This()->InternalFieldCount() != 1)
    {
        Nan::ThrowError("Environment not available for instance");
    }

    storj_env_t *env = (storj_env_t *)args.This()->GetAlignedPointerFromInternalField(0);

    Nan::Callback *callback = new Nan::Callback(args[0].As<Function>());

    storj_bridge_get_info(env, (void *) callback, GetInfoCallback);
}

Local<Date> StrToDate(const char *dateStr) {
  Local<Date> tmp = Nan::New<Date>(0).ToLocalChecked();
  v8::Local<v8::Function> cons = v8::Local<v8::Function>::Cast(
    Nan::Get(tmp, Nan::New("constructor").ToLocalChecked()).ToLocalChecked()
  );
  const int argc = 1;
  v8::Local<v8::Value> argv[argc] = {Nan::New(dateStr).ToLocalChecked()};
  v8::Local<v8::Date> date = v8::Local<v8::Date>::Cast(
    Nan::NewInstance(cons, argc, argv).ToLocalChecked()
  );
  return date;
}

void GetBucketsCallback(uv_work_t *work_req, int status) {
    Nan::HandleScope scope;

    get_buckets_request_t *req = (get_buckets_request_t *) work_req->data;

    Nan::Callback *callback = (Nan::Callback*)req->handle;
    Local<Array> buckets = Nan::New<Array>();
    for (uint8_t i=0; i<req->total_buckets; i++) {
      Local<Object> bucket = Nan::New<Object>();
      bucket->Set(Nan::New("name").ToLocalChecked(), Nan::New(req->buckets[i].name).ToLocalChecked());
      bucket->Set(Nan::New("created").ToLocalChecked(), StrToDate(req->buckets[i].created));
      bucket->Set(Nan::New("id").ToLocalChecked(), Nan::New(req->buckets[i].id).ToLocalChecked());
      bucket->Set(Nan::New("decrypted").ToLocalChecked(), Nan::New<Boolean>(req->buckets[i].decrypted));
      buckets->Set(i, bucket);
    }
    Local<Value> argv[] = {
        Nan::Null(),
        buckets
    };
    callback->Call(2, argv);
    free(req);
    free(work_req);
}

void GetBuckets(const Nan::FunctionCallbackInfo<Value>& args) {
    if (args.This()->InternalFieldCount() != 1)
    {
        Nan::ThrowError("Environment not available for instance");
    }

    storj_env_t *env = (storj_env_t *)args.This()->GetAlignedPointerFromInternalField(0);

    Nan::Callback *callback = new Nan::Callback(args[0].As<Function>());

    storj_bridge_get_buckets(env, (void *) callback, GetBucketsCallback);
}

void Environment(const v8::FunctionCallbackInfo<Value>& args) {
    Nan::EscapableHandleScope scope;

    v8::Local<v8::Object> options = args[0].As<v8::Object>();

    v8::Local<v8::String> bridgeUrl = options->Get(Nan::New("bridgeUrl").ToLocalChecked()).As<v8::String>();
    v8::Local<v8::String> bridgeUser = options->Get(Nan::New("bridgeUser").ToLocalChecked()).As<v8::String>();
    v8::Local<v8::String> bridgePass = options->Get(Nan::New("bridgePass").ToLocalChecked()).As<v8::String>();
    v8::Local<v8::String> encryptionKey = options->Get(Nan::New("encryptionKey").ToLocalChecked()).As<v8::String>();

    v8::Local<v8::FunctionTemplate> constructor = Nan::New<v8::FunctionTemplate>();
    constructor->SetClassName(Nan::New("Environment").ToLocalChecked());
    constructor->InstanceTemplate()->SetInternalFieldCount(1);

    Nan::SetPrototypeMethod(constructor, "getInfo", GetInfo);
    Nan::SetPrototypeMethod(constructor, "getBuckets", GetBuckets);

    Nan::MaybeLocal<v8::Object> maybeInstance;
    v8::Local<v8::Object> instance;

    v8::Local<v8::Value> argv[] = {};<commit_msg>fix tabs<commit_after>#include <node.h>
#include <node_buffer.h>
#include <nan.h>
#include <uv.h>
#include "storj.h"

using namespace v8;
using namespace Nan;

void Timestamp(const v8::FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();

    uint64_t timestamp = storj_util_timestamp();
    Local<Number> timestamp_local = Number::New(isolate, timestamp);

    args.GetReturnValue().Set(timestamp_local);
}

void MnemonicCheck(const v8::FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();

    String::Utf8Value str(args[0]);
    const char *mnemonic = *str;

    bool mnemonic_check_result = storj_mnemonic_check(mnemonic);
    Local<Boolean> mnemonic_check_result_local = Boolean::New(isolate, mnemonic_check_result);

    args.GetReturnValue().Set(mnemonic_check_result_local);
}

void GetInfoCallback(uv_work_t *work_req, int status) {
    Nan::HandleScope scope;

    json_request_t *req = (json_request_t *) work_req->data;

    Nan::Callback *callback = (Nan::Callback*)req->handle;

    const char *result_str = json_object_to_json_string(req->response);

    Local<Value> argv[] = {
        Nan::Null(),
        v8::JSON::Parse(Nan::New(result_str).ToLocalChecked())
    };

    callback->Call(2, argv);
    free(req);
    free(work_req);
}

void GetInfo(const Nan::FunctionCallbackInfo<Value>& args) {
    if (args.This()->InternalFieldCount() != 1) {
        Nan::ThrowError("Environment not available for instance");
    }

    storj_env_t *env = (storj_env_t *)args.This()->GetAlignedPointerFromInternalField(0);

    Nan::Callback *callback = new Nan::Callback(args[0].As<Function>());

    storj_bridge_get_info(env, (void *) callback, GetInfoCallback);
}

Local<Date> StrToDate(const char *dateStr) {
    Local<Date> tmp = Nan::New<Date>(0).ToLocalChecked();
    v8::Local<v8::Function> cons = v8::Local<v8::Function>::Cast(
        Nan::Get(tmp, Nan::New("constructor").ToLocalChecked()).ToLocalChecked()
    );
    const int argc = 1;
    v8::Local<v8::Value> argv[argc] = {Nan::New(dateStr).ToLocalChecked()};
    v8::Local<v8::Date> date = v8::Local<v8::Date>::Cast(
        Nan::NewInstance(cons, argc, argv).ToLocalChecked()
    );
    return date;
}

void GetBucketsCallback(uv_work_t *work_req, int status) {
    Nan::HandleScope scope;

    get_buckets_request_t *req = (get_buckets_request_t *) work_req->data;

    Nan::Callback *callback = (Nan::Callback*)req->handle;
    Local<Array> buckets = Nan::New<Array>();
    for (uint8_t i=0; i<req->total_buckets; i++) {
        Local<Object> bucket = Nan::New<Object>();
        bucket->Set(Nan::New("name").ToLocalChecked(), Nan::New(req->buckets[i].name).ToLocalChecked());
        bucket->Set(Nan::New("created").ToLocalChecked(), StrToDate(req->buckets[i].created));
        bucket->Set(Nan::New("id").ToLocalChecked(), Nan::New(req->buckets[i].id).ToLocalChecked());
        bucket->Set(Nan::New("decrypted").ToLocalChecked(), Nan::New<Boolean>(req->buckets[i].decrypted));
        buckets->Set(i, bucket);
    }
    Local<Value> argv[] = {
        Nan::Null(),
        buckets
    };
    callback->Call(2, argv);
    free(req);
    free(work_req);
}

void GetBuckets(const Nan::FunctionCallbackInfo<Value>& args) {
    if (args.This()->InternalFieldCount() != 1) {
        Nan::ThrowError("Environment not available for instance");
    }

    storj_env_t *env = (storj_env_t *)args.This()->GetAlignedPointerFromInternalField(0);

    Nan::Callback *callback = new Nan::Callback(args[0].As<Function>());

    storj_bridge_get_buckets(env, (void *) callback, GetBucketsCallback);
}

void Environment(const v8::FunctionCallbackInfo<Value>& args) {
    Nan::EscapableHandleScope scope;

    v8::Local<v8::Object> options = args[0].As<v8::Object>();

    v8::Local<v8::String> bridgeUrl = options->Get(Nan::New("bridgeUrl").ToLocalChecked()).As<v8::String>();
    v8::Local<v8::String> bridgeUser = options->Get(Nan::New("bridgeUser").ToLocalChecked()).As<v8::String>();
    v8::Local<v8::String> bridgePass = options->Get(Nan::New("bridgePass").ToLocalChecked()).As<v8::String>();
    v8::Local<v8::String> encryptionKey = options->Get(Nan::New("encryptionKey").ToLocalChecked()).As<v8::String>();

    v8::Local<v8::FunctionTemplate> constructor = Nan::New<v8::FunctionTemplate>();
    constructor->SetClassName(Nan::New("Environment").ToLocalChecked());
    constructor->InstanceTemplate()->SetInternalFieldCount(1);

    Nan::SetPrototypeMethod(constructor, "getInfo", GetInfo);
    Nan::SetPrototypeMethod(constructor, "getBuckets", GetBuckets);

    Nan::MaybeLocal<v8::Object> maybeInstance;
    v8::Local<v8::Object> instance;

    v8::Local<v8::Value> argv[] = {};
{'fraction_non_alphanumeric': 0.17759300714209572, 'fraction_numerical': 0.011939025690224923, 'mean_word_length': 5.254666666666667, 'pattern_counts': {'":': 0, '<': 91, '<?xml version=': 0, '>': 145, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '854564', 'n_tokens_mistral': 3271, 'n_tokens_neox': 3101, 'n_words': 469}