• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <node.h>
2 #include <v8.h>
3 
4 namespace {
5 
6 #define ENCODING_MAP(V) \
7   V(ASCII)              \
8   V(BASE64)             \
9   V(BUFFER)             \
10   V(HEX)                \
11   V(LATIN1)             \
12   V(UCS2)               \
13   V(UTF8)               \
14 
15 static_assert(node::BINARY == node::LATIN1, "BINARY == LATIN1");
16 
ParseEncoding(const v8::FunctionCallbackInfo<v8::Value> & args)17 void ParseEncoding(const v8::FunctionCallbackInfo<v8::Value>& args) {
18   const node::encoding encoding =
19       node::ParseEncoding(args.GetIsolate(), args[0],
20                           static_cast<node::encoding>(-1));
21   const char* encoding_name = "UNKNOWN";
22 #define V(name) if (encoding == node::name) encoding_name = #name;
23   ENCODING_MAP(V)
24 #undef V
25   auto encoding_string =
26       v8::String::NewFromUtf8(args.GetIsolate(), encoding_name,
27                               v8::NewStringType::kNormal)
28       .ToLocalChecked();
29   args.GetReturnValue().Set(encoding_string);
30 }
31 
Initialize(v8::Local<v8::Object> exports)32 void Initialize(v8::Local<v8::Object> exports) {
33   NODE_SET_METHOD(exports, "parseEncoding", ParseEncoding);
34 }
35 
36 }  // anonymous namespace
37 
38 NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
39