From bee85b0646523c0bf911a96b8cfcfdad1a608dfd Mon Sep 17 00:00:00 2001 From: lifansheng Date: Fri, 17 Sep 2021 10:27:58 +0800 Subject: [PATCH] modify js_api_module Signed-off-by: lifansheng --- convertxml/js_convertxml.cpp | 1 + ohos.build | 3 +- {api => uri}/BUILD.gn | 26 +- {api => uri}/js_uri.cpp | 11 +- {api => uri}/js_uri.h | 2 +- uri/js_uri.js | 100 ++++++ uri/native_module_uri.cpp | 299 ++++++++++++++++++ url/BUILD.gn | 59 ++++ {api => url}/js_url.cpp | 2 +- {api => url}/js_url.h | 2 +- api/js_api.js => url/js_url.js | 92 +----- .../native_module_url.cpp | 270 +--------------- 12 files changed, 498 insertions(+), 369 deletions(-) rename {api => uri}/BUILD.gn (73%) rename {api => uri}/js_uri.cpp (95%) rename {api => uri}/js_uri.h (96%) create mode 100755 uri/js_uri.js create mode 100755 uri/native_module_uri.cpp create mode 100755 url/BUILD.gn rename {api => url}/js_url.cpp (97%) rename {api => url}/js_url.h (97%) rename api/js_api.js => url/js_url.js (82%) rename api/native_module_api.cpp => url/native_module_url.cpp (74%) diff --git a/convertxml/js_convertxml.cpp b/convertxml/js_convertxml.cpp index e635af0..d0b5e10 100755 --- a/convertxml/js_convertxml.cpp +++ b/convertxml/js_convertxml.cpp @@ -202,6 +202,7 @@ void ConvertXml::SetPrevInfo(napi_value &recvElement, int flag, int32_t &index1) napi_set_element(env_, recvElement, index1++, m_prevObj[i]); } } + } void ConvertXml::GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag) diff --git a/ohos.build b/ohos.build index 1997a40..64cf5d1 100755 --- a/ohos.build +++ b/ohos.build @@ -7,7 +7,8 @@ "phone" ], "module_list": [ - "//base/compileruntime/js_api_module/api:api_packages", + "//base/compileruntime/js_api_module/uri:uri_packages", + "//base/compileruntime/js_api_module/url:url_packages", "//base/compileruntime/js_api_module/convertxml:convertxml_packages" ], "inner_kits": [ diff --git a/api/BUILD.gn b/uri/BUILD.gn similarity index 73% rename from api/BUILD.gn rename to uri/BUILD.gn index 829f0f2..0249e7a 100755 --- a/api/BUILD.gn +++ b/uri/BUILD.gn @@ -14,30 +14,29 @@ import("//build/ohos.gni") import("//build/ohos/ace/ace.gni") -base_output_path = get_label_info(":js_api", "target_out_dir") -js_api_obj_path = base_output_path + "/api.o" -gen_js_obj("js_api") { - input = "//base/compileruntime/js_api_module/api/js_api.js" - output = js_api_obj_path +base_output_path = get_label_info(":js_uri", "target_out_dir") +js_uri_obj_path = base_output_path + "/uri.o" +gen_js_obj("js_uri") { + input = "//base/compileruntime/js_api_module/uri/js_uri.js" + output = js_uri_obj_path } -ohos_shared_library("api") { +ohos_shared_library("uri") { include_dirs = [ "//third_party/icu/icu4c/source/common", "//third_party/node/src", "//foundation/ace/napi/interfaces/kits", - "//base/compileruntime/js_api_module/api", + "//base/compileruntime/js_api_module/uri", ] sources = [ "js_uri.cpp", - "js_url.cpp", - "native_module_api.cpp", + "native_module_uri.cpp", ] deps = [ - ":js_api", - "//base/compileruntime/js_api_module/api/:js_api", + ":js_uri", + "//base/compileruntime/js_api_module/uri/:js_uri", "//foundation/ace/napi/:ace_napi", "//foundation/ace/napi/:ace_napi_quickjs", "//third_party/icu/icu4c:static_icuuc", @@ -55,6 +54,7 @@ ohos_shared_library("api") { relative_install_dir = "module" } -group("api_packages") { - deps = [ ":api" ] +group("uri_packages") { + deps = [ ":uri" ] } + diff --git a/api/js_uri.cpp b/uri/js_uri.cpp similarity index 95% rename from api/js_uri.cpp rename to uri/js_uri.cpp index 5a49c96..e8192f2 100755 --- a/api/js_uri.cpp +++ b/uri/js_uri.cpp @@ -14,7 +14,7 @@ */ #include "js_uri.h" #include "utils/log.h" -namespace OHOS::Api { +namespace OHOS::Uri { std::bitset g_ruleAlpha; std::bitset g_ruleScheme; std::bitset g_ruleUrlc; @@ -86,9 +86,10 @@ namespace OHOS::Api { if ((pos != std::string::npos) && (pos != 0)) { AnalysisFragment(pos); if (!errStr_.empty()) { - return; + return; + } } - if ((pos != std::string::npos) && (pos == 0)) + if ((pos != std::string::npos) && (pos == 0)) { errStr_ = "#It can't be the first"; return; } @@ -289,10 +290,10 @@ namespace OHOS::Api { bool Uri::AnalysisPort(size_t pos) { std::string port = data_.substr(pos + 1); - if (!CheckCharacter(port, g_rulePort, true)) { // 存在非规则内字符 + if (!CheckCharacter(port, g_rulePort, true)) { errStr_ = "port does not conform to the rule"; return false; - } else if (CheckCharacter(port, g_ruleDigit, false)) { // 纯数字 + } else if (CheckCharacter(port, g_ruleDigit, false)) { uriData_.port = std::stoi(port); data_ = data_.substr(0, pos); return true; diff --git a/api/js_uri.h b/uri/js_uri.h similarity index 96% rename from api/js_uri.h rename to uri/js_uri.h index d2774f3..923be07 100755 --- a/api/js_uri.h +++ b/uri/js_uri.h @@ -24,7 +24,7 @@ #include #include "napi/native_api.h" #include "napi/native_node_api.h" -namespace OHOS::Api { +namespace OHOS::Uri { constexpr int MAX_BIT_SIZE = 128; struct uri_data { int port = -1; diff --git a/uri/js_uri.js b/uri/js_uri.js new file mode 100755 index 0000000..746f2e7 --- /dev/null +++ b/uri/js_uri.js @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; +const uri = requireInternal("uri"); + +class URI { + constructor(input) { + if (typeof input !== 'string' || input.length === 0) { + throw new Error("input type err"); + } + this.uricalss = new uri.Uri(input); + let errStr = this.uricalss.isFailed; + if (errStr.length !== 0) { + throw new Error(errStr); + } + } + toString() { + return toAscllString(this.uricalss.toString()); + } + + equals(other) { + return this.uricalss.equals(other.uricalss); + } + + isAbsolute() { + return this.uricalss.isAbsolute(); + } + + normalize() { + return this.uricalss.normalize(); + } + + get scheme() { + return this.uricalss.scheme; + } + + get authority() { + return this.uricalss.authority; + } + + get ssp() { + return this.uricalss.ssp; + } + + get userinfo() { + return this.uricalss.userinfo; + } + + get host() { + return this.uricalss.host; + } + + get port() { + return this.uricalss.port; + } + + get path() { + return this.uricalss.path; + } + + get query() { + return this.uricalss.query; + } + + get fragment() { + return this.uricalss.fragment; + } + +} + +function toAscllString(uriStr) { + if (uriStr.indexOf('[') !== -1) { + let arr = uriStr.split("["); + let brr = arr[1].split("]"); + arr[1] = '[' + brr[0] + ']'; + arr[2] = brr[1]; + arr[0] = encodeURI(arr[0]); + arr[2] = encodeURI(arr[2]); + return arr.join(''); + } else { + return encodeURI(uriStr); + } +} + +export default { + URI: URI, +} diff --git a/uri/native_module_uri.cpp b/uri/native_module_uri.cpp new file mode 100755 index 0000000..39f3f59 --- /dev/null +++ b/uri/native_module_uri.cpp @@ -0,0 +1,299 @@ + /* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "js_uri.h" +#include "utils/log.h" + +extern const char _binary_js_uri_js_start[]; +extern const char _binary_js_uri_js_end[]; +namespace OHOS::Uri { + napi_value g_uriClass = nullptr; + static napi_value UriConstructor(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + void *data = nullptr; + size_t argc = 1; + napi_value argv[1] = { 0 }; + Uri *object = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); + if (valuetype == napi_string) { + char *type = nullptr; + size_t typelen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + object = new Uri(env, type); + delete[] type; + } else { + napi_throw_error(env, nullptr, "parameter type is error"); + } + NAPI_CALL(env, napi_wrap(env, thisVar, object, + [](napi_env env, void *data, void *hint) { + auto object = (Uri*)data; + if (object != nullptr) { + delete object; + } + }, nullptr, nullptr)); + return thisVar; + } + + static napi_value Normalize(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string normalizeUri = muri->Normalize(); + size_t argc = 1; + napi_value args[1] = { 0 }; + napi_value result = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, normalizeUri.c_str(), normalizeUri.size(), args)); + NAPI_CALL(env, napi_new_instance(env, g_uriClass, argc, args, &result)); + return result; + } + + static napi_value Equals(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + size_t argc = 1; + napi_value argv[1] = { 0 }; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + Uri *other = nullptr; + NAPI_CALL(env, napi_unwrap(env, argv[0], (void**)&other)); + + bool flag = muri->Equals(*other); + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } + + static napi_value IsAbsolute(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + bool flag = muri->IsAbsolute(); + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } + + static napi_value IsFailed(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string temp = muri->IsFailed(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value UriToString(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string temp = muri->ToString(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetScheme(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string temp = muri->GetScheme(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetAuthority(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string temp = muri->GetAuthority(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetSsp(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string temp = muri->GetSsp(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetUserinfo(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string temp = muri->GetUserinfo(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetHost(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string temp = muri->GetHost(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetPort(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string temp = muri->GetPort(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetPath(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string temp = muri->GetPath(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetQuery(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string temp = muri->GetQuery(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetFragment(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + Uri *muri = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + std::string temp = muri->GetFragment(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value UriInit(napi_env env, napi_value exports) + { + const char *uriClassName = "uri"; + napi_value uriClass = nullptr; + static napi_property_descriptor uriDesc[] = { + DECLARE_NAPI_FUNCTION("normalize", Normalize), + DECLARE_NAPI_FUNCTION("equals", Equals), + DECLARE_NAPI_FUNCTION("isAbsolute", IsAbsolute), + DECLARE_NAPI_FUNCTION("toString", UriToString), + DECLARE_NAPI_GETTER("scheme", GetScheme), + DECLARE_NAPI_GETTER("authority", GetAuthority), + DECLARE_NAPI_GETTER("ssp", GetSsp), + DECLARE_NAPI_GETTER("userinfo", GetUserinfo), + DECLARE_NAPI_GETTER("host", GetHost), + DECLARE_NAPI_GETTER("port", GetPort), + DECLARE_NAPI_GETTER("path", GetPath), + DECLARE_NAPI_GETTER("query", GetQuery), + DECLARE_NAPI_GETTER("fragment", GetFragment), + DECLARE_NAPI_GETTER("isFailed", IsFailed), + }; + NAPI_CALL(env, napi_define_class(env, uriClassName, strlen(uriClassName), UriConstructor, + nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass)); + g_uriClass = uriClass; + static napi_property_descriptor desc[] = { + DECLARE_NAPI_PROPERTY("Uri", uriClass) + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; + } + + extern "C" + __attribute__((visibility("default"))) void NAPI_uri_GetJSCode(const char **buf, int *bufLen) + { + if (buf != nullptr) { + *buf = _binary_js_uri_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_uri_js_end - _binary_js_uri_js_start; + } + } + + static napi_module UriModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = UriInit, + .nm_modname = "uri", + .nm_priv = ((void*)0), + .reserved = {0}, + }; + extern "C" __attribute__((constructor)) void RegisterModule() + { + napi_module_register(&UriModule); + } +} // namespace \ No newline at end of file diff --git a/url/BUILD.gn b/url/BUILD.gn new file mode 100755 index 0000000..5e8d16f --- /dev/null +++ b/url/BUILD.gn @@ -0,0 +1,59 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos/ace/ace.gni") + +base_output_path = get_label_info(":js_url", "target_out_dir") +js_url_obj_path = base_output_path + "/url.o" +gen_js_obj("js_url") { + input = "//base/compileruntime/js_api_module/url/js_url.js" + output = js_url_obj_path +} + +ohos_shared_library("url") { + include_dirs = [ + "//third_party/icu/icu4c/source/common", + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + "//base/compileruntime/js_api_module/url", + ] + + sources = [ + "js_url.cpp", + "native_module_url.cpp", + ] + + deps = [ + ":js_url", + "//base/compileruntime/js_api_module/url/:js_url", + "//foundation/ace/napi/:ace_napi", + "//foundation/ace/napi/:ace_napi_quickjs", + "//third_party/icu/icu4c:static_icuuc", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + subsystem_name = "ccruntime" + part_name = "jsapi_api" + + relative_install_dir = "module" +} + +group("url_packages") { + deps = [ ":url" ] +} diff --git a/api/js_url.cpp b/url/js_url.cpp similarity index 97% rename from api/js_url.cpp rename to url/js_url.cpp index 1175b0c..8b80b8c 100755 --- a/api/js_url.cpp +++ b/url/js_url.cpp @@ -18,7 +18,7 @@ #include #include "securec.h" #include "utils/log.h" -namespace OHOS::Api { +namespace OHOS::Url { static std::map g_head = { {"ftp:", 21}, {"file:", -1}, {"gopher:", 70}, {"http:", 80}, {"https:", 443}, {"ws:", 80}, {"wss:", 443} diff --git a/api/js_url.h b/url/js_url.h similarity index 97% rename from api/js_url.h rename to url/js_url.h index f306f3e..ac55ff4 100755 --- a/api/js_url.h +++ b/url/js_url.h @@ -25,7 +25,7 @@ #include #include "napi/native_api.h" #include "napi/native_node_api.h" -namespace OHOS::Api { +namespace OHOS::Url { enum class BitsetStatusFlag { BIT0 = 0, // 0:Bit 0 Set to true,The URL analysis failed BIT1 = 1, // 1:Bit 1 Set to true,The protocol is the default protocol diff --git a/api/js_api.js b/url/js_url.js similarity index 82% rename from api/js_api.js rename to url/js_url.js index 4abd6fb..7eef1b4 100755 --- a/api/js_api.js +++ b/url/js_url.js @@ -14,86 +14,7 @@ */ 'use strict'; -const api = requireInternal("api"); - -class URI { - constructor(input) { - if (typeof input !== 'string' || input.length === 0) { - throw new Error("input type err"); - } - this.uricalss = new api.Uri(input); - let errStr = this.uricalss.isFailed; - if (errStr.length !== 0) { - throw new Error(errStr); - } - } - toString() { - return toAscllString(this.uricalss.toString()); - } - - equals(other) { - return this.uricalss.equals(other.uricalss); - } - - isAbsolute() { - return this.uricalss.isAbsolute(); - } - - normalize() { - return this.uricalss.normalize(); - } - - get scheme() { - return this.uricalss.scheme; - } - - get authority() { - return this.uricalss.authority; - } - - get ssp() { - return this.uricalss.ssp; - } - - get userinfo() { - return this.uricalss.userinfo; - } - - get host() { - return this.uricalss.host; - } - - get port() { - return this.uricalss.port; - } - - get path() { - return this.uricalss.path; - } - - get query() { - return this.uricalss.query; - } - - get fragment() { - return this.uricalss.fragment; - } - -} - -function toAscllString(uriStr) { - if (uriStr.indexOf('[') !== -1) { - let arr = uriStr.split("["); - let brr = arr[1].split("]"); - arr[1] = '[' + brr[0] + ']'; - arr[2] = brr[1]; - arr[0] = encodeURI(arr[0]); - arr[2] = encodeURI(arr[2]); - return arr.join(''); - } else { - return encodeURI(uriStr); - } -} +const Url = requireInternal("url"); let seachParamsArr = []; class URLSearchParams { @@ -101,7 +22,7 @@ class URLSearchParams { constructor(input) { let out = []; out = parameterProcessing(input); - this.urlcalss = new api.URLSearchParams1(); + this.urlcalss = new Url.URLSearchParams1(); this.urlcalss.array = out; } append(params1, params2) { @@ -224,7 +145,7 @@ function initToStringSeachParams(input) { input = input.slice(1); } let strVal = decodeURI(input); - seachParamsArr = api.stringParmas(strVal); + seachParamsArr = Url.stringParmas(strVal); return seachParamsArr; } class URL { @@ -249,7 +170,7 @@ class URL { if (arguments.length === 1) { inputUrl = arguments[0]; if (typeof inputUrl === 'string' && inputUrl.length > 0) { - nativeUrl = new api.Url(inputUrl); + nativeUrl = new Url.Url(inputUrl); } else { console.log('Input parameter error'); } @@ -261,7 +182,7 @@ class URL { if (typeof inputUrl === 'string') { if (typeof inputBase === 'string') { if (inputBase.length > 0) { - nativeUrl = new api.Url(inputUrl, inputBase); + nativeUrl = new Url.Url(inputUrl, inputBase); } else { console.log('Input parameter error'); return; @@ -269,7 +190,7 @@ class URL { } if (typeof inputBase === 'object') { let nativeBase = inputBase.getInfo(); - nativeUrl = new api.Url(inputUrl, nativeBase); + nativeUrl = new Url.Url(inputUrl, nativeBase); } } } @@ -490,7 +411,6 @@ class URL { } export default { - URI: URI, URLSearchParams: URLSearchParams, URL: URL, } diff --git a/api/native_module_api.cpp b/url/native_module_url.cpp similarity index 74% rename from api/native_module_api.cpp rename to url/native_module_url.cpp index 1a2b1ab..2d61ebc 100755 --- a/api/native_module_api.cpp +++ b/url/native_module_url.cpp @@ -15,233 +15,12 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" -#include "js_uri.h" #include "js_url.h" #include "utils/log.h" -extern const char _binary_js_api_js_start[]; -extern const char _binary_js_api_js_end[]; -namespace OHOS::Api { - napi_value g_uriClass = nullptr; - static napi_value UriConstructor(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - void *data = nullptr; - size_t argc = 1; - napi_value argv[1] = { 0 }; - Uri *object = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); - napi_valuetype valuetype; - NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); - if (valuetype == napi_string) { - char *type = nullptr; - size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen + 1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - object = new Uri(env, type); - delete[] type; - } else { - napi_throw_error(env, nullptr, "parameter type is error"); - } - NAPI_CALL(env, napi_wrap(env, thisVar, object, - [](napi_env env, void *data, void *hint) { - auto object = (Uri*)data; - if (object != nullptr) { - delete object; - } - }, nullptr, nullptr)); - return thisVar; - } - - static napi_value Normalize(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string normalizeUri = muri->Normalize(); - size_t argc = 1; - napi_value args[1] = { 0 }; - napi_value result = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, normalizeUri.c_str(), normalizeUri.size(), args)); - NAPI_CALL(env, napi_new_instance(env, g_uriClass, argc, args, &result)); - return result; - } - - static napi_value Equals(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - size_t argc = 1; - napi_value argv[1] = { 0 }; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - Uri *other = nullptr; - NAPI_CALL(env, napi_unwrap(env, argv[0], (void**)&other)); - - bool flag = muri->Equals(*other); - NAPI_CALL(env, napi_get_boolean(env, flag, &result)); - return result; - } - - static napi_value IsAbsolute(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - bool flag = muri->IsAbsolute(); - NAPI_CALL(env, napi_get_boolean(env, flag, &result)); - return result; - } - - static napi_value IsFailed(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string temp = muri->IsFailed(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value UriToString(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string temp = muri->ToString(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetScheme(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string temp = muri->GetScheme(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetAuthority(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string temp = muri->GetAuthority(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetSsp(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string temp = muri->GetSsp(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetUserinfo(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string temp = muri->GetUserinfo(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetHost(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string temp = muri->GetHost(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetPort(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string temp = muri->GetPort(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetPath(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string temp = muri->GetPath(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetQuery(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string temp = muri->GetQuery(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetFragment(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); - std::string temp = muri->GetFragment(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - +extern const char _binary_js_url_js_start[]; +extern const char _binary_js_url_js_end[]; +namespace OHOS::Url { static void UrlStructor(napi_env &env, napi_callback_info &info, URL *&object) { napi_value thisVar = nullptr; @@ -1113,36 +892,6 @@ namespace OHOS::Api { return exports; } - static napi_value UriInit(napi_env env, napi_value exports) - { - const char *uriClassName = "uri"; - napi_value uriClass = nullptr; - static napi_property_descriptor uriDesc[] = { - DECLARE_NAPI_FUNCTION("normalize", Normalize), - DECLARE_NAPI_FUNCTION("equals", Equals), - DECLARE_NAPI_FUNCTION("isAbsolute", IsAbsolute), - DECLARE_NAPI_FUNCTION("toString", UriToString), - DECLARE_NAPI_GETTER("scheme", GetScheme), - DECLARE_NAPI_GETTER("authority", GetAuthority), - DECLARE_NAPI_GETTER("ssp", GetSsp), - DECLARE_NAPI_GETTER("userinfo", GetUserinfo), - DECLARE_NAPI_GETTER("host", GetHost), - DECLARE_NAPI_GETTER("port", GetPort), - DECLARE_NAPI_GETTER("path", GetPath), - DECLARE_NAPI_GETTER("query", GetQuery), - DECLARE_NAPI_GETTER("fragment", GetFragment), - DECLARE_NAPI_GETTER("isFailed", IsFailed), - }; - NAPI_CALL(env, napi_define_class(env, uriClassName, strlen(uriClassName), UriConstructor, - nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass)); - g_uriClass = uriClass; - static napi_property_descriptor desc[] = { - DECLARE_NAPI_PROPERTY("Uri", uriClass) - }; - napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); - return exports; - } - static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor desc[] = { @@ -1151,33 +900,32 @@ namespace OHOS::Api { NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); SeachParamsInit(env, exports); UrlInit(env, exports); - UriInit(env, exports); return exports; } extern "C" - __attribute__((visibility("default"))) void NAPI_api_GetJSCode(const char **buf, int *bufLen) + __attribute__((visibility("default"))) void NAPI_url_GetJSCode(const char **buf, int *bufLen) { if (buf != nullptr) { - *buf = _binary_js_api_js_start; + *buf = _binary_js_url_js_start; } if (bufLen != nullptr) { - *bufLen = _binary_js_api_js_end - _binary_js_api_js_start; + *bufLen = _binary_js_url_js_end - _binary_js_url_js_start; } } - static napi_module ApiModule = { + static napi_module UrlModule = { .nm_version = 1, .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = Init, - .nm_modname = "api", + .nm_modname = "url", .nm_priv = ((void*)0), .reserved = {0}, }; extern "C" __attribute__((constructor)) void RegisterModule() { - napi_module_register(&ApiModule); + napi_module_register(&UrlModule); } } // namespace \ No newline at end of file