1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciimportScripts('trace_streamer_builtin.js');
17fb726d48Sopenharmony_ciimport { execProtoForWorker } from './data-trafic/utils/ExecProtoForWorker';
18fb726d48Sopenharmony_ciimport { QueryEnum, TraficEnum } from './data-trafic/utils/QueryEnum';
19fb726d48Sopenharmony_ci// @ts-ignore
20fb726d48Sopenharmony_ciimport { temp_init_sql_list } from './TempSql';
21fb726d48Sopenharmony_ci// @ts-ignore
22fb726d48Sopenharmony_ciimport { BatchSphData } from '../proto/SphBaseData';
23fb726d48Sopenharmony_ci
24fb726d48Sopenharmony_cienum TsLogLevel {
25fb726d48Sopenharmony_ci  DEBUG = 0,
26fb726d48Sopenharmony_ci  INFO = 1,
27fb726d48Sopenharmony_ci  WARN = 2,
28fb726d48Sopenharmony_ci  ERROR = 3,
29fb726d48Sopenharmony_ci  FATAL = 4,
30fb726d48Sopenharmony_ci  OFF = 5,
31fb726d48Sopenharmony_ci}
32fb726d48Sopenharmony_ci
33fb726d48Sopenharmony_cilet wasmModule: unknown = null;
34fb726d48Sopenharmony_cilet enc = new TextEncoder();
35fb726d48Sopenharmony_cilet dec = new TextDecoder();
36fb726d48Sopenharmony_cilet arr: Uint8Array | undefined;
37fb726d48Sopenharmony_ciconst REQ_BUF_SIZE = 4 * 1024 * 1024;
38fb726d48Sopenharmony_cilet reqBufferAddr: number = -1;
39fb726d48Sopenharmony_cilet bufferSlice: Array<Uint8Array> = [];
40fb726d48Sopenharmony_cilet headUnitArray: Uint8Array | undefined;
41fb726d48Sopenharmony_cilet thirdWasmMap = new Map();
42fb726d48Sopenharmony_cilet thirdJsonResult = new Map();
43fb726d48Sopenharmony_ci
44fb726d48Sopenharmony_ciconst CONTENT_TYPE_CMDLINES = 2;
45fb726d48Sopenharmony_ciconst CONTENT_TYPE_TGIDS = 3;
46fb726d48Sopenharmony_ciconst CONTENT_TYPE_HEADER_PAGE = 30;
47fb726d48Sopenharmony_ciconst CONTENT_TYPE_PRINTK_FORMATS = 31;
48fb726d48Sopenharmony_ciconst CONTENT_TYPE_KALLSYMS = 32;
49fb726d48Sopenharmony_ci
50fb726d48Sopenharmony_cilet arkTsData: Array<Uint8Array> = [];
51fb726d48Sopenharmony_cilet arkTsDataSize: number = 0;
52fb726d48Sopenharmony_ci
53fb726d48Sopenharmony_cilet currentAction: string = '';
54fb726d48Sopenharmony_cilet currentActionId: string = '';
55fb726d48Sopenharmony_cilet ffrtFileCacheKey = '-1';
56fb726d48Sopenharmony_cilet indexDB: IDBDatabase;
57fb726d48Sopenharmony_ciconst maxSize = 48 * 1024 * 1024;
58fb726d48Sopenharmony_ciconst currentTSLogLevel = TsLogLevel.OFF;
59fb726d48Sopenharmony_ci//@ts-ignore
60fb726d48Sopenharmony_cilet protoDataMap: Map<QueryEnum, BatchSphData> = new Map<QueryEnum, BatchSphData>();
61fb726d48Sopenharmony_cifunction clear(): void {
62fb726d48Sopenharmony_ci  if (wasmModule !== null) {
63fb726d48Sopenharmony_ci    //@ts-ignore
64fb726d48Sopenharmony_ci    wasmModule._TraceStreamerReset();
65fb726d48Sopenharmony_ci    wasmModule = null;
66fb726d48Sopenharmony_ci  }
67fb726d48Sopenharmony_ci  if (arr) {
68fb726d48Sopenharmony_ci    arr = undefined;
69fb726d48Sopenharmony_ci  }
70fb726d48Sopenharmony_ci  if (headUnitArray) {
71fb726d48Sopenharmony_ci    headUnitArray = undefined;
72fb726d48Sopenharmony_ci  }
73fb726d48Sopenharmony_ci  if (bufferSlice) {
74fb726d48Sopenharmony_ci    bufferSlice.length = 0;
75fb726d48Sopenharmony_ci  }
76fb726d48Sopenharmony_ci  thirdWasmMap.clear();
77fb726d48Sopenharmony_ci  thirdJsonResult.clear();
78fb726d48Sopenharmony_ci}
79fb726d48Sopenharmony_ci
80fb726d48Sopenharmony_ciself.addEventListener('unhandledrejection', (err) => {
81fb726d48Sopenharmony_ci  self.postMessage({
82fb726d48Sopenharmony_ci    id: currentActionId,
83fb726d48Sopenharmony_ci    action: currentAction,
84fb726d48Sopenharmony_ci    init: false,
85fb726d48Sopenharmony_ci    status: false,
86fb726d48Sopenharmony_ci    msg: err.reason.message,
87fb726d48Sopenharmony_ci  });
88fb726d48Sopenharmony_ci});
89fb726d48Sopenharmony_ci
90fb726d48Sopenharmony_cifunction initWASM(): Promise<unknown> {
91fb726d48Sopenharmony_ci  return new Promise((resolve, reject) => {
92fb726d48Sopenharmony_ci    //@ts-ignore
93fb726d48Sopenharmony_ci    let wasm = trace_streamer_builtin_wasm;
94fb726d48Sopenharmony_ci    wasmModule = wasm({
95fb726d48Sopenharmony_ci      locateFile: (s: unknown) => {
96fb726d48Sopenharmony_ci        return s;
97fb726d48Sopenharmony_ci      },
98fb726d48Sopenharmony_ci      print: (line: string) => {
99fb726d48Sopenharmony_ci        if (currentTSLogLevel < TsLogLevel.OFF) {
100fb726d48Sopenharmony_ci          console.log(line);
101fb726d48Sopenharmony_ci        }
102fb726d48Sopenharmony_ci      },
103fb726d48Sopenharmony_ci      printErr: (line: string) => {
104fb726d48Sopenharmony_ci        if (currentTSLogLevel < TsLogLevel.OFF) {
105fb726d48Sopenharmony_ci          console.error(line);
106fb726d48Sopenharmony_ci        }
107fb726d48Sopenharmony_ci      },
108fb726d48Sopenharmony_ci      onRuntimeInitialized: () => {
109fb726d48Sopenharmony_ci        resolve('ok');
110fb726d48Sopenharmony_ci      },
111fb726d48Sopenharmony_ci      onAbort: () => {
112fb726d48Sopenharmony_ci        reject('on abort');
113fb726d48Sopenharmony_ci      },
114fb726d48Sopenharmony_ci    });
115fb726d48Sopenharmony_ci  });
116fb726d48Sopenharmony_ci}
117fb726d48Sopenharmony_ci
118fb726d48Sopenharmony_cifunction initThirdWASM(wasmFunctionName: string): unknown {
119fb726d48Sopenharmony_ci  function callModelFun(functionName: string): unknown {
120fb726d48Sopenharmony_ci    let func = eval(functionName);
121fb726d48Sopenharmony_ci    return new func({
122fb726d48Sopenharmony_ci      locateFile: (s: unknown): unknown => {
123fb726d48Sopenharmony_ci        return s;
124fb726d48Sopenharmony_ci      },
125fb726d48Sopenharmony_ci      print: (line: string): void => {
126fb726d48Sopenharmony_ci        if (currentTSLogLevel < TsLogLevel.OFF) {
127fb726d48Sopenharmony_ci          console.log(line);
128fb726d48Sopenharmony_ci        }
129fb726d48Sopenharmony_ci      },
130fb726d48Sopenharmony_ci      printErr: (line: string): void => {
131fb726d48Sopenharmony_ci        if (currentTSLogLevel < TsLogLevel.OFF) {
132fb726d48Sopenharmony_ci          console.error(line);
133fb726d48Sopenharmony_ci        }
134fb726d48Sopenharmony_ci      },
135fb726d48Sopenharmony_ci      onRuntimeInitialized: (): void => { },
136fb726d48Sopenharmony_ci      onAbort: (): void => { },
137fb726d48Sopenharmony_ci    });
138fb726d48Sopenharmony_ci  }
139fb726d48Sopenharmony_ci
140fb726d48Sopenharmony_ci  return callModelFun(wasmFunctionName);
141fb726d48Sopenharmony_ci}
142fb726d48Sopenharmony_ci
143fb726d48Sopenharmony_cilet merged = (): Uint8Array => {
144fb726d48Sopenharmony_ci  let length = 0;
145fb726d48Sopenharmony_ci  bufferSlice.forEach((item) => {
146fb726d48Sopenharmony_ci    //@ts-ignore
147fb726d48Sopenharmony_ci    length += item.length;
148fb726d48Sopenharmony_ci  });
149fb726d48Sopenharmony_ci  let mergedArray = new Uint8Array(length);
150fb726d48Sopenharmony_ci  let offset = 0;
151fb726d48Sopenharmony_ci  bufferSlice.forEach((item) => {
152fb726d48Sopenharmony_ci    //@ts-ignore
153fb726d48Sopenharmony_ci    mergedArray.set(item, offset);
154fb726d48Sopenharmony_ci    //@ts-ignore
155fb726d48Sopenharmony_ci    offset += item.length;
156fb726d48Sopenharmony_ci  });
157fb726d48Sopenharmony_ci  return mergedArray;
158fb726d48Sopenharmony_ci};
159fb726d48Sopenharmony_ci
160fb726d48Sopenharmony_cilet translateJsonString = (str: string): string => {
161fb726d48Sopenharmony_ci  return str //   .padding
162fb726d48Sopenharmony_ci    .replace(/[\t|\r|\n]/g, '');
163fb726d48Sopenharmony_ci};
164fb726d48Sopenharmony_ci
165fb726d48Sopenharmony_cilet convertJSON = (): unknown[] => {
166fb726d48Sopenharmony_ci  try {
167fb726d48Sopenharmony_ci    let str = dec.decode(arr);
168fb726d48Sopenharmony_ci    let jsonArray: Array<unknown> = [];
169fb726d48Sopenharmony_ci    str = str.substring(str.indexOf('\n') + 1);
170fb726d48Sopenharmony_ci    if (!str) {
171fb726d48Sopenharmony_ci    } else {
172fb726d48Sopenharmony_ci      let parse;
173fb726d48Sopenharmony_ci      let tansStr: string;
174fb726d48Sopenharmony_ci      try {
175fb726d48Sopenharmony_ci        tansStr = str.replace(/[\t\r\n]/g, '');
176fb726d48Sopenharmony_ci        parse = JSON.parse(tansStr);
177fb726d48Sopenharmony_ci      } catch {
178fb726d48Sopenharmony_ci        try {
179fb726d48Sopenharmony_ci          tansStr = tansStr!.replace(/[^\x20-\x7E]/g, '?'); //匹配乱码字 符,将其转换为?
180fb726d48Sopenharmony_ci          parse = JSON.parse(tansStr);
181fb726d48Sopenharmony_ci        } catch {
182fb726d48Sopenharmony_ci          tansStr = tansStr!.replace(/\\/g, '\\\\');
183fb726d48Sopenharmony_ci          parse = JSON.parse(tansStr);
184fb726d48Sopenharmony_ci        }
185fb726d48Sopenharmony_ci      }
186fb726d48Sopenharmony_ci      let columns = parse.columns;
187fb726d48Sopenharmony_ci      let values = parse.values;
188fb726d48Sopenharmony_ci      for (let i = 0; i < values.length; i++) {
189fb726d48Sopenharmony_ci        let obj: unknown = {};
190fb726d48Sopenharmony_ci        for (let j = 0; j < columns.length; j++) {
191fb726d48Sopenharmony_ci          //@ts-ignore
192fb726d48Sopenharmony_ci          obj[columns[j]] = values[i][j];
193fb726d48Sopenharmony_ci        }
194fb726d48Sopenharmony_ci        jsonArray.push(obj);
195fb726d48Sopenharmony_ci      }
196fb726d48Sopenharmony_ci    }
197fb726d48Sopenharmony_ci    return jsonArray;
198fb726d48Sopenharmony_ci  } catch (e) {
199fb726d48Sopenharmony_ci    self.postMessage({
200fb726d48Sopenharmony_ci      id: currentActionId,
201fb726d48Sopenharmony_ci      action: currentAction,
202fb726d48Sopenharmony_ci      init: false,
203fb726d48Sopenharmony_ci      status: false,
204fb726d48Sopenharmony_ci      //@ts-ignore
205fb726d48Sopenharmony_ci      msg: e.message,
206fb726d48Sopenharmony_ci    });
207fb726d48Sopenharmony_ci    return [];
208fb726d48Sopenharmony_ci  }
209fb726d48Sopenharmony_ci};
210fb726d48Sopenharmony_ci
211fb726d48Sopenharmony_ci/**
212fb726d48Sopenharmony_ci * 计算预留缓存空间,如果空间不够,则删除部分缓存
213fb726d48Sopenharmony_ci * @param size
214fb726d48Sopenharmony_ci */
215fb726d48Sopenharmony_cifunction saveTraceFileBuffer(key: string, buffer: ArrayBuffer): void {
216fb726d48Sopenharmony_ci  obligateFileBufferSpace(buffer.byteLength).then(() => {
217fb726d48Sopenharmony_ci    caches.open(key).then((cache) => {
218fb726d48Sopenharmony_ci      let headers = new Headers();
219fb726d48Sopenharmony_ci      headers.append('Content-Length', `${buffer.byteLength}`);
220fb726d48Sopenharmony_ci      headers.append('Content-Type', 'application/octet-stream');
221fb726d48Sopenharmony_ci      cache
222fb726d48Sopenharmony_ci        .put(
223fb726d48Sopenharmony_ci          key,
224fb726d48Sopenharmony_ci          new Response(buffer, {
225fb726d48Sopenharmony_ci            status: 200,
226fb726d48Sopenharmony_ci            headers: headers,
227fb726d48Sopenharmony_ci          })
228fb726d48Sopenharmony_ci        )
229fb726d48Sopenharmony_ci        .then();
230fb726d48Sopenharmony_ci    });
231fb726d48Sopenharmony_ci  });
232fb726d48Sopenharmony_ci}
233fb726d48Sopenharmony_ci
234fb726d48Sopenharmony_ciasync function obligateFileBufferSpace(size: number): Promise<void> {
235fb726d48Sopenharmony_ci  let es = await navigator.storage.estimate();
236fb726d48Sopenharmony_ci  let remainderByte = (es.quota || 0) - (es.usage || 0) - 20 * 1024 * 1024;
237fb726d48Sopenharmony_ci  if (remainderByte < size) {
238fb726d48Sopenharmony_ci    let keys = await caches.keys();
239fb726d48Sopenharmony_ci    keys.sort((keyA, keyB) => {
240fb726d48Sopenharmony_ci      if (keyA.includes('/') && keyB.includes('/')) {
241fb726d48Sopenharmony_ci        let splitA = keyA.split('/');
242fb726d48Sopenharmony_ci        let splitB = keyB.split('/');
243fb726d48Sopenharmony_ci        let timeA = splitA[splitA.length - 1].split('-')[0];
244fb726d48Sopenharmony_ci        let timeB = splitB[splitB.length - 1].split('-')[0];
245fb726d48Sopenharmony_ci        return parseInt(timeA) - parseInt(timeB);
246fb726d48Sopenharmony_ci      } else {
247fb726d48Sopenharmony_ci        return 0;
248fb726d48Sopenharmony_ci      }
249fb726d48Sopenharmony_ci    });
250fb726d48Sopenharmony_ci    let needSize = size - remainderByte;
251fb726d48Sopenharmony_ci    for (let key of keys) {
252fb726d48Sopenharmony_ci      await caches.delete(key);
253fb726d48Sopenharmony_ci      let keySize = parseInt(key.split('-')[1]);
254fb726d48Sopenharmony_ci      if (keySize > needSize) {
255fb726d48Sopenharmony_ci        return;
256fb726d48Sopenharmony_ci      } else {
257fb726d48Sopenharmony_ci        needSize -= keySize;
258fb726d48Sopenharmony_ci      }
259fb726d48Sopenharmony_ci    }
260fb726d48Sopenharmony_ci  }
261fb726d48Sopenharmony_ci}
262fb726d48Sopenharmony_ci
263fb726d48Sopenharmony_ciasync function onmessageByOpenAction(e: MessageEvent): Promise<void> {
264fb726d48Sopenharmony_ci  await initWASM();
265fb726d48Sopenharmony_ci  ffrtFileCacheKey = '-1';
266fb726d48Sopenharmony_ci  // @ts-ignore
267fb726d48Sopenharmony_ci  self.postMessage({
268fb726d48Sopenharmony_ci    id: e.data.id,
269fb726d48Sopenharmony_ci    action: e.data.action,
270fb726d48Sopenharmony_ci    ready: true,
271fb726d48Sopenharmony_ci    index: 0,
272fb726d48Sopenharmony_ci  });
273fb726d48Sopenharmony_ci  let uint8Array = new Uint8Array(e.data.buffer);
274fb726d48Sopenharmony_ci  initModuleCallBackAndFun();
275fb726d48Sopenharmony_ci  parseThirdWasmByOpenAction(e);
276fb726d48Sopenharmony_ci  let wrSize = 0;
277fb726d48Sopenharmony_ci  let r2 = -1;
278fb726d48Sopenharmony_ci  if (isRawTrace(e.data)) {
279fb726d48Sopenharmony_ci    r2 = parseRawTraceByOpenAction(e, wrSize, r2, uint8Array);
280fb726d48Sopenharmony_ci  } else {
281fb726d48Sopenharmony_ci    r2 = parseNormalTraceByOpenAction(wrSize, r2, uint8Array);
282fb726d48Sopenharmony_ci  }
283fb726d48Sopenharmony_ci  //@ts-ignore
284fb726d48Sopenharmony_ci  wasmModule._TraceStreamerParseDataOver();
285fb726d48Sopenharmony_ci  for (let value of thirdWasmMap.values()) {
286fb726d48Sopenharmony_ci    value.model._TraceStreamerInParseDataOver();
287fb726d48Sopenharmony_ci  }
288fb726d48Sopenharmony_ci  postMessageByOpenAction(r2, e);
289fb726d48Sopenharmony_ci}
290fb726d48Sopenharmony_ci
291fb726d48Sopenharmony_cifunction initModuleCallBackAndFun(): void {
292fb726d48Sopenharmony_ci  let callback = (heapPtr: number, size: number, isEnd: number): void => {
293fb726d48Sopenharmony_ci    //@ts-ignore
294fb726d48Sopenharmony_ci    let out: Uint8Array = wasmModule.HEAPU8.slice(heapPtr, heapPtr + size);
295fb726d48Sopenharmony_ci    bufferSlice.push(out);
296fb726d48Sopenharmony_ci    if (isEnd === 1) {
297fb726d48Sopenharmony_ci      arr = merged();
298fb726d48Sopenharmony_ci      bufferSlice.length = 0;
299fb726d48Sopenharmony_ci    }
300fb726d48Sopenharmony_ci  };
301fb726d48Sopenharmony_ci  let ffrtConvertCallback = (heapPtr: number, size: number, isEnd: number): void => {
302fb726d48Sopenharmony_ci    if (isEnd !== 1) {
303fb726d48Sopenharmony_ci      //@ts-ignore
304fb726d48Sopenharmony_ci      let out: Uint8Array = wasmModule.HEAPU8.slice(heapPtr, heapPtr + size);
305fb726d48Sopenharmony_ci      bufferSlice.push(out);
306fb726d48Sopenharmony_ci    } else {
307fb726d48Sopenharmony_ci      arr = merged();
308fb726d48Sopenharmony_ci      bufferSlice.length = 0;
309fb726d48Sopenharmony_ci      ffrtFileCacheKey = `ffrt/${new Date().getTime()}-${arr.buffer.byteLength}`;
310fb726d48Sopenharmony_ci      saveTraceFileBuffer(ffrtFileCacheKey, arr.buffer);
311fb726d48Sopenharmony_ci    }
312fb726d48Sopenharmony_ci  };
313fb726d48Sopenharmony_ci  let tlvResultCallback = (heapPtr: number, size: number, type: number, isEnd: number): void => {
314fb726d48Sopenharmony_ci    //@ts-ignore
315fb726d48Sopenharmony_ci    let out: Uint8Array = wasmModule.HEAPU8.slice(heapPtr, heapPtr + size);
316fb726d48Sopenharmony_ci    protoDataMap.set(type, BatchSphData.decode(out).values);
317fb726d48Sopenharmony_ci  };
318fb726d48Sopenharmony_ci  //@ts-ignore
319fb726d48Sopenharmony_ci  let fn1 = wasmModule.addFunction(callback, 'viii');
320fb726d48Sopenharmony_ci  //@ts-ignore
321fb726d48Sopenharmony_ci  let fn2 = wasmModule.addFunction(ffrtConvertCallback, 'viii');
322fb726d48Sopenharmony_ci  //@ts-ignore
323fb726d48Sopenharmony_ci  let tlvResultFun = wasmModule.addFunction(tlvResultCallback, 'viiii');
324fb726d48Sopenharmony_ci  //@ts-ignore
325fb726d48Sopenharmony_ci  wasmModule._TraceStreamerSetLogLevel(currentTSLogLevel);
326fb726d48Sopenharmony_ci  //@ts-ignore
327fb726d48Sopenharmony_ci  reqBufferAddr = wasmModule._Initialize(REQ_BUF_SIZE, fn1, tlvResultFun, fn2);
328fb726d48Sopenharmony_ci}
329fb726d48Sopenharmony_ci
330fb726d48Sopenharmony_cifunction parseThirdWasmByOpenAction(e: MessageEvent): void {
331fb726d48Sopenharmony_ci  let parseConfig = e.data.parseConfig;
332fb726d48Sopenharmony_ci  if (parseConfig !== '') {
333fb726d48Sopenharmony_ci    let parseConfigArray = enc.encode(parseConfig);
334fb726d48Sopenharmony_ci    //@ts-ignore
335fb726d48Sopenharmony_ci    let parseConfigAddr = wasmModule._InitializeParseConfig(1024);
336fb726d48Sopenharmony_ci    //@ts-ignore
337fb726d48Sopenharmony_ci    wasmModule.HEAPU8.set(parseConfigArray, parseConfigAddr);
338fb726d48Sopenharmony_ci    //@ts-ignore
339fb726d48Sopenharmony_ci    wasmModule._TraceStreamerParserConfigEx(parseConfigArray.length);
340fb726d48Sopenharmony_ci  }
341fb726d48Sopenharmony_ci  let wasmConfigStr = e.data.wasmConfig;
342fb726d48Sopenharmony_ci  if (wasmConfigStr !== '' && wasmConfigStr.indexOf('WasmFiles') !== -1) {
343fb726d48Sopenharmony_ci    let wasmConfig = JSON.parse(wasmConfigStr);
344fb726d48Sopenharmony_ci    let wasmConfigs = wasmConfig.WasmFiles;
345fb726d48Sopenharmony_ci    let itemArray = wasmConfigs.map((item: unknown) => {
346fb726d48Sopenharmony_ci      //@ts-ignore
347fb726d48Sopenharmony_ci      return item.componentId + ';' + item.pluginName;
348fb726d48Sopenharmony_ci    });
349fb726d48Sopenharmony_ci    let thirdWasmStr: string = itemArray.join(';');
350fb726d48Sopenharmony_ci    let configUintArray = enc.encode(thirdWasmStr + ';');
351fb726d48Sopenharmony_ci    //@ts-ignore
352fb726d48Sopenharmony_ci    wasmModule.HEAPU8.set(configUintArray, reqBufferAddr);
353fb726d48Sopenharmony_ci    //@ts-ignore
354fb726d48Sopenharmony_ci    wasmModule._TraceStreamerInitThirdPartyConfig(configUintArray.length);
355fb726d48Sopenharmony_ci    let first = true;
356fb726d48Sopenharmony_ci    let sendDataCallback = (heapPtr: number, size: number, componentID: number): void => {
357fb726d48Sopenharmony_ci      if (componentID === 100) {
358fb726d48Sopenharmony_ci        if (first) {
359fb726d48Sopenharmony_ci          first = false;
360fb726d48Sopenharmony_ci          //@ts-ignore
361fb726d48Sopenharmony_ci          headUnitArray = wasmModule.HEAPU8.slice(heapPtr, heapPtr + size);
362fb726d48Sopenharmony_ci        }
363fb726d48Sopenharmony_ci        return;
364fb726d48Sopenharmony_ci      }
365fb726d48Sopenharmony_ci      let configs = wasmConfigs.filter((wasmConfig: unknown) => {
366fb726d48Sopenharmony_ci        //@ts-ignore
367fb726d48Sopenharmony_ci        return wasmConfig.componentId === componentID;
368fb726d48Sopenharmony_ci      });
369fb726d48Sopenharmony_ci      if (configs.length > 0) {
370fb726d48Sopenharmony_ci        let config = configs[0];
371fb726d48Sopenharmony_ci        let model = thirdWasmMap.get(componentID);
372fb726d48Sopenharmony_ci        if (!model && config.componentId === componentID) {
373fb726d48Sopenharmony_ci          importScripts(config.wasmJsName);
374fb726d48Sopenharmony_ci          setThirdWasmMap(config, heapPtr, size, componentID);
375fb726d48Sopenharmony_ci        } else {
376fb726d48Sopenharmony_ci          let mm = model.model;
377fb726d48Sopenharmony_ci          //@ts-ignore
378fb726d48Sopenharmony_ci          let out: Uint8Array = wasmModule.HEAPU8.slice(heapPtr, heapPtr + size);
379fb726d48Sopenharmony_ci          mm.HEAPU8.set(out, model.bufferAddr);
380fb726d48Sopenharmony_ci          mm._ParserData(out.length, componentID);
381fb726d48Sopenharmony_ci        }
382fb726d48Sopenharmony_ci      }
383fb726d48Sopenharmony_ci    };
384fb726d48Sopenharmony_ci    //@ts-ignore
385fb726d48Sopenharmony_ci    let fn1 = wasmModule.addFunction(sendDataCallback, 'viii');
386fb726d48Sopenharmony_ci    //@ts-ignore
387fb726d48Sopenharmony_ci    wasmModule._TraceStreamerSetThirdPartyDataDealer(fn1, REQ_BUF_SIZE);
388fb726d48Sopenharmony_ci  }
389fb726d48Sopenharmony_ci}
390fb726d48Sopenharmony_ci
391fb726d48Sopenharmony_cifunction isCommonData(dataType: number): boolean {
392fb726d48Sopenharmony_ci  return (
393fb726d48Sopenharmony_ci    dataType === CONTENT_TYPE_CMDLINES ||
394fb726d48Sopenharmony_ci    dataType === CONTENT_TYPE_TGIDS ||
395fb726d48Sopenharmony_ci    dataType === CONTENT_TYPE_HEADER_PAGE ||
396fb726d48Sopenharmony_ci    dataType === CONTENT_TYPE_PRINTK_FORMATS ||
397fb726d48Sopenharmony_ci    dataType === CONTENT_TYPE_KALLSYMS
398fb726d48Sopenharmony_ci  );
399fb726d48Sopenharmony_ci}
400fb726d48Sopenharmony_ci
401fb726d48Sopenharmony_cifunction parseRawTraceByOpenAction(e: MessageEvent, wrSize: number, r2: number, uint8Array: Uint8Array): number {
402fb726d48Sopenharmony_ci  let commonDataOffsetList: Array<{ startOffset: number; endOffset: number }> = []; // common Data
403fb726d48Sopenharmony_ci  let offset = 12;
404fb726d48Sopenharmony_ci  let tlvTypeLength = 4;
405fb726d48Sopenharmony_ci  let headArray = uint8Array.slice(0, offset);
406fb726d48Sopenharmony_ci  let commonTotalLength = 0;
407fb726d48Sopenharmony_ci  while (offset < uint8Array.length) {
408fb726d48Sopenharmony_ci    let commonDataOffset = { startOffset: offset, endOffset: offset };
409fb726d48Sopenharmony_ci    let dataTypeData = e.data.buffer.slice(offset, offset + tlvTypeLength);
410fb726d48Sopenharmony_ci    offset += tlvTypeLength;
411fb726d48Sopenharmony_ci    let dataType = Array.from(new Uint32Array(dataTypeData));
412fb726d48Sopenharmony_ci    let currentLData = e.data.buffer.slice(offset, offset + tlvTypeLength);
413fb726d48Sopenharmony_ci    offset += tlvTypeLength;
414fb726d48Sopenharmony_ci    let currentVLength = Array.from(new Uint32Array(currentLData));
415fb726d48Sopenharmony_ci    offset += currentVLength[0];
416fb726d48Sopenharmony_ci    commonDataOffset.endOffset = offset;
417fb726d48Sopenharmony_ci    if (isCommonData(dataType[0])) {
418fb726d48Sopenharmony_ci      commonTotalLength += commonDataOffset.endOffset - commonDataOffset.startOffset;
419fb726d48Sopenharmony_ci      commonDataOffsetList.push(commonDataOffset);
420fb726d48Sopenharmony_ci    }
421fb726d48Sopenharmony_ci  }
422fb726d48Sopenharmony_ci  let frontData = new Uint8Array(headArray.byteLength + commonTotalLength); // HeadArray
423fb726d48Sopenharmony_ci  frontData.set(headArray, 0);
424fb726d48Sopenharmony_ci  let lengthOffset = headArray.byteLength;
425fb726d48Sopenharmony_ci  commonDataOffsetList.forEach((item) => {
426fb726d48Sopenharmony_ci    let commonData = uint8Array.slice(item.startOffset, item.endOffset);
427fb726d48Sopenharmony_ci    frontData.set(commonData, lengthOffset);
428fb726d48Sopenharmony_ci    lengthOffset += commonData.byteLength;
429fb726d48Sopenharmony_ci  });
430fb726d48Sopenharmony_ci  let freeData = uint8Array.slice(12);
431fb726d48Sopenharmony_ci  let final = new Uint8Array(frontData.length + freeData.length);
432fb726d48Sopenharmony_ci  final.set(frontData);
433fb726d48Sopenharmony_ci  final.set(freeData, frontData.length);
434fb726d48Sopenharmony_ci  wrSize = 0;
435fb726d48Sopenharmony_ci  while (wrSize < final.length) {
436fb726d48Sopenharmony_ci    const sliceLen = Math.min(final.length - wrSize, REQ_BUF_SIZE);
437fb726d48Sopenharmony_ci    const dataSlice = final.subarray(wrSize, wrSize + sliceLen);
438fb726d48Sopenharmony_ci    //@ts-ignore
439fb726d48Sopenharmony_ci    wasmModule.HEAPU8.set(dataSlice, reqBufferAddr);
440fb726d48Sopenharmony_ci    wrSize += sliceLen;
441fb726d48Sopenharmony_ci    //@ts-ignore
442fb726d48Sopenharmony_ci    r2 = wasmModule._TraceStreamerParseDataEx(sliceLen, wrSize === final.length ? 1 : 0);
443fb726d48Sopenharmony_ci    if (r2 === -1) {
444fb726d48Sopenharmony_ci      break;
445fb726d48Sopenharmony_ci    }
446fb726d48Sopenharmony_ci  }
447fb726d48Sopenharmony_ci  return r2;
448fb726d48Sopenharmony_ci}
449fb726d48Sopenharmony_ci
450fb726d48Sopenharmony_cifunction parseNormalTraceByOpenAction(wrSize: number, r2: number, uint8Array: Uint8Array): number {
451fb726d48Sopenharmony_ci  while (wrSize < uint8Array.length) {
452fb726d48Sopenharmony_ci    const sliceLen = Math.min(uint8Array.length - wrSize, REQ_BUF_SIZE);
453fb726d48Sopenharmony_ci    const dataSlice = uint8Array.subarray(wrSize, wrSize + sliceLen);
454fb726d48Sopenharmony_ci    //@ts-ignore
455fb726d48Sopenharmony_ci    wasmModule.HEAPU8.set(dataSlice, reqBufferAddr);
456fb726d48Sopenharmony_ci    wrSize += sliceLen;
457fb726d48Sopenharmony_ci    //@ts-ignore
458fb726d48Sopenharmony_ci    r2 = wasmModule._TraceStreamerParseDataEx(sliceLen, wrSize === uint8Array.length ? 1 : 0);
459fb726d48Sopenharmony_ci    if (r2 === -1) {
460fb726d48Sopenharmony_ci      break;
461fb726d48Sopenharmony_ci    }
462fb726d48Sopenharmony_ci  }
463fb726d48Sopenharmony_ci  return r2;
464fb726d48Sopenharmony_ci}
465fb726d48Sopenharmony_ci
466fb726d48Sopenharmony_cifunction setThirdWasmMap(config: unknown, heapPtr: number, size: number, componentID: number): void {
467fb726d48Sopenharmony_ci  //@ts-ignore
468fb726d48Sopenharmony_ci  let thirdMode = initThirdWASM(config.wasmName);
469fb726d48Sopenharmony_ci  //@ts-ignore
470fb726d48Sopenharmony_ci  let configPluginName = config.pluginName;
471fb726d48Sopenharmony_ci  let pluginNameUintArray = enc.encode(configPluginName);
472fb726d48Sopenharmony_ci  //@ts-ignore
473fb726d48Sopenharmony_ci  let pluginNameBuffer = thirdMode._InitPluginName(pluginNameUintArray.length);
474fb726d48Sopenharmony_ci  //@ts-ignore
475fb726d48Sopenharmony_ci  thirdMode.HEAPU8.set(pluginNameUintArray, pluginNameBuffer);
476fb726d48Sopenharmony_ci  //@ts-ignore
477fb726d48Sopenharmony_ci  thirdMode._TraceStreamerGetPluginNameEx(configPluginName.length);
478fb726d48Sopenharmony_ci  let thirdQueryDataCallBack = (heapPtr: number, size: number, isEnd: number, isConfig: number): void => {
479fb726d48Sopenharmony_ci    if (isConfig === 1) {
480fb726d48Sopenharmony_ci      //@ts-ignore
481fb726d48Sopenharmony_ci      let out: Uint8Array = thirdMode.HEAPU8.slice(heapPtr, heapPtr + size);
482fb726d48Sopenharmony_ci      thirdJsonResult.set(componentID, {
483fb726d48Sopenharmony_ci        jsonConfig: dec.decode(out),
484fb726d48Sopenharmony_ci        //@ts-ignore
485fb726d48Sopenharmony_ci        disPlayName: config.disPlayName,
486fb726d48Sopenharmony_ci        //@ts-ignore
487fb726d48Sopenharmony_ci        pluginName: config.pluginName,
488fb726d48Sopenharmony_ci      });
489fb726d48Sopenharmony_ci    } else {
490fb726d48Sopenharmony_ci      //@ts-ignore
491fb726d48Sopenharmony_ci      let out: Uint8Array = thirdMode.HEAPU8.slice(heapPtr, heapPtr + size);
492fb726d48Sopenharmony_ci      bufferSlice.push(out);
493fb726d48Sopenharmony_ci      if (isEnd === 1) {
494fb726d48Sopenharmony_ci        arr = merged();
495fb726d48Sopenharmony_ci        bufferSlice.length = 0;
496fb726d48Sopenharmony_ci      }
497fb726d48Sopenharmony_ci    }
498fb726d48Sopenharmony_ci  };
499fb726d48Sopenharmony_ci  //@ts-ignore
500fb726d48Sopenharmony_ci  let fn = thirdMode.addFunction(thirdQueryDataCallBack, 'viiii');
501fb726d48Sopenharmony_ci  //@ts-ignore
502fb726d48Sopenharmony_ci  let thirdreqBufferAddr = thirdMode._Init(fn, REQ_BUF_SIZE);
503fb726d48Sopenharmony_ci  initTraceRange(thirdMode);
504fb726d48Sopenharmony_ci  //@ts-ignore
505fb726d48Sopenharmony_ci  thirdMode._TraceStreamerInJsonConfig();
506fb726d48Sopenharmony_ci  //@ts-ignore
507fb726d48Sopenharmony_ci  thirdMode.HEAPU8.set(headUnitArray, thirdreqBufferAddr);
508fb726d48Sopenharmony_ci  //@ts-ignore
509fb726d48Sopenharmony_ci  thirdMode._ParserData(headUnitArray!.length, 100);
510fb726d48Sopenharmony_ci  //@ts-ignore
511fb726d48Sopenharmony_ci  let out: Uint8Array = wasmModule.HEAPU8.slice(heapPtr, heapPtr + size);
512fb726d48Sopenharmony_ci  //@ts-ignore
513fb726d48Sopenharmony_ci  thirdMode.HEAPU8.set(out, thirdreqBufferAddr);
514fb726d48Sopenharmony_ci  //@ts-ignore
515fb726d48Sopenharmony_ci  thirdMode._ParserData(out.length, componentID);
516fb726d48Sopenharmony_ci  thirdWasmMap.set(componentID, {
517fb726d48Sopenharmony_ci    model: thirdMode,
518fb726d48Sopenharmony_ci    bufferAddr: thirdreqBufferAddr,
519fb726d48Sopenharmony_ci  });
520fb726d48Sopenharmony_ci}
521fb726d48Sopenharmony_ci
522fb726d48Sopenharmony_cifunction postMessageByOpenAction(r2: number, e: MessageEvent): void {
523fb726d48Sopenharmony_ci  if (r2 === -1) {
524fb726d48Sopenharmony_ci    // @ts-ignore
525fb726d48Sopenharmony_ci    self.postMessage({
526fb726d48Sopenharmony_ci      id: e.data.id,
527fb726d48Sopenharmony_ci      action: e.data.action,
528fb726d48Sopenharmony_ci      init: false,
529fb726d48Sopenharmony_ci      msg: 'parse data error',
530fb726d48Sopenharmony_ci    });
531fb726d48Sopenharmony_ci    return;
532fb726d48Sopenharmony_ci  }
533fb726d48Sopenharmony_ci  // @ts-ignore
534fb726d48Sopenharmony_ci  if (temp_init_sql_list && temp_init_sql_list.length > 0) {
535fb726d48Sopenharmony_ci    // @ts-ignore
536fb726d48Sopenharmony_ci    temp_init_sql_list.forEach((item, index) => {
537fb726d48Sopenharmony_ci      createView(item);
538fb726d48Sopenharmony_ci      // @ts-ignore
539fb726d48Sopenharmony_ci      self.postMessage({ id: e.data.id, ready: true, index: index + 1 });
540fb726d48Sopenharmony_ci    });
541fb726d48Sopenharmony_ci  }
542fb726d48Sopenharmony_ci
543fb726d48Sopenharmony_ci  self.postMessage(
544fb726d48Sopenharmony_ci    {
545fb726d48Sopenharmony_ci      id: e.data.id,
546fb726d48Sopenharmony_ci      action: e.data.action,
547fb726d48Sopenharmony_ci      init: true,
548fb726d48Sopenharmony_ci      msg: 'ok',
549fb726d48Sopenharmony_ci      configSqlMap: thirdJsonResult,
550fb726d48Sopenharmony_ci      buffer: e.data.buffer,
551fb726d48Sopenharmony_ci      fileKey: ffrtFileCacheKey,
552fb726d48Sopenharmony_ci    },
553fb726d48Sopenharmony_ci    // @ts-ignore
554fb726d48Sopenharmony_ci    [e.data.buffer]
555fb726d48Sopenharmony_ci  );
556fb726d48Sopenharmony_ci}
557fb726d48Sopenharmony_ci
558fb726d48Sopenharmony_cifunction initTraceRange(thirdMode: unknown): void {
559fb726d48Sopenharmony_ci  let updateTraceTimeCallBack = (heapPtr: number, size: number): void => {
560fb726d48Sopenharmony_ci    //@ts-ignore
561fb726d48Sopenharmony_ci    let out: Uint8Array = thirdMode.HEAPU8.slice(heapPtr, heapPtr + size);
562fb726d48Sopenharmony_ci    //@ts-ignore
563fb726d48Sopenharmony_ci    wasmModule.HEAPU8.set(out, reqBufferAddr);
564fb726d48Sopenharmony_ci    //@ts-ignore
565fb726d48Sopenharmony_ci    wasmModule._UpdateTraceTime(out.length);
566fb726d48Sopenharmony_ci  };
567fb726d48Sopenharmony_ci  //@ts-ignore
568fb726d48Sopenharmony_ci  let traceRangeFn = thirdMode.addFunction(updateTraceTimeCallBack, 'vii');
569fb726d48Sopenharmony_ci  //@ts-ignore
570fb726d48Sopenharmony_ci  thirdMode._InitTraceRange(traceRangeFn, 1024);
571fb726d48Sopenharmony_ci}
572fb726d48Sopenharmony_ci
573fb726d48Sopenharmony_cifunction onmessageByExecAction(e: MessageEvent): void {
574fb726d48Sopenharmony_ci  query(e.data.name, e.data.sql, e.data.params);
575fb726d48Sopenharmony_ci  let jsonArray = convertJSON();
576fb726d48Sopenharmony_ci  // @ts-ignore
577fb726d48Sopenharmony_ci  self.postMessage({
578fb726d48Sopenharmony_ci    id: e.data.id,
579fb726d48Sopenharmony_ci    action: e.data.action,
580fb726d48Sopenharmony_ci    results: jsonArray,
581fb726d48Sopenharmony_ci  });
582fb726d48Sopenharmony_ci}
583fb726d48Sopenharmony_ci
584fb726d48Sopenharmony_cifunction onmessageByExecProtoAction(e: MessageEvent): void {
585fb726d48Sopenharmony_ci  let typeLength = 4;
586fb726d48Sopenharmony_ci  execProtoForWorker(e.data, (sql: string) => {
587fb726d48Sopenharmony_ci    let sqlUintArray = enc.encode(sql);
588fb726d48Sopenharmony_ci    if (e.data.params.trafic !== TraficEnum.ProtoBuffer) {
589fb726d48Sopenharmony_ci      //@ts-ignore
590fb726d48Sopenharmony_ci      wasmModule.HEAPU8.set(sqlUintArray, reqBufferAddr);
591fb726d48Sopenharmony_ci      //@ts-ignore
592fb726d48Sopenharmony_ci      wasmModule._TraceStreamerSqlQueryEx(sqlUintArray.length);
593fb726d48Sopenharmony_ci      let jsonArray = convertJSON();
594fb726d48Sopenharmony_ci      return jsonArray;
595fb726d48Sopenharmony_ci    } else {
596fb726d48Sopenharmony_ci      let allArray = new Uint8Array(typeLength + sqlUintArray.length);
597fb726d48Sopenharmony_ci      allArray[0] = e.data.name;
598fb726d48Sopenharmony_ci      allArray.set(sqlUintArray, typeLength);
599fb726d48Sopenharmony_ci      //@ts-ignore
600fb726d48Sopenharmony_ci      wasmModule.HEAPU8.set(allArray, reqBufferAddr);
601fb726d48Sopenharmony_ci      //@ts-ignore
602fb726d48Sopenharmony_ci      wasmModule._TraceStreamerSqlQueryToProtoCallback(allArray.length);
603fb726d48Sopenharmony_ci      let finalArrayBuffer = [];
604fb726d48Sopenharmony_ci      if (protoDataMap.has(e.data.name)) {
605fb726d48Sopenharmony_ci        //@ts-ignore
606fb726d48Sopenharmony_ci        finalArrayBuffer = protoDataMap.get(e.data.name);
607fb726d48Sopenharmony_ci        protoDataMap.delete(e.data.name);
608fb726d48Sopenharmony_ci      }
609fb726d48Sopenharmony_ci      return finalArrayBuffer;
610fb726d48Sopenharmony_ci    }
611fb726d48Sopenharmony_ci  });
612fb726d48Sopenharmony_ci}
613fb726d48Sopenharmony_ci
614fb726d48Sopenharmony_cifunction onmessageByExecBufAction(e: MessageEvent): void {
615fb726d48Sopenharmony_ci  query(e.data.name, e.data.sql, e.data.params);
616fb726d48Sopenharmony_ci  self.postMessage(
617fb726d48Sopenharmony_ci    { id: e.data.id, action: e.data.action, results: arr!.buffer },
618fb726d48Sopenharmony_ci    // @ts-ignore
619fb726d48Sopenharmony_ci    [arr.buffer]
620fb726d48Sopenharmony_ci  );
621fb726d48Sopenharmony_ci}
622fb726d48Sopenharmony_ci
623fb726d48Sopenharmony_cifunction onmessageByExecSdkAction(e: MessageEvent): void {
624fb726d48Sopenharmony_ci  querySdk(e.data.name, e.data.sql, e.data.params, e.data.action);
625fb726d48Sopenharmony_ci  let jsonArray = convertJSON();
626fb726d48Sopenharmony_ci  // @ts-ignore
627fb726d48Sopenharmony_ci  self.postMessage({
628fb726d48Sopenharmony_ci    id: e.data.id,
629fb726d48Sopenharmony_ci    action: e.data.action,
630fb726d48Sopenharmony_ci    results: jsonArray,
631fb726d48Sopenharmony_ci  });
632fb726d48Sopenharmony_ci}
633fb726d48Sopenharmony_ci
634fb726d48Sopenharmony_cifunction onmessageByExecMetricAction(e: MessageEvent): void {
635fb726d48Sopenharmony_ci  queryMetric(e.data.sql);
636fb726d48Sopenharmony_ci  let metricResult = dec.decode(arr);
637fb726d48Sopenharmony_ci  // @ts-ignore
638fb726d48Sopenharmony_ci  self.postMessage({
639fb726d48Sopenharmony_ci    id: e.data.id,
640fb726d48Sopenharmony_ci    action: e.data.action,
641fb726d48Sopenharmony_ci    results: metricResult,
642fb726d48Sopenharmony_ci  });
643fb726d48Sopenharmony_ci}
644fb726d48Sopenharmony_ci
645fb726d48Sopenharmony_cifunction onmessageByInitPortAction(e: MessageEvent): void {
646fb726d48Sopenharmony_ci  let port = e.ports[0];
647fb726d48Sopenharmony_ci  port.onmessage = (me): void => {
648fb726d48Sopenharmony_ci    query(me.data.action, me.data.sql, me.data.params);
649fb726d48Sopenharmony_ci    let msg = {
650fb726d48Sopenharmony_ci      id: me.data.id,
651fb726d48Sopenharmony_ci      action: me.data.action,
652fb726d48Sopenharmony_ci      results: arr!.buffer,
653fb726d48Sopenharmony_ci    };
654fb726d48Sopenharmony_ci    port.postMessage(msg, [arr!.buffer]);
655fb726d48Sopenharmony_ci  };
656fb726d48Sopenharmony_ci}
657fb726d48Sopenharmony_ci
658fb726d48Sopenharmony_cifunction onmessageByDownloadDBAction(e: MessageEvent): void {
659fb726d48Sopenharmony_ci  let bufferSliceUint: Array<Uint8Array> = [];
660fb726d48Sopenharmony_ci  let mergedUint = (): Uint8Array => {
661fb726d48Sopenharmony_ci    let length = 0;
662fb726d48Sopenharmony_ci    bufferSliceUint.forEach((item) => {
663fb726d48Sopenharmony_ci      length += item.length;
664fb726d48Sopenharmony_ci    });
665fb726d48Sopenharmony_ci    let mergedArray = new Uint8Array(length);
666fb726d48Sopenharmony_ci    let offset = 0;
667fb726d48Sopenharmony_ci    bufferSliceUint.forEach((item) => {
668fb726d48Sopenharmony_ci      mergedArray.set(item, offset);
669fb726d48Sopenharmony_ci      offset += item.length;
670fb726d48Sopenharmony_ci    });
671fb726d48Sopenharmony_ci    return mergedArray;
672fb726d48Sopenharmony_ci  };
673fb726d48Sopenharmony_ci  let getDownloadDb = (heapPtr: number, size: number, isEnd: number): void => {
674fb726d48Sopenharmony_ci    //@ts-ignore
675fb726d48Sopenharmony_ci    let out: Uint8Array = wasmModule.HEAPU8.slice(heapPtr, heapPtr + size);
676fb726d48Sopenharmony_ci    bufferSliceUint.push(out);
677fb726d48Sopenharmony_ci    if (isEnd === 1) {
678fb726d48Sopenharmony_ci      let arr: Uint8Array = mergedUint();
679fb726d48Sopenharmony_ci      self.postMessage({
680fb726d48Sopenharmony_ci        id: e.data.id,
681fb726d48Sopenharmony_ci        action: e.data.action,
682fb726d48Sopenharmony_ci        results: arr,
683fb726d48Sopenharmony_ci      });
684fb726d48Sopenharmony_ci    }
685fb726d48Sopenharmony_ci  };
686fb726d48Sopenharmony_ci  //@ts-ignore
687fb726d48Sopenharmony_ci  let fn1 = wasmModule.addFunction(getDownloadDb, 'viii');
688fb726d48Sopenharmony_ci  //@ts-ignore
689fb726d48Sopenharmony_ci  wasmModule._WasmExportDatabase(fn1);
690fb726d48Sopenharmony_ci}
691fb726d48Sopenharmony_ci
692fb726d48Sopenharmony_cifunction onmessageByUploadSoAction(e: MessageEvent): void {
693fb726d48Sopenharmony_ci  uploadSoActionId = e.data.id;
694fb726d48Sopenharmony_ci  const fileList = e.data.params as Array<File>;
695fb726d48Sopenharmony_ci  failedArray.length = 0;
696fb726d48Sopenharmony_ci  if (fileList) {
697fb726d48Sopenharmony_ci    fileList.sort((a, b) => b.size - a.size);
698fb726d48Sopenharmony_ci    soFileList = fileList;
699fb726d48Sopenharmony_ci    uploadFileIndex = 0;
700fb726d48Sopenharmony_ci    if (!uploadSoCallbackFn) {
701fb726d48Sopenharmony_ci      //@ts-ignore
702fb726d48Sopenharmony_ci      uploadSoCallbackFn = wasmModule.addFunction(uploadSoCallBack, 'viii');
703fb726d48Sopenharmony_ci    }
704fb726d48Sopenharmony_ci    uploadSoFile(soFileList[uploadFileIndex]).then();
705fb726d48Sopenharmony_ci  }
706fb726d48Sopenharmony_ci}
707fb726d48Sopenharmony_ci
708fb726d48Sopenharmony_ciasync function saveDataToIndexDB(
709fb726d48Sopenharmony_ci  currentChunk: Uint8Array,
710fb726d48Sopenharmony_ci  currentChunkOffset: number,
711fb726d48Sopenharmony_ci  fileType: string,
712fb726d48Sopenharmony_ci  timStamp: number,
713fb726d48Sopenharmony_ci  pageNum: number,
714fb726d48Sopenharmony_ci  saveIndex: number,
715fb726d48Sopenharmony_ci  saveStartOffset: number
716fb726d48Sopenharmony_ci): Promise<void> {
717fb726d48Sopenharmony_ci  let freeArray = currentChunk.slice(0, currentChunkOffset);
718fb726d48Sopenharmony_ci  await addDataToIndexeddb(indexDB, {
719fb726d48Sopenharmony_ci    buf: freeArray,
720fb726d48Sopenharmony_ci    id: `${fileType}_new_${timStamp}_${pageNum}_${saveIndex}`,
721fb726d48Sopenharmony_ci    fileType: `${fileType}_new`,
722fb726d48Sopenharmony_ci    pageNum: pageNum,
723fb726d48Sopenharmony_ci    startOffset: saveStartOffset,
724fb726d48Sopenharmony_ci    endOffset: saveStartOffset + maxSize,
725fb726d48Sopenharmony_ci    index: saveIndex,
726fb726d48Sopenharmony_ci    timStamp: timStamp,
727fb726d48Sopenharmony_ci  });
728fb726d48Sopenharmony_ci}
729fb726d48Sopenharmony_ci
730fb726d48Sopenharmony_cifunction cutLongTraceCallBackHandle(
731fb726d48Sopenharmony_ci  traceFileType: string,
732fb726d48Sopenharmony_ci  currentPageNum: number,
733fb726d48Sopenharmony_ci  heapPtr: number,
734fb726d48Sopenharmony_ci  size: number,
735fb726d48Sopenharmony_ci  dataType: number,
736fb726d48Sopenharmony_ci  newCutFilePageInfo: Map<
737fb726d48Sopenharmony_ci    string,
738fb726d48Sopenharmony_ci    {
739fb726d48Sopenharmony_ci      traceFileType: string;
740fb726d48Sopenharmony_ci      dataArray: [{ data: Uint8Array | Array<{ offset: number; size: number }>; dataTypes: string }];
741fb726d48Sopenharmony_ci    }
742fb726d48Sopenharmony_ci  >
743fb726d48Sopenharmony_ci): void {
744fb726d48Sopenharmony_ci  let key = `${traceFileType}_${currentPageNum}`;
745fb726d48Sopenharmony_ci  //@ts-ignore
746fb726d48Sopenharmony_ci  let out: Uint8Array = wasmModule.HEAPU8.slice(heapPtr, heapPtr + size);
747fb726d48Sopenharmony_ci  if (DataTypeEnum.data === dataType) {
748fb726d48Sopenharmony_ci    if (traceFileType === 'arkts') {
749fb726d48Sopenharmony_ci      arkTsData.push(out);
750fb726d48Sopenharmony_ci      arkTsDataSize += size;
751fb726d48Sopenharmony_ci    } else {
752fb726d48Sopenharmony_ci      if (newCutFilePageInfo.has(key)) {
753fb726d48Sopenharmony_ci        let newVar = newCutFilePageInfo.get(key);
754fb726d48Sopenharmony_ci        newVar?.dataArray.push({ data: out, dataTypes: 'data' });
755fb726d48Sopenharmony_ci      } else {
756fb726d48Sopenharmony_ci        newCutFilePageInfo.set(key, {
757fb726d48Sopenharmony_ci          traceFileType: traceFileType,
758fb726d48Sopenharmony_ci          dataArray: [{ data: out, dataTypes: 'data' }],
759fb726d48Sopenharmony_ci        });
760fb726d48Sopenharmony_ci      }
761fb726d48Sopenharmony_ci    }
762fb726d48Sopenharmony_ci  } else if (DataTypeEnum.json === dataType) {
763fb726d48Sopenharmony_ci    let cutFilePageInfo = newCutFilePageInfo.get(key);
764fb726d48Sopenharmony_ci    if (cutFilePageInfo) {
765fb726d48Sopenharmony_ci      let jsonStr: string = dec.decode(out);
766fb726d48Sopenharmony_ci      let jsonObj = JSON.parse(jsonStr);
767fb726d48Sopenharmony_ci      let valueArray: Array<{ offset: number; size: number }> = jsonObj.value;
768fb726d48Sopenharmony_ci      cutFilePageInfo.dataArray.push({ data: valueArray, dataTypes: 'json' });
769fb726d48Sopenharmony_ci    }
770fb726d48Sopenharmony_ci  }
771fb726d48Sopenharmony_ci}
772fb726d48Sopenharmony_ci
773fb726d48Sopenharmony_cifunction initSplitLongTraceModuleAndFun(
774fb726d48Sopenharmony_ci  headArray: Uint8Array,
775fb726d48Sopenharmony_ci  cutFileCallBack: (heapPtr: number, size: number, dataType: number, isEnd: number) => void
776fb726d48Sopenharmony_ci): number {
777fb726d48Sopenharmony_ci  //@ts-ignore
778fb726d48Sopenharmony_ci  splitReqBufferAddr = wasmModule._InitializeSplitFile(wasmModule.addFunction(cutFileCallBack, 'viiii'), REQ_BUF_SIZE);
779fb726d48Sopenharmony_ci  //@ts-ignore
780fb726d48Sopenharmony_ci  wasmModule.HEAPU8.set(headArray, splitReqBufferAddr);
781fb726d48Sopenharmony_ci  //@ts-ignore
782fb726d48Sopenharmony_ci  wasmModule._TraceStreamerGetLongTraceTimeSnapEx(headArray.length);
783fb726d48Sopenharmony_ci  return splitReqBufferAddr;
784fb726d48Sopenharmony_ci}
785fb726d48Sopenharmony_ci
786fb726d48Sopenharmony_ciasync function handleDataTypeBySplitLongTrace(
787fb726d48Sopenharmony_ci  receiveData: { data: Uint8Array | Array<{ offset: number; size: number }>; dataTypes: string },
788fb726d48Sopenharmony_ci  currentChunkOffset: number,
789fb726d48Sopenharmony_ci  currentChunk: Uint8Array,
790fb726d48Sopenharmony_ci  fileType: string,
791fb726d48Sopenharmony_ci  timStamp: number,
792fb726d48Sopenharmony_ci  pageNum: number,
793fb726d48Sopenharmony_ci  saveIndex: number,
794fb726d48Sopenharmony_ci  saveStartOffset: number
795fb726d48Sopenharmony_ci): Promise<[number, number, number, Uint8Array]> {
796fb726d48Sopenharmony_ci  let receiveDataArray = receiveData.data as Uint8Array;
797fb726d48Sopenharmony_ci  if (currentChunkOffset + receiveDataArray.length > maxSize) {
798fb726d48Sopenharmony_ci    let freeSize = maxSize - currentChunkOffset;
799fb726d48Sopenharmony_ci    let freeSaveData = receiveDataArray.slice(0, freeSize);
800fb726d48Sopenharmony_ci    currentChunk.set(freeSaveData, currentChunkOffset);
801fb726d48Sopenharmony_ci    await addDataToIndexeddb(indexDB, {
802fb726d48Sopenharmony_ci      buf: currentChunk,
803fb726d48Sopenharmony_ci      id: `${fileType}_new_${timStamp}_${pageNum}_${saveIndex}`,
804fb726d48Sopenharmony_ci      fileType: `${fileType}_new`,
805fb726d48Sopenharmony_ci      pageNum: pageNum,
806fb726d48Sopenharmony_ci      startOffset: saveStartOffset,
807fb726d48Sopenharmony_ci      endOffset: saveStartOffset + maxSize,
808fb726d48Sopenharmony_ci      index: saveIndex,
809fb726d48Sopenharmony_ci      timStamp: timStamp,
810fb726d48Sopenharmony_ci    });
811fb726d48Sopenharmony_ci    saveStartOffset += maxSize;
812fb726d48Sopenharmony_ci    saveIndex++;
813fb726d48Sopenharmony_ci    currentChunk = new Uint8Array(maxSize);
814fb726d48Sopenharmony_ci    let remnantArray = receiveDataArray.slice(freeSize);
815fb726d48Sopenharmony_ci    currentChunkOffset = 0;
816fb726d48Sopenharmony_ci    currentChunk.set(remnantArray, currentChunkOffset);
817fb726d48Sopenharmony_ci    currentChunkOffset += remnantArray.length;
818fb726d48Sopenharmony_ci  } else {
819fb726d48Sopenharmony_ci    currentChunk.set(receiveDataArray, currentChunkOffset);
820fb726d48Sopenharmony_ci    currentChunkOffset += receiveDataArray.length;
821fb726d48Sopenharmony_ci  }
822fb726d48Sopenharmony_ci  return [currentChunkOffset, saveIndex, saveStartOffset, currentChunk];
823fb726d48Sopenharmony_ci}
824fb726d48Sopenharmony_ci
825fb726d48Sopenharmony_ciasync function saveAllDataByLongTrace(
826fb726d48Sopenharmony_ci  range: IDBKeyRange,
827fb726d48Sopenharmony_ci  nowCutInfoList: Array<unknown>,
828fb726d48Sopenharmony_ci  searchDataInfo: {
829fb726d48Sopenharmony_ci    fileType: string;
830fb726d48Sopenharmony_ci    index: number;
831fb726d48Sopenharmony_ci    pageNum: number;
832fb726d48Sopenharmony_ci    startOffsetSize: number;
833fb726d48Sopenharmony_ci    endOffsetSize: number;
834fb726d48Sopenharmony_ci  }[],
835fb726d48Sopenharmony_ci  currentChunkOffset: number,
836fb726d48Sopenharmony_ci  currentChunk: Uint8Array,
837fb726d48Sopenharmony_ci  fileType: string,
838fb726d48Sopenharmony_ci  timStamp: number,
839fb726d48Sopenharmony_ci  pageNum: number,
840fb726d48Sopenharmony_ci  saveIndex: number,
841fb726d48Sopenharmony_ci  saveStartOffset: number
842fb726d48Sopenharmony_ci): Promise<[number, number, number, Uint8Array]> {
843fb726d48Sopenharmony_ci  let transaction = indexDB.transaction(STORE_NAME, 'readonly');
844fb726d48Sopenharmony_ci  let store = transaction.objectStore(STORE_NAME);
845fb726d48Sopenharmony_ci  let index = store.index('QueryCompleteFile');
846fb726d48Sopenharmony_ci  const getRequest = index.openCursor(range);
847fb726d48Sopenharmony_ci  let queryAllData = await queryDataFromIndexeddb(getRequest);
848fb726d48Sopenharmony_ci  let mergeData = indexedDataToBufferData(queryAllData);
849fb726d48Sopenharmony_ci  for (let cutOffsetObjIndex = 0; cutOffsetObjIndex < nowCutInfoList.length; cutOffsetObjIndex++) {
850fb726d48Sopenharmony_ci    let cutUseOffsetObj = nowCutInfoList[cutOffsetObjIndex];
851fb726d48Sopenharmony_ci    //@ts-ignore
852fb726d48Sopenharmony_ci    let endOffset = cutUseOffsetObj.offset + cutUseOffsetObj.size;
853fb726d48Sopenharmony_ci    let sliceData = mergeData.slice(
854fb726d48Sopenharmony_ci      //@ts-ignore
855fb726d48Sopenharmony_ci      cutUseOffsetObj.offset - searchDataInfo[0].startOffsetSize,
856fb726d48Sopenharmony_ci      endOffset - searchDataInfo[0].startOffsetSize
857fb726d48Sopenharmony_ci    );
858fb726d48Sopenharmony_ci    let sliceDataLength = sliceData.length;
859fb726d48Sopenharmony_ci    if (currentChunkOffset + sliceDataLength >= maxSize) {
860fb726d48Sopenharmony_ci      let handleCurrentData = new Uint8Array(currentChunkOffset + sliceDataLength);
861fb726d48Sopenharmony_ci      let freeSaveArray = currentChunk.slice(0, currentChunkOffset);
862fb726d48Sopenharmony_ci      handleCurrentData.set(freeSaveArray, 0);
863fb726d48Sopenharmony_ci      handleCurrentData.set(sliceData, freeSaveArray.length);
864fb726d48Sopenharmony_ci      let newSliceDataLength: number = Math.ceil(handleCurrentData.length / maxSize);
865fb726d48Sopenharmony_ci      for (let newSliceIndex = 0; newSliceIndex < newSliceDataLength; newSliceIndex++) {
866fb726d48Sopenharmony_ci        let newSliceSize = newSliceIndex * maxSize;
867fb726d48Sopenharmony_ci        let number = Math.min(newSliceSize + maxSize, handleCurrentData.length);
868fb726d48Sopenharmony_ci        let saveArray = handleCurrentData.slice(newSliceSize, number);
869fb726d48Sopenharmony_ci        if (newSliceIndex === newSliceDataLength - 1 && number - newSliceSize < maxSize) {
870fb726d48Sopenharmony_ci          currentChunk = new Uint8Array(maxSize);
871fb726d48Sopenharmony_ci          currentChunkOffset = 0;
872fb726d48Sopenharmony_ci          currentChunk.set(saveArray, currentChunkOffset);
873fb726d48Sopenharmony_ci          currentChunkOffset += saveArray.length;
874fb726d48Sopenharmony_ci        } else {
875fb726d48Sopenharmony_ci          await addDataToIndexeddb(indexDB, {
876fb726d48Sopenharmony_ci            buf: saveArray,
877fb726d48Sopenharmony_ci            id: `${fileType}_new_${timStamp}_${pageNum}_${saveIndex}`,
878fb726d48Sopenharmony_ci            fileType: `${fileType}_new`,
879fb726d48Sopenharmony_ci            pageNum: pageNum,
880fb726d48Sopenharmony_ci            startOffset: saveStartOffset,
881fb726d48Sopenharmony_ci            endOffset: saveStartOffset + maxSize,
882fb726d48Sopenharmony_ci            index: saveIndex,
883fb726d48Sopenharmony_ci            timStamp: timStamp,
884fb726d48Sopenharmony_ci          });
885fb726d48Sopenharmony_ci          saveStartOffset += maxSize;
886fb726d48Sopenharmony_ci          saveIndex++;
887fb726d48Sopenharmony_ci        }
888fb726d48Sopenharmony_ci      }
889fb726d48Sopenharmony_ci    } else {
890fb726d48Sopenharmony_ci      currentChunk.set(sliceData, currentChunkOffset);
891fb726d48Sopenharmony_ci      currentChunkOffset += sliceDataLength;
892fb726d48Sopenharmony_ci    }
893fb726d48Sopenharmony_ci  }
894fb726d48Sopenharmony_ci  return [currentChunkOffset, saveIndex, saveStartOffset, currentChunk];
895fb726d48Sopenharmony_ci}
896fb726d48Sopenharmony_ci
897fb726d48Sopenharmony_ciasync function handleJsonTypeBySplitLongTrace(
898fb726d48Sopenharmony_ci  receiveData: { data: Uint8Array | Array<{ offset: number; size: number }>; dataTypes: string },
899fb726d48Sopenharmony_ci  allIndexDataList: Array<{
900fb726d48Sopenharmony_ci    fileType: string;
901fb726d48Sopenharmony_ci    index: number;
902fb726d48Sopenharmony_ci    pageNum: number;
903fb726d48Sopenharmony_ci    startOffsetSize: number;
904fb726d48Sopenharmony_ci    endOffsetSize: number;
905fb726d48Sopenharmony_ci  }>,
906fb726d48Sopenharmony_ci  fileType: string,
907fb726d48Sopenharmony_ci  timStamp: number,
908fb726d48Sopenharmony_ci  currentChunkOffset: number,
909fb726d48Sopenharmony_ci  currentChunk: Uint8Array,
910fb726d48Sopenharmony_ci  pageNum: number,
911fb726d48Sopenharmony_ci  saveIndex: number,
912fb726d48Sopenharmony_ci  saveStartOffset: number
913fb726d48Sopenharmony_ci): Promise<[number, number, number, Uint8Array]> {
914fb726d48Sopenharmony_ci  let needCutMessage = receiveData.data as Array<{ offset: number; size: number }>;
915fb726d48Sopenharmony_ci  let startOffset = needCutMessage[0].offset;
916fb726d48Sopenharmony_ci  let nowCutInfoList: Array<{
917fb726d48Sopenharmony_ci    offset: number;
918fb726d48Sopenharmony_ci    size: number;
919fb726d48Sopenharmony_ci  }> = [];
920fb726d48Sopenharmony_ci  let isBeforeCutFinish = false;
921fb726d48Sopenharmony_ci  for (let needCutIndex = 0; needCutIndex < needCutMessage.length; needCutIndex++) {
922fb726d48Sopenharmony_ci    let cutInfo = needCutMessage[needCutIndex];
923fb726d48Sopenharmony_ci    if (isBeforeCutFinish) {
924fb726d48Sopenharmony_ci      startOffset = cutInfo.offset;
925fb726d48Sopenharmony_ci      isBeforeCutFinish = false;
926fb726d48Sopenharmony_ci      nowCutInfoList.length = 0;
927fb726d48Sopenharmony_ci    }
928fb726d48Sopenharmony_ci    if (cutInfo.offset + cutInfo.size - startOffset >= maxSize * 10 || needCutIndex === needCutMessage.length - 1) {
929fb726d48Sopenharmony_ci      nowCutInfoList.push(cutInfo);
930fb726d48Sopenharmony_ci      //@ts-ignore
931fb726d48Sopenharmony_ci      let nowStartCutOffset = nowCutInfoList[0].offset;
932fb726d48Sopenharmony_ci      let nowEndCutOffset = cutInfo.offset + cutInfo.size;
933fb726d48Sopenharmony_ci      let searchDataInfo = allIndexDataList.filter(
934fb726d48Sopenharmony_ci        (value: {
935fb726d48Sopenharmony_ci          fileType: string;
936fb726d48Sopenharmony_ci          index: number;
937fb726d48Sopenharmony_ci          pageNum: number;
938fb726d48Sopenharmony_ci          startOffsetSize: number;
939fb726d48Sopenharmony_ci          endOffsetSize: number;
940fb726d48Sopenharmony_ci        }) => {
941fb726d48Sopenharmony_ci          return (
942fb726d48Sopenharmony_ci            value.fileType === fileType &&
943fb726d48Sopenharmony_ci            value.startOffsetSize <= nowEndCutOffset &&
944fb726d48Sopenharmony_ci            value.endOffsetSize >= nowStartCutOffset
945fb726d48Sopenharmony_ci          );
946fb726d48Sopenharmony_ci        }
947fb726d48Sopenharmony_ci      );
948fb726d48Sopenharmony_ci      let startIndex = searchDataInfo[0].index;
949fb726d48Sopenharmony_ci      let endIndex = searchDataInfo[searchDataInfo.length - 1].index;
950fb726d48Sopenharmony_ci      let range = IDBKeyRange.bound(
951fb726d48Sopenharmony_ci        [timStamp, fileType, 0, startIndex],
952fb726d48Sopenharmony_ci        [timStamp, fileType, 0, endIndex],
953fb726d48Sopenharmony_ci        false,
954fb726d48Sopenharmony_ci        false
955fb726d48Sopenharmony_ci      );
956fb726d48Sopenharmony_ci      [currentChunkOffset, saveIndex, saveStartOffset, currentChunk] = await saveAllDataByLongTrace(
957fb726d48Sopenharmony_ci        range,
958fb726d48Sopenharmony_ci        nowCutInfoList,
959fb726d48Sopenharmony_ci        searchDataInfo,
960fb726d48Sopenharmony_ci        currentChunkOffset,
961fb726d48Sopenharmony_ci        currentChunk,
962fb726d48Sopenharmony_ci        fileType,
963fb726d48Sopenharmony_ci        timStamp,
964fb726d48Sopenharmony_ci        pageNum,
965fb726d48Sopenharmony_ci        saveIndex,
966fb726d48Sopenharmony_ci        saveStartOffset
967fb726d48Sopenharmony_ci      );
968fb726d48Sopenharmony_ci      isBeforeCutFinish = true;
969fb726d48Sopenharmony_ci    } else {
970fb726d48Sopenharmony_ci      nowCutInfoList.push(cutInfo);
971fb726d48Sopenharmony_ci    }
972fb726d48Sopenharmony_ci  }
973fb726d48Sopenharmony_ci  return [currentChunkOffset, saveIndex, saveStartOffset, currentChunk];
974fb726d48Sopenharmony_ci}
975fb726d48Sopenharmony_ci
976fb726d48Sopenharmony_ciasync function handleAllTypeDataByLongTrace(
977fb726d48Sopenharmony_ci  newCutFilePageInfo: Map<
978fb726d48Sopenharmony_ci    string,
979fb726d48Sopenharmony_ci    {
980fb726d48Sopenharmony_ci      traceFileType: string;
981fb726d48Sopenharmony_ci      dataArray: [
982fb726d48Sopenharmony_ci        {
983fb726d48Sopenharmony_ci          data: Uint8Array | Array<{ offset: number; size: number }>;
984fb726d48Sopenharmony_ci          dataTypes: string;
985fb726d48Sopenharmony_ci        }
986fb726d48Sopenharmony_ci      ];
987fb726d48Sopenharmony_ci    }
988fb726d48Sopenharmony_ci  >,
989fb726d48Sopenharmony_ci  timStamp: number,
990fb726d48Sopenharmony_ci  allIndexDataList: Array<{
991fb726d48Sopenharmony_ci    fileType: string;
992fb726d48Sopenharmony_ci    index: number;
993fb726d48Sopenharmony_ci    pageNum: number;
994fb726d48Sopenharmony_ci    startOffsetSize: number;
995fb726d48Sopenharmony_ci    endOffsetSize: number;
996fb726d48Sopenharmony_ci  }>
997fb726d48Sopenharmony_ci): Promise<void> {
998fb726d48Sopenharmony_ci  for (const [fileTypePageNum, fileMessage] of newCutFilePageInfo) {
999fb726d48Sopenharmony_ci    let fileTypePageNumArr = fileTypePageNum.split('_');
1000fb726d48Sopenharmony_ci    let fileType = fileTypePageNumArr[0];
1001fb726d48Sopenharmony_ci    let pageNum = Number(fileTypePageNumArr[1]);
1002fb726d48Sopenharmony_ci    let saveIndex = 0;
1003fb726d48Sopenharmony_ci    let saveStartOffset = 0;
1004fb726d48Sopenharmony_ci    let dataArray = fileMessage.dataArray;
1005fb726d48Sopenharmony_ci    let currentChunk = new Uint8Array(maxSize);
1006fb726d48Sopenharmony_ci    let currentChunkOffset = 0;
1007fb726d48Sopenharmony_ci    for (let fileDataIndex = 0; fileDataIndex < dataArray.length; fileDataIndex++) {
1008fb726d48Sopenharmony_ci      let receiveData = dataArray[fileDataIndex];
1009fb726d48Sopenharmony_ci      if (receiveData.dataTypes === 'data') {
1010fb726d48Sopenharmony_ci        [currentChunkOffset, saveIndex, saveStartOffset, currentChunk] = await handleDataTypeBySplitLongTrace(
1011fb726d48Sopenharmony_ci          receiveData,
1012fb726d48Sopenharmony_ci          currentChunkOffset,
1013fb726d48Sopenharmony_ci          currentChunk,
1014fb726d48Sopenharmony_ci          fileType,
1015fb726d48Sopenharmony_ci          timStamp,
1016fb726d48Sopenharmony_ci          pageNum,
1017fb726d48Sopenharmony_ci          saveIndex,
1018fb726d48Sopenharmony_ci          saveStartOffset
1019fb726d48Sopenharmony_ci        );
1020fb726d48Sopenharmony_ci      } else {
1021fb726d48Sopenharmony_ci        if (receiveData.data.length > 0) {
1022fb726d48Sopenharmony_ci          [currentChunkOffset, saveIndex, saveStartOffset, currentChunk] = await handleJsonTypeBySplitLongTrace(
1023fb726d48Sopenharmony_ci            receiveData,
1024fb726d48Sopenharmony_ci            allIndexDataList,
1025fb726d48Sopenharmony_ci            fileType,
1026fb726d48Sopenharmony_ci            timStamp,
1027fb726d48Sopenharmony_ci            currentChunkOffset,
1028fb726d48Sopenharmony_ci            currentChunk,
1029fb726d48Sopenharmony_ci            pageNum,
1030fb726d48Sopenharmony_ci            saveIndex,
1031fb726d48Sopenharmony_ci            saveStartOffset
1032fb726d48Sopenharmony_ci          );
1033fb726d48Sopenharmony_ci        }
1034fb726d48Sopenharmony_ci      }
1035fb726d48Sopenharmony_ci    }
1036fb726d48Sopenharmony_ci    if (currentChunkOffset !== 0) {
1037fb726d48Sopenharmony_ci      await saveDataToIndexDB(
1038fb726d48Sopenharmony_ci        currentChunk,
1039fb726d48Sopenharmony_ci        currentChunkOffset,
1040fb726d48Sopenharmony_ci        fileType,
1041fb726d48Sopenharmony_ci        timStamp,
1042fb726d48Sopenharmony_ci        pageNum,
1043fb726d48Sopenharmony_ci        saveIndex,
1044fb726d48Sopenharmony_ci        saveStartOffset
1045fb726d48Sopenharmony_ci      );
1046fb726d48Sopenharmony_ci      saveStartOffset += maxSize;
1047fb726d48Sopenharmony_ci      saveIndex++;
1048fb726d48Sopenharmony_ci    }
1049fb726d48Sopenharmony_ci  }
1050fb726d48Sopenharmony_ci}
1051fb726d48Sopenharmony_ci
1052fb726d48Sopenharmony_ciasync function onmessageByLongTraceAction(e: MessageEvent): Promise<void> {
1053fb726d48Sopenharmony_ci  await initWASM();
1054fb726d48Sopenharmony_ci  let result = {};
1055fb726d48Sopenharmony_ci  let headArray = e.data.params.headArray;
1056fb726d48Sopenharmony_ci  let timStamp = e.data.params.timeStamp;
1057fb726d48Sopenharmony_ci  let allIndexDataList = e.data.params.splitDataList;
1058fb726d48Sopenharmony_ci  let splitFileInfos = e.data.params.splitFileInfo as Array<{
1059fb726d48Sopenharmony_ci    fileType: string;
1060fb726d48Sopenharmony_ci    startIndex: number;
1061fb726d48Sopenharmony_ci    endIndex: number;
1062fb726d48Sopenharmony_ci    size: number;
1063fb726d48Sopenharmony_ci  }>;
1064fb726d48Sopenharmony_ci  let maxPageNum = headArray.length / 1024;
1065fb726d48Sopenharmony_ci  let currentPageNum = 0;
1066fb726d48Sopenharmony_ci  let splitReqBufferAddr: number;
1067fb726d48Sopenharmony_ci  if (splitFileInfos) {
1068fb726d48Sopenharmony_ci    let splitFileInfo = splitFileInfos.filter((splitFileInfo) => splitFileInfo.fileType !== 'trace');
1069fb726d48Sopenharmony_ci    if (splitFileInfo && splitFileInfo.length > 0) {
1070fb726d48Sopenharmony_ci      let traceFileType: string = '';
1071fb726d48Sopenharmony_ci      indexDB = await openDB();
1072fb726d48Sopenharmony_ci      let newCutFilePageInfo: Map<
1073fb726d48Sopenharmony_ci        string,
1074fb726d48Sopenharmony_ci        {
1075fb726d48Sopenharmony_ci          traceFileType: string;
1076fb726d48Sopenharmony_ci          dataArray: [{ data: Uint8Array | Array<{ offset: number; size: number }>; dataTypes: string }];
1077fb726d48Sopenharmony_ci        }
1078fb726d48Sopenharmony_ci      > = new Map();
1079fb726d48Sopenharmony_ci      let cutFileCallBack = (heapPtr: number, size: number, dataType: number, isEnd: number): void => {
1080fb726d48Sopenharmony_ci        cutLongTraceCallBackHandle(traceFileType, currentPageNum, heapPtr, size, dataType, newCutFilePageInfo);
1081fb726d48Sopenharmony_ci      };
1082fb726d48Sopenharmony_ci      splitReqBufferAddr = initSplitLongTraceModuleAndFun(headArray, cutFileCallBack);
1083fb726d48Sopenharmony_ci      for (let fileIndex = 0; fileIndex < splitFileInfo.length; fileIndex++) {
1084fb726d48Sopenharmony_ci        let fileInfo = splitFileInfo[fileIndex];
1085fb726d48Sopenharmony_ci        traceFileType = fileInfo.fileType;
1086fb726d48Sopenharmony_ci        for (let pageNum = 0; pageNum < maxPageNum; pageNum++) {
1087fb726d48Sopenharmony_ci          currentPageNum = pageNum;
1088fb726d48Sopenharmony_ci          await splitFileAndSave(timStamp, fileInfo, pageNum, splitReqBufferAddr);
1089fb726d48Sopenharmony_ci          await initWASM();
1090fb726d48Sopenharmony_ci          splitReqBufferAddr = initSplitLongTraceModuleAndFun(headArray, cutFileCallBack);
1091fb726d48Sopenharmony_ci        }
1092fb726d48Sopenharmony_ci      }
1093fb726d48Sopenharmony_ci      await handleAllTypeDataByLongTrace(newCutFilePageInfo, timStamp, allIndexDataList);
1094fb726d48Sopenharmony_ci    }
1095fb726d48Sopenharmony_ci  }
1096fb726d48Sopenharmony_ci  self.postMessage({
1097fb726d48Sopenharmony_ci    id: e.data.id,
1098fb726d48Sopenharmony_ci    action: e.data.action,
1099fb726d48Sopenharmony_ci    results: result,
1100fb726d48Sopenharmony_ci  });
1101fb726d48Sopenharmony_ci  return;
1102fb726d48Sopenharmony_ci}
1103fb726d48Sopenharmony_ci
1104fb726d48Sopenharmony_ciself.onmessage = async (e: MessageEvent): Promise<void> => {
1105fb726d48Sopenharmony_ci  currentAction = e.data.action;
1106fb726d48Sopenharmony_ci  currentActionId = e.data.id;
1107fb726d48Sopenharmony_ci  if (e.data.action === 'reset') {
1108fb726d48Sopenharmony_ci    clear();
1109fb726d48Sopenharmony_ci  } else if (e.data.action === 'open') {
1110fb726d48Sopenharmony_ci    await onmessageByOpenAction(e);
1111fb726d48Sopenharmony_ci  } else if (e.data.action === 'exec') {
1112fb726d48Sopenharmony_ci    onmessageByExecAction(e);
1113fb726d48Sopenharmony_ci  } else if (e.data.action === 'exec-proto') {
1114fb726d48Sopenharmony_ci    onmessageByExecProtoAction(e);
1115fb726d48Sopenharmony_ci  } else if (e.data.action === 'exec-buf') {
1116fb726d48Sopenharmony_ci    onmessageByExecBufAction(e);
1117fb726d48Sopenharmony_ci  } else if (e.data.action.startsWith('exec-sdk')) {
1118fb726d48Sopenharmony_ci    onmessageByExecSdkAction(e);
1119fb726d48Sopenharmony_ci  } else if (e.data.action.startsWith('exec-metric')) {
1120fb726d48Sopenharmony_ci    onmessageByExecMetricAction(e);
1121fb726d48Sopenharmony_ci  } else if (e.data.action === 'init-port') {
1122fb726d48Sopenharmony_ci    onmessageByInitPortAction(e);
1123fb726d48Sopenharmony_ci  } else if (e.data.action === 'download-db') {
1124fb726d48Sopenharmony_ci    onmessageByDownloadDBAction(e);
1125fb726d48Sopenharmony_ci  } else if (e.data.action === 'upload-so') {
1126fb726d48Sopenharmony_ci    onmessageByUploadSoAction(e);
1127fb726d48Sopenharmony_ci  } else if (e.data.action === 'cut-file') {
1128fb726d48Sopenharmony_ci    cutFileByRange(e);
1129fb726d48Sopenharmony_ci  } else if (e.data.action === 'long_trace') {
1130fb726d48Sopenharmony_ci    await onmessageByLongTraceAction(e);
1131fb726d48Sopenharmony_ci  }
1132fb726d48Sopenharmony_ci};
1133fb726d48Sopenharmony_ci
1134fb726d48Sopenharmony_cifunction indexedDataToBufferData(sourceData: unknown): Uint8Array {
1135fb726d48Sopenharmony_ci  let uintArrayLength = 0;
1136fb726d48Sopenharmony_ci  //@ts-ignore
1137fb726d48Sopenharmony_ci  let uintDataList = sourceData.map((item: unknown) => {
1138fb726d48Sopenharmony_ci    //@ts-ignore
1139fb726d48Sopenharmony_ci    let currentBufData = new Uint8Array(item.buf);
1140fb726d48Sopenharmony_ci    uintArrayLength += currentBufData.length;
1141fb726d48Sopenharmony_ci    return currentBufData;
1142fb726d48Sopenharmony_ci  });
1143fb726d48Sopenharmony_ci  let resultUintArray = new Uint8Array(uintArrayLength);
1144fb726d48Sopenharmony_ci  let offset = 0;
1145fb726d48Sopenharmony_ci  uintDataList.forEach((currentArray: Uint8Array) => {
1146fb726d48Sopenharmony_ci    resultUintArray.set(currentArray, offset);
1147fb726d48Sopenharmony_ci    offset += currentArray.length;
1148fb726d48Sopenharmony_ci  });
1149fb726d48Sopenharmony_ci  return resultUintArray;
1150fb726d48Sopenharmony_ci}
1151fb726d48Sopenharmony_ci
1152fb726d48Sopenharmony_ciasync function splitFileAndSaveArkTs(
1153fb726d48Sopenharmony_ci  currentChunkOffset: number,
1154fb726d48Sopenharmony_ci  currentChunk: Uint8Array,
1155fb726d48Sopenharmony_ci  fileType: string,
1156fb726d48Sopenharmony_ci  pageNum: number,
1157fb726d48Sopenharmony_ci  saveStartOffset: number,
1158fb726d48Sopenharmony_ci  saveIndex: number,
1159fb726d48Sopenharmony_ci  timStamp: number
1160fb726d48Sopenharmony_ci): Promise<void> {
1161fb726d48Sopenharmony_ci  for (let arkTsAllDataIndex = 0; arkTsAllDataIndex < arkTsData.length; arkTsAllDataIndex++) {
1162fb726d48Sopenharmony_ci    let currentArkTsData = arkTsData[arkTsAllDataIndex];
1163fb726d48Sopenharmony_ci    let freeSize = maxSize - currentChunkOffset;
1164fb726d48Sopenharmony_ci    if (currentArkTsData.length > freeSize) {
1165fb726d48Sopenharmony_ci      let freeSaveData = currentArkTsData.slice(0, freeSize);
1166fb726d48Sopenharmony_ci      currentChunk.set(freeSaveData, currentChunkOffset);
1167fb726d48Sopenharmony_ci      let arg2 = setArg(currentChunk, fileType, pageNum, saveStartOffset, saveIndex, timStamp);
1168fb726d48Sopenharmony_ci      await addDataToIndexeddb(indexDB, arg2);
1169fb726d48Sopenharmony_ci      saveStartOffset += maxSize;
1170fb726d48Sopenharmony_ci      saveIndex++;
1171fb726d48Sopenharmony_ci      let remnantData = currentArkTsData.slice(freeSize);
1172fb726d48Sopenharmony_ci      let remnantDataLength: number = Math.ceil(remnantData.length / maxSize);
1173fb726d48Sopenharmony_ci      for (let newSliceIndex = 0; newSliceIndex < remnantDataLength; newSliceIndex++) {
1174fb726d48Sopenharmony_ci        let newSliceSize = newSliceIndex * maxSize;
1175fb726d48Sopenharmony_ci        let number = Math.min(newSliceSize + maxSize, remnantData.length);
1176fb726d48Sopenharmony_ci        let saveArray = remnantData.slice(newSliceSize, number);
1177fb726d48Sopenharmony_ci        if (newSliceIndex === remnantDataLength - 1 && number - newSliceSize < maxSize) {
1178fb726d48Sopenharmony_ci          currentChunk = new Uint8Array(maxSize);
1179fb726d48Sopenharmony_ci          currentChunkOffset = 0;
1180fb726d48Sopenharmony_ci          currentChunk.set(saveArray, currentChunkOffset);
1181fb726d48Sopenharmony_ci          currentChunkOffset += saveArray.length;
1182fb726d48Sopenharmony_ci        } else {
1183fb726d48Sopenharmony_ci          let arg2 = setArg(saveArray, fileType, pageNum, saveStartOffset, saveIndex, timStamp);
1184fb726d48Sopenharmony_ci          await addDataToIndexeddb(indexDB, arg2);
1185fb726d48Sopenharmony_ci          saveStartOffset += maxSize;
1186fb726d48Sopenharmony_ci          saveIndex++;
1187fb726d48Sopenharmony_ci        }
1188fb726d48Sopenharmony_ci      }
1189fb726d48Sopenharmony_ci    } else {
1190fb726d48Sopenharmony_ci      currentChunk.set(currentArkTsData, currentChunkOffset);
1191fb726d48Sopenharmony_ci      currentChunkOffset += currentArkTsData.length;
1192fb726d48Sopenharmony_ci    }
1193fb726d48Sopenharmony_ci  }
1194fb726d48Sopenharmony_ci}
1195fb726d48Sopenharmony_ci
1196fb726d48Sopenharmony_ciconst splitFileAndSave = async (
1197fb726d48Sopenharmony_ci  timStamp: number,
1198fb726d48Sopenharmony_ci  fileInfo: {
1199fb726d48Sopenharmony_ci    fileType: string;
1200fb726d48Sopenharmony_ci    startIndex: number;
1201fb726d48Sopenharmony_ci    endIndex: number;
1202fb726d48Sopenharmony_ci    size: number;
1203fb726d48Sopenharmony_ci  },
1204fb726d48Sopenharmony_ci  pageNum: number,
1205fb726d48Sopenharmony_ci  splitBufAddr?: number
1206fb726d48Sopenharmony_ci): Promise<void> => {
1207fb726d48Sopenharmony_ci  let fileType = fileInfo.fileType;
1208fb726d48Sopenharmony_ci  let fileSize = fileInfo.size;
1209fb726d48Sopenharmony_ci  let startIndex = fileInfo.startIndex;
1210fb726d48Sopenharmony_ci  let endIndex = fileInfo.endIndex;
1211fb726d48Sopenharmony_ci  let queryStartIndex = startIndex;
1212fb726d48Sopenharmony_ci  let queryEndIndex = startIndex;
1213fb726d48Sopenharmony_ci  let saveIndex = 0;
1214fb726d48Sopenharmony_ci  let saveStartOffset = 0;
1215fb726d48Sopenharmony_ci  let current = new Uint8Array(maxSize);
1216fb726d48Sopenharmony_ci  let currentOffset = 0;
1217fb726d48Sopenharmony_ci  let resSize = 0;
1218fb726d48Sopenharmony_ci  do {
1219fb726d48Sopenharmony_ci    queryEndIndex = queryStartIndex + 9;
1220fb726d48Sopenharmony_ci    if (queryEndIndex > endIndex) {
1221fb726d48Sopenharmony_ci      queryEndIndex = endIndex;
1222fb726d48Sopenharmony_ci    }
1223fb726d48Sopenharmony_ci    let range = getRange(timStamp, fileType, queryStartIndex, queryEndIndex);
1224fb726d48Sopenharmony_ci    let res = await getIndexedDBQueryData(indexDB, range);
1225fb726d48Sopenharmony_ci    queryStartIndex = queryEndIndex + 1;
1226fb726d48Sopenharmony_ci    //@ts-ignore
1227fb726d48Sopenharmony_ci    for (let i = 0; i < res.length; i++) {
1228fb726d48Sopenharmony_ci      //@ts-ignore
1229fb726d48Sopenharmony_ci      let arrayBuffer = res[i];
1230fb726d48Sopenharmony_ci      let uint8Array = new Uint8Array(arrayBuffer.buf);
1231fb726d48Sopenharmony_ci      let cutFileSize = 0;
1232fb726d48Sopenharmony_ci      while (cutFileSize < uint8Array.length) {
1233fb726d48Sopenharmony_ci        [cutFileSize, resSize] = splitLongTrace(pageNum, fileSize, resSize, cutFileSize, uint8Array, splitBufAddr);
1234fb726d48Sopenharmony_ci        if (arkTsDataSize > 0 && fileType === 'arkts') {
1235fb726d48Sopenharmony_ci          splitFileAndSaveArkTs(currentOffset, current, fileType, pageNum, saveStartOffset, saveIndex, timStamp);
1236fb726d48Sopenharmony_ci        }
1237fb726d48Sopenharmony_ci      }
1238fb726d48Sopenharmony_ci    }
1239fb726d48Sopenharmony_ci  } while (queryEndIndex < endIndex);
1240fb726d48Sopenharmony_ci  if (fileType === 'arkts' && currentOffset > 0) {
1241fb726d48Sopenharmony_ci    let remnantArray = new Uint8Array(currentOffset);
1242fb726d48Sopenharmony_ci    let remnantChunk = current.slice(0, currentOffset);
1243fb726d48Sopenharmony_ci    remnantArray.set(remnantChunk, 0);
1244fb726d48Sopenharmony_ci    let arg2 = setArg(remnantArray, fileType, pageNum, saveStartOffset, saveIndex, timStamp);
1245fb726d48Sopenharmony_ci    await addDataToIndexeddb(indexDB, arg2);
1246fb726d48Sopenharmony_ci    arkTsDataSize = 0;
1247fb726d48Sopenharmony_ci    arkTsData.length = 0;
1248fb726d48Sopenharmony_ci  }
1249fb726d48Sopenharmony_ci};
1250fb726d48Sopenharmony_ci
1251fb726d48Sopenharmony_ciasync function getIndexedDBQueryData(db: IDBDatabase, range: IDBKeyRange): Promise<unknown> {
1252fb726d48Sopenharmony_ci  const transaction = db.transaction(STORE_NAME, 'readonly');
1253fb726d48Sopenharmony_ci  const store = transaction.objectStore(STORE_NAME);
1254fb726d48Sopenharmony_ci  const index = store.index('QueryCompleteFile');
1255fb726d48Sopenharmony_ci  const getRequest = index.openCursor(range);
1256fb726d48Sopenharmony_ci  return await queryDataFromIndexeddb(getRequest);
1257fb726d48Sopenharmony_ci}
1258fb726d48Sopenharmony_ci
1259fb726d48Sopenharmony_cifunction splitLongTrace(
1260fb726d48Sopenharmony_ci  pageNum: number,
1261fb726d48Sopenharmony_ci  fileSize: number,
1262fb726d48Sopenharmony_ci  resultFileSize: number,
1263fb726d48Sopenharmony_ci  cutFileSize: number,
1264fb726d48Sopenharmony_ci  uint8Array: Uint8Array,
1265fb726d48Sopenharmony_ci  splitReqBufferAddr?: number
1266fb726d48Sopenharmony_ci): [number, number] {
1267fb726d48Sopenharmony_ci  const sliceLen = Math.min(uint8Array.length - cutFileSize, REQ_BUF_SIZE);
1268fb726d48Sopenharmony_ci  const dataSlice = uint8Array.subarray(cutFileSize, cutFileSize + sliceLen);
1269fb726d48Sopenharmony_ci  //@ts-ignore
1270fb726d48Sopenharmony_ci  wasmModule.HEAPU8.set(dataSlice, splitReqBufferAddr);
1271fb726d48Sopenharmony_ci  cutFileSize += sliceLen;
1272fb726d48Sopenharmony_ci  resultFileSize += sliceLen;
1273fb726d48Sopenharmony_ci  if (resultFileSize >= fileSize) {
1274fb726d48Sopenharmony_ci    //@ts-ignore
1275fb726d48Sopenharmony_ci    wasmModule._TraceStreamerLongTraceSplitFileEx(sliceLen, 1, pageNum);
1276fb726d48Sopenharmony_ci  } else {
1277fb726d48Sopenharmony_ci    //@ts-ignore
1278fb726d48Sopenharmony_ci    wasmModule._TraceStreamerLongTraceSplitFileEx(sliceLen, 0, pageNum);
1279fb726d48Sopenharmony_ci  }
1280fb726d48Sopenharmony_ci  return [cutFileSize, resultFileSize];
1281fb726d48Sopenharmony_ci}
1282fb726d48Sopenharmony_ci
1283fb726d48Sopenharmony_cifunction setArg(
1284fb726d48Sopenharmony_ci  remnantArray: Uint8Array,
1285fb726d48Sopenharmony_ci  fileType: string,
1286fb726d48Sopenharmony_ci  pageNum: number,
1287fb726d48Sopenharmony_ci  saveStartOffset: number,
1288fb726d48Sopenharmony_ci  saveIndex: number,
1289fb726d48Sopenharmony_ci  timStamp: number
1290fb726d48Sopenharmony_ci): unknown {
1291fb726d48Sopenharmony_ci  return {
1292fb726d48Sopenharmony_ci    buf: remnantArray,
1293fb726d48Sopenharmony_ci    id: `${fileType}_new_${timStamp}_${pageNum}_${saveIndex}`,
1294fb726d48Sopenharmony_ci    fileType: `${fileType}_new`,
1295fb726d48Sopenharmony_ci    pageNum: pageNum,
1296fb726d48Sopenharmony_ci    startOffset: saveStartOffset,
1297fb726d48Sopenharmony_ci    endOffset: saveStartOffset + maxSize,
1298fb726d48Sopenharmony_ci    index: saveIndex,
1299fb726d48Sopenharmony_ci    timStamp: timStamp,
1300fb726d48Sopenharmony_ci  };
1301fb726d48Sopenharmony_ci}
1302fb726d48Sopenharmony_cifunction getRange(timStamp: number, fileType: string, queryStartIndex: number, queryEndIndex: number): IDBKeyRange {
1303fb726d48Sopenharmony_ci  return IDBKeyRange.bound(
1304fb726d48Sopenharmony_ci    [timStamp, fileType, 0, queryStartIndex],
1305fb726d48Sopenharmony_ci    [timStamp, fileType, 0, queryEndIndex],
1306fb726d48Sopenharmony_ci    false,
1307fb726d48Sopenharmony_ci    false
1308fb726d48Sopenharmony_ci  );
1309fb726d48Sopenharmony_ci}
1310fb726d48Sopenharmony_ci
1311fb726d48Sopenharmony_cienum DataTypeEnum {
1312fb726d48Sopenharmony_ci  data,
1313fb726d48Sopenharmony_ci  json,
1314fb726d48Sopenharmony_ci}
1315fb726d48Sopenharmony_ci
1316fb726d48Sopenharmony_cilet uploadSoActionId: string = '';
1317fb726d48Sopenharmony_cilet uploadFileIndex: number = 0;
1318fb726d48Sopenharmony_cilet uploadSoCallbackFn: Function;
1319fb726d48Sopenharmony_cilet soFileList: Array<File | null> = [];
1320fb726d48Sopenharmony_ciconst failedArray: Array<string> = [];
1321fb726d48Sopenharmony_ciconst uploadSoFile = async (file: File | null): Promise<void> => {
1322fb726d48Sopenharmony_ci  if (file) {
1323fb726d48Sopenharmony_ci    let fileNameBuffer: Uint8Array | null = enc.encode(file.webkitRelativePath);
1324fb726d48Sopenharmony_ci    let fileNameLength = fileNameBuffer.length;
1325fb726d48Sopenharmony_ci    //@ts-ignore
1326fb726d48Sopenharmony_ci    let addr = wasmModule._InitFileName(uploadSoCallbackFn, fileNameBuffer.length);
1327fb726d48Sopenharmony_ci    //@ts-ignore
1328fb726d48Sopenharmony_ci    wasmModule.HEAPU8.set(fileNameBuffer, addr);
1329fb726d48Sopenharmony_ci    let writeSize = 0;
1330fb726d48Sopenharmony_ci    let upRes = -1;
1331fb726d48Sopenharmony_ci    while (writeSize < file.size) {
1332fb726d48Sopenharmony_ci      let sliceLen = Math.min(file.size - writeSize, REQ_BUF_SIZE);
1333fb726d48Sopenharmony_ci      let blob: Blob | null = file.slice(writeSize, writeSize + sliceLen);
1334fb726d48Sopenharmony_ci      let buffer: ArrayBuffer | null = await blob.arrayBuffer();
1335fb726d48Sopenharmony_ci      let data: Uint8Array | null = new Uint8Array(buffer);
1336fb726d48Sopenharmony_ci      let size = file.size;
1337fb726d48Sopenharmony_ci      //@ts-ignore
1338fb726d48Sopenharmony_ci      wasmModule.HEAPU8.set(data, reqBufferAddr);
1339fb726d48Sopenharmony_ci      writeSize += sliceLen;
1340fb726d48Sopenharmony_ci      //@ts-ignore
1341fb726d48Sopenharmony_ci      upRes = wasmModule._TraceStreamerDownloadELFEx(size, fileNameLength, sliceLen, 1);
1342fb726d48Sopenharmony_ci      data = null;
1343fb726d48Sopenharmony_ci      buffer = null;
1344fb726d48Sopenharmony_ci      blob = null;
1345fb726d48Sopenharmony_ci    }
1346fb726d48Sopenharmony_ci    file = null;
1347fb726d48Sopenharmony_ci    soFileList[uploadFileIndex] = null;
1348fb726d48Sopenharmony_ci    fileNameBuffer = null;
1349fb726d48Sopenharmony_ci  }
1350fb726d48Sopenharmony_ci};
1351fb726d48Sopenharmony_ci
1352fb726d48Sopenharmony_ciconst uploadSoCallBack = (heapPtr: number, size: number, isFinish: number): void => {
1353fb726d48Sopenharmony_ci  //@ts-ignore
1354fb726d48Sopenharmony_ci  let out: Uint8Array | null = wasmModule.HEAPU8.slice(heapPtr, heapPtr + size);
1355fb726d48Sopenharmony_ci  if (out) {
1356fb726d48Sopenharmony_ci    let res = dec.decode(out);
1357fb726d48Sopenharmony_ci    out = null;
1358fb726d48Sopenharmony_ci    if (res.includes('ok') || res.includes('failed')) {
1359fb726d48Sopenharmony_ci      if (res.includes('failed')) {
1360fb726d48Sopenharmony_ci        failedArray.push(soFileList[uploadFileIndex]!.name);
1361fb726d48Sopenharmony_ci      }
1362fb726d48Sopenharmony_ci      if (uploadFileIndex < soFileList.length - 1) {
1363fb726d48Sopenharmony_ci        uploadSoFile(soFileList[uploadFileIndex + 1]).then();
1364fb726d48Sopenharmony_ci      }
1365fb726d48Sopenharmony_ci      uploadFileIndex++;
1366fb726d48Sopenharmony_ci    }
1367fb726d48Sopenharmony_ci    if (uploadFileIndex === soFileList.length) {
1368fb726d48Sopenharmony_ci      soFileList.length = 0;
1369fb726d48Sopenharmony_ci      const result = failedArray.length === 0 ? 'ok' : 'failed';
1370fb726d48Sopenharmony_ci      self.postMessage({
1371fb726d48Sopenharmony_ci        id: uploadSoActionId,
1372fb726d48Sopenharmony_ci        action: 'upload-so',
1373fb726d48Sopenharmony_ci        results: { result: result, failedArray: failedArray },
1374fb726d48Sopenharmony_ci      });
1375fb726d48Sopenharmony_ci    }
1376fb726d48Sopenharmony_ci  }
1377fb726d48Sopenharmony_ci};
1378fb726d48Sopenharmony_ci
1379fb726d48Sopenharmony_cilet splitReqBufferAddr = -1;
1380fb726d48Sopenharmony_ci
1381fb726d48Sopenharmony_cienum FileTypeEnum {
1382fb726d48Sopenharmony_ci  data,
1383fb726d48Sopenharmony_ci  json,
1384fb726d48Sopenharmony_ci}
1385fb726d48Sopenharmony_ci
1386fb726d48Sopenharmony_cifunction isRawTrace(uint8Array: Uint8Array): boolean {
1387fb726d48Sopenharmony_ci  let rowTraceStr = Array.from(new Uint16Array(uint8Array.buffer.slice(0, 2)));
1388fb726d48Sopenharmony_ci  return rowTraceStr[0] === 57161;
1389fb726d48Sopenharmony_ci}
1390fb726d48Sopenharmony_ci
1391fb726d48Sopenharmony_cifunction cutFileBufferByOffSet(out: Uint8Array, uint8Array: Uint8Array): Uint8Array {
1392fb726d48Sopenharmony_ci  let jsonStr: string = dec.decode(out);
1393fb726d48Sopenharmony_ci  let jsonObj = JSON.parse(jsonStr);
1394fb726d48Sopenharmony_ci  let valueArray: Array<{ type: number; offset: number; size: number }> = jsonObj.value;
1395fb726d48Sopenharmony_ci  let cutBuffer: Uint8Array;
1396fb726d48Sopenharmony_ci  if (isRawTrace(uint8Array)) {
1397fb726d48Sopenharmony_ci    let commDataSize = 0;
1398fb726d48Sopenharmony_ci    let otherDataSize = 0;
1399fb726d48Sopenharmony_ci    valueArray.forEach((item) => {
1400fb726d48Sopenharmony_ci      const type = item.type;
1401fb726d48Sopenharmony_ci      if (type === 0) {
1402fb726d48Sopenharmony_ci        commDataSize += item.size;
1403fb726d48Sopenharmony_ci      } else {
1404fb726d48Sopenharmony_ci        otherDataSize += item.size;
1405fb726d48Sopenharmony_ci        otherDataSize += 8;
1406fb726d48Sopenharmony_ci      }
1407fb726d48Sopenharmony_ci    });
1408fb726d48Sopenharmony_ci    cutBuffer = new Uint8Array(commDataSize + otherDataSize);
1409fb726d48Sopenharmony_ci    let commOffset = 0;
1410fb726d48Sopenharmony_ci    let tlvOffset = commDataSize;
1411fb726d48Sopenharmony_ci    valueArray.forEach((item) => {
1412fb726d48Sopenharmony_ci      if (item.type !== 0) {
1413fb726d48Sopenharmony_ci        let typeArray = new Uint32Array(1);
1414fb726d48Sopenharmony_ci        typeArray[0] = item.type;
1415fb726d48Sopenharmony_ci        cutBuffer.set(new Uint8Array(typeArray.buffer), tlvOffset);
1416fb726d48Sopenharmony_ci        tlvOffset += typeArray.byteLength;
1417fb726d48Sopenharmony_ci        let lengthArray = new Uint32Array(1);
1418fb726d48Sopenharmony_ci        lengthArray[0] = item.size;
1419fb726d48Sopenharmony_ci        cutBuffer.set(new Uint8Array(lengthArray.buffer), tlvOffset);
1420fb726d48Sopenharmony_ci        tlvOffset += typeArray.byteLength;
1421fb726d48Sopenharmony_ci        const dataSlice = uint8Array.subarray(item.offset, item.offset + item.size);
1422fb726d48Sopenharmony_ci        cutBuffer.set(dataSlice, tlvOffset);
1423fb726d48Sopenharmony_ci        tlvOffset += item.size;
1424fb726d48Sopenharmony_ci      } else {
1425fb726d48Sopenharmony_ci        const dataSlice = uint8Array.subarray(item.offset, item.offset + item.size);
1426fb726d48Sopenharmony_ci        cutBuffer.set(dataSlice, commOffset);
1427fb726d48Sopenharmony_ci        commOffset += item.size;
1428fb726d48Sopenharmony_ci      }
1429fb726d48Sopenharmony_ci    });
1430fb726d48Sopenharmony_ci  } else {
1431fb726d48Sopenharmony_ci    const sum = valueArray.reduce((total, obj) => total + obj.size, 0);
1432fb726d48Sopenharmony_ci    cutBuffer = new Uint8Array(sum);
1433fb726d48Sopenharmony_ci    let offset = 0;
1434fb726d48Sopenharmony_ci    valueArray.forEach((item, index) => {
1435fb726d48Sopenharmony_ci      const dataSlice = uint8Array.subarray(item.offset, item.offset + item.size);
1436fb726d48Sopenharmony_ci      cutBuffer.set(dataSlice, offset);
1437fb726d48Sopenharmony_ci      offset += item.size;
1438fb726d48Sopenharmony_ci    });
1439fb726d48Sopenharmony_ci  }
1440fb726d48Sopenharmony_ci  return cutBuffer;
1441fb726d48Sopenharmony_ci}
1442fb726d48Sopenharmony_ci
1443fb726d48Sopenharmony_cifunction cutFileByRange(e: MessageEvent): void {
1444fb726d48Sopenharmony_ci  let cutLeftTs = e.data.leftTs;
1445fb726d48Sopenharmony_ci  let cutRightTs = e.data.rightTs;
1446fb726d48Sopenharmony_ci  let uint8Array = new Uint8Array(e.data.buffer);
1447fb726d48Sopenharmony_ci  let resultBuffer: Array<Uint8Array> = [];
1448fb726d48Sopenharmony_ci  let cutFileCallBack = cutFileCallBackFunc(resultBuffer, uint8Array, e);
1449fb726d48Sopenharmony_ci  //@ts-ignore
1450fb726d48Sopenharmony_ci  splitReqBufferAddr = wasmModule._InitializeSplitFile(wasmModule.addFunction(cutFileCallBack, 'viiii'), REQ_BUF_SIZE);
1451fb726d48Sopenharmony_ci  let cutTimeRange = `${cutLeftTs};${cutRightTs};`;
1452fb726d48Sopenharmony_ci  let cutTimeRangeBuffer = enc.encode(cutTimeRange);
1453fb726d48Sopenharmony_ci  //@ts-ignore
1454fb726d48Sopenharmony_ci  wasmModule.HEAPU8.set(cutTimeRangeBuffer, splitReqBufferAddr);
1455fb726d48Sopenharmony_ci  //@ts-ignore
1456fb726d48Sopenharmony_ci  wasmModule._TraceStreamerSplitFileEx(cutTimeRangeBuffer.length);
1457fb726d48Sopenharmony_ci  let cutFileSize = 0;
1458fb726d48Sopenharmony_ci  let receiveFileResult = -1;
1459fb726d48Sopenharmony_ci  while (cutFileSize < uint8Array.length) {
1460fb726d48Sopenharmony_ci    const sliceLen = Math.min(uint8Array.length - cutFileSize, REQ_BUF_SIZE);
1461fb726d48Sopenharmony_ci    const dataSlice = uint8Array.subarray(cutFileSize, cutFileSize + sliceLen);
1462fb726d48Sopenharmony_ci    //@ts-ignore
1463fb726d48Sopenharmony_ci    wasmModule.HEAPU8.set(dataSlice, splitReqBufferAddr);
1464fb726d48Sopenharmony_ci    cutFileSize += sliceLen;
1465fb726d48Sopenharmony_ci    try {
1466fb726d48Sopenharmony_ci      if (cutFileSize >= uint8Array.length) {
1467fb726d48Sopenharmony_ci        //@ts-ignore
1468fb726d48Sopenharmony_ci        receiveFileResult = wasmModule._TraceStreamerReciveFileEx(sliceLen, 1);
1469fb726d48Sopenharmony_ci      } else {
1470fb726d48Sopenharmony_ci        //@ts-ignore
1471fb726d48Sopenharmony_ci        receiveFileResult = wasmModule._TraceStreamerReciveFileEx(sliceLen, 0);
1472fb726d48Sopenharmony_ci      }
1473fb726d48Sopenharmony_ci    } catch (error) {
1474fb726d48Sopenharmony_ci      self.postMessage(
1475fb726d48Sopenharmony_ci        {
1476fb726d48Sopenharmony_ci          id: e.data.id,
1477fb726d48Sopenharmony_ci          action: e.data.action,
1478fb726d48Sopenharmony_ci          cutStatus: false,
1479fb726d48Sopenharmony_ci          msg: 'split failed',
1480fb726d48Sopenharmony_ci          buffer: e.data.buffer,
1481fb726d48Sopenharmony_ci        },
1482fb726d48Sopenharmony_ci        // @ts-ignore
1483fb726d48Sopenharmony_ci        [e.data.buffer]
1484fb726d48Sopenharmony_ci      );
1485fb726d48Sopenharmony_ci    }
1486fb726d48Sopenharmony_ci  }
1487fb726d48Sopenharmony_ci}
1488fb726d48Sopenharmony_cifunction cutFileCallBackFunc(resultBuffer: Array<Uint8Array>, uint8Array: Uint8Array, e: MessageEvent): Function {
1489fb726d48Sopenharmony_ci  return (heapPtr: number, size: number, fileType: number, isEnd: number) => {
1490fb726d48Sopenharmony_ci    //@ts-ignore
1491fb726d48Sopenharmony_ci    let out: Uint8Array = wasmModule.HEAPU8.slice(heapPtr, heapPtr + size);
1492fb726d48Sopenharmony_ci    if (FileTypeEnum.data === fileType) {
1493fb726d48Sopenharmony_ci      resultBuffer.push(out);
1494fb726d48Sopenharmony_ci    } else if (FileTypeEnum.json === fileType) {
1495fb726d48Sopenharmony_ci      let cutBuffer = cutFileBufferByOffSet(out, uint8Array);
1496fb726d48Sopenharmony_ci      resultBuffer.push(cutBuffer);
1497fb726d48Sopenharmony_ci    }
1498fb726d48Sopenharmony_ci    if (isEnd) {
1499fb726d48Sopenharmony_ci      //@ts-ignore
1500fb726d48Sopenharmony_ci      const cutResultFileLength = resultBuffer.reduce((total, obj) => total + obj.length, 0);
1501fb726d48Sopenharmony_ci      //@ts-ignore
1502fb726d48Sopenharmony_ci      let cutBuffer = new Uint8Array(cutResultFileLength);
1503fb726d48Sopenharmony_ci      let offset = 0;
1504fb726d48Sopenharmony_ci      resultBuffer.forEach((item) => {
1505fb726d48Sopenharmony_ci        //@ts-ignore
1506fb726d48Sopenharmony_ci        cutBuffer.set(item, offset);
1507fb726d48Sopenharmony_ci        //@ts-ignore
1508fb726d48Sopenharmony_ci        offset += item.length;
1509fb726d48Sopenharmony_ci      });
1510fb726d48Sopenharmony_ci      resultBuffer.length = 0;
1511fb726d48Sopenharmony_ci      self.postMessage(
1512fb726d48Sopenharmony_ci        {
1513fb726d48Sopenharmony_ci          id: e.data.id,
1514fb726d48Sopenharmony_ci          action: e.data.action,
1515fb726d48Sopenharmony_ci          cutStatus: true,
1516fb726d48Sopenharmony_ci          msg: 'split success',
1517fb726d48Sopenharmony_ci          buffer: e.data.buffer,
1518fb726d48Sopenharmony_ci          cutBuffer: cutBuffer.buffer,
1519fb726d48Sopenharmony_ci        },
1520fb726d48Sopenharmony_ci        // @ts-ignore
1521fb726d48Sopenharmony_ci        [e.data.buffer, cutBuffer.buffer]
1522fb726d48Sopenharmony_ci      );
1523fb726d48Sopenharmony_ci    }
1524fb726d48Sopenharmony_ci  };
1525fb726d48Sopenharmony_ci}
1526fb726d48Sopenharmony_ci
1527fb726d48Sopenharmony_cifunction createView(sql: string): void {
1528fb726d48Sopenharmony_ci  let array = enc.encode(sql);
1529fb726d48Sopenharmony_ci  //@ts-ignore
1530fb726d48Sopenharmony_ci  wasmModule.HEAPU8.set(array, reqBufferAddr);
1531fb726d48Sopenharmony_ci  //@ts-ignore
1532fb726d48Sopenharmony_ci  wasmModule._TraceStreamerSqlOperateEx(array.length);
1533fb726d48Sopenharmony_ci}
1534fb726d48Sopenharmony_ci
1535fb726d48Sopenharmony_cifunction query(name: string, sql: string, params: unknown): void {
1536fb726d48Sopenharmony_ci  if (params) {
1537fb726d48Sopenharmony_ci    Reflect.ownKeys(params).forEach((key: unknown) => {
1538fb726d48Sopenharmony_ci      //@ts-ignore
1539fb726d48Sopenharmony_ci      if (typeof params[key] === 'string') {
1540fb726d48Sopenharmony_ci        //@ts-ignore
1541fb726d48Sopenharmony_ci        sql = sql.replace(new RegExp(`\\${key}`, 'g'), `'${params[key]}'`);
1542fb726d48Sopenharmony_ci      } else {
1543fb726d48Sopenharmony_ci        //@ts-ignore
1544fb726d48Sopenharmony_ci        sql = sql.replace(new RegExp(`\\${key}`, 'g'), params[key]);
1545fb726d48Sopenharmony_ci      }
1546fb726d48Sopenharmony_ci    });
1547fb726d48Sopenharmony_ci  }
1548fb726d48Sopenharmony_ci  let sqlUintArray = enc.encode(sql);
1549fb726d48Sopenharmony_ci  //@ts-ignore
1550fb726d48Sopenharmony_ci  wasmModule.HEAPU8.set(sqlUintArray, reqBufferAddr);
1551fb726d48Sopenharmony_ci  //@ts-ignore
1552fb726d48Sopenharmony_ci  wasmModule._TraceStreamerSqlQueryEx(sqlUintArray.length);
1553fb726d48Sopenharmony_ci}
1554fb726d48Sopenharmony_ci
1555fb726d48Sopenharmony_cifunction querySdk(name: string, sql: string, sdkParams: unknown, action: string): void {
1556fb726d48Sopenharmony_ci  if (sdkParams) {
1557fb726d48Sopenharmony_ci    Reflect.ownKeys(sdkParams).forEach((key: unknown) => {
1558fb726d48Sopenharmony_ci      //@ts-ignore
1559fb726d48Sopenharmony_ci      if (typeof sdkParams[key] === 'string') {
1560fb726d48Sopenharmony_ci        //@ts-ignore
1561fb726d48Sopenharmony_ci        sql = sql.replace(new RegExp(`\\${key}`, 'g'), `'${sdkParams[key]}'`);
1562fb726d48Sopenharmony_ci      } else {
1563fb726d48Sopenharmony_ci        //@ts-ignore
1564fb726d48Sopenharmony_ci        sql = sql.replace(new RegExp(`\\${key}`, 'g'), sdkParams[key]);
1565fb726d48Sopenharmony_ci      }
1566fb726d48Sopenharmony_ci    });
1567fb726d48Sopenharmony_ci  }
1568fb726d48Sopenharmony_ci  let sqlUintArray = enc.encode(sql);
1569fb726d48Sopenharmony_ci  let commentId = action.substring(action.lastIndexOf('-') + 1);
1570fb726d48Sopenharmony_ci  let key = Number(commentId);
1571fb726d48Sopenharmony_ci  let wasm = thirdWasmMap.get(key);
1572fb726d48Sopenharmony_ci  if (wasm !== undefined) {
1573fb726d48Sopenharmony_ci    let wasmModel = wasm.model;
1574fb726d48Sopenharmony_ci    wasmModel.HEAPU8.set(sqlUintArray, wasm.bufferAddr);
1575fb726d48Sopenharmony_ci    wasmModel._TraceStreamerSqlQueryEx(sqlUintArray.length);
1576fb726d48Sopenharmony_ci  }
1577fb726d48Sopenharmony_ci}
1578fb726d48Sopenharmony_ci
1579fb726d48Sopenharmony_cifunction queryMetric(name: string): void {
1580fb726d48Sopenharmony_ci  let metricArray = enc.encode(name);
1581fb726d48Sopenharmony_ci  //@ts-ignore
1582fb726d48Sopenharmony_ci  wasmModule.HEAPU8.set(metricArray, reqBufferAddr);
1583fb726d48Sopenharmony_ci  //@ts-ignore
1584fb726d48Sopenharmony_ci  wasmModule._TraceStreamerSqlMetricsQuery(metricArray.length);
1585fb726d48Sopenharmony_ci}
1586fb726d48Sopenharmony_ci
1587fb726d48Sopenharmony_ciconst DB_NAME = 'sp';
1588fb726d48Sopenharmony_ciconst DB_VERSION = 1;
1589fb726d48Sopenharmony_ciconst STORE_NAME = 'longTable';
1590fb726d48Sopenharmony_ci
1591fb726d48Sopenharmony_cifunction openDB(): Promise<IDBDatabase> {
1592fb726d48Sopenharmony_ci  return new Promise((resolve, reject) => {
1593fb726d48Sopenharmony_ci    const openRequest = indexedDB.open(DB_NAME, DB_VERSION);
1594fb726d48Sopenharmony_ci    openRequest.onerror = (): void => reject(openRequest.error);
1595fb726d48Sopenharmony_ci    openRequest.onsuccess = (): void => {
1596fb726d48Sopenharmony_ci      resolve(openRequest.result);
1597fb726d48Sopenharmony_ci    };
1598fb726d48Sopenharmony_ci  });
1599fb726d48Sopenharmony_ci}
1600fb726d48Sopenharmony_ci
1601fb726d48Sopenharmony_cifunction queryDataFromIndexeddb(getRequest: IDBRequest<IDBCursorWithValue | null>): Promise<unknown> {
1602fb726d48Sopenharmony_ci  return new Promise((resolve, reject) => {
1603fb726d48Sopenharmony_ci    let results: unknown[] = [];
1604fb726d48Sopenharmony_ci    getRequest.onerror = (event): void => {
1605fb726d48Sopenharmony_ci      // @ts-ignore
1606fb726d48Sopenharmony_ci      reject(event.target.error);
1607fb726d48Sopenharmony_ci    };
1608fb726d48Sopenharmony_ci    getRequest.onsuccess = (event): void => {
1609fb726d48Sopenharmony_ci      // @ts-ignore
1610fb726d48Sopenharmony_ci      const cursor = event.target!.result;
1611fb726d48Sopenharmony_ci      if (cursor) {
1612fb726d48Sopenharmony_ci        results.push(cursor.value);
1613fb726d48Sopenharmony_ci        cursor.continue();
1614fb726d48Sopenharmony_ci      } else {
1615fb726d48Sopenharmony_ci        // @ts-ignore
1616fb726d48Sopenharmony_ci        resolve(results);
1617fb726d48Sopenharmony_ci      }
1618fb726d48Sopenharmony_ci    };
1619fb726d48Sopenharmony_ci  });
1620fb726d48Sopenharmony_ci}
1621fb726d48Sopenharmony_ci
1622fb726d48Sopenharmony_cifunction addDataToIndexeddb(db: IDBDatabase, value: unknown, key?: IDBValidKey): Promise<unknown> {
1623fb726d48Sopenharmony_ci  return new Promise((resolve, reject) => {
1624fb726d48Sopenharmony_ci    const transaction = db.transaction(STORE_NAME, 'readwrite');
1625fb726d48Sopenharmony_ci    const objectStore = transaction.objectStore(STORE_NAME);
1626fb726d48Sopenharmony_ci    const request = objectStore.add(value, key);
1627fb726d48Sopenharmony_ci    request.onsuccess = function (event): void {
1628fb726d48Sopenharmony_ci      // @ts-ignore
1629fb726d48Sopenharmony_ci      resolve(event.target.result);
1630fb726d48Sopenharmony_ci    };
1631fb726d48Sopenharmony_ci    request.onerror = (event): void => {
1632fb726d48Sopenharmony_ci      reject(event);
1633fb726d48Sopenharmony_ci    };
1634fb726d48Sopenharmony_ci  });
1635fb726d48Sopenharmony_ci}
1636