@vope/utils 1.5.0 → 1.6.0-bate.10

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 (80) hide show
  1. package/{types → dist}/index.d.ts +0 -1
  2. package/dist/index.js +1265 -0
  3. package/{lib/index.js → dist/index.mjs} +0 -2
  4. package/package.json +19 -16
  5. package/sub_storage/offline.d.ts +51 -0
  6. package/sub_storage/offline.js +255 -0
  7. package/sub_storage/package.json +5 -0
  8. package/cjs/index.js +0 -6436
  9. package/lib/memory/type.d.js +0 -0
  10. /package/{types → dist}/clamp.d.ts +0 -0
  11. /package/{lib/clamp.js → dist/clamp.mjs} +0 -0
  12. /package/{types → dist}/classnames.d.ts +0 -0
  13. /package/{lib/classnames.js → dist/classnames.mjs} +0 -0
  14. /package/{types → dist}/compare/version.d.ts +0 -0
  15. /package/{lib/compare/version.js → dist/compare/version.mjs} +0 -0
  16. /package/{types → dist}/compare/weak-equal.d.ts +0 -0
  17. /package/{lib/compare/weak-equal.js → dist/compare/weak-equal.mjs} +0 -0
  18. /package/{types → dist}/convert/json.d.ts +0 -0
  19. /package/{lib/convert/json.js → dist/convert/json.mjs} +0 -0
  20. /package/{types → dist}/convert/object.d.ts +0 -0
  21. /package/{lib/convert/object.js → dist/convert/object.mjs} +0 -0
  22. /package/{types → dist}/convert/strings.d.ts +0 -0
  23. /package/{lib/convert/strings.js → dist/convert/strings.mjs} +0 -0
  24. /package/{types → dist}/desens.d.ts +0 -0
  25. /package/{lib/desens.js → dist/desens.mjs} +0 -0
  26. /package/{types → dist}/encoding.d.ts +0 -0
  27. /package/{lib/encoding.js → dist/encoding.mjs} +0 -0
  28. /package/{types → dist}/input-regex.d.ts +0 -0
  29. /package/{lib/input-regex.js → dist/input-regex.mjs} +0 -0
  30. /package/{types → dist}/is/class.d.ts +0 -0
  31. /package/{lib/is/class.js → dist/is/class.mjs} +0 -0
  32. /package/{types → dist}/is/empty.d.ts +0 -0
  33. /package/{lib/is/empty.js → dist/is/empty.mjs} +0 -0
  34. /package/{types → dist}/is/key.d.ts +0 -0
  35. /package/{lib/is/key.js → dist/is/key.mjs} +0 -0
  36. /package/{types → dist}/is/null.d.ts +0 -0
  37. /package/{lib/is/null.js → dist/is/null.mjs} +0 -0
  38. /package/{types → dist}/load/image.d.ts +0 -0
  39. /package/{lib/load/image.js → dist/load/image.mjs} +0 -0
  40. /package/{types → dist}/memory/basic.d.ts +0 -0
  41. /package/{lib/memory/basic.js → dist/memory/basic.mjs} +0 -0
  42. /package/{types → dist}/memory/list.d.ts +0 -0
  43. /package/{lib/memory/list.js → dist/memory/list.mjs} +0 -0
  44. /package/{types → dist}/memory/power.d.ts +0 -0
  45. /package/{lib/memory/power.js → dist/memory/power.mjs} +0 -0
  46. /package/{types → dist}/memory/simple.d.ts +0 -0
  47. /package/{lib/memory/simple.js → dist/memory/simple.mjs} +0 -0
  48. /package/{types → dist}/memory/type.d.ts +0 -0
  49. /package/{types → dist}/neo4j-anchors.d.ts +0 -0
  50. /package/{lib/neo4j-anchors.js → dist/neo4j-anchors.mjs} +0 -0
  51. /package/{types → dist}/neo4j.d.ts +0 -0
  52. /package/{lib/neo4j.js → dist/neo4j.mjs} +0 -0
  53. /package/{types → dist}/once.d.ts +0 -0
  54. /package/{lib/once.js → dist/once.mjs} +0 -0
  55. /package/{types → dist}/parse/num.d.ts +0 -0
  56. /package/{lib/parse/num.js → dist/parse/num.mjs} +0 -0
  57. /package/{types → dist}/power/decon.d.ts +0 -0
  58. /package/{lib/power/decon.js → dist/power/decon.mjs} +0 -0
  59. /package/{types → dist}/rotate/angle.d.ts +0 -0
  60. /package/{lib/rotate/angle.js → dist/rotate/angle.mjs} +0 -0
  61. /package/{types → dist}/rotate/rad.d.ts +0 -0
  62. /package/{lib/rotate/rad.js → dist/rotate/rad.mjs} +0 -0
  63. /package/{types → dist}/serialize/url-params.d.ts +0 -0
  64. /package/{lib/serialize/url-params.js → dist/serialize/url-params.mjs} +0 -0
  65. /package/{types → dist}/storage/offline.d.ts +0 -0
  66. /package/{types → dist}/style/css.d.ts +0 -0
  67. /package/{lib/style/css.js → dist/style/css.mjs} +0 -0
  68. /package/{types → dist}/subscriber.d.ts +0 -0
  69. /package/{lib/subscriber.js → dist/subscriber.mjs} +0 -0
  70. /package/{types → dist}/table/flex.d.ts +0 -0
  71. /package/{lib/table/flex.js → dist/table/flex.mjs} +0 -0
  72. /package/{types → dist}/union.d.ts +0 -0
  73. /package/{lib/union.js → dist/union.mjs} +0 -0
  74. /package/{types → dist}/url/join.d.ts +0 -0
  75. /package/{lib/url/join.js → dist/url/join.mjs} +0 -0
  76. /package/{types → dist}/url/params.d.ts +0 -0
  77. /package/{lib/url/params.js → dist/url/params.mjs} +0 -0
  78. /package/{types → dist}/wait.d.ts +0 -0
  79. /package/{lib/wait.js → dist/wait.mjs} +0 -0
  80. /package/{lib/storage/offline.js → sub_storage/offline.mjs} +0 -0
@@ -25,7 +25,6 @@ import { deconPowersSum } from "./power/decon";
25
25
  import { rotateAngle } from "./rotate/angle";
26
26
  import { rotateRad } from "./rotate/rad";
27
27
  import { cubeURLParams, flatURLParams } from "./serialize/url-params";
28
- import { OfflineStorage } from "./storage/offline";
29
28
  import { css } from "./style/css";
30
29
  import { default as default4 } from "./subscriber";
31
30
  import { FlexTable } from "./table/flex";
@@ -41,7 +40,6 @@ export {
41
40
  MemorySimple,
42
41
  default3 as Neo4j,
43
42
  default2 as Neo4jAhchors,
44
- OfflineStorage,
45
43
  default4 as Subscriber,
46
44
  Union,
47
45
  clamp,
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@vope/utils",
3
- "version": "1.5.0",
3
+ "version": "1.6.0-bate.10",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "publishConfig": {
7
7
  "access": "public",
8
8
  "registry": "https://registry.npmjs.org/"
9
9
  },
10
- "types": "./types/index.d.ts",
11
- "typings": "./types/index.d.ts",
12
- "module": "./lib/index.js",
13
- "main": "./cjs/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "typings": "./dist/index.d.ts",
12
+ "module": "./dist/index.mjs",
13
+ "main": "./dist/index.js",
14
14
  "scripts": {
15
15
  "start": "esbuild ./start.ts --outfile=start.cjs --format=cjs & node ./start.cjs",
16
16
  "poststart": "esbuild ./pre.ts --outfile=pre.cjs --format=cjs & node ./pre.cjs",
@@ -19,24 +19,26 @@
19
19
  "mocha": "mocha __tests/**/*.js",
20
20
  "test": "npm run mocha",
21
21
  "prepublishOnly": "npm run build & npm run test",
22
- "debug": "npm run start & npm run premocha"
22
+ "debug": "npm run start & npm run premocha",
23
+ "clean": "rimraf start.cjs pre.cjs build.cjs dist/ __tests/ __examples/ sub_storage/"
23
24
  },
24
25
  "exports": {
25
26
  ".": {
26
- "import": "./lib/index.js",
27
- "require": "./cjs/index.js"
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.mjs",
29
+ "require": "./dist/index.js"
30
+ },
31
+ "./storage": {
32
+ "types": "./sub_storage/offline.d.ts",
33
+ "import": "./sub_storage/offline.mjs",
34
+ "require": "./sub_storage/offline.js"
28
35
  }
29
36
  },
30
37
  "author": "vope",
31
38
  "license": "ISC",
32
- "sideEffects": [
33
- "lib/storage/offline.js",
34
- "src/storage/offline.ts"
35
- ],
36
39
  "files": [
37
- "cjs",
38
- "lib",
39
- "types"
40
+ "dist",
41
+ "sub_storage"
40
42
  ],
41
43
  "devDependencies": {
42
44
  "@babel/parser": "^7.26.9",
@@ -59,7 +61,8 @@
59
61
  "rimraf": "^5.0.5",
60
62
  "rollup": "^4.34.8",
61
63
  "tslib": "^2.8.1",
62
- "typescript": "^5.7.3"
64
+ "typescript": "^5.7.3",
65
+ "uuid": "^11.1.0"
63
66
  },
64
67
  "dependencies": {
65
68
  "@type-challenges/utils": "^0.1.1",
@@ -0,0 +1,51 @@
1
+ export interface ICrypto<From, To> {
2
+ encrypt(plain: From): To | Promise<To>;
3
+ decrypt(cipher: To): From | Promise<From>;
4
+ }
5
+ interface Options<From, To> {
6
+ /** 存储名称 */
7
+ name: string;
8
+ /** 存储认证 */
9
+ token?: string;
10
+ /** 存储期限 */
11
+ expire?: number;
12
+ /** 存储加密 */
13
+ crypto: ICrypto<From, To>;
14
+ }
15
+ /**
16
+ * 离线存储
17
+ */
18
+ export declare class OfflineStorage<From, To> {
19
+ #private;
20
+ get size(): number;
21
+ get mounted(): boolean;
22
+ constructor({ name, expire: newExpire, token: newToken, crypto, }: Options<From, To>);
23
+ ready(): Promise<this>;
24
+ /**
25
+ * 校验存储是否过期
26
+ */
27
+ validateExpire(): boolean;
28
+ /**
29
+ * 校验
30
+ */
31
+ validate(callback: (error?: Error) => void, expireMessage?: string): void;
32
+ /**
33
+ * 插入数据
34
+ */
35
+ push(data: From): Promise<number>;
36
+ /**
37
+ * 修改数据
38
+ */
39
+ update(index: number, data: From): Promise<boolean>;
40
+ /**
41
+ * 获取数据
42
+ */
43
+ get(index: number): Promise<From | null>;
44
+ /**
45
+ * 清理存储
46
+ */
47
+ clear(): Promise<void>;
48
+ static default<T>(options: string | Omit<Options<T, T>, 'crypto'>): OfflineStorage<T, T>;
49
+ static clear(token?: string, callback?: (name: string) => boolean): Promise<void>;
50
+ }
51
+ export {};
@@ -0,0 +1,255 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key2 of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key2) && key2 !== except)
16
+ __defProp(to, key2, { get: () => from[key2], enumerable: !(desc = __getOwnPropDesc(from, key2)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/storage/offline.ts
31
+ var offline_exports = {};
32
+ __export(offline_exports, {
33
+ OfflineStorage: () => OfflineStorage
34
+ });
35
+ module.exports = __toCommonJS(offline_exports);
36
+ var import_localforage = __toESM(require("localforage"), 1);
37
+ var import_lodash_es = require("lodash-es");
38
+ var import_sm_crypto = require("sm-crypto");
39
+ var NAME = "@vope/utils";
40
+ var PREFIX = NAME + "_";
41
+ var key = [25, 144, 1, 32, 25, 135, 6, 40, 32, 23, 18, 19, 32, 36, 9, 5];
42
+ var iv = [182, 59, 9, 29, 86, 64, 118, 111, 112, 101, 47, 117, 116, 105, 108, 115];
43
+ var config = {
44
+ output: "string",
45
+ mode: "cbc",
46
+ iv
47
+ };
48
+ var HEADER = "." + import_sm_crypto.sm4.encrypt(NAME, key, config);
49
+ var encrypt = (data) => {
50
+ let text = "";
51
+ for (const key2 in data) {
52
+ const value = data[key2];
53
+ if (value === void 0) continue;
54
+ text && (text += "&");
55
+ text += key2 + ":" + value;
56
+ }
57
+ return import_sm_crypto.sm4.encrypt(text, key, config);
58
+ };
59
+ var decrypt = (text) => {
60
+ const data = {};
61
+ let value;
62
+ try {
63
+ value = import_sm_crypto.sm4.decrypt(text, key, config);
64
+ } catch {
65
+ value = "";
66
+ }
67
+ value.split("&").forEach((pair) => {
68
+ const [key2, value2] = pair.split(":");
69
+ if (key2 === "expire") {
70
+ data[key2] = +value2;
71
+ } else {
72
+ data[key2] = value2;
73
+ }
74
+ });
75
+ return data;
76
+ };
77
+ var structKey = (index) => index.toString(16).padStart(8, "0");
78
+ var reductKey = (key2) => parseInt(key2, 16);
79
+ var MESSAGES = {
80
+ mount: "Please wait for the mounting to complete",
81
+ auth: "Unauthorized operation",
82
+ push: "Cannot add data to expired databases",
83
+ update: "Cannot update data to expired databases",
84
+ get: "Cannot get data from expired databases"
85
+ };
86
+ var createInstance = (name, version = 2) => import_localforage.default.createInstance({
87
+ driver: import_localforage.default.INDEXEDDB,
88
+ name,
89
+ version
90
+ });
91
+ var OfflineStorage = class _OfflineStorage {
92
+ #forage;
93
+ #expire;
94
+ #auth = false;
95
+ #crypto;
96
+ #size = 0;
97
+ #mount;
98
+ #mounted = false;
99
+ get size() {
100
+ return this.#size;
101
+ }
102
+ get mounted() {
103
+ return this.#mounted;
104
+ }
105
+ constructor({
106
+ name,
107
+ expire: newExpire,
108
+ token: newToken,
109
+ crypto
110
+ }) {
111
+ const forage = this.#forage = createInstance(import_sm_crypto.sm4.encrypt(PREFIX + name, key, config));
112
+ this.#mount = new Promise(async (resolve) => {
113
+ const keys = await forage.keys().then((keys2) => keys2).catch(() => []);
114
+ let size = 0;
115
+ let expire = newExpire;
116
+ let token = newToken;
117
+ let auth = true;
118
+ for (const key2 of keys) {
119
+ if (key2 === HEADER) {
120
+ const {
121
+ expire: oldExpire,
122
+ token: oldToken
123
+ } = decrypt(await forage.getItem(key2));
124
+ expire = oldExpire === void 0 ? newExpire : Date.now() < oldExpire ? newExpire : oldExpire;
125
+ auth = oldToken ? oldToken === newToken : true;
126
+ token = oldToken || newToken;
127
+ } else {
128
+ const value = reductKey(key2) + 1;
129
+ value > size && (size = value);
130
+ }
131
+ }
132
+ this.#size = size;
133
+ this.#expire = expire;
134
+ this.#auth = auth;
135
+ this.#crypto = crypto;
136
+ await forage.setItem(HEADER, encrypt({
137
+ expire,
138
+ token
139
+ }));
140
+ this.#mounted = true;
141
+ resolve(this);
142
+ });
143
+ }
144
+ ready() {
145
+ return this.#mount;
146
+ }
147
+ /**
148
+ * 校验存储是否过期
149
+ */
150
+ validateExpire() {
151
+ const expire = this.#expire;
152
+ return expire === void 0 || Date.now() < expire;
153
+ }
154
+ /**
155
+ * 校验
156
+ */
157
+ validate(callback, expireMessage) {
158
+ if (!this.#mounted) {
159
+ callback(new Error(MESSAGES.mount));
160
+ } else if (!this.#auth) {
161
+ callback(new Error(MESSAGES.auth));
162
+ } else if (expireMessage && !this.validateExpire()) {
163
+ callback(new Error(expireMessage));
164
+ } else {
165
+ callback();
166
+ }
167
+ }
168
+ /**
169
+ * 插入数据
170
+ */
171
+ async push(data) {
172
+ this.validate((e) => {
173
+ if (e) throw e;
174
+ }, MESSAGES.push);
175
+ const size = this.#size;
176
+ const key2 = structKey(size);
177
+ await this.#forage.setItem(key2, await this.#crypto.encrypt(data));
178
+ return this.#size = size + 1;
179
+ }
180
+ /**
181
+ * 修改数据
182
+ */
183
+ async update(index, data) {
184
+ this.validate((e) => {
185
+ if (e) throw e;
186
+ }, MESSAGES.update);
187
+ const forage = this.#forage;
188
+ const key2 = structKey(index);
189
+ const value = await forage.getItem(key2);
190
+ if (value === null) return false;
191
+ await forage.setItem(key2, await this.#crypto.encrypt(data));
192
+ return true;
193
+ }
194
+ /**
195
+ * 获取数据
196
+ */
197
+ async get(index) {
198
+ this.validate((e) => {
199
+ if (e) throw e;
200
+ }, MESSAGES.get);
201
+ const key2 = structKey(index);
202
+ const value = await this.#forage.getItem(key2);
203
+ return value === null ? null : await this.#crypto.decrypt(value);
204
+ }
205
+ /**
206
+ * 清理存储
207
+ */
208
+ async clear() {
209
+ this.validate((e) => {
210
+ if (e) throw e;
211
+ });
212
+ this.#size = 0;
213
+ await import_localforage.default.dropInstance({
214
+ name: this.#forage.config().name
215
+ });
216
+ }
217
+ static default(options) {
218
+ (0, import_lodash_es.isString)(options) && (options = { name: options });
219
+ return new _OfflineStorage({
220
+ ...options,
221
+ crypto: {
222
+ encrypt(plain) {
223
+ return plain;
224
+ },
225
+ decrypt(cipher) {
226
+ return cipher;
227
+ }
228
+ }
229
+ });
230
+ }
231
+ static async clear(token, callback) {
232
+ const size = PREFIX.length;
233
+ const databases = await indexedDB.databases();
234
+ const tasks = [];
235
+ for (const { name, version } of databases) {
236
+ if (name === void 0) continue;
237
+ let text;
238
+ try {
239
+ text = import_sm_crypto.sm4.decrypt(name, key, config);
240
+ } catch {
241
+ continue;
242
+ }
243
+ if (text.slice(0, size) !== PREFIX) continue;
244
+ const forage = createInstance(name, version);
245
+ const value = await forage.getItem(HEADER);
246
+ if (value === null || decrypt(value).token !== token || callback && callback(text.slice(size))) continue;
247
+ const idb = indexedDB.deleteDatabase(text);
248
+ tasks.push(new Promise((resolve) => {
249
+ idb.onerror = () => resolve();
250
+ idb.onsuccess = () => resolve();
251
+ }));
252
+ }
253
+ await Promise.all(tasks);
254
+ }
255
+ };
@@ -0,0 +1,5 @@
1
+ {
2
+ "types": "offline.d.ts",
3
+ "import": "offline.mjs",
4
+ "require": "offline.js"
5
+ }