@peerbit/react 0.0.2
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 +202 -0
- package/README.md +1 -0
- package/lib/esm/__tests__/lockstorage.test.d.ts +1 -0
- package/lib/esm/__tests__/lockstorage.test.js +225 -0
- package/lib/esm/__tests__/lockstorage.test.js.map +1 -0
- package/lib/esm/__tests__/utils.test.d.ts +1 -0
- package/lib/esm/__tests__/utils.test.js +61 -0
- package/lib/esm/__tests__/utils.test.js.map +1 -0
- package/lib/esm/index.d.ts +3 -0
- package/lib/esm/index.js +4 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/lockstorage.d.ts +32 -0
- package/lib/esm/lockstorage.js +178 -0
- package/lib/esm/lockstorage.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/useMount.d.ts +2 -0
- package/lib/esm/useMount.js +12 -0
- package/lib/esm/useMount.js.map +1 -0
- package/lib/esm/usePeer.d.ts +25 -0
- package/lib/esm/usePeer.js +180 -0
- package/lib/esm/usePeer.js.map +1 -0
- package/lib/esm/utils.d.ts +11 -0
- package/lib/esm/utils.js +96 -0
- package/lib/esm/utils.js.map +1 -0
- package/package.json +76 -0
- package/src/__tests__/lockstorage.test.ts +264 -0
- package/src/__tests__/utils.test.ts +83 -0
- package/src/index.ts +8 -0
- package/src/lockstorage.ts +249 -0
- package/src/useMount.tsx +15 -0
- package/src/usePeer.tsx +270 -0
- package/src/utils.ts +116 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2022 dao.xyz
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# 🚧 WIP
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/*
|
|
2
|
+
ISC License (ISC)
|
|
3
|
+
Copyright (c) 2016, Wes Cruver <chieffancypants@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
6
|
+
with or without fee is hereby granted, provided that the above copyright notice
|
|
7
|
+
and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
11
|
+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
13
|
+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
14
|
+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
15
|
+
THIS SOFTWARE.
|
|
16
|
+
*/
|
|
17
|
+
import { FastMutex } from "../lockstorage.js";
|
|
18
|
+
import sinon from "sinon";
|
|
19
|
+
import nodelocalstorage from "node-localstorage";
|
|
20
|
+
import { jest } from "@jest/globals";
|
|
21
|
+
import { delay } from "@peerbit/time";
|
|
22
|
+
var LocalStorage = nodelocalstorage.LocalStorage;
|
|
23
|
+
var localStorage = new LocalStorage("./tmp/FastMutex");
|
|
24
|
+
globalThis.localStorage = localStorage;
|
|
25
|
+
describe("FastMutex", () => {
|
|
26
|
+
let sandbox;
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
sandbox = sinon.createSandbox();
|
|
29
|
+
localStorage.clear();
|
|
30
|
+
});
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
sandbox.restore();
|
|
33
|
+
localStorage.clear();
|
|
34
|
+
expect(localStorage.length).toEqual(0);
|
|
35
|
+
});
|
|
36
|
+
it("should immediately establish a lock when there is no contention", async () => {
|
|
37
|
+
const fm1 = new FastMutex({ localStorage: localStorage });
|
|
38
|
+
expect(fm1.isLocked("clientId")).toBeFalse();
|
|
39
|
+
const stats = await fm1.lock("clientId");
|
|
40
|
+
expect(fm1.isLocked("clientId")).toBeTrue();
|
|
41
|
+
});
|
|
42
|
+
it("When another client has a lock (Y is not 0), it should restart to acquire a lock at a later time", function () {
|
|
43
|
+
const fm1 = new FastMutex({
|
|
44
|
+
xPrefix: "xPrefix_",
|
|
45
|
+
yPrefix: "yPrefix_",
|
|
46
|
+
localStorage: localStorage,
|
|
47
|
+
});
|
|
48
|
+
const key = "clientId";
|
|
49
|
+
fm1.setItem(`yPrefix_${key}`, "someOtherMutexId");
|
|
50
|
+
setTimeout(() => {
|
|
51
|
+
localStorage.removeItem(`yPrefix_${key}`);
|
|
52
|
+
}, 20);
|
|
53
|
+
return fm1.lock(key).then(() => {
|
|
54
|
+
expect(fm1.getLockedInfo(key)).toBeDefined();
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
it("when contending for a lock and ultimately losing, it should restart", () => {
|
|
58
|
+
const key = "somekey";
|
|
59
|
+
const fm = new FastMutex({
|
|
60
|
+
localStorage: localStorage,
|
|
61
|
+
clientId: "uniqueId",
|
|
62
|
+
});
|
|
63
|
+
const stub = sandbox.stub(fm, "getItem");
|
|
64
|
+
// Set up scenario for lock contention where we lost Y
|
|
65
|
+
stub.onCall(0).returns(null); // getItem Y
|
|
66
|
+
stub.onCall(1).returns("lockcontention"); // getItem X
|
|
67
|
+
stub.onCall(2).returns("youLostTheLock"); // getItem Y
|
|
68
|
+
// fastmutex should have restarted, so let's free up the lock:
|
|
69
|
+
stub.onCall(3).returns(null);
|
|
70
|
+
stub.onCall(4).returns("uniqueId");
|
|
71
|
+
return fm.lock(key).then((stats) => {
|
|
72
|
+
expect(stats.restartCount).toEqual(1);
|
|
73
|
+
expect(stats.locksLost).toEqual(1);
|
|
74
|
+
expect(stats.contentionCount).toEqual(1);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
it("When contending for a lock and ultimately winning, it should not restart", () => {
|
|
78
|
+
const key = "somekey";
|
|
79
|
+
const fm = new FastMutex({
|
|
80
|
+
localStorage: localStorage,
|
|
81
|
+
clientId: "uniqueId",
|
|
82
|
+
});
|
|
83
|
+
const stub = sandbox.stub(fm, "getItem");
|
|
84
|
+
// Set up scenario for lock contention where we lost Y
|
|
85
|
+
stub.onCall(0).returns(null); // getItem Y
|
|
86
|
+
stub.onCall(1).returns("lockContention");
|
|
87
|
+
stub.onCall(2).returns("uniqueId");
|
|
88
|
+
const spy = sandbox.spy(fm, "lock");
|
|
89
|
+
return fm.lock(key).then((stats) => {
|
|
90
|
+
expect(stats.restartCount).toEqual(0);
|
|
91
|
+
expect(stats.locksLost).toEqual(0);
|
|
92
|
+
expect(stats.contentionCount).toEqual(1);
|
|
93
|
+
expect(spy.callCount).toEqual(1);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
// This is just to ensure that the internals of FastMutex have prefixes on the
|
|
97
|
+
// X and Y locks such that two different FM clients can acquire locks on
|
|
98
|
+
// different keys concurrently without clashing.
|
|
99
|
+
it("should not clash with other fastMutex locks", async () => {
|
|
100
|
+
const yPrefix = "yLock";
|
|
101
|
+
const xPrefix = "xLock";
|
|
102
|
+
const opts = { localStorage, yPrefix, xPrefix };
|
|
103
|
+
const fm1 = new FastMutex(opts);
|
|
104
|
+
const fm2 = new FastMutex(opts);
|
|
105
|
+
let lock1Acquired = false;
|
|
106
|
+
let lock2Acquired = false;
|
|
107
|
+
/* eslint-disable jest/valid-expect-in-promise */
|
|
108
|
+
const lock1Promise = fm1.lock("lock1").then((stats) => {
|
|
109
|
+
lock1Acquired = true;
|
|
110
|
+
expect(localStorage.getItem(yPrefix + "lock1")).toBeDefined();
|
|
111
|
+
return stats;
|
|
112
|
+
});
|
|
113
|
+
/* eslint-disable jest/valid-expect-in-promise */
|
|
114
|
+
const lock2Promise = fm2.lock("lock2").then((stats) => {
|
|
115
|
+
lock2Acquired = true;
|
|
116
|
+
expect(localStorage.getItem(yPrefix + "lock2")).toBeDefined();
|
|
117
|
+
return stats;
|
|
118
|
+
});
|
|
119
|
+
await Promise.all([lock1Promise, lock2Promise]).then(() => {
|
|
120
|
+
expect(lock1Acquired).toBeTrue();
|
|
121
|
+
expect(lock2Acquired).toBeTrue();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
it("release() should remove the y lock in localStorage", () => {
|
|
125
|
+
const key = "somekey";
|
|
126
|
+
const fm1 = new FastMutex({
|
|
127
|
+
localStorage: localStorage,
|
|
128
|
+
clientId: "releaseTestId",
|
|
129
|
+
yPrefix: "yLock",
|
|
130
|
+
});
|
|
131
|
+
return fm1
|
|
132
|
+
.lock(key)
|
|
133
|
+
.then(() => {
|
|
134
|
+
expect(fm1.getItem("yLock" + key)).toEqual("releaseTestId");
|
|
135
|
+
return fm1.release(key);
|
|
136
|
+
})
|
|
137
|
+
.then(() => {
|
|
138
|
+
expect(fm1.getItem("yLock" + key)).toBeUndefined();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
// this is essentially just a better way to test that two locks cannot get
|
|
142
|
+
// an exclusive lock until the other releases. It's a bit more accurate
|
|
143
|
+
// than the test above ("release should remove the y lock in localstorage")
|
|
144
|
+
it("two clients should never get locks at the same time", function () {
|
|
145
|
+
const fm1 = new FastMutex({ localStorage: localStorage });
|
|
146
|
+
const fm2 = new FastMutex({ localStorage: localStorage });
|
|
147
|
+
let fm1LockReleased = false;
|
|
148
|
+
const lockHoldTime = 10;
|
|
149
|
+
return fm1
|
|
150
|
+
.lock("clientId")
|
|
151
|
+
.then(() => {
|
|
152
|
+
// before the lock is released, try to establish another lock:
|
|
153
|
+
var lock2Promise = fm2.lock("clientId");
|
|
154
|
+
expect(fm1LockReleased).toBeFalse();
|
|
155
|
+
// in a few milliseconds, release the lock
|
|
156
|
+
setTimeout(() => {
|
|
157
|
+
fm1.release("clientId");
|
|
158
|
+
fm1LockReleased = true;
|
|
159
|
+
}, lockHoldTime);
|
|
160
|
+
return lock2Promise;
|
|
161
|
+
})
|
|
162
|
+
.then((lock2) => {
|
|
163
|
+
// this will only execute once the other lock was released
|
|
164
|
+
expect(fm1LockReleased).toBeTrue();
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
it("should throw if lock is never acquired after set time period", function () {
|
|
168
|
+
jest.setTimeout(1000);
|
|
169
|
+
// this.slow(500);
|
|
170
|
+
const fm1 = new FastMutex({ localStorage: localStorage, timeout: 50 });
|
|
171
|
+
const fm2 = new FastMutex({ localStorage: localStorage, timeout: 50 });
|
|
172
|
+
const p = fm1.lock("timeoutTest").then(() => {
|
|
173
|
+
// fm2 will never get a lock as we're not releasing fm1's lock:
|
|
174
|
+
return fm2.lock("timeoutTest");
|
|
175
|
+
});
|
|
176
|
+
return expect(p).rejects.toThrowError();
|
|
177
|
+
});
|
|
178
|
+
it("should ignore expired locks", () => {
|
|
179
|
+
const fm1 = new FastMutex({
|
|
180
|
+
localStorage: localStorage,
|
|
181
|
+
timeout: 5000,
|
|
182
|
+
yPrefix: "yLock",
|
|
183
|
+
clientId: "timeoutClient",
|
|
184
|
+
});
|
|
185
|
+
const expiredRecord = {
|
|
186
|
+
expiresAt: new Date().getTime() - 5000,
|
|
187
|
+
value: "oldclient",
|
|
188
|
+
};
|
|
189
|
+
localStorage.setItem("yLocktimeoutTest", JSON.stringify(expiredRecord));
|
|
190
|
+
expect(JSON.parse(localStorage.getItem("yLocktimeoutTest")).value).toEqual("oldclient");
|
|
191
|
+
return expect(fm1.lock("timeoutTest")).toResolve();
|
|
192
|
+
});
|
|
193
|
+
it("should reset the client stats after lock is released", async () => {
|
|
194
|
+
// without resetting the stats, the acquireStart will always be set, and
|
|
195
|
+
// after `timeout` ms, will be unable to acquire a lock anymore
|
|
196
|
+
const fm1 = new FastMutex({ localStorage: localStorage, timeout: 50 });
|
|
197
|
+
let keepLock = true;
|
|
198
|
+
let keepLockFn = () => keepLock;
|
|
199
|
+
await fm1.lock("resetStats", keepLockFn);
|
|
200
|
+
expect(fm1.isLocked("resetStats")).toBeTrue();
|
|
201
|
+
keepLock = false;
|
|
202
|
+
await delay(100); // await timeout
|
|
203
|
+
expect(fm1.isLocked("resetStats")).toBeFalse();
|
|
204
|
+
const p = fm1.lock("resetStats").then(() => fm1.release("resetStats"));
|
|
205
|
+
await expect(p).toResolve();
|
|
206
|
+
});
|
|
207
|
+
it("can keep lock with callback function", async () => {
|
|
208
|
+
const fm1 = new FastMutex({ localStorage: localStorage, timeout: 50 });
|
|
209
|
+
await fm1.lock("x");
|
|
210
|
+
await fm1.release("x");
|
|
211
|
+
expect(fm1.isLocked("x")).toBeFalse();
|
|
212
|
+
await fm1.lock("x").then(() => fm1.release("x"));
|
|
213
|
+
});
|
|
214
|
+
it("should reset the client stats if the lock has expired", async () => {
|
|
215
|
+
// in the event a lock cannot be acquired within `timeout`, acquireStart
|
|
216
|
+
// will never be reset, and a subsequent call (after the `timeout`) would
|
|
217
|
+
// immediately fail
|
|
218
|
+
const fm1 = new FastMutex({ localStorage: localStorage, timeout: 50 });
|
|
219
|
+
await fm1.lock("resetStats");
|
|
220
|
+
// try to acquire a lock after `timeout`:
|
|
221
|
+
await delay(50);
|
|
222
|
+
await expect(fm1.lock("resetStats")).toResolve();
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
//# sourceMappingURL=lockstorage.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lockstorage.test.js","sourceRoot":"","sources":["../../../src/__tests__/lockstorage.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;EAeE;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C,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { getAllKeyPairs, getFreeKeypair, releaseKey } from "../utils";
|
|
2
|
+
import nodelocalstorage from "node-localstorage";
|
|
3
|
+
import { FastMutex } from "../lockstorage";
|
|
4
|
+
import { delay } from "@peerbit/time";
|
|
5
|
+
import { default as sodium } from "libsodium-wrappers";
|
|
6
|
+
import { v4 as uuid } from "uuid";
|
|
7
|
+
var LocalStorage = nodelocalstorage.LocalStorage;
|
|
8
|
+
var localStorage = new LocalStorage("./tmp/getKeypair");
|
|
9
|
+
globalThis.localStorage = localStorage;
|
|
10
|
+
describe("getKeypair", () => {
|
|
11
|
+
beforeAll(async () => {
|
|
12
|
+
await sodium.ready;
|
|
13
|
+
});
|
|
14
|
+
it("can aquire multiple keypairs", async () => {
|
|
15
|
+
let timeout = 1000;
|
|
16
|
+
let mutex = new FastMutex({ localStorage, timeout });
|
|
17
|
+
let lock = true;
|
|
18
|
+
const lockCondition = () => lock;
|
|
19
|
+
let id = uuid();
|
|
20
|
+
const { key: keypair, path: path1 } = await getFreeKeypair(id, mutex, lockCondition);
|
|
21
|
+
const { key: keypair2, path: path2 } = await getFreeKeypair(id, mutex);
|
|
22
|
+
expect(keypair.equals(keypair2)).toBeFalse();
|
|
23
|
+
expect(path1).not.toEqual(path2);
|
|
24
|
+
lock = false;
|
|
25
|
+
await delay(timeout);
|
|
26
|
+
const { path: path3, key: keypair3 } = await getFreeKeypair(id, mutex);
|
|
27
|
+
expect(path3).toEqual(path1);
|
|
28
|
+
expect(keypair3.equals(keypair)).toBeTrue();
|
|
29
|
+
const allKeypair = await getAllKeyPairs(id);
|
|
30
|
+
expect(allKeypair.map((x) => x.publicKey.hashcode())).toEqual([
|
|
31
|
+
keypair3.publicKey.hashcode(),
|
|
32
|
+
keypair2.publicKey.hashcode(),
|
|
33
|
+
]);
|
|
34
|
+
});
|
|
35
|
+
it("can release if same id", async () => {
|
|
36
|
+
let timeout = 1000;
|
|
37
|
+
let mutex = new FastMutex({ localStorage, timeout });
|
|
38
|
+
let lock = true;
|
|
39
|
+
const lockCondition = () => lock;
|
|
40
|
+
let id = uuid();
|
|
41
|
+
const { key: keypair, path: path1 } = await getFreeKeypair(id, mutex, lockCondition, true);
|
|
42
|
+
const { key: keypair2, path: path2 } = await getFreeKeypair(id, mutex, undefined, true);
|
|
43
|
+
expect(keypair.equals(keypair2)).toBeTrue();
|
|
44
|
+
expect(path1).toEqual(path2);
|
|
45
|
+
const allKeypair = await getAllKeyPairs(id);
|
|
46
|
+
expect(allKeypair).toHaveLength(1);
|
|
47
|
+
});
|
|
48
|
+
it("releases manually", async () => {
|
|
49
|
+
let timeout = 1000;
|
|
50
|
+
let mutex = new FastMutex({ localStorage, timeout });
|
|
51
|
+
const id = uuid();
|
|
52
|
+
const { key: keypair, path: path1 } = await getFreeKeypair(id, mutex);
|
|
53
|
+
const { key: keypair2, path: path2 } = await getFreeKeypair(id, mutex);
|
|
54
|
+
expect(path1).not.toEqual(path2);
|
|
55
|
+
releaseKey(path1, mutex);
|
|
56
|
+
expect(mutex.getLockedInfo(path1)).toBeUndefined();
|
|
57
|
+
const { key: keypair3, path: path3 } = await getFreeKeypair(id, mutex);
|
|
58
|
+
expect(path1).toEqual(path3); // we can now acquire key at path1 again, since we released it
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
//# sourceMappingURL=utils.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.test.js","sourceRoot":"","sources":["../../../src/__tests__/utils.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtE,OAAO,gBAAgB,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MA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|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,YAAY,EACZ,WAAW,EACX,OAAO,EACP,mBAAmB,GACtB,MAAM,cAAc,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export declare class FastMutex {
|
|
2
|
+
clientId: string;
|
|
3
|
+
xPrefix: string;
|
|
4
|
+
yPrefix: string;
|
|
5
|
+
timeout: number;
|
|
6
|
+
localStorage: any;
|
|
7
|
+
intervals: Map<string, any>;
|
|
8
|
+
constructor({ clientId, xPrefix, yPrefix, timeout, localStorage, }?: {
|
|
9
|
+
clientId?: any;
|
|
10
|
+
xPrefix?: string | undefined;
|
|
11
|
+
yPrefix?: string | undefined;
|
|
12
|
+
timeout?: number | undefined;
|
|
13
|
+
localStorage?: undefined;
|
|
14
|
+
});
|
|
15
|
+
lock(key: string, keepLocked?: () => boolean): Promise<{
|
|
16
|
+
restartCount: number;
|
|
17
|
+
contentionCount: any;
|
|
18
|
+
locksLost: number;
|
|
19
|
+
}>;
|
|
20
|
+
isLocked(key: string): boolean;
|
|
21
|
+
getLockedInfo(key: string): string | undefined;
|
|
22
|
+
release(key: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Helper function to wrap all values in an object that includes the time (so
|
|
25
|
+
* that we can expire it in the future) and json.stringify's it
|
|
26
|
+
*/
|
|
27
|
+
setItem(key: string, value: any, keepLocked?: () => boolean): any;
|
|
28
|
+
/**
|
|
29
|
+
* Helper function to parse JSON encoded values set in localStorage
|
|
30
|
+
*/
|
|
31
|
+
getItem(key: string): string | undefined;
|
|
32
|
+
}
|