@esportsplus/web-storage 0.1.30 → 0.1.32
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.
- package/.editorconfig +9 -9
- package/.gitattributes +2 -2
- package/.github/dependabot.yml +23 -0
- package/.github/workflows/bump.yml +7 -0
- package/.github/workflows/publish.yml +14 -0
- package/package.json +22 -22
- package/src/index.ts +1 -1
- package/src/local.ts +176 -176
- package/src/types.ts +22 -22
- package/tsconfig.json +10 -9
package/.editorconfig
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
root = true
|
|
2
|
-
|
|
3
|
-
[*]
|
|
4
|
-
indent_style = space
|
|
5
|
-
indent_size = 4
|
|
6
|
-
charset = utf-8
|
|
7
|
-
trim_trailing_whitespace = true
|
|
8
|
-
insert_final_newline = true
|
|
9
|
-
end_of_line = lf
|
|
1
|
+
root = true
|
|
2
|
+
|
|
3
|
+
[*]
|
|
4
|
+
indent_style = space
|
|
5
|
+
indent_size = 4
|
|
6
|
+
charset = utf-8
|
|
7
|
+
trim_trailing_whitespace = true
|
|
8
|
+
insert_final_newline = true
|
|
9
|
+
end_of_line = lf
|
package/.gitattributes
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
# Auto detect text files and perform LF normalization
|
|
2
|
-
* text=auto
|
|
1
|
+
# Auto detect text files and perform LF normalization
|
|
2
|
+
* text=auto
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
|
3
|
+
# Please see the documentation for all configuration options:
|
|
4
|
+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
|
5
|
+
|
|
6
|
+
version: 2
|
|
7
|
+
registries:
|
|
8
|
+
npm-npmjs:
|
|
9
|
+
token: ${{secrets.NPM_TOKEN}}
|
|
10
|
+
type: npm-registry
|
|
11
|
+
url: https://registry.npmjs.org
|
|
12
|
+
updates:
|
|
13
|
+
- package-ecosystem: "npm"
|
|
14
|
+
directory: "/"
|
|
15
|
+
groups:
|
|
16
|
+
production-dependencies:
|
|
17
|
+
dependency-type: "production"
|
|
18
|
+
development-dependencies:
|
|
19
|
+
dependency-type: "development"
|
|
20
|
+
registries:
|
|
21
|
+
- npm-npmjs
|
|
22
|
+
schedule:
|
|
23
|
+
interval: "daily"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
name: publish to npm
|
|
2
|
+
on:
|
|
3
|
+
release:
|
|
4
|
+
types: [published]
|
|
5
|
+
workflow_dispatch:
|
|
6
|
+
workflow_run:
|
|
7
|
+
workflows: [bump]
|
|
8
|
+
types:
|
|
9
|
+
- completed
|
|
10
|
+
jobs:
|
|
11
|
+
publish:
|
|
12
|
+
secrets:
|
|
13
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN_PUBLISHING }}
|
|
14
|
+
uses: esportsplus/workflows/.github/workflows/publish.yml@main
|
package/package.json
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
{
|
|
2
|
-
"author": "ICJR",
|
|
3
|
-
"dependencies": {
|
|
4
|
-
"@esportsplus/crypto": "^0.0.
|
|
5
|
-
"localforage": "^1.10.0"
|
|
6
|
-
},
|
|
7
|
-
"description": "Web storage utility",
|
|
8
|
-
"devDependencies": {
|
|
9
|
-
"@esportsplus/
|
|
10
|
-
},
|
|
11
|
-
"main": "build/index.js",
|
|
12
|
-
"name": "@esportsplus/web-storage",
|
|
13
|
-
"private": false,
|
|
14
|
-
"scripts": {
|
|
15
|
-
"build": "tsc && tsc-alias",
|
|
16
|
-
"-": "-",
|
|
17
|
-
"prepare": "npm run build",
|
|
18
|
-
"prepublishOnly": "npm run build"
|
|
19
|
-
},
|
|
20
|
-
"types": "build/index.d.ts",
|
|
21
|
-
"version": "0.1.
|
|
22
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"author": "ICJR",
|
|
3
|
+
"dependencies": {
|
|
4
|
+
"@esportsplus/crypto": "^0.0.6",
|
|
5
|
+
"localforage": "^1.10.0"
|
|
6
|
+
},
|
|
7
|
+
"description": "Web storage utility",
|
|
8
|
+
"devDependencies": {
|
|
9
|
+
"@esportsplus/typescript": "^0.1.2"
|
|
10
|
+
},
|
|
11
|
+
"main": "build/index.js",
|
|
12
|
+
"name": "@esportsplus/web-storage",
|
|
13
|
+
"private": false,
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc && tsc-alias",
|
|
16
|
+
"-": "-",
|
|
17
|
+
"prepare": "npm run build",
|
|
18
|
+
"prepublishOnly": "npm run build"
|
|
19
|
+
},
|
|
20
|
+
"types": "build/index.d.ts",
|
|
21
|
+
"version": "0.1.32"
|
|
22
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default as local } from './local';
|
|
1
|
+
export { default as local } from './local';
|
package/src/local.ts
CHANGED
|
@@ -1,177 +1,177 @@
|
|
|
1
|
-
import { Driver, Filter, LocalForage, Options } from '~/types';
|
|
2
|
-
import { decrypt, encrypt } from '@esportsplus/crypto';
|
|
3
|
-
import localforage from 'localforage';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
async function deserialize(value: unknown, secret: null | string = null) {
|
|
7
|
-
if (secret && typeof value === 'string') {
|
|
8
|
-
value = await decrypt(value, secret);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
if (typeof value === 'string') {
|
|
12
|
-
value = JSON.parse(value);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return value;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async function serialize(value: unknown, secret: null | string = null) {
|
|
19
|
-
if (value === null || value === undefined) {
|
|
20
|
-
return undefined;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
value = JSON.stringify(value);
|
|
24
|
-
|
|
25
|
-
if (secret) {
|
|
26
|
-
value = await encrypt(value as string, secret);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return value as string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class Local<T> {
|
|
34
|
-
instance: LocalForage;
|
|
35
|
-
iterate: LocalForage['iterate'];
|
|
36
|
-
keys: LocalForage['keys'];
|
|
37
|
-
length: LocalForage['length'];
|
|
38
|
-
secret: null | string = null;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
constructor(options: Options, secret?: string) {
|
|
42
|
-
switch ((options.driver || Driver.IndexedDB) as Driver) {
|
|
43
|
-
case Driver.LocalStorage:
|
|
44
|
-
options.driver = localforage.LOCALSTORAGE;
|
|
45
|
-
break;
|
|
46
|
-
default:
|
|
47
|
-
options.driver = localforage.INDEXEDDB;
|
|
48
|
-
break;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
this.instance = localforage.createInstance(
|
|
52
|
-
Object.assign(options, { storeName: options.name })
|
|
53
|
-
);
|
|
54
|
-
this.iterate = this.instance.iterate;
|
|
55
|
-
this.keys = this.instance.keys;
|
|
56
|
-
this.length = this.instance.length;
|
|
57
|
-
|
|
58
|
-
if (secret) {
|
|
59
|
-
this.secret = secret;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
async all(): Promise<T> {
|
|
65
|
-
let stack: Promise<void>[] = [],
|
|
66
|
-
values: T = {} as T;
|
|
67
|
-
|
|
68
|
-
await this.instance.iterate((v: unknown, k: string) => {
|
|
69
|
-
stack.push(
|
|
70
|
-
deserialize(v, this.secret)
|
|
71
|
-
.then((value) => {
|
|
72
|
-
if (value === undefined) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
values[k as keyof T] = value as T[keyof T];
|
|
77
|
-
})
|
|
78
|
-
.catch(() => {})
|
|
79
|
-
)
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
await Promise.allSettled(stack);
|
|
83
|
-
|
|
84
|
-
return values;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async clear() {
|
|
88
|
-
await this.instance.clear();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
async delete(...keys: (keyof T)[]) {
|
|
92
|
-
let stack: Promise<void>[] = [];
|
|
93
|
-
|
|
94
|
-
for (let i = 0, n = keys.length; i < n; i++) {
|
|
95
|
-
stack.push( this.instance.removeItem(keys[i] as string) );
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
await Promise.allSettled(stack);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
async filter(fn: Filter<T>): Promise<T> {
|
|
102
|
-
let stop: VoidFunction = () => {
|
|
103
|
-
stopped = true;
|
|
104
|
-
},
|
|
105
|
-
stopped: boolean = false,
|
|
106
|
-
values: T = {} as T;
|
|
107
|
-
|
|
108
|
-
await this.instance.iterate(async (v, k, i) => {
|
|
109
|
-
let key = k as keyof T,
|
|
110
|
-
value = await deserialize(v, this.secret).catch(() => undefined) as T[keyof T];
|
|
111
|
-
|
|
112
|
-
if (value === undefined) {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (await fn({ i, key, stop, value })) {
|
|
117
|
-
values[key] = value;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// LocalForage iterate will stop once a non
|
|
121
|
-
// undefined value is returned
|
|
122
|
-
if (stopped) {
|
|
123
|
-
return true;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
return values;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async get(key: keyof T) {
|
|
131
|
-
return await deserialize( await this.instance.getItem(key as string), this.secret ).catch(() => undefined);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async only(...keys: (keyof T)[]) {
|
|
135
|
-
return await this.filter( ({ key }) => keys.includes(key) );
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
async replace(values: T) {
|
|
139
|
-
let failed: string[] = [],
|
|
140
|
-
stack: Promise<void>[] = [];
|
|
141
|
-
|
|
142
|
-
for (let key in values) {
|
|
143
|
-
stack.push(
|
|
144
|
-
this.set(key, values[key])
|
|
145
|
-
.then((ok) => {
|
|
146
|
-
if (ok) {
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
failed.push(key);
|
|
151
|
-
})
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
await Promise.allSettled(stack);
|
|
156
|
-
|
|
157
|
-
return failed;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
async set(key: keyof T, value: T[keyof T]) {
|
|
161
|
-
let ok = true;
|
|
162
|
-
|
|
163
|
-
await this.instance.setItem(
|
|
164
|
-
key as string,
|
|
165
|
-
await serialize(value, this.secret)
|
|
166
|
-
.catch(() => {
|
|
167
|
-
ok = false;
|
|
168
|
-
return undefined;
|
|
169
|
-
})
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
return ok;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
|
|
1
|
+
import { Driver, Filter, LocalForage, Options } from '~/types';
|
|
2
|
+
import { decrypt, encrypt } from '@esportsplus/crypto';
|
|
3
|
+
import localforage from 'localforage';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
async function deserialize(value: unknown, secret: null | string = null) {
|
|
7
|
+
if (secret && typeof value === 'string') {
|
|
8
|
+
value = await decrypt(value, secret);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (typeof value === 'string') {
|
|
12
|
+
value = JSON.parse(value);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function serialize(value: unknown, secret: null | string = null) {
|
|
19
|
+
if (value === null || value === undefined) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
value = JSON.stringify(value);
|
|
24
|
+
|
|
25
|
+
if (secret) {
|
|
26
|
+
value = await encrypt(value as string, secret);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return value as string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Local<T> {
|
|
34
|
+
instance: LocalForage;
|
|
35
|
+
iterate: LocalForage['iterate'];
|
|
36
|
+
keys: LocalForage['keys'];
|
|
37
|
+
length: LocalForage['length'];
|
|
38
|
+
secret: null | string = null;
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
constructor(options: Options, secret?: string) {
|
|
42
|
+
switch ((options.driver || Driver.IndexedDB) as Driver) {
|
|
43
|
+
case Driver.LocalStorage:
|
|
44
|
+
options.driver = localforage.LOCALSTORAGE;
|
|
45
|
+
break;
|
|
46
|
+
default:
|
|
47
|
+
options.driver = localforage.INDEXEDDB;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this.instance = localforage.createInstance(
|
|
52
|
+
Object.assign(options, { storeName: options.name })
|
|
53
|
+
);
|
|
54
|
+
this.iterate = this.instance.iterate;
|
|
55
|
+
this.keys = this.instance.keys;
|
|
56
|
+
this.length = this.instance.length;
|
|
57
|
+
|
|
58
|
+
if (secret) {
|
|
59
|
+
this.secret = secret;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
async all(): Promise<T> {
|
|
65
|
+
let stack: Promise<void>[] = [],
|
|
66
|
+
values: T = {} as T;
|
|
67
|
+
|
|
68
|
+
await this.instance.iterate((v: unknown, k: string) => {
|
|
69
|
+
stack.push(
|
|
70
|
+
deserialize(v, this.secret)
|
|
71
|
+
.then((value) => {
|
|
72
|
+
if (value === undefined) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
values[k as keyof T] = value as T[keyof T];
|
|
77
|
+
})
|
|
78
|
+
.catch(() => {})
|
|
79
|
+
)
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
await Promise.allSettled(stack);
|
|
83
|
+
|
|
84
|
+
return values;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async clear() {
|
|
88
|
+
await this.instance.clear();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async delete(...keys: (keyof T)[]) {
|
|
92
|
+
let stack: Promise<void>[] = [];
|
|
93
|
+
|
|
94
|
+
for (let i = 0, n = keys.length; i < n; i++) {
|
|
95
|
+
stack.push( this.instance.removeItem(keys[i] as string) );
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
await Promise.allSettled(stack);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async filter(fn: Filter<T>): Promise<T> {
|
|
102
|
+
let stop: VoidFunction = () => {
|
|
103
|
+
stopped = true;
|
|
104
|
+
},
|
|
105
|
+
stopped: boolean = false,
|
|
106
|
+
values: T = {} as T;
|
|
107
|
+
|
|
108
|
+
await this.instance.iterate(async (v, k, i) => {
|
|
109
|
+
let key = k as keyof T,
|
|
110
|
+
value = await deserialize(v, this.secret).catch(() => undefined) as T[keyof T];
|
|
111
|
+
|
|
112
|
+
if (value === undefined) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (await fn({ i, key, stop, value })) {
|
|
117
|
+
values[key] = value;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// LocalForage iterate will stop once a non
|
|
121
|
+
// undefined value is returned
|
|
122
|
+
if (stopped) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
return values;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async get(key: keyof T) {
|
|
131
|
+
return await deserialize( await this.instance.getItem(key as string), this.secret ).catch(() => undefined);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async only(...keys: (keyof T)[]) {
|
|
135
|
+
return await this.filter( ({ key }) => keys.includes(key) );
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async replace(values: T) {
|
|
139
|
+
let failed: string[] = [],
|
|
140
|
+
stack: Promise<void>[] = [];
|
|
141
|
+
|
|
142
|
+
for (let key in values) {
|
|
143
|
+
stack.push(
|
|
144
|
+
this.set(key, values[key])
|
|
145
|
+
.then((ok) => {
|
|
146
|
+
if (ok) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
failed.push(key);
|
|
151
|
+
})
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
await Promise.allSettled(stack);
|
|
156
|
+
|
|
157
|
+
return failed;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async set(key: keyof T, value: T[keyof T]) {
|
|
161
|
+
let ok = true;
|
|
162
|
+
|
|
163
|
+
await this.instance.setItem(
|
|
164
|
+
key as string,
|
|
165
|
+
await serialize(value, this.secret)
|
|
166
|
+
.catch(() => {
|
|
167
|
+
ok = false;
|
|
168
|
+
return undefined;
|
|
169
|
+
})
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
return ok;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
|
|
177
177
|
export default <T>(options: Options, secret?: string) => new Local<T>(options, secret);
|
package/src/types.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import localforage from 'localforage';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
enum Driver {
|
|
5
|
-
IndexedDB,
|
|
6
|
-
LocalStorage
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
type Filter<T> = (data: { i: number, key: keyof T, stop: VoidFunction, value: T[keyof T] }) => boolean | Promise<boolean>;
|
|
10
|
-
|
|
11
|
-
type LocalForage = typeof localforage;
|
|
12
|
-
|
|
13
|
-
type Options = {
|
|
14
|
-
description?: string;
|
|
15
|
-
driver?: string | string[];
|
|
16
|
-
name: string;
|
|
17
|
-
size?: number;
|
|
18
|
-
version?: number;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export { Driver, Filter, LocalForage, Options };
|
|
1
|
+
import localforage from 'localforage';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
enum Driver {
|
|
5
|
+
IndexedDB,
|
|
6
|
+
LocalStorage
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type Filter<T> = (data: { i: number, key: keyof T, stop: VoidFunction, value: T[keyof T] }) => boolean | Promise<boolean>;
|
|
10
|
+
|
|
11
|
+
type LocalForage = typeof localforage;
|
|
12
|
+
|
|
13
|
+
type Options = {
|
|
14
|
+
description?: string;
|
|
15
|
+
driver?: string | string[];
|
|
16
|
+
name: string;
|
|
17
|
+
size?: number;
|
|
18
|
+
version?: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
export { Driver, Filter, LocalForage, Options };
|
package/tsconfig.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"baseUrl": ".",
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": ".",
|
|
4
|
+
"declaration": true,
|
|
5
|
+
"declarationDir": "build",
|
|
6
|
+
"outDir": "build"
|
|
7
|
+
},
|
|
8
|
+
"exclude": ["node_modules"],
|
|
9
|
+
"extends": "./node_modules/@esportsplus/typescript/tsconfig.base.json",
|
|
10
|
+
"include": ["src"]
|
|
10
11
|
}
|