@vope/utils 1.5.0 → 1.6.0-bate.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/package.json +15 -12
  2. package/cjs/index.js +0 -6436
  3. package/lib/clamp.js +0 -11
  4. package/lib/classnames.js +0 -17
  5. package/lib/compare/version.js +0 -15
  6. package/lib/compare/weak-equal.js +0 -33
  7. package/lib/convert/json.js +0 -7
  8. package/lib/convert/object.js +0 -7
  9. package/lib/convert/strings.js +0 -7
  10. package/lib/desens.js +0 -51
  11. package/lib/encoding.js +0 -56
  12. package/lib/index.js +0 -75
  13. package/lib/input-regex.js +0 -8
  14. package/lib/is/class.js +0 -22
  15. package/lib/is/empty.js +0 -5
  16. package/lib/is/key.js +0 -4
  17. package/lib/is/null.js +0 -4
  18. package/lib/load/image.js +0 -11
  19. package/lib/memory/basic.js +0 -167
  20. package/lib/memory/list.js +0 -12
  21. package/lib/memory/power.js +0 -13
  22. package/lib/memory/simple.js +0 -94
  23. package/lib/memory/type.d.js +0 -0
  24. package/lib/neo4j-anchors.js +0 -169
  25. package/lib/neo4j.js +0 -161
  26. package/lib/once.js +0 -8
  27. package/lib/parse/num.js +0 -31
  28. package/lib/power/decon.js +0 -22
  29. package/lib/rotate/angle.js +0 -5
  30. package/lib/rotate/rad.js +0 -11
  31. package/lib/serialize/url-params.js +0 -53
  32. package/lib/storage/offline.js +0 -223
  33. package/lib/style/css.js +0 -25
  34. package/lib/subscriber.js +0 -64
  35. package/lib/table/flex.js +0 -96
  36. package/lib/union.js +0 -47
  37. package/lib/url/join.js +0 -9
  38. package/lib/url/params.js +0 -8
  39. package/lib/wait.js +0 -4
  40. package/types/clamp.d.ts +0 -4
  41. package/types/classnames.d.ts +0 -7
  42. package/types/compare/version.d.ts +0 -5
  43. package/types/compare/weak-equal.d.ts +0 -4
  44. package/types/convert/json.d.ts +0 -12
  45. package/types/convert/object.d.ts +0 -12
  46. package/types/convert/strings.d.ts +0 -6
  47. package/types/desens.d.ts +0 -40
  48. package/types/encoding.d.ts +0 -9
  49. package/types/index.d.ts +0 -35
  50. package/types/input-regex.d.ts +0 -12
  51. package/types/is/class.d.ts +0 -4
  52. package/types/is/empty.d.ts +0 -4
  53. package/types/is/key.d.ts +0 -4
  54. package/types/is/null.d.ts +0 -4
  55. package/types/load/image.d.ts +0 -5
  56. package/types/memory/basic.d.ts +0 -79
  57. package/types/memory/list.d.ts +0 -8
  58. package/types/memory/power.d.ts +0 -8
  59. package/types/memory/simple.d.ts +0 -60
  60. package/types/memory/type.d.ts +0 -11
  61. package/types/neo4j-anchors.d.ts +0 -50
  62. package/types/neo4j.d.ts +0 -82
  63. package/types/once.d.ts +0 -6
  64. package/types/parse/num.d.ts +0 -8
  65. package/types/power/decon.d.ts +0 -9
  66. package/types/rotate/angle.d.ts +0 -8
  67. package/types/rotate/rad.d.ts +0 -8
  68. package/types/serialize/url-params.d.ts +0 -43
  69. package/types/storage/offline.d.ts +0 -51
  70. package/types/style/css.d.ts +0 -9
  71. package/types/subscriber.d.ts +0 -11
  72. package/types/table/flex.d.ts +0 -43
  73. package/types/union.d.ts +0 -34
  74. package/types/url/join.d.ts +0 -4
  75. package/types/url/params.d.ts +0 -8
  76. package/types/wait.d.ts +0 -4
@@ -1,169 +0,0 @@
1
- import { rotateRad } from "./rotate/rad";
2
- class Neo4jAhchors {
3
- constructor(neo4j, config) {
4
- this.neo4j = neo4j;
5
- this.config = Object.assign({}, config);
6
- }
7
- config;
8
- #anchors = {};
9
- getDir(start, end) {
10
- const { neo4j } = this;
11
- return neo4j.dirKey({
12
- [neo4j.sourceField]: start,
13
- [neo4j.targetField]: end
14
- });
15
- }
16
- /**
17
- * 设置多边间隔弧长
18
- */
19
- setRaidx(value) {
20
- this.config.radOffsetRaidx = value;
21
- }
22
- /**
23
- * 当图数据准备好后调用
24
- */
25
- read() {
26
- const { neo4j } = this;
27
- const { nodeField, edgeField, sourceField, targetField } = neo4j;
28
- const anchors = {};
29
- neo4j.nodes.forEach((node) => {
30
- const nodeId = node[nodeField];
31
- let count = 0;
32
- [
33
- neo4j.sources.get(nodeId),
34
- neo4j.targets.get(nodeId)
35
- ].forEach((list, i) => {
36
- if (!list) return;
37
- let diffCount = 0;
38
- let sameCount = 0;
39
- list.forEach((edge) => {
40
- const edgeId = edge[edgeField];
41
- if (edge[sourceField] !== edge[targetField]) {
42
- const anchor = anchors[edgeId] || (anchors[edgeId] = {});
43
- anchor[i ? "target" : "source"] = count + diffCount++;
44
- } else if (edgeId in anchors === false) {
45
- anchors[edgeId] = {
46
- source: sameCount,
47
- target: sameCount++
48
- };
49
- }
50
- });
51
- count += diffCount;
52
- });
53
- });
54
- this.#anchors = anchors;
55
- }
56
- calcPoints(edge, index, total, type) {
57
- const { neo4j, config: { getBox, radOffsetRaidx = 0.3125 } } = this;
58
- const source = getBox(edge[neo4j.sourceField]);
59
- const target = getBox(edge[neo4j.targetField]);
60
- const rad = Math.atan2(
61
- (source.centerY - target.centerY) * type,
62
- (source.centerX - target.centerX) * type
63
- );
64
- let point = rotateRad([0.5, 0], rad);
65
- if (total !== 1) {
66
- point = rotateRad(point, radOffsetRaidx * (total / 2 - 0.5 - index));
67
- }
68
- const [x, y] = point;
69
- return [x + 0.5, 0.5 - y];
70
- }
71
- /**
72
- * 获取指定节点上出入边连接点的坐标
73
- */
74
- getPoints(id) {
75
- const { neo4j } = this;
76
- const { dirs, edgeField, sourceField, targetField } = neo4j;
77
- const anchors = this.#anchors;
78
- const points = [];
79
- const caches = {};
80
- neo4j.sources.get(id)?.forEach((edge) => {
81
- const source = edge[sourceField];
82
- const target = edge[targetField];
83
- const direct = this.getDir(source, target);
84
- const invert = this.getDir(target, source);
85
- if (direct in caches) return;
86
- const directs = dirs.get(direct) || [];
87
- const inverts = dirs.get(invert) || [];
88
- const directsLength = directs.length;
89
- const invertsLength = inverts.length;
90
- const total = directsLength + invertsLength;
91
- directs.forEach((data, index) => {
92
- points[anchors[data[edgeField]].source] = this.calcPoints(data, directsLength - 1 - index, total, -1);
93
- });
94
- caches[direct] = direct;
95
- });
96
- neo4j.targets.get(id)?.forEach((edge) => {
97
- const source = edge[sourceField];
98
- const target = edge[targetField];
99
- const direct = this.getDir(source, target);
100
- const invert = this.getDir(target, source);
101
- if (direct in caches) return;
102
- const directs = neo4j.dirs.get(direct) || [];
103
- const inverts = neo4j.dirs.get(invert) || [];
104
- const directsLength = directs.length;
105
- const invertsLength = inverts.length;
106
- const total = directsLength + invertsLength;
107
- directs.forEach((data, index) => {
108
- points[anchors[data[edgeField]].target] = this.calcPoints(data, invertsLength + index, total, 1);
109
- });
110
- caches[direct] = direct;
111
- });
112
- return points;
113
- }
114
- /**
115
- * 查找边连接点的位置
116
- */
117
- findAnchor(id) {
118
- const anchor = this.#anchors[id];
119
- if (!anchor) return null;
120
- return {
121
- source: anchor.source || 0,
122
- target: anchor.target || 0
123
- };
124
- }
125
- /**
126
- * 获取指定边控制点距离两端点连线的距离(可理解为控制边的弯曲程度)
127
- * @description 返回值是一个相对值,需要乘以一个基数值(基数值应为负值,如-30)
128
- */
129
- getOffset(id) {
130
- const { neo4j } = this;
131
- const edge = neo4j.edges.get(id);
132
- if (!edge) return -1;
133
- const { dirs, edgeField, sourceField, targetField } = neo4j;
134
- const source = edge[sourceField];
135
- const target = edge[targetField];
136
- const directs = dirs.get(this.getDir(source, target)) || [];
137
- const inverts = dirs.get(this.getDir(target, source)) || [];
138
- let count = 0;
139
- let index = 0;
140
- [directs, inverts].some((list) => {
141
- const result = list.some((data, i) => {
142
- if (data[edgeField] === id) {
143
- index = i + count;
144
- return true;
145
- }
146
- });
147
- count += list.length;
148
- return result;
149
- });
150
- const directsSize = directs.length;
151
- const invertsSize = inverts.length;
152
- const total = directsSize + invertsSize;
153
- if (total === 1) return -1;
154
- if (directsSize === 0 || invertsSize === 0) {
155
- return total / 2 - index - 0.5;
156
- } else {
157
- return Math.abs(directsSize - invertsSize) * (directsSize >= invertsSize ? 1 : -1) / 2 - index - 0.5;
158
- }
159
- }
160
- /**
161
- * 清理数据
162
- */
163
- clear() {
164
- this.#anchors = {};
165
- }
166
- }
167
- export {
168
- Neo4jAhchors as default
169
- };
package/lib/neo4j.js DELETED
@@ -1,161 +0,0 @@
1
- import { hash8strings } from "./convert/strings";
2
- import { MemoryList } from "./memory/list";
3
- import { MemorySimple } from "./memory/simple";
4
- class Neo4j {
5
- #nPrimaryKey;
6
- #ePrimaryKey;
7
- #sourceKey;
8
- #targetKey;
9
- #dirKey;
10
- #nodes;
11
- #edges;
12
- #sources;
13
- #targets;
14
- #dirs;
15
- get nodeField() {
16
- return this.#nPrimaryKey;
17
- }
18
- get edgeField() {
19
- return this.#ePrimaryKey;
20
- }
21
- get sourceField() {
22
- return this.#sourceKey;
23
- }
24
- get targetField() {
25
- return this.#targetKey;
26
- }
27
- get dirKey() {
28
- return this.#dirKey;
29
- }
30
- /**
31
- * 点视图
32
- */
33
- nodes;
34
- /**
35
- * 边视图
36
- */
37
- edges;
38
- /**
39
- * (边)出点视图
40
- */
41
- sources;
42
- /**
43
- * (边)入点视图
44
- */
45
- targets;
46
- /**
47
- * (边)方向视图
48
- */
49
- dirs;
50
- constructor(nodeList, edgeList, {
51
- nid,
52
- eid,
53
- sourceKey,
54
- targetKey,
55
- dir = (edge) => edge[sourceKey] + "|" + edge[targetKey]
56
- }) {
57
- shield(this, "nodes", this.#nodes = new MemorySimple(this.#nPrimaryKey = nid));
58
- shield(this, "edges", this.#edges = new MemorySimple(this.#ePrimaryKey = eid));
59
- shield(this, "sources", this.#sources = new MemoryList(this.#sourceKey = sourceKey));
60
- shield(this, "targets", this.#targets = new MemoryList(this.#targetKey = targetKey));
61
- shield(this, "dirs", this.#dirs = new MemoryList(this.#dirKey = dir));
62
- nodeList.forEach((node) => this.node("add", node));
63
- edgeList.forEach((edge) => this.edge("add", edge));
64
- }
65
- node(type, value) {
66
- const nodes = this.#nodes;
67
- if (type === "add") {
68
- nodes.push(value);
69
- } else if (type === "delete") {
70
- const node = nodes.get(value);
71
- if (!node) return [];
72
- nodes.delete(node);
73
- const id = node[this.#nPrimaryKey];
74
- const sourceKey = this.#sourceKey;
75
- const targetKey = this.#targetKey;
76
- const edges = this.#edges;
77
- const sources = this.#sources;
78
- const targets = this.#targets;
79
- const dirs = this.#dirs;
80
- const list = [];
81
- sources.get(id)?.reduce((_, edge) => {
82
- edges.delete(edge);
83
- targets.delete(edge);
84
- edge[sourceKey] === edge[targetKey] || dirs.delete(edge);
85
- list.push(edge);
86
- return true;
87
- }, false) && sources.deleteType(id);
88
- targets.get(id)?.reduce((_, edge) => {
89
- edges.delete(edge);
90
- sources.delete(edge);
91
- edge[sourceKey] === edge[targetKey] || dirs.delete(edge);
92
- list.push(edge);
93
- return true;
94
- }, false) && targets.deleteType(id);
95
- return list;
96
- }
97
- }
98
- edge(type, value) {
99
- const sourceKey = this.#sourceKey;
100
- const targetKey = this.#targetKey;
101
- const edges = this.#edges;
102
- const sources = this.#sources;
103
- const targets = this.#targets;
104
- const dirs = this.#dirs;
105
- if (type === "add") {
106
- const edge = value;
107
- edges.push(edge);
108
- sources.push(edge);
109
- targets.push(edge);
110
- edge[sourceKey] === edge[targetKey] || dirs.push(edge);
111
- } else if (type === "delete") {
112
- const edge = edges.get(value);
113
- if (!edge) return false;
114
- edges.delete(edge);
115
- sources.delete(edge);
116
- targets.delete(edge);
117
- edge[sourceKey] === edge[targetKey] || dirs.delete(edge);
118
- return true;
119
- }
120
- }
121
- /**
122
- * 清理图管理数据
123
- */
124
- clear() {
125
- this.#nodes.clear();
126
- this.#edges.clear();
127
- this.#sources.clear();
128
- this.#targets.clear();
129
- this.#dirs.clear();
130
- }
131
- }
132
- const shield_keys = hash8strings([
133
- "push",
134
- "delete",
135
- "deleteType",
136
- "clear"
137
- ]);
138
- const shield = (obj, key, origin) => {
139
- const proxy = new Proxy({}, {
140
- get(target, p) {
141
- if (p in shield_keys) return;
142
- const value = origin[p];
143
- if (typeof value === "function") {
144
- return function(...args) {
145
- const result = origin[p](...args);
146
- return result === origin ? proxy : result;
147
- };
148
- } else {
149
- return value;
150
- }
151
- }
152
- });
153
- Object.defineProperty(obj, key, {
154
- get() {
155
- return proxy;
156
- }
157
- });
158
- };
159
- export {
160
- Neo4j as default
161
- };
package/lib/once.js DELETED
@@ -1,8 +0,0 @@
1
- const once = (func, thisArg) => {
2
- let local;
3
- let inited = false;
4
- return (...list) => inited ? local : (inited = true, local = func.apply(thisArg, list));
5
- };
6
- export {
7
- once
8
- };
package/lib/parse/num.js DELETED
@@ -1,31 +0,0 @@
1
- import { isNumber } from "lodash-es";
2
- const pow = Math.floor(Math.log10(Number.MAX_SAFE_INTEGER));
3
- const re_num = /^([-+]?)0*(\d+)(?:(n)|(?:\.(\d+))?(?:[Ee]([-+]?)(\d+))?)$/;
4
- const parseNum = (num) => {
5
- if (isNumber(num) || typeof num === "bigint") return num;
6
- const match = num.match(re_num);
7
- if (!match) return Number.NaN;
8
- const [, valueSymbol, integer, bigint, decimal, powSymbol, powValue] = match;
9
- if (bigint === "n") return BigInt(valueSymbol + integer);
10
- const powSize = integer.length - 1 + (powValue ? powSymbol === "-" ? -powValue : +powValue : 0);
11
- if (powSize < pow || powSize === pow && +integer.slice(0, -1) < Number.MAX_SAFE_INTEGER / 10) return +num;
12
- try {
13
- const bigValue = BigInt(integer.slice(0, powSize + 1).padEnd(powSize + 1, "0"));
14
- let smallValue;
15
- if (decimal && powValue && powSymbol !== "-") {
16
- const powNum = +powValue;
17
- const effectDecimal = decimal.slice(0, Math.min(pow, powNum));
18
- const tinyValue = decimal.length > pow && powNum > pow ? BigInt(+decimal.slice(pow, powNum)) * BigInt(10 ** Math.max(powNum - decimal.length, 0)) : BigInt(0);
19
- smallValue = BigInt(effectDecimal) * BigInt(10 ** (powNum - effectDecimal.length)) + tinyValue;
20
- } else {
21
- smallValue = BigInt(0);
22
- }
23
- const value = bigValue + smallValue;
24
- return valueSymbol === "-" ? value * BigInt(-1) : value;
25
- } catch {
26
- return Number.NaN;
27
- }
28
- };
29
- export {
30
- parseNum
31
- };
@@ -1,22 +0,0 @@
1
- const ERROR_MESSAGE = {
2
- INT: "\u53C2\u6570\u5FC5\u987B\u662F\u6574\u6570",
3
- MIN: "\u591A\u4E2A2\u7684\u6B63\u6574\u6570\u5E42(\u4E24\u4E24\u4E0D\u76F8\u7B49)\u4E4B\u548C\u5FC5\u5B9A\u5927\u4E8E\u7B49\u4E8E1"
4
- };
5
- const deconPowersSum = (sum, sort) => {
6
- if (sum < 1) {
7
- throw new Error(ERROR_MESSAGE.MIN);
8
- } else if (sum !== Math.floor(sum)) {
9
- throw new Error(ERROR_MESSAGE.INT);
10
- }
11
- const list = [];
12
- let cache = sum;
13
- while (cache) {
14
- const power = Math.log2(cache) ^ 0;
15
- list.push(power);
16
- cache -= Math.pow(2, power);
17
- }
18
- return sort === "asc" ? list.reverse() : list;
19
- };
20
- export {
21
- deconPowersSum
22
- };
@@ -1,5 +0,0 @@
1
- import { rotateRad } from "./rad";
2
- const rotateAngle = (from, angle, visit) => rotateRad(from, angle / 180 * Math.PI, visit);
3
- export {
4
- rotateAngle
5
- };
package/lib/rotate/rad.js DELETED
@@ -1,11 +0,0 @@
1
- const rotateRad = (from, rad, visit = [0, 0]) => {
2
- const x = from[0] - visit[0];
3
- const y = from[1] - visit[1];
4
- return [
5
- x * Math.cos(rad) + y * Math.sin(rad) + visit[0],
6
- y * Math.cos(rad) - x * Math.sin(rad) + visit[1]
7
- ];
8
- };
9
- export {
10
- rotateRad
11
- };
@@ -1,53 +0,0 @@
1
- import { isString } from "lodash-es";
2
- import { isKey } from "../is/key";
3
- const cubes = {
4
- string: (text) => text,
5
- boolean: (text) => text === "true",
6
- number: (text) => Number(text),
7
- bigint: (text) => BigInt(text),
8
- date: (text) => new Date(text),
9
- regexp: (text) => new RegExp(text),
10
- null: () => null
11
- };
12
- const flats = {
13
- string: (data) => flatEncode(data),
14
- boolean: (data) => `${data}`,
15
- number: (data) => `${data}`,
16
- bigint: (data) => `${data}`,
17
- date: (data) => data.toJSON(),
18
- regexp: (data) => flatEncode(data.source),
19
- null: () => "null"
20
- };
21
- const cubeURLParams = (text, serializes) => {
22
- const params = new URLSearchParams(text);
23
- const result = {};
24
- for (const [key, value] of params) {
25
- const serialize = serializes[key];
26
- if (!serialize) continue;
27
- if (isString(serialize) && isKey(serialize, cubes)) {
28
- result[key] = cubes[serialize](value);
29
- } else {
30
- result[key] = serialize.cube(value);
31
- }
32
- }
33
- return result;
34
- };
35
- const re_flat = /\+/g;
36
- const flatEncode = (text) => text.replace(re_flat, () => "%2B");
37
- const flatURLParams = (data, serializes) => {
38
- const params = new URLSearchParams();
39
- for (const key in data) {
40
- const value = data[key];
41
- const serialize = serializes[key];
42
- if (!serialize) continue;
43
- params.append(
44
- key,
45
- isString(serialize) && isKey(serialize, flats) ? flats[serialize](value) : serialize.flat(value)
46
- );
47
- }
48
- return params.toString();
49
- };
50
- export {
51
- cubeURLParams,
52
- flatURLParams
53
- };
@@ -1,223 +0,0 @@
1
- import localforage from "localforage";
2
- import { isString } from "lodash-es";
3
- import { sm4 } from "sm-crypto";
4
- const NAME = "@vope/utils";
5
- const PREFIX = NAME + "_";
6
- const key = [25, 144, 1, 32, 25, 135, 6, 40, 32, 23, 18, 19, 32, 36, 9, 5];
7
- const iv = [182, 59, 9, 29, 86, 64, 118, 111, 112, 101, 47, 117, 116, 105, 108, 115];
8
- const config = {
9
- output: "string",
10
- mode: "cbc",
11
- iv
12
- };
13
- const HEADER = "." + sm4.encrypt(NAME, key, config);
14
- const encrypt = (data) => {
15
- let text = "";
16
- for (const key2 in data) {
17
- const value = data[key2];
18
- if (value === void 0) continue;
19
- text && (text += "&");
20
- text += key2 + ":" + value;
21
- }
22
- return sm4.encrypt(text, key, config);
23
- };
24
- const decrypt = (text) => {
25
- const data = {};
26
- let value;
27
- try {
28
- value = sm4.decrypt(text, key, config);
29
- } catch {
30
- value = "";
31
- }
32
- value.split("&").forEach((pair) => {
33
- const [key2, value2] = pair.split(":");
34
- if (key2 === "expire") {
35
- data[key2] = +value2;
36
- } else {
37
- data[key2] = value2;
38
- }
39
- });
40
- return data;
41
- };
42
- const structKey = (index) => index.toString(16).padStart(8, "0");
43
- const reductKey = (key2) => parseInt(key2, 16);
44
- const MESSAGES = {
45
- mount: "Please wait for the mounting to complete",
46
- auth: "Unauthorized operation",
47
- push: "Cannot add data to expired databases",
48
- update: "Cannot update data to expired databases",
49
- get: "Cannot get data from expired databases"
50
- };
51
- const createInstance = (name, version = 2) => localforage.createInstance({
52
- driver: localforage.INDEXEDDB,
53
- name,
54
- version
55
- });
56
- class OfflineStorage {
57
- #forage;
58
- #expire;
59
- #auth = false;
60
- #crypto;
61
- #size = 0;
62
- #mount;
63
- #mounted = false;
64
- get size() {
65
- return this.#size;
66
- }
67
- get mounted() {
68
- return this.#mounted;
69
- }
70
- constructor({
71
- name,
72
- expire: newExpire,
73
- token: newToken,
74
- crypto
75
- }) {
76
- const forage = this.#forage = createInstance(sm4.encrypt(PREFIX + name, key, config));
77
- this.#mount = new Promise(async (resolve) => {
78
- const keys = await forage.keys().then((keys2) => keys2).catch(() => []);
79
- let size = 0;
80
- let expire = newExpire;
81
- let token = newToken;
82
- let auth = true;
83
- for (const key2 of keys) {
84
- if (key2 === HEADER) {
85
- const {
86
- expire: oldExpire,
87
- token: oldToken
88
- } = decrypt(await forage.getItem(key2));
89
- expire = oldExpire === void 0 ? newExpire : Date.now() < oldExpire ? newExpire : oldExpire;
90
- auth = oldToken ? oldToken === newToken : true;
91
- token = oldToken || newToken;
92
- } else {
93
- const value = reductKey(key2) + 1;
94
- value > size && (size = value);
95
- }
96
- }
97
- this.#size = size;
98
- this.#expire = expire;
99
- this.#auth = auth;
100
- this.#crypto = crypto;
101
- await forage.setItem(HEADER, encrypt({
102
- expire,
103
- token
104
- }));
105
- this.#mounted = true;
106
- resolve(this);
107
- });
108
- }
109
- ready() {
110
- return this.#mount;
111
- }
112
- /**
113
- * 校验存储是否过期
114
- */
115
- validateExpire() {
116
- const expire = this.#expire;
117
- return expire === void 0 || Date.now() < expire;
118
- }
119
- /**
120
- * 校验
121
- */
122
- validate(callback, expireMessage) {
123
- if (!this.#mounted) {
124
- callback(new Error(MESSAGES.mount));
125
- } else if (!this.#auth) {
126
- callback(new Error(MESSAGES.auth));
127
- } else if (expireMessage && !this.validateExpire()) {
128
- callback(new Error(expireMessage));
129
- } else {
130
- callback();
131
- }
132
- }
133
- /**
134
- * 插入数据
135
- */
136
- async push(data) {
137
- this.validate((e) => {
138
- if (e) throw e;
139
- }, MESSAGES.push);
140
- const size = this.#size;
141
- const key2 = structKey(size);
142
- await this.#forage.setItem(key2, await this.#crypto.encrypt(data));
143
- return this.#size = size + 1;
144
- }
145
- /**
146
- * 修改数据
147
- */
148
- async update(index, data) {
149
- this.validate((e) => {
150
- if (e) throw e;
151
- }, MESSAGES.update);
152
- const forage = this.#forage;
153
- const key2 = structKey(index);
154
- const value = await forage.getItem(key2);
155
- if (value === null) return false;
156
- await forage.setItem(key2, await this.#crypto.encrypt(data));
157
- return true;
158
- }
159
- /**
160
- * 获取数据
161
- */
162
- async get(index) {
163
- this.validate((e) => {
164
- if (e) throw e;
165
- }, MESSAGES.get);
166
- const key2 = structKey(index);
167
- const value = await this.#forage.getItem(key2);
168
- return value === null ? null : await this.#crypto.decrypt(value);
169
- }
170
- /**
171
- * 清理存储
172
- */
173
- async clear() {
174
- this.validate((e) => {
175
- if (e) throw e;
176
- });
177
- this.#size = 0;
178
- await localforage.dropInstance({
179
- name: this.#forage.config().name
180
- });
181
- }
182
- static default(options) {
183
- isString(options) && (options = { name: options });
184
- return new OfflineStorage({
185
- ...options,
186
- crypto: {
187
- encrypt(plain) {
188
- return plain;
189
- },
190
- decrypt(cipher) {
191
- return cipher;
192
- }
193
- }
194
- });
195
- }
196
- static async clear(token, callback) {
197
- const size = PREFIX.length;
198
- const databases = await indexedDB.databases();
199
- const tasks = [];
200
- for (const { name, version } of databases) {
201
- if (name === void 0) continue;
202
- let text;
203
- try {
204
- text = sm4.decrypt(name, key, config);
205
- } catch {
206
- continue;
207
- }
208
- if (text.slice(0, size) !== PREFIX) continue;
209
- const forage = createInstance(name, version);
210
- const value = await forage.getItem(HEADER);
211
- if (value === null || decrypt(value).token !== token || callback && callback(text.slice(size))) continue;
212
- const idb = indexedDB.deleteDatabase(text);
213
- tasks.push(new Promise((resolve) => {
214
- idb.onerror = () => resolve();
215
- idb.onsuccess = () => resolve();
216
- }));
217
- }
218
- await Promise.all(tasks);
219
- }
220
- }
221
- export {
222
- OfflineStorage
223
- };