@vope/utils 1.6.0-bate.1 → 1.6.0-bate.3

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 (77) hide show
  1. package/package.json +15 -18
  2. package/cjs/storage/offline.js +0 -5186
  3. package/lib/memory/type.d.js +0 -0
  4. package/lib/storage/offline.js +0 -223
  5. package/types/memory/type.d.ts +0 -11
  6. /package/{types → dist}/clamp.d.ts +0 -0
  7. /package/{lib/clamp.js → dist/clamp.mjs} +0 -0
  8. /package/{types → dist}/classnames.d.ts +0 -0
  9. /package/{lib/classnames.js → dist/classnames.mjs} +0 -0
  10. /package/{types → dist}/compare/version.d.ts +0 -0
  11. /package/{lib/compare/version.js → dist/compare/version.mjs} +0 -0
  12. /package/{types → dist}/compare/weak-equal.d.ts +0 -0
  13. /package/{lib/compare/weak-equal.js → dist/compare/weak-equal.mjs} +0 -0
  14. /package/{types → dist}/convert/json.d.ts +0 -0
  15. /package/{lib/convert/json.js → dist/convert/json.mjs} +0 -0
  16. /package/{types → dist}/convert/object.d.ts +0 -0
  17. /package/{lib/convert/object.js → dist/convert/object.mjs} +0 -0
  18. /package/{types → dist}/convert/strings.d.ts +0 -0
  19. /package/{lib/convert/strings.js → dist/convert/strings.mjs} +0 -0
  20. /package/{types → dist}/desens.d.ts +0 -0
  21. /package/{lib/desens.js → dist/desens.mjs} +0 -0
  22. /package/{types → dist}/encoding.d.ts +0 -0
  23. /package/{lib/encoding.js → dist/encoding.mjs} +0 -0
  24. /package/{types → dist}/index.d.ts +0 -0
  25. /package/{cjs → dist}/index.js +0 -0
  26. /package/{lib/index.js → dist/index.mjs} +0 -0
  27. /package/{types → dist}/input-regex.d.ts +0 -0
  28. /package/{lib/input-regex.js → dist/input-regex.mjs} +0 -0
  29. /package/{types → dist}/is/class.d.ts +0 -0
  30. /package/{lib/is/class.js → dist/is/class.mjs} +0 -0
  31. /package/{types → dist}/is/empty.d.ts +0 -0
  32. /package/{lib/is/empty.js → dist/is/empty.mjs} +0 -0
  33. /package/{types → dist}/is/key.d.ts +0 -0
  34. /package/{lib/is/key.js → dist/is/key.mjs} +0 -0
  35. /package/{types → dist}/is/null.d.ts +0 -0
  36. /package/{lib/is/null.js → dist/is/null.mjs} +0 -0
  37. /package/{types → dist}/load/image.d.ts +0 -0
  38. /package/{lib/load/image.js → dist/load/image.mjs} +0 -0
  39. /package/{types → dist}/memory/basic.d.ts +0 -0
  40. /package/{lib/memory/basic.js → dist/memory/basic.mjs} +0 -0
  41. /package/{types → dist}/memory/list.d.ts +0 -0
  42. /package/{lib/memory/list.js → dist/memory/list.mjs} +0 -0
  43. /package/{types → dist}/memory/power.d.ts +0 -0
  44. /package/{lib/memory/power.js → dist/memory/power.mjs} +0 -0
  45. /package/{types → dist}/memory/simple.d.ts +0 -0
  46. /package/{lib/memory/simple.js → dist/memory/simple.mjs} +0 -0
  47. /package/{types → dist}/neo4j-anchors.d.ts +0 -0
  48. /package/{lib/neo4j-anchors.js → dist/neo4j-anchors.mjs} +0 -0
  49. /package/{types → dist}/neo4j.d.ts +0 -0
  50. /package/{lib/neo4j.js → dist/neo4j.mjs} +0 -0
  51. /package/{types → dist}/once.d.ts +0 -0
  52. /package/{lib/once.js → dist/once.mjs} +0 -0
  53. /package/{types → dist}/parse/num.d.ts +0 -0
  54. /package/{lib/parse/num.js → dist/parse/num.mjs} +0 -0
  55. /package/{types → dist}/power/decon.d.ts +0 -0
  56. /package/{lib/power/decon.js → dist/power/decon.mjs} +0 -0
  57. /package/{types → dist}/rotate/angle.d.ts +0 -0
  58. /package/{lib/rotate/angle.js → dist/rotate/angle.mjs} +0 -0
  59. /package/{types → dist}/rotate/rad.d.ts +0 -0
  60. /package/{lib/rotate/rad.js → dist/rotate/rad.mjs} +0 -0
  61. /package/{types → dist}/serialize/url-params.d.ts +0 -0
  62. /package/{lib/serialize/url-params.js → dist/serialize/url-params.mjs} +0 -0
  63. /package/{types → dist}/storage/offline.d.ts +0 -0
  64. /package/{types → dist}/style/css.d.ts +0 -0
  65. /package/{lib/style/css.js → dist/style/css.mjs} +0 -0
  66. /package/{types → dist}/subscriber.d.ts +0 -0
  67. /package/{lib/subscriber.js → dist/subscriber.mjs} +0 -0
  68. /package/{types → dist}/table/flex.d.ts +0 -0
  69. /package/{lib/table/flex.js → dist/table/flex.mjs} +0 -0
  70. /package/{types → dist}/union.d.ts +0 -0
  71. /package/{lib/union.js → dist/union.mjs} +0 -0
  72. /package/{types → dist}/url/join.d.ts +0 -0
  73. /package/{lib/url/join.js → dist/url/join.mjs} +0 -0
  74. /package/{types → dist}/url/params.d.ts +0 -0
  75. /package/{lib/url/params.js → dist/url/params.mjs} +0 -0
  76. /package/{types → dist}/wait.d.ts +0 -0
  77. /package/{lib/wait.js → dist/wait.mjs} +0 -0
File without changes
@@ -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
- };
@@ -1,11 +0,0 @@
1
- import { GetGivenProps } from '@vope/types'
2
-
3
- export namespace Memory {
4
- type Type = string | number | symbol
5
-
6
- type Pitch<Data, Type> = (
7
- Data extends object
8
- ? GetGivenProps<Data, Type>
9
- : never
10
- ) | ((data: Data) => Type)
11
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes