• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15let hilog = requireNapi('hilog');
16
17let domainID = 0xD001320;
18let TAG = 'JSENV';
19
20const URI_SPLIT = '/';
21
22const ERROR_CODE_INVALID_PARAM = 401;
23const ERROR_CODE_INNER_ERROR = 16000050;
24
25const ERROR_MSG_INVALID_PARAM = 'Invalid input parameter.';
26const ERROR_MSG_INNER_ERROR = 'Inner Error.';
27
28let errMap = new Map();
29errMap.set(ERROR_CODE_INVALID_PARAM, ERROR_MSG_INVALID_PARAM);
30errMap.set(ERROR_CODE_INNER_ERROR, ERROR_MSG_INNER_ERROR);
31
32class DataUriError extends Error {
33  constructor(code) {
34    let msg = '';
35    if (errMap.has(code)) {
36      msg = errMap.get(code);
37    } else {
38      msg = ERROR_MSG_INNER_ERROR;
39    }
40    super(msg);
41    this.code = code;
42  }
43}
44
45let dataUriUtils = {
46  getId: (uri) => {
47    hilog.sLogD(domainID, TAG, 'DataUriUtils getId called.');
48    if (typeof uri !== 'string') {
49      throw new DataUriError(ERROR_CODE_INVALID_PARAM);
50    }
51    let index = uri.lastIndexOf(URI_SPLIT);
52    if (index === -1) {
53      throw new DataUriError(ERROR_CODE_INVALID_PARAM);
54    }
55    let ret = uri.substring(index + 1);
56    if (ret === '' || isNaN(Number(ret))) {
57      throw new DataUriError(ERROR_CODE_INVALID_PARAM);
58    }
59    return Number(ret);
60  },
61  updateId: (uri, id) => {
62    hilog.sLogD(domainID, TAG, 'DataUriUtils updateId called.');
63    if (typeof uri !== 'string' || typeof id !== 'number') {
64      throw new DataUriError(ERROR_CODE_INVALID_PARAM);
65    }
66    let ret = dataUriUtils.deleteId(uri);
67    if (ret === uri) {
68      throw new DataUriError(ERROR_CODE_INVALID_PARAM);
69    }
70    return ret + URI_SPLIT + id;
71  },
72  deleteId: (uri) => {
73    hilog.sLogD(domainID, TAG, 'DataUriUtils deleteId called.');
74    if (typeof uri !== 'string') {
75      throw new DataUriError(ERROR_CODE_INVALID_PARAM);
76    }
77    let index = uri.lastIndexOf(URI_SPLIT);
78    if (index === -1) {
79      throw new DataUriError(ERROR_CODE_INVALID_PARAM);
80    }
81    let id = uri.substring(index + 1);
82    if (id === '' || isNaN(Number(id))) {
83      throw new DataUriError(ERROR_CODE_INVALID_PARAM);
84    }
85    return uri.substring(0, index);
86  },
87  attachId: (uri, id) => {
88    hilog.sLogD(domainID, TAG, 'DataUriUtils attachId called.');
89    if (typeof uri !== 'string' || typeof id !== 'number') {
90      throw new DataUriError(ERROR_CODE_INVALID_PARAM);
91    }
92    return uri + URI_SPLIT + id;
93  }
94};
95
96export default dataUriUtils;