• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hanazuki / node-jsonnet / 26250272240

21 May 2026 08:08PM UTC coverage: 72.706% (+0.6%) from 72.133%
26250272240

Pull #436

github

web-flow
Merge 799349dcb into 2a00d60f7
Pull Request #436: support custom import callback

331 of 624 branches covered (53.04%)

Branch coverage included in aggregate %.

106 of 111 new or added lines in 4 files covered. (95.5%)

588 of 640 relevant lines covered (91.88%)

193.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.63
/src/JsonnetImportCallback.cpp
1
// SPDX-License-Identifier: MIT
2
#include "JsonnetImportCallback.hpp"
3
#include <cstring>
4
#include <stdexcept>
5

6
namespace nodejsonnet {
7

8
  JsonnetImportCallback::JsonnetImportCallback(Napi::Env env, Napi::Function fun)
56✔
9
    : tsfn{ThreadSafeFunction::New(env, fun, "Jsonnet Import Callback", 0, 1)} {
56✔
10
  }
56✔
11

NEW
12
  JsonnetImportCallback::~JsonnetImportCallback() {
×
NEW
13
    this->tsfn.Release();
×
NEW
14
  }
×
15

16
  JsonnetVm::ImportResult JsonnetImportCallback::call(
60✔
17
    std::shared_ptr<JsonnetVm> vm, std::string const &base, std::string const &rel) {
18
    // This function runs in a worker thread and cannot access Node VM.
19

20
    Payload payload(std::move(vm), base, rel);
60!
21
    tsfn.BlockingCall(&payload);
60!
22
    return payload.getFuture().get();
60!
23
  }
68✔
24

25
  void JsonnetImportCallback::resolveResult(Payload *payload, Napi::Value val) {
52✔
26
    auto const obj = val.As<Napi::Object>();
52✔
27
    auto const vm = payload->getVm();
52✔
28

29
    auto const foundHereStr = obj.Get("foundHere").As<Napi::String>().Utf8Value();
52!
30
    auto foundHereBuf = vm->allocBuffer(foundHereStr.size() + 1);
52!
31
    std::memcpy(foundHereBuf.get(), foundHereStr.c_str(), foundHereStr.size() + 1);
52✔
32

33
    auto const contentVal = obj.Get("content");
52!
34
    JsonnetVm::Buffer contentBuf;
52✔
35
    size_t contentLen;
52✔
36

37
    if(contentVal.IsString()) {
52!
38
      auto const str = contentVal.As<Napi::String>().Utf8Value();
36!
39
      contentBuf = vm->allocBuffer(str.size());
36!
40
      std::memcpy(contentBuf.get(), str.data(), str.size());
36✔
41
      contentLen = str.size();
36✔
42
    } else {
36✔
43
      auto const ta = contentVal.As<Napi::TypedArray>();
16!
44
      contentBuf = vm->allocBuffer(ta.ByteLength());
16!
45
      std::memcpy(contentBuf.get(), static_cast<char *>(ta.ArrayBuffer().Data()) + ta.ByteOffset(),
16!
46
        ta.ByteLength());
16!
47
      contentLen = ta.ByteLength();
16!
48
    }
16✔
49

50
    payload->setResult(JsonnetVm::ImportResult{
208!
51
      std::move(foundHereBuf),
52✔
52
      std::move(contentBuf),
52✔
53
      contentLen,
52✔
54
    });
55
  }
52✔
56

57
  void JsonnetImportCallback::callback(
60✔
58
    Napi::Env env, Napi::Function fun, std::nullptr_t *, Payload *payload) {
59
    // This function runs in the Node main thread.
60

61
    auto const base = Napi::String::New(env, payload->getBase());
60✔
62
    auto const rel = Napi::String::New(env, payload->getRel());
60✔
63

64
    auto const result = fun.Call({base, rel});
60✔
65

66
    if(!result.IsPromise()) {
60!
NEW
67
      resolveResult(payload, result);
×
NEW
68
      return;
×
69
    }
70

71
    auto const on_success = Napi::Function::New(
120✔
72
      env,
60✔
73
      [](Napi::CallbackInfo const &info) {
52✔
74
        resolveResult(static_cast<Payload *>(info.Data()), info[0]);
52✔
75
      },
52✔
76
      "onSuccess", payload);
60✔
77

78
    auto const on_failure = Napi::Function::New(
120✔
79
      env,
60✔
80
      [](Napi::CallbackInfo const &info) {
8✔
81
        auto const payload = static_cast<Payload *>(info.Data());
8✔
82
        auto const error = info[0].ToString();
8✔
83
        payload->setError(std::make_exception_ptr(std::runtime_error(error)));
8!
84
      },
8✔
85
      "onFailure", payload);
60✔
86

87
    result.As<Napi::Promise>().Then(on_success, on_failure);
60✔
88
  }
60!
89

90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc