anys-web-plugin-store-offline 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2023 tangshuang
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/cjs/index.js ADDED
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.AnysStoreOfflinePlugin = void 0;
7
+ var _anysShared = require("anys-shared");
8
+ var _index = require("indb/es/index.js");
9
+ let AnysStoreOfflinePlugin = /*#__PURE__*/function () {
10
+ function AnysStoreOfflinePlugin(anys) {
11
+ this.anys = anys;
12
+ }
13
+ var _proto = AnysStoreOfflinePlugin.prototype;
14
+ _proto.options = function options() {
15
+ return {
16
+ namespace: new Error('[Anys]: options.namespace is required!'),
17
+ /**
18
+ * offline log will close autoReport as default,
19
+ * developers should must invoke anys.send to send logs to server side during
20
+ * which should use this.[offlineLogger].select to pick logs,
21
+ * and this.[offlineLogger].remove to clear logs
22
+ */
23
+ autoReport: false,
24
+ /**
25
+ * a offline log will be delete after how long time,
26
+ * default 2 day (48 hours)
27
+ */
28
+ expireTime: 3600000 * 48
29
+ };
30
+ };
31
+ _proto.init = function init() {
32
+ const storeName = 'anyslogs';
33
+ const idb = new _index.InDB({
34
+ name: this.anys.options.namespace,
35
+ version: 1,
36
+ stores: [{
37
+ name: storeName,
38
+ primaryKeyPath: '_id',
39
+ autoIncrement: true
40
+ }]
41
+ });
42
+ this.db = idb.use(storeName);
43
+ this.queue = new _anysShared.AsyncQueue();
44
+
45
+ /**
46
+ * clear logs which should be delete from db
47
+ */
48
+ const {
49
+ expireTime
50
+ } = this.anys.options;
51
+ if (expireTime) {
52
+ let offset = 0;
53
+ const timer = setInterval(() => {
54
+ this.queue.push(() => this.db.some(100, offset)).then(logs => {
55
+ if (!logs.length) {
56
+ clearInterval(timer);
57
+ return;
58
+ }
59
+ const curr = Date.now();
60
+ const ids = [];
61
+ logs.forEach(log => {
62
+ const {
63
+ _id,
64
+ time
65
+ } = log;
66
+ if (curr - time > expireTime) {
67
+ ids.push(_id);
68
+ }
69
+ });
70
+ if (ids.length) {
71
+ this.queue.push(() => this.db.delete(ids));
72
+ }
73
+ offset += 100;
74
+ offset -= ids.length;
75
+ });
76
+ }, 10000);
77
+ }
78
+ };
79
+ _proto.write = function write(data) {
80
+ this.queue.push(() => this.db.add(data)).then(id => {
81
+ this.anys.emit('writeOfflineLog', {
82
+ id,
83
+ data
84
+ });
85
+ });
86
+ }
87
+
88
+ /**
89
+ * select records from db
90
+ * anys.report([
91
+ * { key: 'time', value: Date.now() - 3600000, compare: '>' },
92
+ * { key: 'trace', value: 'xxx' },
93
+ * ]);
94
+ * https://github.com/tangshuang/indb#select
95
+ * @param {Array} message
96
+ * @returns
97
+ */;
98
+ _proto.read = function read(message) {
99
+ if (!message) {
100
+ message = [{
101
+ key: 'client',
102
+ value: this.anys.clientId
103
+ }, {
104
+ key: 'trace',
105
+ value: this.anys.traceId
106
+ }];
107
+ } else if (!Array.isArray(message)) {
108
+ return;
109
+ } else if (message.some(item => !item.key)) {
110
+ return;
111
+ }
112
+ return this.select(message);
113
+ }
114
+
115
+ /**
116
+ * select records from db
117
+ * https://github.com/tangshuang/indb#select
118
+ * @param {Array} conditions
119
+ * @returns
120
+ */;
121
+ _proto.select = function select(conditions) {
122
+ return this.queue.push(() => this.db.select(conditions));
123
+ }
124
+
125
+ /**
126
+ * delete records from db
127
+ * @param {number|Array<number>} ids
128
+ * @returns
129
+ */;
130
+ _proto.remove = function remove(ids) {
131
+ return this.queue.push(() => this.db.delete(ids)).catch(() => {});
132
+ };
133
+ return AnysStoreOfflinePlugin;
134
+ }();
135
+ exports.AnysStoreOfflinePlugin = AnysStoreOfflinePlugin;
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ (()=>{var e={d:(t,n)=>{for(var r in n)e.o(n,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:n[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};(()=>{"use strict";e.r(t),e.d(t,{AnysStoreOfflinePlugin:()=>u});let n=function(){function e(){this.queue=[],this.status=0}return e.prototype.push=function(e){let t,n;const r=new Promise(((e,r)=>{t=e,n=r})),s={runner:e,resolve:t,reject:n};this.queue.push(s);const o=()=>{setTimeout((()=>{if(!this.queue.length)return void(this.status=0);const{runner:e,resolve:t,reject:n}=this.queue.shift();e().then(t,n).finally(o)}),0),this.status=1};return this.status||o(),r},e}(),r=function(){function e(e={}){let{name:t,version:n=1,stores:r}=e;const s=!t;t||(t="__indb__"),r&&Array.isArray(r)&&r.length||(r=[{name:"__indb__",isKv:!0}]),this.cache={},this.connection=null,this.name=t,this.version=n,this.stores=r;const o=indexedDB.open(t,n);if(o.onupgradeneeded=e=>{const t=e.target.result,n=Array.from(t.objectStoreNames),s=[];r.forEach((r=>{let o=null;if(n.indexOf(r.name)>-1)o=e.target.transaction.objectStore(r.name);else{const e=r.isKv?"key":r.primaryKeyPath,n=!r.isKv&&r.autoIncrement;o=t.createObjectStore(r.name,{keyPath:e,autoIncrement:n})}const i=o.indexNames;i&&i.length&&Array.from(i).forEach((e=>o.deleteIndex(e))),r.indexes&&r.indexes.length&&r.indexes.forEach((e=>{o.createIndex(e.name,e.keyPath||e.name,{unique:e.unique,multiEntry:Array.isArray(e.keyPath)})})),s.push(r.name)})),n&&n.forEach((e=>{-1===s.indexOf(e)&&t.deleteObjectStore(e)}))},o.onblocked=e=>{console.error(i(new Error("indexedDB "+t+" is blocked")))},s)return this.use(t)}var t=e.prototype;return t.connect=function(){return new Promise(((e,t)=>{const n=indexedDB.open(this.name,this.version);n.onerror=e=>{t(i(e))},n.onsuccess=t=>{e(t.target.result)}}))},t.use=function(e){const t=this.stores.find((t=>t.name===e));if(!t)throw new Error(`[InDB]: store ${e} is not existing.`);if(this.cache[e])return this.cache[e];const n=new s({db:this,store:t});return t.isKv&&Object.defineProperties(n,{key:{value:e=>n.keys().then((t=>t&&t[e]))},getItem:{value:e=>n.get(e).then((e=>e&&e.value))},setItem:{value:(e,t)=>n.put({key:e,value:t})},removeItem:{value:e=>n.delete(e)}}),this.cache[e]=n,n},t.close=function(){return this.cache=null,this.stores=null,this.connect().then((e=>{e.close()}))},e.deleteDatabase=function(e){return new Promise(((t,n)=>{const r=indexedDB.deleteDatabase(e);r.onsuccess=()=>{t()},r.onerror=e=>{n(e)}}))},e.databases=function(){return indexedDB.databases()},e}();let s=function(){function e(e={}){const{store:t,db:n}=e;if("object"!=typeof t||!t.name||"string"!=typeof t.name)throw new Error("[InDBStore]: options.store should be a store config object.");if(!(n instanceof r))throw new Error("[InDBStore]: options.db should be an instanceof InDB.");this.db=n,this.store=t,this.name=t.name,this.primaryKeyPath=t.isKv?"key":t.primaryKeyPath,this._queue=[]}var t=e.prototype;return t.transaction=function(e=!1){const t=()=>{const t=this.name,n=e?"readwrite":"readonly",r=this.db.connection;return(r?Promise.resolve(r):this.db.connect()).then((e=>{this.db.connection=e;const r=e.transaction(t,n),s=()=>{this.db.connection=null,this._queue.shift()};return r.oncomplete=s,r.onabort=s,r.onerror=s,r}))},n=this._queue[this._queue.length-1],r=n?n.then((()=>t())):t();return this._queue.push(r),r},t.objectStore=function(e=!1){const t=this.name;return this.transaction(e).then((e=>e.objectStore(t)))},t.cursor=function(e){const{index:t,range:n,direction:r,onTouch:s,onDone:o,onError:u,writable:a=!1}=e;return this.objectStore(a).then((e=>{const a=t?e.index(t):e,c=a.openCursor(n,r);c.onsuccess=e=>{const t=e.target.result;t?s(t,a):o(t,a)},c.onerror=e=>{u(i(e))}}))},t.request=function(e,t={}){const{writable:n=!1}=t;return new Promise(((t,r)=>{this.objectStore(n).then((n=>{const s=e(n);s.onsuccess=e=>{const n=e.target.result;t(n)},s.onerror=e=>{r(i(e))}}))}))},t.iterate=function(e,t={}){const{index:n,range:r,writable:s=!1,direction:o="next"}=t;return new Promise(((t,i)=>{this.cursor({index:n,range:r,writable:s,direction:o,onTouch:(n,r)=>{e(n,(()=>n.continue()),(()=>{r.transaction.commit(),t()}))},onDone:()=>{t()},onError:e=>{i(e)}})}))},t.batch=function(e,t={}){const{writable:n=!0}=t;return this.transaction(n).then((t=>{const n=this.name,r=[],s=t.objectStore(n);return e.forEach((e=>{const t=new Promise(((t,n)=>{const r=e(s);r.onsuccess=e=>{const n=e.target.result;t(n)},r.onerror=e=>{n(i(e))}}));r.push(t)})),Promise.all(r)}))},t.get=function(e){if(!Array.isArray(e))return this.request((t=>t.get(e)));const t=e.map((e=>t=>t.get(e)));return this.batch(t,{writable:!1})},t.keys=function(){const e=this.primaryKeyPath,t=[];return this.each((n=>{const r=o(n,e);t.push(r)})).then((()=>t))},t.all=function(){const e=[];return this.each((t=>{e.push(t)})).then((()=>e))},t.count=function(){return this.request((e=>e.count()))},t.each=function(e){return this.iterate(((t,n)=>{const r=t.value;e(r),n()}))},t.reverse=function(e){return this.iterate(((t,n)=>{const r=t.value;e(r),n()}),{direction:"prev"})},t.some=function(e=10,t=0){return new Promise(((n,r)=>{const s=[];let o,i=0,u=t,a=t+e;t<0&&(o="prev",e=Math.min(e,-t),u=-(t+e)||0,a=u+e),this.iterate(((e,t,n)=>{i<u?(i++,t()):i<a?(s.push(e.value),i++,t()):n()}),{direction:o}).then((()=>{t<0&&s.reverse(),n(s)})).catch(r)}))},t.first=function(){return this.some(1).then((e=>e[0]))},t.last=function(){return this.some(1,-1).then((e=>e[0]))},t.find=function(e,t){return this.request((n=>n.index(e).get(t)))},t.query=function(e,t,n){const r=function(){switch(n){case">":return IDBKeyRange.lowerBound(t,!0);case">=":return IDBKeyRange.lowerBound(t);case"<":return IDBKeyRange.upperBound(t,!0);case"<=":return IDBKeyRange.upperBound(t);case"%":case"!=":case"in":return;default:return IDBKeyRange.only(t)}}(),s=[];return new Promise(((i,u)=>{this.cursor({index:e,range:r,onTouch:(e,r)=>{const i=e.value,u=o(i,r.keyPath);"!="===n?u!==t&&s.push(i):"%"===n?"string"==typeof u&&u.indexOf(t)>-1&&s.push(i):"in"===n?Array.isArray(t)&&t.indexOf(u)>-1&&s.push(i):s.push(i),e.continue()},onDone:()=>{i(s)},onError:e=>{u(e)}})}))},t.select=function(...e){const t=this.store.indexes||[],n={};t.forEach((e=>{const{name:t,keyPath:r}=e;n[t]=r}));const r=function(e,t,n){if(void 0===e)return!1;switch(n){case">":return e>t;case">=":return e>=t;case"<":return e<t;case"<=":return e<=t;case"!=":return e!==t;case"%":return"string"==typeof e&&e.indexOf(t)>-1;case"in":return Array.isArray(t)&&t.indexOf(e)>-1;default:return e===t}},s=function(e,t,n){if(!t.length&&!n.length)return!1;for(let n=0,s=t.length;n<s;n++){const{keyPath:s,value:i,compare:u}=t[n],a=o(e,s);if(!r(a,i,u))return!1}if(!n.length)return!0;for(let t=0,s=n.length;t<s;t++){const{keyPath:s,value:i,compare:u}=n[t],a=o(e,s);if(r(a,i,u))return!0}return!1},i=[];e.forEach((e=>{const t=[],r=[];for(let s=0,o=e.length;s<o;s++){const{key:o,value:i,compare:u,optional:a}=e[s],c=n[o]||o;a?t.push({keyPath:c,value:i,compare:u}):r.push({keyPath:c,value:i,compare:u})}i.push([r,t])}));const u=[];return this.each((e=>{(e=>{for(let t=0,n=i.length;t<n;t++){const[n,r]=i[t];if(s(e,n,r))return!0}return!1})(e)&&u.push(e)})).then((()=>u))},t.add=function(e,t){if(Array.isArray(e)){const n=e;if(n.length<2)return this.add(e[0],t);const r=n.map((e=>n=>n.add(e,t)));return this.batch(r)}return e?this.request((n=>n.add(e,t)),{writable:!0}):Promise.resolve()},t.put=function(e,t){if(Array.isArray(e)){const n=e;if(n.length<2)return this.put(n[0],t);const r=n.map((e=>n=>n.put(e,t)));return this.batch(r)}return e?this.request((n=>n.put(e,t)),{writable:!0}):Promise.resolve()},t.delete=function(e){if(Array.isArray(e)){const t=e;if(t.length<2)return this.delete(t[0]);const n=t.map((e=>t=>t.delete(e)));return this.batch(n)}return e?this.request((t=>t.delete(e)),{writable:!0}):Promise.resolve()},t.remove=function(e){const t=this.primaryKeyPath;if(Array.isArray(e)){const n=e;if(n.length<2)return this.remove(n[0]);const r=n.map((e=>{const n=o(e,t);return e=>e.delete(n)}));return this.batch(r)}if(!e)return Promise.resolve();const n=o(e,t);return n?this.delete(n):Promise.resolve()},t.clear=function(){return this.request((e=>e.clear()),{writable:!0})},e}();function o(e,t){if(null==t)return;if(Array.isArray(t)){for(let n=0,r=t.length;n<r;n++){const r=o(e,t[n]);if(void 0!==r)return r}return}let n=function(e){return e.toString().split(/\.|\[|\]/).filter((e=>!!e))}(t);if(!n.length)return e;let r=e;for(let e=0,t=n.length;e<t;e++){let t=n[e];if(void 0===r[t])return;r=r[t]}return r}function i(e){const{message:t}=e;return e.message=-1===t.indexOf("[IndexedDB]")?"[IndexedDB]: "+t:t,e}let u=function(){function e(e){this.anys=e}var t=e.prototype;return t.options=function(){return{namespace:new Error("[Anys]: options.namespace is required!"),autoReport:!1,expireTime:1728e5}},t.init=function(){const e="anyslogs",t=new r({name:this.anys.options.namespace,version:1,stores:[{name:e,primaryKeyPath:"_id",autoIncrement:!0}]});this.db=t.use(e),this.queue=new n;const{expireTime:s}=this.anys.options;if(s){let e=0;const t=setInterval((()=>{this.queue.push((()=>this.db.some(100,e))).then((n=>{if(!n.length)return void clearInterval(t);const r=Date.now(),o=[];n.forEach((e=>{const{_id:t,time:n}=e;r-n>s&&o.push(t)})),o.length&&this.queue.push((()=>this.db.delete(o))),e+=100,e-=o.length}))}),1e4)}},t.write=function(e){this.queue.push((()=>this.db.add(e))).then((t=>{this.anys.emit("writeOfflineLog",{id:t,data:e})}))},t.read=function(e){if(e){if(!Array.isArray(e))return;if(e.some((e=>!e.key)))return}else e=[{key:"client",value:this.anys.clientId},{key:"trace",value:this.anys.traceId}];return this.select(e)},t.select=function(e){return this.queue.push((()=>this.db.select(e)))},t.remove=function(e){return this.queue.push((()=>this.db.delete(e))).catch((()=>{}))},e}()})();var n=anys="undefined"==typeof anys?{}:anys;for(var r in t)n[r]=t[r];t.__esModule&&Object.defineProperty(n,"__esModule",{value:!0})})();
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","mappings":"MACA,IAAIA,EAAsB,CCA1BA,EAAwB,CAACC,EAASC,KACjC,IAAI,IAAIC,KAAOD,EACXF,EAAoBI,EAAEF,EAAYC,KAASH,EAAoBI,EAAEH,EAASE,IAC5EE,OAAOC,eAAeL,EAASE,EAAK,CAAEI,YAAY,EAAMC,IAAKN,EAAWC,IAE1E,ECNDH,EAAwB,CAACS,EAAKC,IAAUL,OAAOM,UAAUC,eAAeC,KAAKJ,EAAKC,GCClFV,EAAyBC,IACH,oBAAXa,QAA0BA,OAAOC,aAC1CV,OAAOC,eAAeL,EAASa,OAAOC,YAAa,CAAEC,MAAO,WAE7DX,OAAOC,eAAeL,EAAS,aAAc,CAAEe,OAAO,GAAO,G,qECLvD,IAAMC,EAAU,WACnB,SAAAA,IACIC,KAAKC,MAAQ,GACbD,KAAKE,OAAS,CAClB,CAkCC,OAlCAH,EAAAN,UAEDU,KAAA,SAAKC,GACD,IAAIC,EACAC,EACJ,MAAMC,EAAQ,IAAIC,SAAQ,CAACC,EAAIC,KAC3BL,EAAUI,EACVH,EAASI,CAAE,IAETC,EAAO,CACTP,SACAC,UACAC,UAEJN,KAAKC,MAAME,KAAKQ,GAEhB,MAAMC,EAAMA,KACRC,YAAW,KACP,IAAKb,KAAKC,MAAMa,OAEZ,YADAd,KAAKE,OAAS,GAIlB,MAAQE,OAAQW,EAAIV,QAASI,EAAIH,OAAQI,GAAOV,KAAKC,MAAMe,QAC3DD,IAAKE,KAAKR,EAAIC,GAAIQ,QAAQN,EAAI,GAC/B,GACHZ,KAAKE,OAAS,CAAC,E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ys/webpack/runtime/define property getters","webpack://anys/webpack/runtime/hasOwnProperty shorthand","webpack://anys/webpack/runtime/make namespace object","webpack://anys/../@shared/src/async-queue.js","webpack://anys/./node_modules/indb/es/index.js","webpack://anys/./src/index.js"],"sourcesContent":["// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","export class AsyncQueue {\n constructor() {\n this.queue = [];\n this.status = 0;\n }\n\n push(runner) {\n let resolve; let\n reject;\n const defer = new Promise((rs, rj) => {\n resolve = rs;\n reject = rj;\n });\n const item = {\n runner,\n resolve,\n reject,\n };\n this.queue.push(item);\n\n const run = () => {\n setTimeout(() => {\n if (!this.queue.length) {\n this.status = 0;\n return;\n }\n\n const { runner: fn, resolve: rs, reject: rj } = this.queue.shift();\n fn().then(rs, rj).finally(run);\n }, 0);\n this.status = 1;\n };\n\n if (!this.status) {\n run();\n }\n\n return defer;\n }\n}\n","export class InDB {\n\t/**\n\t * InDB\n\t * @param {object} [options]\n\t * @param {string} [options.name] database name\n\t * @param {number} [options.version] database version\n\t * @param {object[]} [options.stores]\n\t * @param {string} [options.stores[].name] store name\n\t * @param {string} [options.stores[].primaryKeyPath] store primary keyPath\n\t * @param {boolean} [options.stores[].autoIncrement] is store primary keyPath autoIncrement, if true, primary key will be number\n\t * @param {boolean} [options.stores[].isKv] is store set to be Key-Value mode, if true, the store will have Storage methods, and `primaryKeyPath` `autoIncrement` not working\n\t * @param {object[]} [options.stores[].indexes]\n\t * @param {string} [options.stores[].indexes[].name] index name\n\t * @param {string} [options.stores[].indexes[].keyPath] index keyPath\n\t * @param {boolean} [options.stores[].indexes[].unique] should index value be unique\n\t * @returns\n\t */\n\tconstructor(options = {}) {\n\t\tlet { name, version = 1, stores } = options\n\t\tconst asStorage = !name\n\n\t\tif (!name) {\n\t\t\tname = '__indb__'\n\t\t}\n\n\t\tif (!stores || !Array.isArray(stores) || !stores.length) {\n\t\t\tstores = [\n\t\t\t\t{\n\t\t\t\t\tname: '__indb__',\n\t\t\t\t\tisKv: true,\n\t\t\t\t},\n\t\t\t]\n\t\t}\n\n\t\tthis.cache = {}\n\t\tthis.connection = null\n\n\t\tthis.name = name\n\t\tthis.version = version\n\t\tthis.stores = stores\n\n\t\t// update database structure\n\t\tconst request = indexedDB.open(name, version)\n\t\trequest.onupgradeneeded = (e) => {\n\t\t\t// @ts-ignore\n\t\t\tconst db = e.target.result\n\t\t\tconst existStoreNames = Array.from(db.objectStoreNames)\n\t\t\tconst passStoreNames = []\n\n\t\t\tstores.forEach((item) => {\n\t\t\t\tlet objectStore = null\n\t\t\t\tif (existStoreNames.indexOf(item.name) > -1) {\n\t\t\t\t\t// @ts-ignore\n\t\t\t\t\tobjectStore = e.target.transaction.objectStore(item.name)\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tconst keyPath = item.isKv ? 'key' : item.primaryKeyPath\n\t\t\t\t\tconst autoIncrement = item.isKv ? false : item.autoIncrement\n\t\t\t\t\tobjectStore = db.createObjectStore(item.name, { keyPath, autoIncrement })\n\t\t\t\t}\n\n\t\t\t\t// delete old indexes\n\t\t\t\tconst indexNames = objectStore.indexNames\n\t\t\t\tif (indexNames && indexNames.length) {\n\t\t\t\t\tArray.from(indexNames).forEach((item) => objectStore.deleteIndex(item))\n\t\t\t\t}\n\n\t\t\t\t// add new indexes\n\t\t\t\tif (item.indexes && item.indexes.length) {\n\t\t\t\t\titem.indexes.forEach((item) => {\n\t\t\t\t\t\tobjectStore.createIndex(item.name, item.keyPath || item.name, { unique: item.unique, multiEntry: Array.isArray(item.keyPath) })\n\t\t\t\t\t})\n\t\t\t\t}\n\n\t\t\t\tpassStoreNames.push(item.name)\n\t\t\t})\n\n\t\t\t// delete objectStores which is not in config information\n\t\t\t// notice, developers should backup data firstly\n\t\t\tif (existStoreNames) {\n\t\t\t\texistStoreNames.forEach((item) => {\n\t\t\t\t\tif (passStoreNames.indexOf(item) === -1) {\n\t\t\t\t\t\tdb.deleteObjectStore(item)\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t\trequest.onblocked = (e) => {\n\t\t\tconsole.error(modifyError(new Error('indexedDB ' + name + ' is blocked')))\n\t\t}\n\n\t\t// use as a storage like:\n\t\t// const store = new InDB()\n\t\t// store.setItem('key', 'value')\n\t\tif (asStorage) {\n\t\t\t// @ts-ignore\n\t\t\treturn this.use(name)\n\t\t}\n\t}\n\n\tconnect() {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst request = indexedDB.open(this.name, this.version)\n\t\t\trequest.onerror = (e) => {\n\t\t\t\treject(modifyError(e))\n\t\t\t}\n\t\t\trequest.onsuccess = (e) => {\n\t\t\t\t// @ts-ignore\n\t\t\t\tresolve(e.target.result)\n\t\t\t}\n\t\t})\n\t}\n\n\t/**\n\t *\n\t * @param {string} name\n\t * @returns {InDBStore}\n\t */\n\tuse(name) {\n\t\tconst currentStore = this.stores.find(item => item.name === name)\n\n\t\tif (!currentStore) {\n\t\t\tthrow new Error(`[InDB]: store ${name} is not existing.`)\n\t\t}\n\n\t\t// use connected store\n\t\tif (this.cache[name]) {\n\t\t\treturn this.cache[name]\n\t\t}\n\n\t\tconst store = new InDBStore({\n\t\t\tdb: this,\n\t\t\tstore: currentStore,\n\t\t})\n\n\t\t// if it is a key-value store, append special methods\n\t\tif (currentStore.isKv) {\n\t\t\tObject.defineProperties(store, {\n\t\t\t\tkey: {\n\t\t\t\t\tvalue: i => store.keys().then(keys => keys && keys[i]),\n\t\t\t\t},\n\t\t\t\tgetItem: {\n\t\t\t\t\tvalue: key => store.get(key).then(obj => obj && obj.value),\n\t\t\t\t},\n\t\t\t\tsetItem: {\n\t\t\t\t\tvalue: (key, value) => store.put({ key, value }),\n\t\t\t\t},\n\t\t\t\tremoveItem: {\n\t\t\t\t\tvalue: key => store.delete(key),\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\n\t\tthis.cache[name] = store\n\n\t\treturn store\n\t}\n\n\tclose() {\n\t\tthis.cache = null\n\t\tthis.stores = null\n\n\t\treturn this.connect().then((db) => {\n\t\t\tdb.close()\n\t\t})\n\t}\n\n\tstatic deleteDatabase(name) {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst request = indexedDB.deleteDatabase(name)\n\t\t\trequest.onsuccess = () => {\n\t\t\t\tresolve()\n\t\t\t}\n\t\t\trequest.onerror = (e) => {\n\t\t\t\treject(e)\n\t\t\t}\n\t\t})\n\t}\n\n\tstatic databases() {\n\t\treturn indexedDB.databases()\n\t}\n}\n\nexport default InDB\n\nclass InDBStore {\n\tconstructor(options = {}) {\n\t\tconst { store, db } = options\n\n\t\tif (typeof store !== 'object' || !store.name || typeof store.name !== 'string') {\n\t\t\tthrow new Error(`[InDBStore]: options.store should be a store config object.`)\n\t\t}\n\n\t\tif (!(db instanceof InDB)) {\n\t\t\tthrow new Error(`[InDBStore]: options.db should be an instanceof InDB.`)\n\t\t}\n\n\t\tthis.db = db\n\n\t\tthis.store = store\n\t\tthis.name = store.name\n\t\tthis.primaryKeyPath = store.isKv ? 'key' : store.primaryKeyPath\n\n\t\tthis._queue = []\n\t}\n\n\ttransaction(writable = false) {\n\t\tconst create = () => {\n\t\t\tconst name = this.name\n\t\t\tconst mode = writable ? 'readwrite' : 'readonly'\n\n\t\t\t// share the same connection\n\t\t\tconst connection = this.db.connection\n\t\t\tconst deferer = connection ? Promise.resolve(connection) : this.db.connect()\n\t\t\treturn deferer.then((db) => {\n\t\t\t\tthis.db.connection = db\n\t\t\t\tconst tx = db.transaction(name, mode)\n\t\t\t\tconst disconnect = () => {\n\t\t\t\t\tthis.db.connection = null\n\t\t\t\t\tthis._queue.shift()\n\t\t\t\t}\n\t\t\t\ttx.oncomplete = disconnect\n\t\t\t\ttx.onabort = disconnect\n\t\t\t\ttx.onerror = disconnect\n\t\t\t\treturn tx\n\t\t\t})\n\t\t}\n\n\t\tconst latest = this._queue[this._queue.length - 1]\n\t\tconst deferer = latest ? latest.then(() => create()) : create()\n\t\tthis._queue.push(deferer)\n\t\treturn deferer\n\t}\n\n\tobjectStore(writable = false) {\n\t\tconst name = this.name\n\t\treturn this.transaction(writable).then(tx => tx.objectStore(name))\n\t}\n\n\tcursor(options) {\n\t\tconst { index, range, direction, onTouch, onDone, onError, writable = false } = options\n\t\treturn this.objectStore(writable).then((objectStore) => {\n\t\t\tconst owner = index ? objectStore.index(index) : objectStore\n\t\t\tconst request = owner.openCursor(range, direction)\n\t\t\trequest.onsuccess = (e) => {\n\t\t\t\tconst cursor = e.target.result\n\t\t\t\tif (cursor) {\n\t\t\t\t\tonTouch(cursor, owner)\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tonDone(cursor, owner)\n\t\t\t\t}\n\t\t\t}\n\t\t\trequest.onerror = (e) => {\n\t\t\t\tonError(modifyError(e))\n\t\t\t}\n\t\t})\n\t}\n\n\trequest(fn, options = {}) {\n\t\tconst { writable = false } = options\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tthis.objectStore(writable).then((objectStore) => {\n\t\t\t\tconst request = fn(objectStore)\n\t\t\t\trequest.onsuccess = (e) => {\n\t\t\t\t\tconst result = e.target.result\n\t\t\t\t\tresolve(result)\n\t\t\t\t}\n\t\t\t\trequest.onerror = (e) => {\n\t\t\t\t\treject(modifyError(e))\n\t\t\t\t}\n\t\t\t})\n\t\t})\n\t}\n\n\titerate(fn, options = {}) {\n\t\tconst { index, range, writable = false, direction = 'next' } = options\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tthis.cursor({\n\t\t\t\tindex,\n\t\t\t\trange,\n\t\t\t\twritable,\n\t\t\t\tdirection,\n\t\t\t\tonTouch: (cursor, owner) => {\n\t\t\t\t\tconst next = () => cursor.continue()\n\t\t\t\t\tconst stop = () => {\n\t\t\t\t\t\t// should commit when writable is true\n\t\t\t\t\t\towner.transaction.commit()\n\t\t\t\t\t\tresolve()\n\t\t\t\t\t}\n\t\t\t\t\tfn(cursor, next, stop)\n\t\t\t\t},\n\t\t\t\tonDone: () => {\n\t\t\t\t\tresolve()\n\t\t\t\t},\n\t\t\t\tonError: (e) => {\n\t\t\t\t\treject(e)\n\t\t\t\t},\n\t\t\t})\n\t\t})\n\t}\n\n\tbatch(fns, options = {}) {\n\t\tconst { writable = true } = options\n\t\treturn this.transaction(writable).then((tx) => {\n\t\t\tconst name = this.name\n\t\t\tconst promises = []\n\t\t\tconst objectStore = tx.objectStore(name)\n\t\t\tfns.forEach((fn) => {\n\t\t\t\tconst deferer = new Promise((resolve, reject) => {\n\t\t\t\t\tconst request = fn(objectStore)\n\t\t\t\t\trequest.onsuccess = (e) => {\n\t\t\t\t\t\tconst result = e.target.result\n\t\t\t\t\t\tresolve(result)\n\t\t\t\t\t}\n\t\t\t\t\trequest.onerror = (e) => {\n\t\t\t\t\t\treject(modifyError(e))\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\tpromises.push(deferer)\n\t\t\t})\n\t\t\treturn Promise.all(promises)\n\t\t})\n\t}\n\n\t// ==========================================\n\n\t/**\n\t *\n\t * @param {string} key\n\t * @returns\n\t */\n\tget(key) {\n\t\t// single key\n\t\tif (!Array.isArray(key)) {\n\t\t\treturn this.request(objectStore => objectStore.get(key))\n\t\t}\n\n\t\t// multiple keys\n\t\tconst keys = key\n\t\tconst fns = keys.map(key => objectStore => objectStore.get(key))\n\t\treturn this.batch(fns, { writable: false })\n\t}\n\n\tkeys() {\n\t\tconst keyPath = this.primaryKeyPath\n\t\tconst results = []\n\t\treturn this.each((obj) => {\n\t\t\tconst key = parse(obj, keyPath)\n\t\t\tresults.push(key)\n\t\t}).then(() => {\n\t\t\treturn results\n\t\t})\n\t}\n\n\tall() {\n\t\tconst results = []\n\t\treturn this.each((obj) => {\n\t\t\tresults.push(obj)\n\t\t}).then(() => {\n\t\t\treturn results\n\t\t})\n\t}\n\n\tcount() {\n\t\treturn this.request(objectStore => objectStore.count())\n\t}\n\n\t// ==========================================\n\n\teach(fn) {\n\t\treturn this.iterate((cursor, next) => {\n\t\t\tconst obj = cursor.value\n\t\t\tfn(obj)\n\t\t\tnext()\n\t\t})\n\t}\n\n\treverse(fn) {\n\t\treturn this.iterate((cursor, next) => {\n\t\t\tconst obj = cursor.value\n\t\t\tfn(obj)\n\t\t\tnext()\n\t\t}, { direction: 'prev' })\n\t}\n\n\tsome(count = 10, offset = 0) {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst results = []\n\t\t\tlet i = 0\n\t\t\tlet start = offset\n\t\t\tlet end = offset + count\n\t\t\tlet direction\n\n\t\t\t// offset < 0, means begining from the latest item,\n\t\t\t// for example, offset = -1, means begining from the last item\n\t\t\tif (offset < 0) {\n\t\t\t\tdirection = 'prev'\n\t\t\t\tcount = Math.min(count, -offset)\n\t\t\t\tstart = -(offset + count) || 0\n\t\t\t\tend = start + count\n\t\t\t}\n\n\t\t\tthis.iterate((cursor, next, stop) => {\n\t\t\t\tif (i < start) {\n\t\t\t\t\ti ++\n\t\t\t\t\tnext()\n\t\t\t\t}\n\t\t\t\telse if (i < end) {\n\t\t\t\t\tresults.push(cursor.value)\n\t\t\t\t\ti ++\n\t\t\t\t\tnext()\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tstop()\n\t\t\t\t}\n\t\t\t}, { direction }).then(() => {\n\t\t\t\tif (offset < 0) {\n\t\t\t\t\tresults.reverse()\n\t\t\t\t}\n\t\t\t\tresolve(results)\n\t\t\t}).catch(reject)\n\t\t})\n\t}\n\n\tfirst() {\n\t\treturn this.some(1).then(items => items[0])\n\t}\n\n\tlast() {\n\t\treturn this.some(1, -1).then(items => items[0])\n\t}\n\n\t// =========================\n\n\t/**\n\t * find a record with given index\n\t * @param {string} key index name\n\t * @param {*} value\n\t * @returns\n\t */\n\tfind(key, value) {\n\t\treturn this.request(objectStore => objectStore.index(key).get(value))\n\t}\n\t/**\n\t * query several records with given index and compare condition\n\t * @param {string} key index name\n\t * @param {*} value\n\t * @param {string} [compare]\n\t * @returns\n\t */\n\tquery(key, value, compare) {\n\t\tconst range = (function() {\n\t\t\tswitch (compare) {\n\t\t\t\tcase '>':\n\t\t\t\t\treturn IDBKeyRange.lowerBound(value, true)\n\t\t\t\tcase '>=':\n\t\t\t\t\treturn IDBKeyRange.lowerBound(value)\n\t\t\t\tcase '<':\n\t\t\t\t\treturn IDBKeyRange.upperBound(value, true)\n\t\t\t\tcase '<=':\n\t\t\t\t\treturn IDBKeyRange.upperBound(value)\n\t\t\t\tcase '%':\n\t\t\t\tcase '!=':\n\t\t\t\tcase 'in':\n\t\t\t\t\treturn undefined\n\t\t\t\tdefault:\n\t\t\t\t\treturn IDBKeyRange.only(value)\n\t\t\t}\n\t\t}())\n\n\t\tconst results = []\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tthis.cursor({\n\t\t\t\tindex: key,\n\t\t\t\trange,\n\t\t\t\tonTouch: (cursor, owner) => {\n\t\t\t\t\tconst targetObj = cursor.value\n\t\t\t\t\tconst keyPath = owner.keyPath\n\t\t\t\t\tconst targetValue = parse(targetObj, keyPath)\n\n\t\t\t\t\tif (compare === '!=') {\n\t\t\t\t\t\tif (targetValue !== value) {\n\t\t\t\t\t\t\tresults.push(targetObj)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse if (compare === '%') {\n\t\t\t\t\t\tif (typeof targetValue == 'string' && targetValue.indexOf(value) > -1) {\n\t\t\t\t\t\t\tresults.push(targetObj)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse if (compare === 'in') {\n\t\t\t\t\t\tif (Array.isArray(value) && value.indexOf(targetValue) > -1) {\n\t\t\t\t\t\t\tresults.push(targetObj)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tresults.push(targetObj)\n\t\t\t\t\t}\n\n\t\t\t\t\tcursor.continue()\n\t\t\t\t},\n\t\t\t\tonDone: () => {\n\t\t\t\t\tresolve(results)\n\t\t\t\t},\n\t\t\t\tonError: (e) => {\n\t\t\t\t\treject(e)\n\t\t\t\t},\n\t\t\t})\n\t\t})\n\t}\n\n\t/**\n\t * query several records with conditions,\n\t * notice that, it works with iterator not with index.\n\t * @param {...object[]} rules\n\t * @param {string} rules[][].key index name or any keyPath.\n\t * @param {*} rules[][].value\n\t * @param {string=} rules[][].compare\n\t * @param {boolean=} rules[][].optional whether this condition is optional\n\t * @returns\n\t */\n\tselect(...rules) {\n\t\tconst currentStore = this.store\n\t\tconst indexes = currentStore.indexes || []\n\t\tconst indexesMapping = {}\n\t\tindexes.forEach((item) => {\n\t\t\tconst { name, keyPath } = item\n\t\t\tindexesMapping[name] = keyPath\n\t\t})\n\n\t\tconst compareAandB = function(a, b, compare) {\n\t\t\tif (a === undefined) {\n\t\t\t\treturn false\n\t\t\t}\n\t\t\tswitch (compare) {\n\t\t\t\tcase '>':\n\t\t\t\t\treturn a > b\n\t\t\t\tcase '>=':\n\t\t\t\t\treturn a >= b\n\t\t\t\tcase '<':\n\t\t\t\t\treturn a < b\n\t\t\t\tcase '<=':\n\t\t\t\t\treturn a <= b\n\t\t\t\tcase '!=':\n\t\t\t\t\treturn a !== b\n\t\t\t\tcase '%':\n\t\t\t\t\treturn typeof a === 'string' && a.indexOf(b) > -1\n\t\t\t\tcase 'in':\n\t\t\t\t\treturn Array.isArray(b) && b.indexOf(a) > -1\n\t\t\t\tdefault:\n\t\t\t\t\treturn a === b\n\t\t\t}\n\t\t}\n\n\t\tconst determine = function(obj, and_conditions, or_conditions) {\n\t\t\tif (!and_conditions.length && !or_conditions.length) {\n\t\t\t\treturn false\n\t\t\t}\n\n\t\t\tfor (let i = 0, len = and_conditions.length; i < len; i ++) {\n\t\t\t\tconst { keyPath, value, compare } = and_conditions[i]\n\t\t\t\tconst current = parse(obj, keyPath)\n\t\t\t\tif (!compareAandB(current, value, compare)) {\n\t\t\t\t\treturn false\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!or_conditions.length) {\n\t\t\t\treturn true\n\t\t\t}\n\n\t\t\tfor (let i = 0, len = or_conditions.length; i < len; i ++) {\n\t\t\t\tconst { keyPath, value, compare } = or_conditions[i]\n\t\t\t\tconst current = parse(obj, keyPath)\n\t\t\t\tif (compareAandB(current, value, compare)) {\n\t\t\t\t\treturn true\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn false\n\t\t}\n\n\t\tconst groups = []\n\t\trules.forEach((conditions) => {\n\t\t\tconst or_conditions = []\n\t\t\tconst and_conditions = []\n\t\t\tfor (let i = 0, len = conditions.length; i < len; i ++) {\n\t\t\t\tconst { key, value, compare, optional } = conditions[i]\n\t\t\t\tconst keyPath = indexesMapping[key] || key // if there is not such index, use original key as keyPath\n\t\t\t\tif (optional) {\n\t\t\t\t\tor_conditions.push({ keyPath, value, compare })\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tand_conditions.push({ keyPath, value, compare })\n\t\t\t\t}\n\t\t\t}\n\t\t\tgroups.push([and_conditions, or_conditions])\n\t\t})\n\n\t\tconst isOk = (obj) => {\n\t\t\tfor (let i = 0, len = groups.length; i < len; i ++) {\n\t\t\t\tconst [and_conditions, or_conditions] = groups[i]\n\t\t\t\tconst res = determine(obj, and_conditions, or_conditions)\n\t\t\t\tif (res) {\n\t\t\t\t\treturn true\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false\n\t\t}\n\n\t\tconst results = []\n\t\treturn this.each((obj) => {\n\t\t\tif (isOk(obj)) {\n\t\t\t\tresults.push(obj)\n\t\t\t}\n\t\t}).then(() => {\n\t\t\treturn results\n\t\t})\n\t}\n\n\t// =====================================\n\n\t/**\n\t * add a record into store\n\t * @param {*} obj\n\t * @param {string} [key]\n\t * @returns\n\t */\n\tadd(obj, key) {\n\t\tif (Array.isArray(obj)) {\n\t\t\tconst objs = obj\n\t\t\tif (objs.length < 2) {\n\t\t\t\treturn this.add(obj[0], key)\n\t\t\t}\n\n\t\t\tconst fns = objs.map(obj => objectStore => objectStore.add(obj, key))\n\t\t\treturn this.batch(fns)\n\t\t}\n\n\t\tif (!obj) {\n\t\t\treturn Promise.resolve()\n\t\t}\n\n\t\treturn this.request(objectStore => objectStore.add(obj, key), { writable: true })\n\t}\n\n\t/**\n\t *\n\t * @param {*} obj\n\t * @param {string} [key] if not pass, primaryKey will read from obj\n\t * @returns\n\t */\n\tput(obj, key) {\n\t\tif (Array.isArray(obj)) {\n\t\t\tconst objs = obj\n\t\t\tif (objs.length < 2) {\n\t\t\t\treturn this.put(objs[0], key)\n\t\t\t}\n\n\t\t\tconst fns = objs.map(obj => objectStore => objectStore.put(obj, key))\n\t\t\treturn this.batch(fns)\n\t\t}\n\n\t\tif (!obj) {\n\t\t\treturn Promise.resolve()\n\t\t}\n\n\t\treturn this.request(objectStore => objectStore.put(obj, key), { writable: true })\n\t}\n\n\tdelete(key) {\n\t\tif (Array.isArray(key)) {\n\t\t\tconst keys = key\n\t\t\tif (keys.length < 2) {\n\t\t\t\treturn this.delete(keys[0])\n\t\t\t}\n\n\t\t\tconst fns = keys.map(key => objectStore => objectStore.delete(key))\n\t\t\treturn this.batch(fns)\n\t\t}\n\n\t\tif (!key) {\n\t\t\treturn Promise.resolve()\n\t\t}\n\n\t\treturn this.request(objectStore => objectStore.delete(key), { writable: true })\n\t}\n\n\tremove(obj) {\n\t\tconst keyPath = this.primaryKeyPath\n\n\t\tif (Array.isArray(obj)) {\n\t\t\tconst objs = obj\n\t\t\tif (objs.length < 2) {\n\t\t\t\treturn this.remove(objs[0])\n\t\t\t}\n\n\t\t\tconst fns = objs.map(obj => {\n\t\t\t\tconst key = parse(obj, keyPath)\n\t\t\t\treturn objectStore => objectStore.delete(key)\n\t\t\t})\n\t\t\treturn this.batch(fns)\n\t\t}\n\n\t\tif (!obj) {\n\t\t\treturn Promise.resolve()\n\t\t}\n\n\t\tconst key = parse(obj, keyPath)\n\t\tif (!key) {\n\t\t\treturn Promise.resolve()\n\t\t}\n\n\t\treturn this.delete(key)\n\t}\n\n\tclear() {\n\t\treturn this.request(objectStore => objectStore.clear(), { writable: true })\n\t}\n}\n\nfunction makeKeyChain(path) {\n\tlet chain = path.toString().split(/\\.|\\[|\\]/).filter(item => !!item)\n\treturn chain\n}\n\nfunction parse(obj, path) {\n\tif (typeof path === 'undefined' || path === null) {\n\t return;\n\t}\n\n\tif (Array.isArray(path)) {\n\t for (let i = 0, len = path.length; i < len; i ++) {\n\t\tconst item = path[i]\n\t\tconst res = parse(obj, item)\n\t\tif (res !== undefined) {\n\t\t return res\n\t\t}\n\t }\n\t return\n\t}\n\n\tlet chain = makeKeyChain(path)\n\n\tif (!chain.length) {\n\t return obj\n\t}\n\n\tlet target = obj\n\tfor (let i = 0, len = chain.length; i < len; i ++) {\n\t let key = chain[i]\n\t if (target[key] === undefined) {\n\t\treturn\n\t }\n\t target = target[key]\n\t}\n\treturn target\n}\n\nfunction modifyError(e) {\n\tconst { message } = e\n\te.message = message.indexOf('[IndexedDB]') === -1 ? '[IndexedDB]: ' + message : message\n\treturn e\n}\n","import { AsyncQueue } from 'anys-shared';\nimport { InDB } from 'indb/es/index.js';\n\nexport class AnysStoreOfflinePlugin {\n constructor(anys) {\n this.anys = anys;\n }\n\n options() {\n return {\n namespace: new Error('[Anys]: options.namespace is required!'),\n /**\n * offline log will close autoReport as default,\n * developers should must invoke anys.send to send logs to server side during\n * which should use this.[offlineLogger].select to pick logs,\n * and this.[offlineLogger].remove to clear logs\n */\n autoReport: false,\n /**\n * a offline log will be delete after how long time,\n * default 2 day (48 hours)\n */\n expireTime: 3600000 * 48,\n };\n }\n\n init() {\n const storeName = 'anyslogs';\n const idb = new InDB({\n name: this.anys.options.namespace,\n version: 1,\n stores: [\n {\n name: storeName,\n primaryKeyPath: '_id',\n autoIncrement: true,\n },\n ],\n });\n this.db = idb.use(storeName);\n this.queue = new AsyncQueue();\n\n /**\n * clear logs which should be delete from db\n */\n const { expireTime } = this.anys.options;\n if (expireTime) {\n let offset = 0;\n const timer = setInterval(() => {\n this.queue\n .push(() => this.db.some(100, offset))\n .then((logs) => {\n if (!logs.length) {\n clearInterval(timer);\n return;\n }\n const curr = Date.now();\n const ids = [];\n logs.forEach((log) => {\n const { _id, time } = log;\n if (curr - time > expireTime) {\n ids.push(_id);\n }\n });\n if (ids.length) {\n this.queue.push(() => this.db.delete(ids));\n }\n offset += 100;\n offset -= ids.length;\n });\n }, 10000);\n }\n }\n\n write(data) {\n this.queue\n .push(() => this.db.add(data))\n .then((id) => {\n this.anys.emit('writeOfflineLog', { id, data });\n });\n }\n\n /**\n * select records from db\n * anys.report([\n * { key: 'time', value: Date.now() - 3600000, compare: '>' },\n * { key: 'trace', value: 'xxx' },\n * ]);\n * https://github.com/tangshuang/indb#select\n * @param {Array} message\n * @returns\n */\n read(message) {\n if (!message) {\n message = [\n { key: 'client', value: this.anys.clientId },\n { key: 'trace', value: this.anys.traceId },\n ];\n }\n else if (!Array.isArray(message)) {\n return;\n }\n else if (message.some(item => !item.key)) {\n return;\n }\n return this.select(message);\n }\n\n /**\n * select records from db\n * https://github.com/tangshuang/indb#select\n * @param {Array} conditions\n * @returns\n */\n select(conditions) {\n return this.queue.push(() => this.db.select(conditions));\n }\n\n /**\n * delete records from db\n * @param {number|Array<number>} ids\n * @returns\n */\n remove(ids) {\n return this.queue.push(() => this.db.delete(ids)).catch(() => {});\n }\n}\n"],"names":["__webpack_require__","exports","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","AsyncQueue","this","queue","status","push","runner","resolve","reject","defer","Promise","rs","rj","item","run","setTimeout","length","fn","shift","then","finally","InDB","options","name","version","stores","asStorage","Array","isArray","isKv","cache","connection","request","indexedDB","open","onupgradeneeded","e","db","target","result","existStoreNames","from","objectStoreNames","passStoreNames","forEach","objectStore","indexOf","transaction","keyPath","primaryKeyPath","autoIncrement","createObjectStore","indexNames","deleteIndex","indexes","createIndex","unique","multiEntry","deleteObjectStore","onblocked","console","error","modifyError","Error","use","_proto","connect","onerror","onsuccess","currentStore","find","store","InDBStore","defineProperties","i","keys","getItem","setItem","put","removeItem","delete","close","deleteDatabase","databases","_queue","_proto2","writable","create","mode","tx","disconnect","oncomplete","onabort","latest","deferer","cursor","index","range","direction","onTouch","onDone","onError","owner","openCursor","iterate","next","continue","stop","commit","batch","fns","promises","all","map","results","each","parse","count","reverse","some","offset","start","end","Math","min","catch","first","items","last","query","compare","IDBKeyRange","lowerBound","upperBound","only","targetObj","targetValue","select","rules","indexesMapping","compareAandB","a","b","undefined","determine","and_conditions","or_conditions","len","current","groups","conditions","optional","isOk","add","objs","remove","clear","path","res","chain","toString","split","filter","makeKeyChain","message","AnysStoreOfflinePlugin","anys","namespace","autoReport","expireTime","init","storeName","idb","timer","setInterval","logs","clearInterval","curr","Date","now","ids","log","_id","time","write","data","id","emit","read","clientId","traceId"],"sourceRoot":""}
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "anys-web-plugin-store-offline",
3
+ "version": "1.0.1",
4
+ "description": "",
5
+ "main": "cjs/index.js",
6
+ "module": "src/index.js",
7
+ "type": "module",
8
+ "exports": {
9
+ "import": "./src/index.js",
10
+ "require": "./cjs/index.js"
11
+ },
12
+ "scripts": {
13
+ "build:cjs": "rimraf cjs && babel src --out-dir cjs --root-mode upward",
14
+ "build:dist": "rimraf dist && webpack --config ../../webpack.config.js",
15
+ "build": "npm run build:cjs && npm run build:dist"
16
+ },
17
+ "keywords": [],
18
+ "author": "tangshuang",
19
+ "license": "Apache-2.0",
20
+ "dependencies": {
21
+ "anys-shared": "^1.0.1",
22
+ "indb": "^4.0.1"
23
+ },
24
+ "publishConfig": {
25
+ "registry": "https://registry.npmjs.org/"
26
+ },
27
+ "gitHead": "3fa341400870b891a5b6b0f5e5dedb030a6e15a7"
28
+ }
package/src/index.js ADDED
@@ -0,0 +1,127 @@
1
+ import { AsyncQueue } from 'anys-shared';
2
+ import { InDB } from 'indb/es/index.js';
3
+
4
+ export class AnysStoreOfflinePlugin {
5
+ constructor(anys) {
6
+ this.anys = anys;
7
+ }
8
+
9
+ options() {
10
+ return {
11
+ namespace: new Error('[Anys]: options.namespace is required!'),
12
+ /**
13
+ * offline log will close autoReport as default,
14
+ * developers should must invoke anys.send to send logs to server side during
15
+ * which should use this.[offlineLogger].select to pick logs,
16
+ * and this.[offlineLogger].remove to clear logs
17
+ */
18
+ autoReport: false,
19
+ /**
20
+ * a offline log will be delete after how long time,
21
+ * default 2 day (48 hours)
22
+ */
23
+ expireTime: 3600000 * 48,
24
+ };
25
+ }
26
+
27
+ init() {
28
+ const storeName = 'anyslogs';
29
+ const idb = new InDB({
30
+ name: this.anys.options.namespace,
31
+ version: 1,
32
+ stores: [
33
+ {
34
+ name: storeName,
35
+ primaryKeyPath: '_id',
36
+ autoIncrement: true,
37
+ },
38
+ ],
39
+ });
40
+ this.db = idb.use(storeName);
41
+ this.queue = new AsyncQueue();
42
+
43
+ /**
44
+ * clear logs which should be delete from db
45
+ */
46
+ const { expireTime } = this.anys.options;
47
+ if (expireTime) {
48
+ let offset = 0;
49
+ const timer = setInterval(() => {
50
+ this.queue
51
+ .push(() => this.db.some(100, offset))
52
+ .then((logs) => {
53
+ if (!logs.length) {
54
+ clearInterval(timer);
55
+ return;
56
+ }
57
+ const curr = Date.now();
58
+ const ids = [];
59
+ logs.forEach((log) => {
60
+ const { _id, time } = log;
61
+ if (curr - time > expireTime) {
62
+ ids.push(_id);
63
+ }
64
+ });
65
+ if (ids.length) {
66
+ this.queue.push(() => this.db.delete(ids));
67
+ }
68
+ offset += 100;
69
+ offset -= ids.length;
70
+ });
71
+ }, 10000);
72
+ }
73
+ }
74
+
75
+ write(data) {
76
+ this.queue
77
+ .push(() => this.db.add(data))
78
+ .then((id) => {
79
+ this.anys.emit('writeOfflineLog', { id, data });
80
+ });
81
+ }
82
+
83
+ /**
84
+ * select records from db
85
+ * anys.report([
86
+ * { key: 'time', value: Date.now() - 3600000, compare: '>' },
87
+ * { key: 'trace', value: 'xxx' },
88
+ * ]);
89
+ * https://github.com/tangshuang/indb#select
90
+ * @param {Array} message
91
+ * @returns
92
+ */
93
+ read(message) {
94
+ if (!message) {
95
+ message = [
96
+ { key: 'client', value: this.anys.clientId },
97
+ { key: 'trace', value: this.anys.traceId },
98
+ ];
99
+ }
100
+ else if (!Array.isArray(message)) {
101
+ return;
102
+ }
103
+ else if (message.some(item => !item.key)) {
104
+ return;
105
+ }
106
+ return this.select(message);
107
+ }
108
+
109
+ /**
110
+ * select records from db
111
+ * https://github.com/tangshuang/indb#select
112
+ * @param {Array} conditions
113
+ * @returns
114
+ */
115
+ select(conditions) {
116
+ return this.queue.push(() => this.db.select(conditions));
117
+ }
118
+
119
+ /**
120
+ * delete records from db
121
+ * @param {number|Array<number>} ids
122
+ * @returns
123
+ */
124
+ remove(ids) {
125
+ return this.queue.push(() => this.db.delete(ids)).catch(() => {});
126
+ }
127
+ }