| 1 | #include <napi.h> |
| 2 | #include <memory> |
| 3 | #include <string> |
| 4 | #include <string_view> |
| 5 | |
| 6 | #include "src/Config.hpp" |
| 7 | #include "src/Converter.hpp" |
| 8 | #include "src/DictConverter.hpp" |
| 9 | #include "src/Exception.hpp" |
| 10 | #include "src/ResourceProvider.hpp" |
| 11 | |
| 12 | using namespace opencc; |
| 13 | |
| 14 | std::string ToUtf8String(const Napi::Value& val) { |
| 15 | return val.As<Napi::String>().Utf8Value(); |
| 16 | } |
| 17 | |
| 18 | class OpenccBinding : public Napi::ObjectWrap<OpenccBinding> { |
| 19 | class ConvertWorker : public Napi::AsyncWorker { |
| 20 | OpenccBinding* instance_; |
| 21 | std::string input_; |
| 22 | std::string output_; |
| 23 | |
| 24 | public: |
| 25 | ConvertWorker(OpenccBinding* instance, const std::string& input, |
| 26 | const Napi::Function& callback) |
| 27 | : Napi::AsyncWorker(callback, "opencc:convert-async-cb"), |
| 28 | instance_(instance), input_(input) { |
| 29 | instance_->Ref(); |
| 30 | } |
| 31 | |
| 32 | ~ConvertWorker() override { |
| 33 | instance_->Unref(); |
| 34 | } |
| 35 | |
| 36 | void Execute() override { |
| 37 | try { |
| 38 | output_ = instance_->Convert(input_); |
| 39 | } catch (opencc::Exception& e) { |
| 40 | SetError(e.what()); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void OnOK() override { |
| 45 | Callback().Call({Env().Undefined(), Napi::String::New(Env(), output_)}); |
| 46 | } |
| 47 | |
| 48 | void OnError(const Napi::Error& e) override { |
| 49 | Callback().Call({Napi::String::New(Env(), e.Message()), |
| 50 | Napi::String::New(Env(), "")}); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | Config config_; |
| 55 | ConverterPtr converter_; |
| 56 | |
| 57 | public: |
| 58 | explicit OpenccBinding(const Napi::CallbackInfo& info) |
| 59 | : Napi::ObjectWrap<OpenccBinding>(info), config_(), converter_() { |
| 60 | Napi::Env env = info.Env(); |
| 61 | |
| 62 | if (info.Length() >= 3 && info[0].IsString() && info[1].IsString() && |
| 63 | info[2].IsBoolean()) { |
| 64 | // Three-argument mode: |
| 65 | // NewFromFile(configFileName, ZipResourceProvider(resourceZipFileName)). |
| 66 | const std::string configFile = ToUtf8String(info[0]); |
| 67 | const std::string resourceZipFile = ToUtf8String(info[1]); |
| 68 | ConfigLoadOptions options; |
| 69 | options.includeTofuRiskDictionaries = |
| 70 | info[2].As<Napi::Boolean>().Value(); |
| 71 | try { |
| 72 | std::shared_ptr<ResourceProvider> provider( |
| 73 | new ZipResourceProvider(resourceZipFile)); |
| 74 | converter_ = config_.NewFromFile(configFile, provider, options); |
| 75 | } catch (opencc::Exception& e) { |
| 76 | Napi::Error::New(env, e.what()).ThrowAsJavaScriptException(); |
| 77 | } |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if (info.Length() >= 2 && info[0].IsString() && info[1].IsString()) { |
| 82 | // Two-argument mode: NewFromString(jsonString, configDirectory) |
| 83 | // Used by the JS layer to pass patched JSON with absolute paths. |
| 84 | const std::string json = ToUtf8String(info[0]); |
| 85 | const std::string configDir = ToUtf8String(info[1]); |
| 86 | try { |
| 87 | converter_ = config_.NewFromString(json, configDir); |
| 88 | } catch (opencc::Exception& e) { |
| 89 | Napi::Error::New(env, e.what()).ThrowAsJavaScriptException(); |
| 90 | } |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | // Single-argument mode: NewFromFile(configFilePath) |
| 95 | std::string configFile = "s2t.json"; |
| 96 | if (info.Length() >= 1) { |
| 97 | if (!info[0].IsString()) { |
| 98 | Napi::TypeError::New(env, "Wrong arguments") |
| 99 | .ThrowAsJavaScriptException(); |
| 100 | return; |
| 101 | } |
| 102 | configFile = ToUtf8String(info[0]); |
| 103 | } |
| 104 | |
| 105 | try { |
| 106 | converter_ = config_.NewFromFile(configFile); |
| 107 | } catch (opencc::Exception& e) { |
| 108 | Napi::Error::New(env, e.what()).ThrowAsJavaScriptException(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | ~OpenccBinding() override {} |
| 113 | |
| 114 | std::string Convert(std::string_view input) { |
| 115 | return converter_->Convert(input); |
| 116 | } |
| 117 | |
| 118 | ConverterPtr GetConverter() const { return converter_; } |
| 119 | |
| 120 | static Napi::Value Version(const Napi::CallbackInfo& info) { |
| 121 | return Napi::String::New(info.Env(), OPENCC_VERSION); |
| 122 | } |
| 123 | |
| 124 | Napi::Value Convert(const Napi::CallbackInfo& info) { |
| 125 | Napi::Env env = info.Env(); |
| 126 | if (info.Length() < 2 || !info[0].IsString() || !info[1].IsFunction()) { |
| 127 | Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); |
| 128 | return env.Undefined(); |
| 129 | } |
| 130 | |
| 131 | ConvertWorker* worker = |
| 132 | new ConvertWorker(this, ToUtf8String(info[0]), |
| 133 | info[1].As<Napi::Function>()); |
| 134 | worker->Queue(); |
| 135 | return env.Undefined(); |
| 136 | } |
| 137 | |
| 138 | Napi::Value ConvertSync(const Napi::CallbackInfo& info) { |
| 139 | Napi::Env env = info.Env(); |
| 140 | if (info.Length() < 1 || !info[0].IsString()) { |
| 141 | Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); |
| 142 | return env.Undefined(); |
| 143 | } |
| 144 | |
| 145 | const std::string input = ToUtf8String(info[0]); |
| 146 | std::string output; |
| 147 | try { |
| 148 | output = Convert(std::string_view(input)); |
| 149 | } catch (opencc::Exception& e) { |
| 150 | Napi::Error::New(env, e.what()).ThrowAsJavaScriptException(); |
| 151 | return env.Undefined(); |
| 152 | } |
| 153 | |
| 154 | return Napi::String::New(env, output); |
| 155 | } |
| 156 | |
| 157 | static Napi::Value GenerateDict(const Napi::CallbackInfo& info) { |
| 158 | Napi::Env env = info.Env(); |
| 159 | if (info.Length() < 4 || !info[0].IsString() || !info[1].IsString() || |
| 160 | !info[2].IsString() || !info[3].IsString()) { |
| 161 | Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); |
| 162 | return env.Undefined(); |
| 163 | } |
| 164 | const std::string inputFileName = ToUtf8String(info[0]); |
| 165 | const std::string outputFileName = ToUtf8String(info[1]); |
| 166 | const std::string formatFrom = ToUtf8String(info[2]); |
| 167 | const std::string formatTo = ToUtf8String(info[3]); |
| 168 | try { |
| 169 | opencc::ConvertDictionary(inputFileName, outputFileName, formatFrom, |
| 170 | formatTo); |
| 171 | } catch (opencc::Exception& e) { |
| 172 | Napi::Error::New(env, e.what()).ThrowAsJavaScriptException(); |
| 173 | } |
| 174 | return env.Undefined(); |
| 175 | } |
| 176 | |
| 177 | static Napi::Object Init(Napi::Env env, Napi::Object exports) { |
| 178 | Napi::Function cons = DefineClass( |
| 179 | env, "Opencc", |
| 180 | { |
| 181 | StaticMethod("version", &OpenccBinding::Version), |
| 182 | StaticMethod("generateDict", &OpenccBinding::GenerateDict), |
| 183 | InstanceMethod("convert", &OpenccBinding::Convert), |
| 184 | InstanceMethod("convertSync", &OpenccBinding::ConvertSync), |
| 185 | }); |
| 186 | exports.Set("Opencc", cons); |
| 187 | return exports; |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | class OpenccStreamBinding : public Napi::ObjectWrap<OpenccStreamBinding> { |
| 192 | std::unique_ptr<ConverterStream> stream_; |
| 193 | |
| 194 | public: |
| 195 | explicit OpenccStreamBinding(const Napi::CallbackInfo& info) |
| 196 | : Napi::ObjectWrap<OpenccStreamBinding>(info), stream_() { |
| 197 | Napi::Env env = info.Env(); |
| 198 | if (info.Length() < 1 || !info[0].IsObject()) { |
| 199 | Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | OpenccBinding* owner = |
| 204 | Napi::ObjectWrap<OpenccBinding>::Unwrap(info[0].As<Napi::Object>()); |
| 205 | stream_.reset(new ConverterStream(owner->GetConverter())); |
| 206 | } |
| 207 | |
| 208 | Napi::Value ConvertChunk(const Napi::CallbackInfo& info) { |
| 209 | Napi::Env env = info.Env(); |
| 210 | if (info.Length() < 1 || (!info[0].IsBuffer() && !info[0].IsString())) { |
| 211 | Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); |
| 212 | return env.Undefined(); |
| 213 | } |
| 214 | |
| 215 | try { |
| 216 | std::string output; |
| 217 | if (info[0].IsBuffer()) { |
| 218 | Napi::Buffer<char> input = info[0].As<Napi::Buffer<char>>(); |
| 219 | output = stream_->ConvertChunk({input.Data(), input.Length()}); |
| 220 | } else { |
| 221 | const std::string input = ToUtf8String(info[0]); |
| 222 | output = stream_->ConvertChunk(input); |
| 223 | } |
| 224 | return Napi::String::New(env, output); |
| 225 | } catch (opencc::Exception& e) { |
| 226 | Napi::Error::New(env, e.what()).ThrowAsJavaScriptException(); |
| 227 | return env.Undefined(); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | Napi::Value Finish(const Napi::CallbackInfo& info) { |
| 232 | Napi::Env env = info.Env(); |
| 233 | if (info.Length() >= 1 && !info[0].IsBuffer() && !info[0].IsString()) { |
| 234 | Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); |
| 235 | return env.Undefined(); |
| 236 | } |
| 237 | |
| 238 | try { |
| 239 | if (info.Length() >= 1 && info[0].IsBuffer()) { |
| 240 | Napi::Buffer<char> input = info[0].As<Napi::Buffer<char>>(); |
| 241 | return Napi::String::New( |
| 242 | env, stream_->Finish({input.Data(), input.Length()})); |
| 243 | } |
| 244 | if (info.Length() >= 1) { |
| 245 | const std::string input = ToUtf8String(info[0]); |
| 246 | return Napi::String::New(env, stream_->Finish(input)); |
| 247 | } |
| 248 | return Napi::String::New(env, stream_->Finish()); |
| 249 | } catch (opencc::Exception& e) { |
| 250 | Napi::Error::New(env, e.what()).ThrowAsJavaScriptException(); |
| 251 | return env.Undefined(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | static Napi::Object Init(Napi::Env env, Napi::Object exports) { |
| 256 | Napi::Function cons = DefineClass( |
| 257 | env, "OpenccStream", |
| 258 | { |
| 259 | InstanceMethod("convertChunk", &OpenccStreamBinding::ConvertChunk), |
| 260 | InstanceMethod("finish", &OpenccStreamBinding::Finish), |
| 261 | }); |
| 262 | exports.Set("OpenccStream", cons); |
| 263 | return exports; |
| 264 | } |
| 265 | }; |
| 266 | |
| 267 | Napi::Object InitAll(Napi::Env env, Napi::Object exports) { |
| 268 | OpenccBinding::Init(env, exports); |
| 269 | OpenccStreamBinding::Init(env, exports); |
| 270 | return exports; |
| 271 | } |
| 272 | |
| 273 | NODE_API_MODULE(opencc, InitAll); |