1 /* 2 * Copyright (c) 2024 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 #ifndef JS_CONCURRENT_MODULE_COMMON_HELPER_PATH_HELPER_H 17 #define JS_CONCURRENT_MODULE_COMMON_HELPER_PATH_HELPER_H 18 19 #include <vector> 20 #include <string> 21 22 #include "native_engine/native_engine.h" 23 24 namespace Commonlibrary::Concurrent::Common::Helper { 25 class PathHelper { 26 public: 27 static constexpr char PREFIX_BUNDLE[] = "@bundle:"; 28 static constexpr char DOUBLE_POINT_TAG[] = ".."; 29 static constexpr char NAME_SPACE_TAG = '@'; 30 static constexpr char POINT_TAG[] = "."; 31 static constexpr char SLASH_TAG = '/'; 32 static constexpr char EXT_NAME_ETS[] = ".ets"; 33 static constexpr char EXT_NAME_TS[] = ".ts"; 34 static constexpr char EXT_NAME_JS[] = ".js"; 35 ConcatFileNameForWorker(napi_env env,std::string & script,std::string & fileName,bool & isRelativePath)36 static void ConcatFileNameForWorker(napi_env env, std::string &script, std::string &fileName, bool &isRelativePath) 37 { 38 std::string moduleName; 39 if (script.find_first_of(POINT_TAG) == 0) { 40 isRelativePath = true; 41 } 42 reinterpret_cast<NativeEngine*>(env)->GetCurrentModuleInfo(moduleName, fileName, isRelativePath); 43 if (isRelativePath) { 44 // if input is relative path, need to concat new recordName. 45 size_t pos = moduleName.rfind(SLASH_TAG); 46 if (pos != std::string::npos) { 47 moduleName = moduleName.substr(0, pos + 1); // from spcific file to dir 48 } 49 script = moduleName + script; 50 script = NormalizePath(script); // remove ../ and .ets 51 } else { 52 script = moduleName + script; 53 } 54 } 55 NormalizePath(const std::string & entryPoint)56 static std::string NormalizePath(const std::string &entryPoint) 57 { 58 std::string res; 59 size_t prev = 0; 60 size_t curr = entryPoint.find(SLASH_TAG); 61 std::vector<std::string> elems; 62 // eliminate parent directory path 63 while (curr != std::string::npos) { 64 if (curr > prev) { 65 std::string elem = entryPoint.substr(prev, curr - prev); 66 if (elem == DOUBLE_POINT_TAG && entryPoint.at(curr) == SLASH_TAG 67 && !elems.empty()) { // looking for xxx/../ 68 elems.pop_back(); 69 } else if (elem != POINT_TAG && elem != DOUBLE_POINT_TAG) { // remove ./ 70 elems.push_back(elem); 71 } 72 } 73 prev = curr + 1; 74 curr = entryPoint.find(SLASH_TAG, prev); 75 } 76 if (prev != entryPoint.size()) { 77 elems.push_back(entryPoint.substr(prev)); 78 } 79 for (auto e : elems) { 80 if (res.size() == 0 && entryPoint.at(0) != SLASH_TAG) { 81 res.append(e); 82 continue; 83 } 84 res.append(1, SLASH_TAG).append(e); 85 } 86 // remore suffix 87 size_t pos = res.rfind(POINT_TAG); 88 if (pos != std::string::npos) { 89 std::string suffix = res.substr(pos); 90 if (suffix == EXT_NAME_ETS || suffix == EXT_NAME_TS || suffix == EXT_NAME_JS) { 91 res.erase(pos, suffix.length()); 92 } 93 } 94 return res; 95 } 96 }; 97 } // namespace Commonlibrary::Concurrent::Common::Helper 98 #endif // JS_CONCURRENT_MODULE_COMMON_HELPER_OBJECT_HELPER_H 99