• 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(BASE64URL)          \
10   V(BUFFER)             \
11   V(HEX)                \
12   V(LATIN1)             \
13   V(UCS2)               \
14   V(UTF8)               \
15 
16 static_assert(node::BINARY == node::LATIN1, "BINARY == LATIN1");
17 
ParseEncoding(const v8::FunctionCallbackInfo<v8::Value> & args)18 void ParseEncoding(const v8::FunctionCallbackInfo<v8::Value>& args) {
19   const node::encoding encoding =
20       node::ParseEncoding(args.GetIsolate(), args[0],
21                           static_cast<node::encoding>(-1));
22   const char* encoding_name = "UNKNOWN";
23 #define V(name) if (encoding == node::name) encoding_name = #name;
24   ENCODING_MAP(V)
25 #undef V
26   auto encoding_string =
27       v8::String::NewFromUtf8(args.GetIsolate(), encoding_name)
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