• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <securec.h>
17 #include <regex>
18 #include <string>
19 #include "js_util.h"
20 
21 using namespace OHOS::Request::Upload;
22 namespace OHOS::Request::UploadNapi {
23 
24 static const std::map<Download::ExceptionErrorCode, std::string> ErrorCodeToMsg {
25     {Download::EXCEPTION_OK, Download::EXCEPTION_OK_INFO },
26     {Download::EXCEPTION_PERMISSION, Download::EXCEPTION_PERMISSION_INFO },
27     {Download::EXCEPTION_PARAMETER_CHECK, Download::EXCEPTION_PARAMETER_CHECK_INFO },
28     {Download::EXCEPTION_UNSUPPORTED, Download::EXCEPTION_UNSUPPORTED_INFO },
29     {Download::EXCEPTION_FILE_IO, Download::EXCEPTION_FILE_IO_INFO },
30     {Download::EXCEPTION_FILE_PATH, Download::EXCEPTION_FILE_PATH_INFO },
31     {Download::EXCEPTION_SERVICE_ERROR, Download::EXCEPTION_SERVICE_ERROR_INFO },
32     {Download::EXCEPTION_OTHER, Download::EXCEPTION_OTHER_INFO },
33 };
34 
ThrowError(napi_env env,Download::ExceptionErrorCode code,const std::string & msg)35 void JSUtil::ThrowError(napi_env env, Download::ExceptionErrorCode code, const std::string &msg)
36 {
37     napi_value error = CreateBusinessError(env, code, msg);
38     napi_throw(env, error);
39 }
40 
Convert2String(napi_env env,napi_value jsString)41 std::string JSUtil::Convert2String(napi_env env, napi_value jsString)
42 {
43     size_t maxLen = JSUtil::MAX_LEN;
44     napi_status status = napi_get_value_string_utf8(env, jsString, NULL, 0, &maxLen);
45     if (status != napi_ok) {
46         GET_AND_THROW_LAST_ERROR((env));
47         maxLen = JSUtil::MAX_LEN;
48     }
49     if (maxLen == 0) {
50         return std::string();
51     }
52     char *buf = new char[maxLen + 1];
53     if (buf == nullptr) {
54         return std::string();
55     }
56     size_t len = 0;
57     status = napi_get_value_string_utf8(env, jsString, buf, maxLen + 1, &len);
58     if (status != napi_ok) {
59         GET_AND_THROW_LAST_ERROR((env));
60     }
61     buf[len] = 0;
62     std::string value(buf);
63     delete[] buf;
64     return value;
65 }
66 
Convert2StrVector(napi_env env,napi_value value)67 std::vector<std::string> JSUtil::Convert2StrVector(napi_env env, napi_value value)
68 {
69     uint32_t arrLen = 0;
70     napi_get_array_length(env, value, &arrLen);
71     if (arrLen == 0) {
72         return {};
73     }
74     std::vector<std::string> result;
75     for (size_t i = 0; i < arrLen; ++i) {
76         napi_value element;
77         napi_get_element(env, value, i, &element);
78         result.push_back(Convert2String(env, element));
79     }
80     return result;
81 }
82 
Convert2Header(napi_env env,napi_value value)83 std::vector<std::string> JSUtil::Convert2Header(napi_env env, napi_value value)
84 {
85     std::vector<std::string> result;
86     napi_value keyArr = nullptr;
87     napi_status status = napi_get_property_names(env, value, &keyArr);
88     if (status != napi_ok) {
89         return result;
90     }
91 
92     uint32_t len = 0;
93     status = napi_get_array_length(env, keyArr, &len);
94     if (status != napi_ok) {
95         UPLOAD_HILOGE(UPLOAD_MODULE_JS_NAPI, "Convert2Header. napi_get_array_length statue Error");
96         return result;
97     }
98     for (uint32_t i = 0; i < len; i++) {
99         napi_value keyNapiValue = nullptr;
100         napi_get_element(env, keyArr, i, &keyNapiValue);
101 
102         napi_valuetype valueType;
103         napi_typeof(env, keyNapiValue, &valueType);
104         if (valueType != napi_valuetype::napi_string) {
105             continue;
106         }
107 
108         char key[JSUtil::MAX_LEN] = { 0 };
109         size_t cValueLength = 0;
110         napi_get_value_string_utf8(env, keyNapiValue, key, JSUtil::MAX_LEN - 1, &cValueLength);
111 
112         napi_value jsvalue = nullptr;
113         napi_get_named_property(env, value, key, &jsvalue);
114         if (jsvalue != nullptr) {
115             std::string item(key);
116             item.append(SEPARATOR);
117             item.append(Convert2String(env, jsvalue));
118             result.push_back(item);
119         }
120     }
121     return result;
122 }
123 
Convert2JSStringVector(napi_env env,const std::vector<std::string> & cStrings)124 napi_value JSUtil::Convert2JSStringVector(napi_env env, const std::vector<std::string> &cStrings)
125 {
126     napi_value jsStrings = nullptr;
127     napi_create_array_with_length(env, cStrings.size(), &jsStrings);
128     int index = 0;
129     for (const auto &cString : cStrings) {
130         napi_value jsString = Convert2JSString(env, cString);
131         napi_set_element(env, jsStrings, index++, jsString);
132     }
133     return jsStrings;
134 }
135 
Convert2JSValue(napi_env env,const std::vector<int32_t> & cInts)136 napi_value JSUtil::Convert2JSValue(napi_env env, const std::vector<int32_t> &cInts)
137 {
138     napi_value jsInts = nullptr;
139     napi_create_array_with_length(env, cInts.size(), &jsInts);
140     int index = 0;
141     for (const auto &cInt : cInts) {
142         napi_value jsInt = Convert2JSValue(env, cInt);
143         napi_set_element(env, jsInts, index++, jsInt);
144     }
145     return jsInts;
146 }
147 
Convert2JSUploadResponse(napi_env env,const Upload::UploadResponse & response)148 napi_value JSUtil::Convert2JSUploadResponse(napi_env env, const Upload::UploadResponse &response)
149 {
150     napi_value jsResponse = nullptr;
151     napi_create_object(env, &jsResponse);
152     napi_set_named_property(env, jsResponse, "code", Convert2JSValue(env, response.code));
153     napi_set_named_property(env, jsResponse, "data", Convert2JSString(env, response.data));
154     napi_set_named_property(env, jsResponse, "headers", Convert2JSString(env, response.headers));
155     return jsResponse;
156 }
157 
Convert2JSValue(napi_env env,const std::vector<Upload::TaskState> & taskStates)158 napi_value JSUtil::Convert2JSValue(napi_env env, const std::vector<Upload::TaskState> &taskStates)
159 {
160     napi_value jsTaskStates = nullptr;
161     napi_create_array_with_length(env, taskStates.size(), &jsTaskStates);
162     int index = 0;
163     for (const auto &taskState : taskStates) {
164         napi_value jsTaskState = nullptr;
165         napi_create_object(env, &jsTaskState);
166         napi_set_named_property(env, jsTaskState, "path", Convert2JSString(env, taskState.path));
167         napi_set_named_property(env, jsTaskState, "responseCode", Convert2JSValue(env, taskState.responseCode));
168         napi_set_named_property(env, jsTaskState, "message", Convert2JSString(env, taskState.message));
169         napi_set_element(env, jsTaskStates, index++, jsTaskState);
170     }
171     return jsTaskStates;
172 }
173 
Convert2JSValue(napi_env env,int32_t value)174 napi_value JSUtil::Convert2JSValue(napi_env env, int32_t value)
175 {
176     napi_value jsValue;
177     napi_status status = napi_create_int32(env, value, &jsValue);
178     if (status != napi_ok) {
179         return nullptr;
180     }
181     return jsValue;
182 }
183 
ParseFunction(napi_env env,napi_value & object,const char * name,napi_ref & output)184 bool JSUtil::ParseFunction(napi_env env, napi_value &object, const char *name, napi_ref &output)
185 {
186     napi_value value = GetNamedProperty(env, object, name);
187     if (value == nullptr) {
188         return false;
189     }
190     napi_valuetype valueType = napi_null;
191     auto ret = napi_typeof(env, value, &valueType);
192     if ((ret != napi_ok) || (valueType != napi_function)) {
193         return false;
194     }
195     napi_create_reference(env, value, 1, &output);
196     return true;
197 }
198 
Convert2JSString(napi_env env,const std::string & cString)199 napi_value JSUtil::Convert2JSString(napi_env env, const std::string &cString)
200 {
201     napi_value jsValue = nullptr;
202     napi_create_string_utf8(env, cString.c_str(), cString.size(), &jsValue);
203     return jsValue;
204 }
205 
ParseUploadConfig(napi_env env,napi_value jsConfig,const std::string & version)206 std::shared_ptr<UploadConfig> JSUtil::ParseUploadConfig(napi_env env, napi_value jsConfig,
207     const std::string &version)
208 {
209     UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "ParseUploadConfig in");
210     UploadConfig config;
211     config.protocolVersion = version;
212     auto func = (config.protocolVersion == API5) ? ToUploadOption : ToUploadConfig;
213     bool ret = func(env, jsConfig, config);
214     if ((!ret) || (!CheckConfig(config))) {
215         return nullptr;
216     }
217     return std::make_shared<UploadConfig>(config);
218 }
219 
CheckConfig(const UploadConfig & config)220 bool JSUtil::CheckConfig(const UploadConfig &config)
221 {
222     if (!CheckUrl(config.url)) {
223         return false;
224     }
225     if (config.files.empty()) {
226         return false;
227     }
228     return CheckMethod(config.method);
229 }
230 
CheckUrl(const std::string & url)231 bool JSUtil::CheckUrl(const std::string &url)
232 {
233     if (url.empty()) {
234         return false;
235     }
236     return regex_match(url, std::regex("^http(s)?:\\/\\/.+"));
237 }
238 
CheckMethod(const std::string & method)239 bool JSUtil::CheckMethod(const std::string &method)
240 {
241     return (method == POST || method == PUT);
242 }
243 
GetNamedProperty(napi_env env,napi_value object,const std::string & propertyName)244 napi_value JSUtil::GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName)
245 {
246     napi_value value = nullptr;
247     bool hasProperty = false;
248     NAPI_CALL(env, napi_has_named_property(env, object, propertyName.c_str(), &hasProperty));
249     if (!hasProperty) {
250         return value;
251     }
252     NAPI_CALL(env, napi_get_named_property(env, object, propertyName.c_str(), &value));
253     return value;
254 }
255 
HasNamedProperty(napi_env env,napi_value object,const std::string & propertyName)256 bool JSUtil::HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName)
257 {
258     bool hasProperty = false;
259     NAPI_CALL_BASE(env, napi_has_named_property(env, object, propertyName.c_str(), &hasProperty), false);
260     return hasProperty;
261 }
262 
SetData(napi_env env,napi_value jsConfig,UploadConfig & config)263 bool JSUtil::SetData(napi_env env, napi_value jsConfig, UploadConfig &config)
264 {
265     if (!HasNamedProperty(env, jsConfig, "data")) {
266         return true;
267     }
268     napi_value data = nullptr;
269     napi_get_named_property(env, jsConfig, "data", &data);
270     if (data == nullptr) {
271         UPLOAD_HILOGE(UPLOAD_MODULE_JS_NAPI, "GetNamedProperty SetData failed");
272         return false;
273     }
274     config.data = Convert2RequestDataVector(env, data);
275     return true;
276 }
277 
SetFiles(napi_env env,napi_value jsConfig,UploadConfig & config)278 bool JSUtil::SetFiles(napi_env env, napi_value jsConfig, UploadConfig &config)
279 {
280     napi_value files = GetNamedProperty(env, jsConfig, "files");
281     if (files == nullptr) {
282         UPLOAD_HILOGE(UPLOAD_MODULE_JS_NAPI, "GetNamedProperty SetFiles failed");
283         return false;
284     }
285     config.files = Convert2FileVector(env, files, config.protocolVersion);
286     return true;
287 }
288 
SetHeader(napi_env env,napi_value jsConfig,UploadConfig & config)289 bool JSUtil::SetHeader(napi_env env, napi_value jsConfig, UploadConfig &config)
290 {
291     if (!HasNamedProperty(env, jsConfig, "header")) {
292         return true;
293     }
294     napi_value header = nullptr;
295     napi_get_named_property(env, jsConfig, "header", &header);
296     if (header == nullptr) {
297         UPLOAD_HILOGE(UPLOAD_MODULE_JS_NAPI, "GetNamedProperty SetHeader failed");
298         return false;
299     }
300     config.header = Convert2Header(env, header);
301     return true;
302 }
303 
ToUploadOption(napi_env env,napi_value jsConfig,UploadConfig & config)304 bool JSUtil::ToUploadOption(napi_env env, napi_value jsConfig, UploadConfig &config)
305 {
306     if (!SetMandatoryParam(env, jsConfig, "url", config.url)) {
307         return false;
308     }
309     if (!SetData(env, jsConfig, config)) {
310         return false;
311     }
312     if (!SetFiles(env, jsConfig, config)) {
313         return false;
314     }
315     if (!SetHeader(env, jsConfig, config)) {
316         return false;
317     }
318     if (!SetOptionalParam(env, jsConfig, "method", config.method)) {
319         return false;
320     }
321     return true;
322 }
323 
ToUploadConfig(napi_env env,napi_value jsConfig,UploadConfig & config)324 bool JSUtil::ToUploadConfig(napi_env env, napi_value jsConfig, UploadConfig &config)
325 {
326     napi_value url = GetNamedProperty(env, jsConfig, "url");
327     if (url == nullptr) {
328         return false;
329     }
330     config.url = Convert2String(env, url);
331 
332     napi_value header = GetNamedProperty(env, jsConfig, "header");
333     if (header == nullptr) {
334         return false;
335     }
336     config.header = Convert2Header(env, header);
337 
338     napi_value method = GetNamedProperty(env, jsConfig, "method");
339     if (method == nullptr) {
340         return false;
341     }
342     config.method = Convert2String(env, method);
343     transform(config.method.begin(), config.method.end(), config.method.begin(), ::toupper);
344 
345     napi_value files = GetNamedProperty(env, jsConfig, "files");
346     if (files == nullptr) {
347         return false;
348     }
349     config.files = Convert2FileVector(env, files, config.protocolVersion);
350 
351     napi_value data = GetNamedProperty(env, jsConfig, "data");
352     if (data == nullptr) {
353         return false;
354     }
355     config.data = Convert2RequestDataVector(env, data);
356     return true;
357 }
358 
Convert2JSUploadConfig(napi_env env,const UploadConfig & config)359 napi_value JSUtil::Convert2JSUploadConfig(napi_env env, const UploadConfig &config)
360 {
361     napi_value jsConfig = nullptr;
362     napi_create_object(env, &jsConfig);
363     napi_set_named_property(env, jsConfig, "url", Convert2JSString(env, config.url));
364     napi_set_named_property(env, jsConfig, "header", Convert2JSStringVector(env, config.header));
365     napi_set_named_property(env, jsConfig, "method", Convert2JSString(env, config.method));
366     napi_set_named_property(env, jsConfig, "files", Convert2JSFileVector(env, config.files));
367     napi_set_named_property(env, jsConfig, "data", Convert2JSRequestDataVector(env, config.data));
368     return jsConfig;
369 }
370 
Convert2File(napi_env env,napi_value jsFile,Upload::File & file)371 bool JSUtil::Convert2File(napi_env env, napi_value jsFile, Upload::File &file)
372 {
373     napi_value filename = GetNamedProperty(env, jsFile, "filename");
374     if (filename == nullptr) {
375         return false;
376     }
377     file.filename = Convert2String(env, filename);
378 
379     napi_value name = GetNamedProperty(env, jsFile, "name");
380     if (name == nullptr) {
381         return false;
382     }
383     file.name = Convert2String(env, name);
384 
385     napi_value uri = GetNamedProperty(env, jsFile, "uri");
386     if (uri == nullptr) {
387         return false;
388     }
389     file.uri = Convert2String(env, uri);
390 
391     napi_value type = GetNamedProperty(env, jsFile, "type");
392     if (type == nullptr) {
393         return false;
394     }
395     file.type = Convert2String(env, type);
396     return true;
397 }
398 
SetMandatoryParam(napi_env env,napi_value jsValue,const std::string & str,std::string & out)399 bool JSUtil::SetMandatoryParam(napi_env env, napi_value jsValue, const std::string &str, std::string &out)
400 {
401     napi_value value = GetNamedProperty(env, jsValue, str);
402     if (value == nullptr) {
403         UPLOAD_HILOGE(UPLOAD_MODULE_JS_NAPI, "SetMandatoryParam failed");
404         return false;
405     }
406     out = Convert2String(env, value);
407     return true;
408 }
409 
SetOptionalParam(napi_env env,napi_value jsValue,const std::string & str,std::string & out)410 bool JSUtil::SetOptionalParam(napi_env env, napi_value jsValue, const std::string &str, std::string &out)
411 {
412     if (!HasNamedProperty(env, jsValue, str)) {
413         out = (str == "method" ? "POST" : "");
414         return true;
415     }
416     napi_value value = nullptr;
417     napi_get_named_property(env, jsValue, str.c_str(), &value);
418     if (value == nullptr) {
419         UPLOAD_HILOGE(UPLOAD_MODULE_JS_NAPI, "SetOptionalParam failed");
420         return false;
421     }
422     out = Convert2String(env, value);
423     return true;
424 }
425 
Convert2FileL5(napi_env env,napi_value jsFile,Upload::File & file)426 bool JSUtil::Convert2FileL5(napi_env env, napi_value jsFile, Upload::File &file)
427 {
428     if (!SetOptionalParam(env, jsFile, "filename", file.filename)) {
429         return false;
430     }
431     if (!SetOptionalParam(env, jsFile, "name", file.name)) {
432         return false;
433     }
434     if (!SetMandatoryParam(env, jsFile, "uri", file.uri)) {
435         return false;
436     }
437     if (!SetOptionalParam(env, jsFile, "type", file.type)) {
438         return false;
439     }
440     return true;
441 }
442 
Convert2JSFile(napi_env env,const Upload::File & file)443 napi_value JSUtil::Convert2JSFile(napi_env env, const Upload::File &file)
444 {
445     napi_value jsFile = nullptr;
446     napi_create_object(env, &jsFile);
447     napi_set_named_property(env, jsFile, "filename", Convert2JSString(env, file.filename));
448     napi_set_named_property(env, jsFile, "name", Convert2JSString(env, file.name));
449     napi_set_named_property(env, jsFile, "uri", Convert2JSString(env, file.uri));
450     napi_set_named_property(env, jsFile, "type", Convert2JSString(env, file.type));
451     return jsFile;
452 }
453 
Convert2FileVector(napi_env env,napi_value jsFiles,const std::string & version)454 std::vector<Upload::File> JSUtil::Convert2FileVector(napi_env env, napi_value jsFiles, const std::string &version)
455 {
456     bool isArray = false;
457     napi_is_array(env, jsFiles, &isArray);
458     NAPI_ASSERT_BASE(env, isArray, "not array", { });
459     uint32_t length = 0;
460     napi_get_array_length(env, jsFiles, &length);
461     std::vector<Upload::File> files;
462     for (uint32_t i = 0; i < length; ++i) {
463         napi_value jsFile = nullptr;
464         napi_handle_scope scope = nullptr;
465         napi_open_handle_scope(env, &scope);
466         napi_get_element(env, jsFiles, i, &jsFile);
467         if (jsFile == nullptr) {
468             continue;
469         }
470 
471         Upload::File file;
472         auto func = (version == API5) ? Convert2FileL5 : Convert2File;
473         bool ret = func(env, jsFile, file);
474         if (!ret) {
475             continue;
476         }
477         files.push_back(file);
478         napi_close_handle_scope(env, scope);
479     }
480     return files;
481 }
482 
Convert2JSFileVector(napi_env env,const std::vector<Upload::File> & files)483 napi_value JSUtil::Convert2JSFileVector(napi_env env, const std::vector<Upload::File> &files)
484 {
485     napi_value jsFiles = nullptr;
486     napi_create_array_with_length(env, files.size(), &jsFiles);
487     int index = 0;
488     for (const auto &file : files) {
489         napi_value jsFile = Convert2JSFile(env, file);
490         napi_set_element(env, jsFiles, index++, jsFile);
491     }
492     return jsFiles;
493 }
494 
Convert2RequestData(napi_env env,napi_value jsRequestData)495 Upload::RequestData JSUtil::Convert2RequestData(napi_env env, napi_value jsRequestData)
496 {
497     Upload::RequestData requestData;
498     napi_value value = nullptr;
499     napi_get_named_property(env, jsRequestData, "name", &value);
500     if (value != nullptr) {
501         requestData.name = Convert2String(env, value);
502     }
503     value = nullptr;
504     napi_get_named_property(env, jsRequestData, "value", &value);
505     if (value != nullptr) {
506         requestData.value = Convert2String(env, value);
507     }
508     return requestData;
509 }
510 
Convert2JSRequestData(napi_env env,const Upload::RequestData & requestData)511 napi_value JSUtil::Convert2JSRequestData(napi_env env, const Upload::RequestData &requestData)
512 {
513     napi_value jsRequestData = nullptr;
514     napi_create_object(env, &jsRequestData);
515     napi_set_named_property(env, jsRequestData, "name", Convert2JSString(env, requestData.name));
516     napi_set_named_property(env, jsRequestData, "value", Convert2JSString(env, requestData.value));
517     return jsRequestData;
518 }
519 
Convert2RequestDataVector(napi_env env,napi_value jsRequestDatas)520 std::vector<Upload::RequestData> JSUtil::Convert2RequestDataVector(napi_env env, napi_value jsRequestDatas)
521 {
522     bool isArray = false;
523     napi_is_array(env, jsRequestDatas, &isArray);
524     NAPI_ASSERT_BASE(env, isArray, "not array", { });
525     uint32_t length = 0;
526     napi_get_array_length(env, jsRequestDatas, &length);
527     std::vector<Upload::RequestData> requestDatas;
528     for (uint32_t i = 0; i < length; ++i) {
529         napi_value requestData = nullptr;
530         napi_get_element(env, jsRequestDatas, i, &requestData);
531         if (requestData == nullptr) {
532             continue;
533         }
534         requestDatas.push_back(Convert2RequestData(env, requestData));
535     }
536     return requestDatas;
537 }
538 
Convert2JSRequestDataVector(napi_env env,const std::vector<Upload::RequestData> & requestDatas)539 napi_value JSUtil::Convert2JSRequestDataVector(napi_env env, const std::vector<Upload::RequestData> &requestDatas)
540 {
541     napi_value jsRequestDatas = nullptr;
542     napi_create_array_with_length(env, requestDatas.size(), &jsRequestDatas);
543     int index = 0;
544     for (const auto &requestData : requestDatas) {
545         napi_value jsRequestData = Convert2JSRequestData(env, requestData);
546         napi_set_element(env, jsRequestData, index++, jsRequestDatas);
547     }
548     return jsRequestDatas;
549 }
550 
Equals(napi_env env,napi_value value,napi_ref copy)551 bool JSUtil::Equals(napi_env env, napi_value value, napi_ref copy)
552 {
553     if (copy == nullptr) {
554         return (value == nullptr);
555     }
556     napi_value copyValue = nullptr;
557     napi_get_reference_value(env, copy, &copyValue);
558 
559     bool isEquals = false;
560     napi_strict_equals(env, value, copyValue, &isEquals);
561     return isEquals;
562 }
563 
CheckParamNumber(size_t argc,bool IsRequiredParam)564 bool JSUtil::CheckParamNumber(size_t argc, bool IsRequiredParam)
565 {
566     if (IsRequiredParam) {
567         return argc == TWO_ARG;
568     }
569     return (argc == ONE_ARG || argc == TWO_ARG);
570 }
571 
CheckParamType(napi_env env,napi_value jsType,napi_valuetype type)572 bool JSUtil::CheckParamType(napi_env env, napi_value jsType, napi_valuetype type)
573 {
574     napi_valuetype valueType = napi_undefined;
575     napi_status status = napi_typeof(env, jsType, &valueType);
576     if (status != napi_ok || valueType != type) {
577         return false;
578     }
579     return true;
580 }
581 
CreateBusinessError(napi_env env,const Download::ExceptionErrorCode & errorCode,const std::string & errorMessage)582 napi_value JSUtil::CreateBusinessError(napi_env env, const
583     Download::ExceptionErrorCode &errorCode, const std::string &errorMessage)
584 {
585     napi_value error = nullptr;
586     napi_value code = nullptr;
587     napi_value msg = nullptr;
588     auto iter = ErrorCodeToMsg.find(errorCode);
589     std::string strMsg = (iter != ErrorCodeToMsg.end() ? iter->second : "") + "   "+ errorMessage;
590     NAPI_CALL(env, napi_create_string_utf8(env, strMsg.c_str(), strMsg.length(), &msg));
591     NAPI_CALL(env, napi_create_uint32(env, errorCode, &code));
592     NAPI_CALL(env, napi_create_error(env, nullptr, msg, &error));
593     napi_set_named_property(env, error, "code", code);
594     return error;
595 }
596 
GetMessage(const std::vector<Upload::TaskState> & taskStates,std::string & msg)597 void JSUtil::GetMessage(const std::vector<Upload::TaskState> &taskStates, std::string &msg)
598 {
599     for (auto &vmem : taskStates) {
600         std::string strMsg;
601         auto iter = ErrorCodeToMsg.find(static_cast<Download::ExceptionErrorCode>(vmem.responseCode));
602         if (iter != ErrorCodeToMsg.end()) {
603             strMsg = " --{" + vmem.path + " " + std::to_string(vmem.responseCode) + " " + iter->second + "}-- ";
604         }
605         msg += strMsg;
606     }
607 }
608 } // namespace OHOS::Request::UploadNapi