@esportsplus/web-storage 0.0.0 → 0.0.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.
package/build/index.d.ts CHANGED
@@ -1,14 +1,10 @@
1
- import ipfs from './ipfs';
2
1
  import local from './local';
3
2
  declare const _default: {
4
- ipfs: {
5
- upload: (data: import("./ipfs/types").Data, compress?: boolean | undefined) => Promise<string>;
6
- uploadable: (value: any) => boolean;
7
- };
8
3
  local: {
9
- clear: (callback?: ((err: any) => void) | undefined) => Promise<void>;
4
+ clear: () => void;
10
5
  delete: (key: string) => void;
11
- get: (key: string) => Promise<any>;
6
+ get: (key: string, fallback?: any) => Promise<any>;
7
+ has: (key: string) => Promise<boolean>;
12
8
  set: (key: string, value: any) => void;
13
9
  useIndexedDB: (options?: import("./local/types").Options) => void;
14
10
  useLocalStorage: (options?: import("./local/types").Options) => void;
@@ -16,4 +12,4 @@ declare const _default: {
16
12
  };
17
13
  };
18
14
  export default _default;
19
- export { ipfs, local };
15
+ export { local };
package/build/index.js CHANGED
@@ -1,4 +1,3 @@
1
- import ipfs from './ipfs';
2
1
  import local from './local';
3
- export default { ipfs, local };
4
- export { ipfs, local };
2
+ export default { local };
3
+ export { local };
@@ -1,8 +1,9 @@
1
1
  import type { Options } from './types';
2
2
  declare const _default: {
3
- clear: (callback?: ((err: any) => void) | undefined) => Promise<void>;
3
+ clear: () => void;
4
4
  delete: (key: string) => void;
5
- get: (key: string) => Promise<any>;
5
+ get: (key: string, fallback?: any) => Promise<any>;
6
+ has: (key: string) => Promise<boolean>;
6
7
  set: (key: string, value: any) => void;
7
8
  useIndexedDB: (options?: Options) => void;
8
9
  useLocalStorage: (options?: Options) => void;
@@ -1,16 +1,37 @@
1
+ import dot from '@esportsplus/dot';
1
2
  import localforage from 'localforage';
2
- let driver = localforage.LOCALSTORAGE;
3
+ let cache = {}, driver = localforage.LOCALSTORAGE;
3
4
  function init(options = {}) {
4
5
  localforage.config(Object.assign({ name: 'store' }, options, { driver }));
5
6
  }
6
- const clear = localforage.clear;
7
+ const clear = () => {
8
+ cache = {};
9
+ localforage.clear();
10
+ };
7
11
  const del = (key) => {
12
+ dot.set(cache, key, undefined);
8
13
  localforage.removeItem(key);
9
14
  };
10
- const get = (key) => {
11
- return localforage.getItem(key);
15
+ const get = async (key, fallback = null) => {
16
+ if (dot.has(cache, key)) {
17
+ return dot.get(cache, key);
18
+ }
19
+ let root = `${key}`.split('.')[0], value = await localforage.getItem(root);
20
+ if (value === null && typeof fallback === 'function') {
21
+ value = await fallback();
22
+ }
23
+ set(root, value);
24
+ value = dot.get(cache, key);
25
+ if (value === null) {
26
+ throw new Error(`'${key}' has not been set in storage`);
27
+ }
28
+ return value;
29
+ };
30
+ const has = async (key) => {
31
+ return dot.has(cache, key) || (await localforage.getItem(key)) === null;
12
32
  };
13
33
  const set = (key, value) => {
34
+ dot.set(cache, key, value);
14
35
  localforage.setItem(key, value);
15
36
  };
16
37
  const useIndexedDB = (options = {}) => {
@@ -25,4 +46,4 @@ const useOptions = (options = {}) => {
25
46
  init(options);
26
47
  };
27
48
  init();
28
- export default { clear, delete: del, get, set, useIndexedDB, useLocalStorage, useOptions };
49
+ export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };
package/package.json CHANGED
@@ -1,33 +1,30 @@
1
- {
2
- "author": "ICJR",
3
- "dependencies": {
4
- "@esportsplus/dot": "^0.0.3",
5
- "browser-image-compression": "^1.0.17",
6
- "dash-core": "npm:dash@*",
7
- "ipfs-core": "*",
8
- "localforage": "^1.10.0"
9
- },
10
- "description": "Web storage utility",
11
- "devDependencies": {
12
- "glob": "^7.1.7",
13
- "node-polyfill-webpack-plugin": "^1.1.4",
14
- "npm-run-all": "^4.1.5",
15
- "ts-loader": "^9.2.6",
16
- "tsc-alias": "^1.5.0",
17
- "tsconfig-paths-webpack-plugin": "^3.5.2",
18
- "typescript": "^4.5.2",
19
- "webpack": "^5.64.4",
20
- "webpack-cli": "^4.9.1"
21
- },
22
- "main": "./build/index.js",
23
- "name": "@esportsplus/web-storage",
24
- "private": false,
25
- "scripts": {
26
- "build": "tsc",
27
- "-": "-",
28
- "prepare": "npm run build",
29
- "prepublishOnly": "npm run build"
30
- },
31
- "types": "./build/index.d.ts",
32
- "version": "0.0.0"
33
- }
1
+ {
2
+ "author": "ICJR",
3
+ "dependencies": {
4
+ "@esportsplus/dot": "^0.0.3",
5
+ "localforage": "^1.10.0"
6
+ },
7
+ "description": "Web storage utility",
8
+ "devDependencies": {
9
+ "glob": "^7.1.7",
10
+ "node-polyfill-webpack-plugin": "^1.1.4",
11
+ "npm-run-all": "^4.1.5",
12
+ "ts-loader": "^9.2.6",
13
+ "tsc-alias": "^1.5.0",
14
+ "tsconfig-paths-webpack-plugin": "^3.5.2",
15
+ "typescript": "^4.5.2",
16
+ "webpack": "^5.64.4",
17
+ "webpack-cli": "^4.9.1"
18
+ },
19
+ "main": "./build/index.js",
20
+ "name": "@esportsplus/web-storage",
21
+ "private": false,
22
+ "scripts": {
23
+ "build": "tsc",
24
+ "-": "-",
25
+ "prepare": "npm run build",
26
+ "prepublishOnly": "npm run build"
27
+ },
28
+ "types": "./build/index.d.ts",
29
+ "version": "0.0.3"
30
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,5 @@
1
- import ipfs from './ipfs';
2
- import local from './local';
3
-
4
-
5
- export default { ipfs, local };
6
- export { ipfs, local };
1
+ import local from './local';
2
+
3
+
4
+ export default { local };
5
+ export { local };
@@ -1,46 +1,76 @@
1
- import type { Options } from './types';
2
- import localforage from 'localforage';
3
-
4
-
5
- let driver: any = localforage.LOCALSTORAGE;
6
-
7
-
8
- function init(options: Options = {}): void {
9
- localforage.config(Object.assign({ name: 'store' }, options, { driver }));
10
- }
11
-
12
-
13
- const clear = localforage.clear;
14
-
15
- const del = (key: string): void => {
16
- localforage.removeItem(key);
17
- }
18
-
19
- const get = (key: string): Promise<any> => {
20
- return localforage.getItem(key);
21
- };
22
-
23
- const set = (key: string, value: any): void => {
24
- localforage.setItem(key, value);
25
- };
26
-
27
- const useIndexedDB = (options: Options = {}): void => {
28
- driver = localforage.INDEXEDDB;
29
- init(options);
30
- };
31
-
32
- const useLocalStorage = (options: Options = {}): void => {
33
- driver = localforage.LOCALSTORAGE;
34
- init(options);
35
- };
36
-
37
- const useOptions = (options: Options = {}): void => {
38
- init(options);
39
- };
40
-
41
-
42
- // Initialize using localstorage as default storage
43
- init();
44
-
45
-
46
- export default { clear, delete: del, get, set, useIndexedDB, useLocalStorage, useOptions };
1
+ import type { Options } from './types';
2
+ import dot from '@esportsplus/dot';
3
+ import localforage from 'localforage';
4
+
5
+
6
+ let cache: any = {},
7
+ driver: any = localforage.LOCALSTORAGE;
8
+
9
+
10
+ function init(options: Options = {}): void {
11
+ localforage.config(Object.assign({ name: 'store' }, options, { driver }));
12
+ }
13
+
14
+
15
+ const clear = () => {
16
+ cache = {};
17
+ localforage.clear();
18
+ };
19
+
20
+ const del = (key: string): void => {
21
+ dot.set(cache, key, undefined);
22
+ localforage.removeItem(key);
23
+ }
24
+
25
+ const get = async (key: string, fallback: any = null): Promise<any> => {
26
+ if (dot.has(cache, key)) {
27
+ return dot.get(cache, key);
28
+ }
29
+
30
+ let root: any = `${key}`.split('.')[0],
31
+ value: any = await localforage.getItem(root);
32
+
33
+ if (value === null && typeof fallback === 'function' ) {
34
+ value = await fallback();
35
+ }
36
+
37
+ set(root, value);
38
+
39
+ value = dot.get(cache, key);
40
+
41
+ if (value === null) {
42
+ throw new Error(`'${key}' has not been set in storage`);
43
+ }
44
+
45
+ return value;
46
+ };
47
+
48
+ const has = async (key: string): Promise<boolean> => {
49
+ return dot.has(cache, key) || (await localforage.getItem(key)) === null;
50
+ };
51
+
52
+ const set = (key: string, value: any): void => {
53
+ dot.set(cache, key, value);
54
+ localforage.setItem(key, value);
55
+ };
56
+
57
+ const useIndexedDB = (options: Options = {}): void => {
58
+ driver = localforage.INDEXEDDB;
59
+ init(options);
60
+ };
61
+
62
+ const useLocalStorage = (options: Options = {}): void => {
63
+ driver = localforage.LOCALSTORAGE;
64
+ init(options);
65
+ };
66
+
67
+ const useOptions = (options: Options = {}): void => {
68
+ init(options);
69
+ };
70
+
71
+
72
+ // Initialize using localstorage as default storage
73
+ init();
74
+
75
+
76
+ export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };
@@ -1,6 +0,0 @@
1
- import type { Data } from './types';
2
- declare const _default: {
3
- upload: (data: Data, compress?: boolean | undefined) => Promise<string>;
4
- uploadable: (value: any) => boolean;
5
- };
6
- export default _default;
@@ -1,54 +0,0 @@
1
- import compressor from 'browser-image-compression';
2
- import * as IPFS from 'ipfs-core';
3
- let node;
4
- async function connect() {
5
- if (node) {
6
- return;
7
- }
8
- node = await IPFS.create();
9
- }
10
- ;
11
- const upload = async (data, compress) => {
12
- let cid = '';
13
- await connect();
14
- if (Array.isArray(data)) {
15
- for await (const item of node.addAll(data, {
16
- pin: true,
17
- wrapWithDirectory: true
18
- })) {
19
- if (item.path) {
20
- continue;
21
- }
22
- cid = item.cid.toString();
23
- }
24
- }
25
- else {
26
- if (data instanceof File) {
27
- if (data.type.startsWith('image') && compress) {
28
- data = await compressor(data, { useWebWorker: true });
29
- }
30
- data = await data.text();
31
- }
32
- cid = (await node.add(data)).cid.toString();
33
- }
34
- return `ipfs://${cid}`;
35
- };
36
- const uploadable = (value) => {
37
- let valid = false;
38
- if (value instanceof File) {
39
- valid = true;
40
- }
41
- else if (typeof value === 'object' && value !== null) {
42
- valid = 'content' in value;
43
- }
44
- else if (Array.isArray(value)) {
45
- for (let i = 0, n = value.length; i < n; i++) {
46
- valid = uploadable(value[i]);
47
- if (!valid) {
48
- break;
49
- }
50
- }
51
- }
52
- return valid;
53
- };
54
- export default { upload, uploadable };
@@ -1,5 +0,0 @@
1
- declare type Data = {
2
- content: string;
3
- path: string;
4
- }[] | File | string;
5
- export type { Data };
@@ -1 +0,0 @@
1
- export {};
package/src/ipfs/index.ts DELETED
@@ -1,74 +0,0 @@
1
- import type { Data } from './types';
2
- import compressor from 'browser-image-compression';
3
- import * as IPFS from 'ipfs-core';
4
-
5
-
6
- let node: any;
7
-
8
-
9
- // TODO: Enable custom IPFS host connections
10
- async function connect(): Promise<void> {
11
- if (node) {
12
- return;
13
- }
14
-
15
- node = await IPFS.create();
16
- };
17
-
18
-
19
- const upload = async (data: Data, compress?: boolean): Promise<string> => {
20
- let cid: string = '';
21
-
22
- await connect();
23
-
24
- if (Array.isArray(data)) {
25
- for await (const item of node.addAll(data, {
26
- pin: true,
27
- wrapWithDirectory: true
28
- })) {
29
- if (item.path) {
30
- continue;
31
- }
32
-
33
- cid = item.cid.toString();
34
- }
35
- }
36
- else {
37
- if (data instanceof File) {
38
- if (data.type.startsWith('image') && compress) {
39
- data = await compressor(data, { useWebWorker: true });
40
- }
41
-
42
- data = await data.text();
43
- }
44
-
45
- cid = ( await node.add(data) ).cid.toString();
46
- }
47
-
48
- return `ipfs://${cid}`;
49
- };
50
-
51
- const uploadable = (value: any): boolean => {
52
- let valid = false;
53
-
54
- if (value instanceof File) {
55
- valid = true;
56
- }
57
- else if (typeof value === 'object' && value !== null) {
58
- valid = 'content' in value;
59
- }
60
- else if (Array.isArray(value)) {
61
- for (let i = 0, n = value.length; i < n; i++) {
62
- valid = uploadable(value[i]);
63
-
64
- if (!valid) {
65
- break;
66
- }
67
- }
68
- }
69
-
70
- return valid;
71
- };
72
-
73
-
74
- export default { upload, uploadable };
package/src/ipfs/types.ts DELETED
@@ -1,4 +0,0 @@
1
- type Data = { content: string, path: string }[] | File | string;
2
-
3
-
4
- export type { Data };