mirror of
https://github.com/openharmony/js_api_module.git
synced 2026-07-22 14:55:25 -04:00
e5868849c3
Signed-off-by: xliu <liuxin259@huawei.com> Change-Id: Idf8617614e82e8875164368771aedfa19742e93d
852 lines
29 KiB
C++
852 lines
29 KiB
C++
/*
|
|
* 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_url.h"
|
|
#include "utils/log.h"
|
|
|
|
extern const char _binary_js_url_js_start[];
|
|
extern const char _binary_js_url_js_end[];
|
|
|
|
static napi_value UrlConstructor(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
void* data = nullptr;
|
|
size_t argc = 0;
|
|
napi_value argv[2] = { 0 };
|
|
URL* object = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, &data));
|
|
if (argc == 1) {
|
|
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 URL(env,type);
|
|
delete[] type;
|
|
} else {
|
|
HILOG_INFO("Parameter error");
|
|
}
|
|
} else if (argc == 2) {
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
|
|
napi_valuetype valuetype1;
|
|
napi_valuetype valuetype2;
|
|
NAPI_CALL(env,napi_typeof(env, argv[0], &valuetype1));
|
|
if (valuetype1 == napi_string) {
|
|
char* tem = nullptr;
|
|
size_t temlen = 0;
|
|
NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &temlen));
|
|
tem = new char[temlen+1];
|
|
NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], tem, temlen + 1, &temlen));
|
|
std::string input_(tem);
|
|
delete[] tem;
|
|
NAPI_CALL(env,napi_typeof(env, argv[1], &valuetype2));
|
|
if (valuetype2 == napi_string){
|
|
char* type1 = nullptr;
|
|
size_t typelen1 = 0;
|
|
NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], nullptr, 0, &typelen1));
|
|
type1 = new char[typelen1 + 1];
|
|
NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], type1, typelen1 + 1, &typelen1));
|
|
std::string base_(type1);
|
|
delete[] type1;
|
|
object = new URL(env, input_, base_);
|
|
} else if (valuetype2 == napi_object) {
|
|
URL * temp = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, argv[1], (void**)&temp));
|
|
object = new URL(env,input_,*temp);
|
|
} else {
|
|
HILOG_INFO("secondParameter error");
|
|
}
|
|
} else {
|
|
HILOG_INFO("firstParameter error");
|
|
}
|
|
}
|
|
NAPI_CALL(env, napi_wrap(env, thisVar, object,
|
|
[](napi_env env, void* data, void* hint) {
|
|
auto object = (URL*)data;
|
|
if (object != nullptr) {
|
|
delete object;
|
|
}
|
|
}, nullptr, nullptr));
|
|
return thisVar;
|
|
}
|
|
|
|
static napi_value GetHostname(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetHostname();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value GetSearch(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetSearch();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value GetUsername(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetUsername();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value GetPassword(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetPassword();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value GetFragment(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetFragment();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value GetScheme(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetScheme();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value GetPort(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetPort();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value GetHost(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetHost();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value GetPath(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetPath();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value GetOnOrOff(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetOnOrOff();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value GetIsIpv6(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetIsIpv6();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value SetHref(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1] = {0};
|
|
size_t argc = 1;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
|
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));
|
|
std::string input(type);
|
|
if (type != nullptr) {
|
|
delete[] type;
|
|
type = nullptr;
|
|
}
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
murl->SetHref(input);
|
|
napi_value result = nullptr;
|
|
NAPI_CALL(env, napi_get_undefined(env, &result));
|
|
return result;
|
|
}
|
|
|
|
static napi_value SetHostname(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1] = {0};
|
|
size_t argc = 1;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
|
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));
|
|
std::string input(type);
|
|
if (type != nullptr) {
|
|
delete[] type;
|
|
type = nullptr;
|
|
}
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
murl->SetHostname(input);
|
|
napi_value result = nullptr;
|
|
NAPI_CALL(env, napi_get_undefined(env, &result));
|
|
return result;
|
|
}
|
|
|
|
static napi_value SetPort(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1] = {0};
|
|
size_t argc = 1;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
|
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));
|
|
std::string input(type);
|
|
if (type != nullptr) {
|
|
delete[] type;
|
|
type = nullptr;
|
|
}
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
murl->SetPort(input);
|
|
napi_value result = nullptr;
|
|
NAPI_CALL(env, napi_get_undefined(env, &result));
|
|
return result;
|
|
}
|
|
|
|
static napi_value SetHost(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1] = {0};
|
|
size_t argc = 1;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
|
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));
|
|
std::string input(type);
|
|
if (type != nullptr) {
|
|
delete[] type;
|
|
type = nullptr;
|
|
}
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
murl->SetHost(input);
|
|
napi_value result = nullptr;
|
|
NAPI_CALL(env, napi_get_undefined(env, &result));
|
|
return result;
|
|
}
|
|
|
|
static napi_value SetSearch(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1] = {0};
|
|
size_t argc = 1;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
|
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));
|
|
std::string input(type);
|
|
if (type != nullptr) {
|
|
delete[] type;
|
|
type = nullptr;
|
|
}
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
murl->SetSearch(input);
|
|
napi_value result = nullptr;
|
|
NAPI_CALL(env, napi_get_undefined(env, &result));
|
|
return result;
|
|
}
|
|
|
|
static napi_value SetScheme(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1] = {0};
|
|
size_t argc = 1;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
|
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));
|
|
std::string input(type);
|
|
if (type != nullptr) {
|
|
delete[] type;
|
|
type = nullptr;
|
|
}
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
murl->SetScheme(input);
|
|
napi_value result = nullptr;
|
|
NAPI_CALL(env, napi_get_undefined(env, &result));
|
|
return result;
|
|
}
|
|
|
|
static napi_value SetFragment(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1] = {0};
|
|
size_t argc = 1;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
|
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));
|
|
std::string input(type);
|
|
if (type != nullptr) {
|
|
delete[] type;
|
|
type = nullptr;
|
|
}
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
murl->SetFragment(input);
|
|
napi_value result = nullptr;
|
|
NAPI_CALL(env, napi_get_undefined(env, &result));
|
|
return result;
|
|
}
|
|
|
|
static napi_value SetUsername(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1] = {0};
|
|
size_t argc = 1;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
|
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));
|
|
std::string input(type);
|
|
if (type != nullptr) {
|
|
delete[] type;
|
|
type = nullptr;
|
|
}
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
murl->SetUsername(input);
|
|
napi_value result = nullptr;
|
|
NAPI_CALL(env, napi_get_undefined(env, &result));
|
|
return result;
|
|
}
|
|
|
|
static napi_value SetPath(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1] = {0};
|
|
size_t argc = 1;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
|
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));
|
|
std::string input(type);
|
|
if (type != nullptr) {
|
|
delete[] type;
|
|
type = nullptr;
|
|
}
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
murl->SetPath(input);
|
|
napi_value result = nullptr;
|
|
NAPI_CALL(env, napi_get_undefined(env, &result));
|
|
return result;
|
|
}
|
|
|
|
static napi_value SetPassword(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1] = {0};
|
|
size_t argc = 1;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
|
|
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));
|
|
std::string input(type);
|
|
if (type != nullptr) {
|
|
delete[] type;
|
|
type = nullptr;
|
|
}
|
|
URL* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
murl->SetPassword(input);
|
|
napi_value result = nullptr;
|
|
NAPI_CALL(env, napi_get_undefined(env, &result));
|
|
return result;
|
|
}
|
|
|
|
static napi_value SeachParamsConstructor(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
void* data = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data));
|
|
auto object = new URLSearchParams(env);
|
|
NAPI_CALL(env, napi_wrap(
|
|
env, thisVar, object,
|
|
[](napi_env env, void* data, void* hint) {
|
|
auto object = (URLSearchParams*)data;
|
|
if (object != nullptr) {
|
|
delete object;
|
|
}
|
|
},
|
|
nullptr, nullptr));
|
|
return thisVar;
|
|
}
|
|
|
|
static napi_value SetArray(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1];
|
|
size_t argc = 1;
|
|
uint32_t length = 0;
|
|
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
|
napi_get_array_length(env, argv[0], &length);
|
|
std::vector<std::string> vec;
|
|
char* cstr = nullptr;
|
|
size_t arraySize = 0;
|
|
napi_value napiStr = nullptr;
|
|
for (size_t i = 0; i < length; i++) {
|
|
napi_get_element(env, argv[0], i, &napiStr);
|
|
napi_get_value_string_utf8(env, napiStr, nullptr, 0, &arraySize);
|
|
cstr = new char[arraySize + 1];
|
|
napi_get_value_string_utf8(env, napiStr, cstr, arraySize + 1, &arraySize);
|
|
vec.push_back(cstr);
|
|
delete []cstr;
|
|
cstr = nullptr;
|
|
}
|
|
URLSearchParams* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
murl->SetArray(vec);
|
|
napi_value result = nullptr;
|
|
NAPI_CALL(env, napi_get_undefined(env, &result));
|
|
return result;
|
|
}
|
|
|
|
static napi_value GetArray(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
|
|
URLSearchParams* murl = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
|
|
napi_value retVal = murl->GetArray();
|
|
return retVal;
|
|
}
|
|
|
|
static napi_value Get(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 1;
|
|
napi_value args = nullptr;
|
|
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
|
|
|
|
if(argc != 1) {
|
|
HILOG_INFO("One arg needs to be specified");
|
|
return nullptr;
|
|
}
|
|
URLSearchParams* object = nullptr;
|
|
napi_unwrap(env, thisVar, (void**)&object);
|
|
napi_value result = object->Get(args);
|
|
return result;
|
|
}
|
|
|
|
static napi_value GetAll(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 1;
|
|
napi_value args = nullptr;
|
|
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
|
|
|
|
if(argc != 1) {
|
|
HILOG_INFO("One arg needs to be specified");
|
|
return nullptr;
|
|
}
|
|
URLSearchParams* object = nullptr;
|
|
napi_unwrap(env, thisVar, (void**)&object);
|
|
napi_value result = object->GetAll(args);
|
|
return result;
|
|
}
|
|
|
|
static napi_value Append(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 2;
|
|
napi_value args[2] = { 0 };
|
|
void* data = nullptr;
|
|
napi_get_cb_info(env, info, &argc, args, &thisVar, &data);
|
|
|
|
if(argc != 2) {
|
|
HILOG_INFO("Two args needs to be specified");
|
|
return nullptr;
|
|
}
|
|
URLSearchParams* object = nullptr;
|
|
napi_unwrap(env, thisVar, (void**)&object);
|
|
object->Append(args[0], args[1]);
|
|
return nullptr;
|
|
}
|
|
|
|
static napi_value Delete(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 1;
|
|
napi_value args = nullptr;
|
|
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
|
|
|
|
if(argc != 1) {
|
|
HILOG_INFO("One arg needs to be specified");
|
|
return nullptr;
|
|
}
|
|
URLSearchParams* object = nullptr;
|
|
napi_unwrap(env, thisVar, (void**)&object);
|
|
object->Delete(args);
|
|
return nullptr;
|
|
}
|
|
|
|
static napi_value ForEach(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 1;
|
|
napi_value args = nullptr;
|
|
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
|
|
|
|
URLSearchParams* object = nullptr;
|
|
napi_unwrap(env, thisVar, (void**)&object);
|
|
object->ForEach(args, thisVar);
|
|
return nullptr;
|
|
}
|
|
|
|
static napi_value Entries(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 0;
|
|
napi_value args = nullptr;
|
|
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
|
|
URLSearchParams* object = nullptr;
|
|
napi_unwrap(env, thisVar, (void**)&object);
|
|
napi_value result = object->Entries();
|
|
return result;
|
|
}
|
|
|
|
static napi_value IsHas(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 1;
|
|
napi_value args = nullptr;
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr));
|
|
URLSearchParams* object = nullptr;
|
|
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
|
|
napi_value result = object->IsHas(args);
|
|
return result;
|
|
}
|
|
|
|
static napi_value Set(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 2;
|
|
napi_value args[2] = { 0 };
|
|
napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
|
|
URLSearchParams* object = nullptr;
|
|
napi_unwrap(env, thisVar, (void**)&object);
|
|
object->Set(args[0], args[1]);
|
|
return nullptr;
|
|
}
|
|
|
|
static napi_value Sort(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 0;
|
|
napi_value args = nullptr;
|
|
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
|
|
URLSearchParams* object = nullptr;
|
|
napi_unwrap(env, thisVar, (void**)&object);
|
|
object->Sort();
|
|
return nullptr;
|
|
}
|
|
|
|
static napi_value ToString(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 0;
|
|
napi_value args = nullptr;
|
|
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
|
|
URLSearchParams* object = nullptr;
|
|
napi_unwrap(env, thisVar, (void**)&object);
|
|
napi_value result = object->ToString();
|
|
return result;
|
|
}
|
|
|
|
static napi_value IterByKeys(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 0;
|
|
napi_value args = nullptr;
|
|
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
|
|
URLSearchParams* object = nullptr;
|
|
napi_unwrap(env, thisVar, (void**)&object);
|
|
napi_value result = object->IterByKeys();
|
|
return result;
|
|
}
|
|
|
|
static napi_value IterByValues(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
size_t argc = 0;
|
|
napi_value args = nullptr;
|
|
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
|
|
URLSearchParams* object = nullptr;
|
|
napi_unwrap(env, thisVar, (void**)&object);
|
|
napi_value result = object->IterByValues();
|
|
return result;
|
|
}
|
|
|
|
std::vector<std::string> stringParsing (std::string Stringpar)
|
|
{
|
|
std::vector<std::string> seachParasVec;
|
|
size_t strStartPos = 0;
|
|
size_t strLastPos = 0;
|
|
bool isHasSpace = false;
|
|
std::string buf = "";
|
|
size_t i;
|
|
for(i = 0;i < Stringpar.length(); i++) {
|
|
char code = Stringpar[i];
|
|
switch(code) {
|
|
case '&' :
|
|
{
|
|
if (strStartPos == i){
|
|
strLastPos = i + 1;
|
|
strStartPos = i + 1;
|
|
break;
|
|
}
|
|
if (strLastPos < i) {
|
|
buf += Stringpar.substr(strLastPos, i - strLastPos);
|
|
}
|
|
seachParasVec.push_back(buf);
|
|
if (!isHasSpace) {
|
|
seachParasVec.push_back("");
|
|
}
|
|
isHasSpace = false;
|
|
buf = "";
|
|
strLastPos = i + 1;
|
|
strStartPos = i + 1;
|
|
break;
|
|
}
|
|
case '=' :
|
|
{
|
|
if (isHasSpace) {
|
|
break;
|
|
}
|
|
if (strLastPos < i) {
|
|
buf += Stringpar.substr(strLastPos, i- strLastPos);
|
|
}
|
|
seachParasVec.push_back(buf);
|
|
isHasSpace = true;
|
|
buf = "";
|
|
strLastPos = i + 1;
|
|
break;
|
|
}
|
|
case '+' :
|
|
{
|
|
if (strLastPos < i) {
|
|
buf += Stringpar.substr(strLastPos, i - strLastPos);
|
|
}
|
|
buf += "";
|
|
strLastPos = i + 1;
|
|
break;
|
|
}
|
|
default:break;
|
|
}
|
|
}
|
|
if (strStartPos == i) {
|
|
return seachParasVec;
|
|
}
|
|
if (strLastPos < i) {
|
|
buf += Stringpar.substr(strLastPos, i - strLastPos);
|
|
}
|
|
seachParasVec.push_back(buf);
|
|
if (!isHasSpace) {
|
|
seachParasVec.push_back("");
|
|
}
|
|
return seachParasVec;
|
|
}
|
|
|
|
static napi_value StringParmas(napi_env env, napi_callback_info info)
|
|
{
|
|
napi_value thisVar = nullptr;
|
|
napi_value argv[1] = {0};
|
|
size_t argc = 1;
|
|
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
|
|
char* type = nullptr;
|
|
size_t typelen = 0;
|
|
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen);
|
|
type = new char[typelen+1];
|
|
napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen);
|
|
std::string input(type);
|
|
delete[] type;
|
|
std::vector<std::string> seachParasmsString;
|
|
seachParasmsString = stringParsing(input);
|
|
napi_value arr = nullptr;
|
|
napi_create_array(env, &arr);
|
|
for (size_t i = 0; i < seachParasmsString.size(); i++) {
|
|
napi_value result = nullptr;
|
|
napi_create_string_utf8(env, seachParasmsString[i].c_str(), seachParasmsString[i].size(), &result);
|
|
napi_set_element(env, arr, i, result);
|
|
}
|
|
return arr;
|
|
}
|
|
|
|
static napi_value SeachParamsInit(napi_env env, napi_value exports)
|
|
{
|
|
const char* SeachParamsClassName = "URLSearchParams";
|
|
napi_value SeachParamsInitClass = nullptr;
|
|
static napi_property_descriptor UrlDesc[] =
|
|
{
|
|
DECLARE_NAPI_FUNCTION("has", IsHas),
|
|
DECLARE_NAPI_FUNCTION("set", Set),
|
|
DECLARE_NAPI_FUNCTION("sort", Sort),
|
|
DECLARE_NAPI_FUNCTION("toString", ToString),
|
|
DECLARE_NAPI_FUNCTION("keys", IterByKeys),
|
|
DECLARE_NAPI_FUNCTION("values", IterByValues),
|
|
DECLARE_NAPI_FUNCTION("get", Get),
|
|
DECLARE_NAPI_FUNCTION("getAll", GetAll),
|
|
DECLARE_NAPI_FUNCTION("append", Append),
|
|
DECLARE_NAPI_FUNCTION("delete", Delete),
|
|
DECLARE_NAPI_FUNCTION("forEach", ForEach),
|
|
DECLARE_NAPI_FUNCTION("entries", Entries),
|
|
DECLARE_NAPI_GETTER_SETTER("array", GetArray, SetArray),
|
|
};
|
|
NAPI_CALL(env, napi_define_class(env, SeachParamsClassName, strlen(SeachParamsClassName), SeachParamsConstructor,
|
|
nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &SeachParamsInitClass));
|
|
static napi_property_descriptor desc[] = {
|
|
DECLARE_NAPI_PROPERTY("URLSearchParams1", SeachParamsInitClass)
|
|
};
|
|
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
|
|
return exports;
|
|
};
|
|
|
|
static napi_value UrlInit(napi_env env, napi_value exports)
|
|
{
|
|
const char* UrlClassName = "Url";
|
|
napi_value UrlClass = nullptr;
|
|
static napi_property_descriptor UrlDesc[] =
|
|
{
|
|
DECLARE_NAPI_GETTER_SETTER("hostname", GetHostname, SetHostname),
|
|
DECLARE_NAPI_FUNCTION("href", SetHref),
|
|
DECLARE_NAPI_GETTER_SETTER("search", GetSearch, SetSearch),
|
|
DECLARE_NAPI_GETTER_SETTER("username", GetUsername, SetUsername),
|
|
DECLARE_NAPI_GETTER_SETTER("password", GetPassword, SetPassword),
|
|
DECLARE_NAPI_GETTER_SETTER("host", GetHost, SetHost),
|
|
DECLARE_NAPI_GETTER_SETTER("hash", GetFragment, SetFragment),
|
|
DECLARE_NAPI_GETTER_SETTER("protocol", GetScheme, SetScheme),
|
|
DECLARE_NAPI_GETTER_SETTER("pathname", GetPath, SetPath),
|
|
DECLARE_NAPI_GETTER_SETTER("port", GetPort, SetPort),
|
|
DECLARE_NAPI_GETTER("onOrOff", GetOnOrOff),
|
|
DECLARE_NAPI_GETTER("GetIsIpv6", GetIsIpv6),
|
|
};
|
|
NAPI_CALL(env, napi_define_class(env, UrlClassName, strlen(UrlClassName), UrlConstructor,
|
|
nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc,
|
|
&UrlClass));
|
|
static napi_property_descriptor desc[] = {
|
|
DECLARE_NAPI_PROPERTY("Url", UrlClass)
|
|
};
|
|
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[] = {
|
|
DECLARE_NAPI_FUNCTION("stringParmas",StringParmas),
|
|
};
|
|
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
|
SeachParamsInit(env, exports);
|
|
UrlInit(env, exports);
|
|
return exports;
|
|
}
|
|
|
|
extern "C"
|
|
__attribute__((visibility("default"))) void NAPI_url_GetJSCode(const char** buf, int* bufLen)
|
|
{
|
|
if (buf != nullptr) {
|
|
*buf = _binary_js_url_js_start;
|
|
}
|
|
|
|
if (bufLen != nullptr) {
|
|
*bufLen = _binary_js_url_js_end - _binary_js_url_js_start;
|
|
}
|
|
}
|
|
|
|
static napi_module UrlModule =
|
|
{
|
|
.nm_version = 1,
|
|
.nm_flags = 0,
|
|
.nm_filename = nullptr,
|
|
.nm_register_func = Init,
|
|
.nm_modname = "url",
|
|
.nm_priv = ((void*)0),
|
|
.reserved = {0},
|
|
};
|
|
extern "C" __attribute__((constructor)) void RegisterModule()
|
|
{
|
|
napi_module_register(&UrlModule);
|
|
} |