@dxos/client-services 0.8.3 → 0.8.4-main.3f58842
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/dist/lib/browser/{chunk-LBG3C332.mjs → chunk-K37Z5ZKM.mjs} +125 -124
- package/dist/lib/browser/chunk-K37Z5ZKM.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +6 -6
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +6 -6
- package/dist/lib/node-esm/{chunk-SKGQLRKS.mjs → chunk-P5OU3OJB.mjs} +124 -123
- package/dist/lib/node-esm/chunk-P5OU3OJB.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +6 -6
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +6 -6
- package/dist/types/src/packlets/invitations/index.d.ts +1 -1
- package/dist/types/src/packlets/invitations/index.d.ts.map +1 -1
- package/dist/types/src/packlets/locks/index.d.ts +1 -1
- package/dist/types/src/packlets/locks/index.d.ts.map +1 -1
- package/dist/types/src/packlets/logging/logging-service.d.ts.map +1 -1
- package/dist/types/src/packlets/services/service-host.d.ts +1 -1
- package/dist/types/src/packlets/services/service-host.d.ts.map +1 -1
- package/dist/types/src/version.d.ts +1 -1
- package/dist/types/src/version.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +39 -39
- package/src/packlets/invitations/device-invitation-protocol.test.ts +3 -3
- package/src/packlets/invitations/index.ts +1 -1
- package/src/packlets/invitations/invitations-handler.test.ts +298 -289
- package/src/packlets/invitations/space-invitation-protocol.test.ts +8 -8
- package/src/packlets/locks/index.ts +1 -1
- package/src/packlets/logging/logging-service.ts +1 -0
- package/src/packlets/services/service-host.ts +6 -5
- package/src/version.ts +1 -5
- package/README.yml +0 -5
- package/dist/lib/browser/chunk-LBG3C332.mjs.map +0 -7
- package/dist/lib/node/chunk-LMGLGOUU.cjs +0 -9311
- package/dist/lib/node/chunk-LMGLGOUU.cjs.map +0 -7
- package/dist/lib/node/index.cjs +0 -437
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/lib/node/testing/index.cjs +0 -452
- package/dist/lib/node/testing/index.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-SKGQLRKS.mjs.map +0 -7
|
@@ -32,335 +32,344 @@ type StateUpdateSink = PushStream<Invitation> & {
|
|
|
32
32
|
waitFor(state: Invitation.State): Promise<void>;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
// TODO(burdon): Flaky.
|
|
36
|
+
describe.skipIf(process.env.CI && !process.env.RUN_FLAKY_TESTS)(
|
|
37
|
+
'InvitationHandler',
|
|
38
|
+
{ retry: 3, timeout: 30_000 },
|
|
39
|
+
() => {
|
|
40
|
+
let testBuilder: TestBuilder;
|
|
41
|
+
|
|
42
|
+
beforeEach(() => {
|
|
43
|
+
testBuilder = new TestBuilder();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('delegated invitations', () => {
|
|
47
|
+
for (const multiUse of [false, true]) {
|
|
48
|
+
test(`base case success multiUse=${multiUse}`, async () => {
|
|
49
|
+
const host = await createPeer();
|
|
50
|
+
const invitation = await createInvitation(host, { multiUse });
|
|
51
|
+
await hostInvitation(host, invitation);
|
|
37
52
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
53
|
+
const guest = await createPeer(host.spaceKey);
|
|
54
|
+
await performAuth(guest, invitation);
|
|
55
|
+
|
|
56
|
+
await sleep(15);
|
|
57
|
+
expect(guest.ctx.disposed).to.be.true;
|
|
58
|
+
if (multiUse) {
|
|
59
|
+
expect(host.ctx.disposed).to.be.false;
|
|
60
|
+
} else {
|
|
61
|
+
expect(host.ctx.disposed).to.be.true;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
41
65
|
|
|
42
|
-
|
|
43
|
-
for (const multiUse of [false, true]) {
|
|
44
|
-
test(`base case success multiUse=${multiUse}`, async () => {
|
|
66
|
+
test('invitation timeout', async () => {
|
|
45
67
|
const host = await createPeer();
|
|
46
|
-
const invitation = await createInvitation(host, {
|
|
68
|
+
const invitation = await createInvitation(host, {
|
|
69
|
+
timeout: 100,
|
|
70
|
+
});
|
|
47
71
|
await hostInvitation(host, invitation);
|
|
48
72
|
|
|
49
73
|
const guest = await createPeer(host.spaceKey);
|
|
50
|
-
await
|
|
74
|
+
await acceptInvitation(guest, invitation);
|
|
51
75
|
|
|
52
|
-
await
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
} else {
|
|
57
|
-
expect(host.ctx.disposed).to.be.true;
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
}
|
|
76
|
+
await guest.sink.waitFor(Invitation.State.READY_FOR_AUTHENTICATION);
|
|
77
|
+
await sleep(200);
|
|
78
|
+
await host.sink.waitFor(Invitation.State.CONNECTING);
|
|
79
|
+
await guest.sink.waitFor(Invitation.State.TIMEOUT);
|
|
61
80
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
timeout: 100,
|
|
81
|
+
await sleep(10);
|
|
82
|
+
expect(host.ctx.disposed).to.be.false;
|
|
83
|
+
expect(guest.ctx.disposed).to.be.false;
|
|
66
84
|
});
|
|
67
|
-
await hostInvitation(host, invitation);
|
|
68
85
|
|
|
69
|
-
|
|
70
|
-
|
|
86
|
+
test('host ctx active after guest disconnects', async () => {
|
|
87
|
+
const host = await createPeer();
|
|
88
|
+
const invitation = await createInvitation(host, {
|
|
89
|
+
timeout: 100,
|
|
90
|
+
});
|
|
91
|
+
await hostInvitation(host, invitation);
|
|
71
92
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
await host.sink.waitFor(Invitation.State.CONNECTING);
|
|
75
|
-
await guest.sink.waitFor(Invitation.State.TIMEOUT);
|
|
93
|
+
const guest = await createPeer(host.spaceKey);
|
|
94
|
+
await acceptInvitation(guest, invitation);
|
|
76
95
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
});
|
|
96
|
+
await guest.sink.waitFor(Invitation.State.READY_FOR_AUTHENTICATION);
|
|
97
|
+
await guest.peer.networkManager.close();
|
|
98
|
+
await host.sink.waitFor(Invitation.State.CONNECTING);
|
|
81
99
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const invitation = await createInvitation(host, {
|
|
85
|
-
timeout: 100,
|
|
100
|
+
await sleep(10);
|
|
101
|
+
expect(host.ctx.disposed).to.be.false;
|
|
86
102
|
});
|
|
87
|
-
await hostInvitation(host, invitation);
|
|
88
|
-
|
|
89
|
-
const guest = await createPeer(host.spaceKey);
|
|
90
|
-
await acceptInvitation(guest, invitation);
|
|
91
103
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
104
|
+
test('invitation success after first guest error', async () => {
|
|
105
|
+
const host = await createPeer();
|
|
106
|
+
const invitation = await createInvitation(host);
|
|
107
|
+
await hostInvitation(host, invitation);
|
|
95
108
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
109
|
+
const badGuest = await createPeer(host.spaceKey);
|
|
110
|
+
await failAuth(badGuest, invitation);
|
|
111
|
+
await badGuest.sink.waitFor(Invitation.State.ERROR);
|
|
99
112
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
await hostInvitation(host, invitation);
|
|
113
|
+
const goodGuest = await createPeer(host.spaceKey);
|
|
114
|
+
await performAuth(goodGuest, invitation);
|
|
115
|
+
await host.sink.waitFor(Invitation.State.SUCCESS);
|
|
104
116
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
117
|
+
await sleep(10);
|
|
118
|
+
expect(goodGuest.ctx.disposed).to.be.true;
|
|
119
|
+
expect(host.ctx.disposed).to.be.true;
|
|
120
|
+
});
|
|
108
121
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
122
|
+
test('multiUse invitation with multiple guests', async () => {
|
|
123
|
+
const host = await createPeer();
|
|
124
|
+
const invitation = await createInvitation(host, { multiUse: true });
|
|
125
|
+
await hostInvitation(host, invitation);
|
|
112
126
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
127
|
+
const guest1 = await createPeer(host.spaceKey);
|
|
128
|
+
await performAuth(guest1, invitation);
|
|
129
|
+
const guest2 = await createPeer(host.spaceKey);
|
|
130
|
+
await performAuth(guest2, invitation);
|
|
117
131
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
132
|
+
await sleep(5);
|
|
133
|
+
[guest1, guest2].forEach((g) => expect(g.ctx.disposed).to.be.true);
|
|
134
|
+
expect(host.ctx.disposed).to.be.false;
|
|
135
|
+
});
|
|
122
136
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
137
|
+
test('invitation success after host error with another host', async () => {
|
|
138
|
+
const host = await createPeer();
|
|
139
|
+
const invitation = await createInvitation(host, { multiUse: true });
|
|
140
|
+
await hostInvitation(host, invitation);
|
|
141
|
+
await createNewHost(invitation);
|
|
127
142
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
143
|
+
const guest = await createPeer(host.spaceKey);
|
|
144
|
+
const codeInput = await failAuth(guest, invitation);
|
|
145
|
+
while (!guest.ctx.disposed) {
|
|
146
|
+
codeInput.wake(invitation.authCode!);
|
|
147
|
+
await sleep(10);
|
|
148
|
+
}
|
|
149
|
+
await guest.sink.waitFor(Invitation.State.SUCCESS);
|
|
150
|
+
});
|
|
132
151
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
152
|
+
test('single guest - many hosts', async () => {
|
|
153
|
+
const hosts: PeerSetup[] = [await createPeer()];
|
|
154
|
+
const [host] = hosts;
|
|
155
|
+
const invitation = await createInvitation(host, { multiUse: true });
|
|
156
|
+
await hostInvitation(host, invitation);
|
|
157
|
+
for (let i = 0; i < 4; i++) {
|
|
158
|
+
hosts.push(await createNewHost(invitation));
|
|
159
|
+
}
|
|
138
160
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
codeInput.wake(invitation.authCode!);
|
|
161
|
+
const guest = await createPeer(host.spaceKey);
|
|
162
|
+
await performAuth(guest, invitation);
|
|
163
|
+
await guest.sink.waitFor(Invitation.State.SUCCESS);
|
|
143
164
|
await sleep(10);
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
});
|
|
165
|
+
expect(guest.ctx.disposed).to.be.true;
|
|
166
|
+
});
|
|
147
167
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
168
|
+
test('guest gives up after trying with three hosts', { timeout: 20_000 }, async () => {
|
|
169
|
+
const hosts: PeerSetup[] = [await createPeer()];
|
|
170
|
+
const [host] = hosts;
|
|
171
|
+
const invitation = await createInvitation(host, { multiUse: true });
|
|
172
|
+
await hostInvitation(host, invitation);
|
|
173
|
+
for (let i = 0; i < 2; i++) {
|
|
174
|
+
hosts.push(await createNewHost(invitation));
|
|
175
|
+
}
|
|
156
176
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
177
|
+
const guest = await createPeer(host.spaceKey);
|
|
178
|
+
const codeInput = await acceptInvitation(guest, invitation);
|
|
179
|
+
while (!guest.ctx.disposed) {
|
|
180
|
+
await failCodeInput(guest, codeInput, invitation);
|
|
181
|
+
await sleep(10);
|
|
182
|
+
}
|
|
163
183
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
hosts
|
|
171
|
-
|
|
184
|
+
await sleep(10);
|
|
185
|
+
expect(guest.sink.lastState).to.eq(Invitation.State.ERROR);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test('single host - many guests', async () => {
|
|
189
|
+
const hosts: PeerSetup[] = [await createPeer()];
|
|
190
|
+
const [host] = hosts;
|
|
191
|
+
const invitation = await createInvitation(host, { multiUse: true });
|
|
192
|
+
await hostInvitation(host, invitation);
|
|
193
|
+
const guests = await Promise.all(
|
|
194
|
+
range(5).map(async () => {
|
|
195
|
+
const guest = await createPeer(host.spaceKey);
|
|
196
|
+
await performAuth(guest, invitation);
|
|
197
|
+
return guest;
|
|
198
|
+
}),
|
|
199
|
+
);
|
|
172
200
|
|
|
173
|
-
const guest = await createPeer(host.spaceKey);
|
|
174
|
-
const codeInput = await acceptInvitation(guest, invitation);
|
|
175
|
-
while (!guest.ctx.disposed) {
|
|
176
|
-
await failCodeInput(guest, codeInput, invitation);
|
|
177
201
|
await sleep(10);
|
|
178
|
-
|
|
202
|
+
guests.forEach((g) => {
|
|
203
|
+
expect(g.ctx.disposed).to.be.true;
|
|
204
|
+
expect(g.sink.lastState).to.eq(Invitation.State.SUCCESS);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
179
207
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
208
|
+
test('many guests - many hosts', async () => {
|
|
209
|
+
const hosts: PeerSetup[] = [await createPeer()];
|
|
210
|
+
const [host] = hosts;
|
|
211
|
+
const invitation = await createInvitation(host, { multiUse: true });
|
|
212
|
+
await hostInvitation(host, invitation);
|
|
213
|
+
for (let i = 0; i < 4; i++) {
|
|
214
|
+
hosts.push(await createNewHost(invitation));
|
|
215
|
+
}
|
|
216
|
+
const guests = await Promise.all(
|
|
217
|
+
range(5).map(async () => {
|
|
218
|
+
const guest = await createPeer(host.spaceKey);
|
|
219
|
+
await performAuth(guest, invitation);
|
|
220
|
+
return guest;
|
|
221
|
+
}),
|
|
222
|
+
);
|
|
223
|
+
await sleep(10);
|
|
224
|
+
guests.forEach((g) => {
|
|
225
|
+
expect(g.ctx.disposed).to.be.true;
|
|
226
|
+
expect(g.sink.lastState).to.eq(Invitation.State.SUCCESS);
|
|
227
|
+
});
|
|
228
|
+
});
|
|
183
229
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
230
|
+
test('single use invitation - many guests - only one admitted', async () => {
|
|
231
|
+
const host = await createPeer();
|
|
232
|
+
const invitation = await createInvitation(host);
|
|
233
|
+
await hostInvitation(host, invitation);
|
|
234
|
+
const guests = await Promise.all(
|
|
235
|
+
range(5).map(async () => {
|
|
236
|
+
const guest = await createPeer(invitation.spaceKey);
|
|
237
|
+
const authCodeInput2 = await acceptInvitation(guest, invitation);
|
|
238
|
+
authCodeInput2.wake(invitation.authCode!);
|
|
239
|
+
return guest;
|
|
240
|
+
}),
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
await waitForCondition({
|
|
244
|
+
condition: () => guests.find((g) => g.sink.lastState === Invitation.State.SUCCESS) != null,
|
|
245
|
+
});
|
|
246
|
+
await sleep(40);
|
|
247
|
+
const success = guests.filter((g) => g.sink.lastState === Invitation.State.SUCCESS);
|
|
248
|
+
expect(success.length).to.eq(1);
|
|
201
249
|
});
|
|
202
250
|
});
|
|
203
251
|
|
|
204
|
-
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
await
|
|
209
|
-
|
|
210
|
-
|
|
252
|
+
const createPeer = async (spaceKey: PublicKey | null = null): Promise<PeerSetup> => {
|
|
253
|
+
const peer = testBuilder.createPeer();
|
|
254
|
+
await peer.createIdentity();
|
|
255
|
+
await openAndClose(peer.echoHost, peer.dataSpaceManager);
|
|
256
|
+
await peer.echoHost.addReplicator(peer.meshEchoReplicator);
|
|
257
|
+
if (spaceKey == null) {
|
|
258
|
+
const space = await peer.dataSpaceManager.createSpace();
|
|
259
|
+
spaceKey = space.key;
|
|
211
260
|
}
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
const guest = await createPeer(host.spaceKey);
|
|
215
|
-
await performAuth(guest, invitation);
|
|
216
|
-
return guest;
|
|
217
|
-
}),
|
|
218
|
-
);
|
|
219
|
-
await sleep(10);
|
|
220
|
-
guests.forEach((g) => {
|
|
221
|
-
expect(g.ctx.disposed).to.be.true;
|
|
222
|
-
expect(g.sink.lastState).to.eq(Invitation.State.SUCCESS);
|
|
261
|
+
const invitationHandler = new InvitationsHandler(peer.networkManager, undefined, {
|
|
262
|
+
teleport: { controlHeartbeatInterval: 250 }, // faster peer failure detection
|
|
223
263
|
});
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
const invitation = await createInvitation(host);
|
|
229
|
-
await hostInvitation(host, invitation);
|
|
230
|
-
const guests = await Promise.all(
|
|
231
|
-
range(5).map(async () => {
|
|
232
|
-
const guest = await createPeer(invitation.spaceKey);
|
|
233
|
-
const authCodeInput2 = await acceptInvitation(guest, invitation);
|
|
234
|
-
authCodeInput2.wake(invitation.authCode!);
|
|
235
|
-
return guest;
|
|
236
|
-
}),
|
|
237
|
-
);
|
|
238
|
-
|
|
239
|
-
await waitForCondition({
|
|
240
|
-
condition: () => guests.find((g) => g.sink.lastState === Invitation.State.SUCCESS) != null,
|
|
264
|
+
const protocol = new SpaceInvitationProtocol(peer.dataSpaceManager, peer.identity, peer.keyring, spaceKey);
|
|
265
|
+
const ctx = new Context();
|
|
266
|
+
onTestFinished(async () => {
|
|
267
|
+
await ctx.dispose();
|
|
241
268
|
});
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
const createPeer = async (spaceKey: PublicKey | null = null): Promise<PeerSetup> => {
|
|
249
|
-
const peer = testBuilder.createPeer();
|
|
250
|
-
await peer.createIdentity();
|
|
251
|
-
await openAndClose(peer.echoHost, peer.dataSpaceManager);
|
|
252
|
-
await peer.echoHost.addReplicator(peer.meshEchoReplicator);
|
|
253
|
-
if (spaceKey == null) {
|
|
254
|
-
const space = await peer.dataSpaceManager.createSpace();
|
|
255
|
-
spaceKey = space.key;
|
|
256
|
-
}
|
|
257
|
-
const invitationHandler = new InvitationsHandler(peer.networkManager, undefined, {
|
|
258
|
-
teleport: { controlHeartbeatInterval: 250 }, // faster peer failure detection
|
|
259
|
-
});
|
|
260
|
-
const protocol = new SpaceInvitationProtocol(peer.dataSpaceManager, peer.identity, peer.keyring, spaceKey);
|
|
261
|
-
const ctx = new Context();
|
|
262
|
-
onTestFinished(async () => {
|
|
263
|
-
await ctx.dispose();
|
|
264
|
-
});
|
|
265
|
-
const sink = newStateUpdateSink();
|
|
266
|
-
return { ctx, sink, peer, protocol, handler: invitationHandler, spaceKey };
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
const hostInvitation = async (setup: PeerSetup, invitation: Invitation) => {
|
|
270
|
-
await setup.ctx.dispose();
|
|
271
|
-
setup.ctx = new Context();
|
|
272
|
-
onTestFinished(async () => {
|
|
269
|
+
const sink = newStateUpdateSink();
|
|
270
|
+
return { ctx, sink, peer, protocol, handler: invitationHandler, spaceKey };
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const hostInvitation = async (setup: PeerSetup, invitation: Invitation) => {
|
|
273
274
|
await setup.ctx.dispose();
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
275
|
+
setup.ctx = new Context();
|
|
276
|
+
onTestFinished(async () => {
|
|
277
|
+
await setup.ctx.dispose();
|
|
278
|
+
});
|
|
279
|
+
setup.handler.handleInvitationFlow(setup.ctx, setup.sink, setup.protocol, invitation);
|
|
280
|
+
};
|
|
277
281
|
|
|
278
|
-
|
|
279
|
-
await setup.ctx.dispose();
|
|
280
|
-
setup.ctx = new Context();
|
|
281
|
-
onTestFinished(async () => {
|
|
282
|
+
const acceptInvitation = async (setup: PeerSetup, invitation: Invitation): Promise<Trigger<string>> => {
|
|
282
283
|
await setup.ctx.dispose();
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
const wrongAuthCodeInput = await acceptInvitation(setup, invitation);
|
|
291
|
-
await setup.sink.waitFor(Invitation.State.READY_FOR_AUTHENTICATION);
|
|
292
|
-
await failCodeInput(setup, wrongAuthCodeInput, invitation);
|
|
293
|
-
return wrongAuthCodeInput;
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
const failCodeInput = async (setup: PeerSetup, codeInput: Trigger<string>, invitation: Invitation): Promise<void> => {
|
|
297
|
-
const checkFrom = setup.sink.sink.length;
|
|
298
|
-
while (
|
|
299
|
-
!setup.ctx.disposed &&
|
|
300
|
-
!setup.sink.hasState(checkFrom, Invitation.State.ERROR) &&
|
|
301
|
-
!setup.sink.hasState(checkFrom, Invitation.State.CONNECTED)
|
|
302
|
-
) {
|
|
303
|
-
codeInput.wake(invitation.authCode + '1');
|
|
304
|
-
await sleep(20);
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
const createNewHost = async (invitation: Invitation): Promise<PeerSetup> => {
|
|
309
|
-
const newHost = await createPeer(invitation.spaceKey!);
|
|
310
|
-
await performAuth(newHost, invitation);
|
|
311
|
-
await sleep(30);
|
|
312
|
-
await hostInvitation(newHost, invitation);
|
|
313
|
-
return newHost;
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
const performAuth = async (setup: PeerSetup, invitation: Invitation) => {
|
|
317
|
-
const authCodeInput2 = await acceptInvitation(setup, invitation);
|
|
318
|
-
await setup.sink.waitFor(Invitation.State.READY_FOR_AUTHENTICATION);
|
|
319
|
-
authCodeInput2.wake(invitation.authCode!);
|
|
320
|
-
await setup.sink.waitFor(Invitation.State.SUCCESS);
|
|
321
|
-
};
|
|
322
|
-
|
|
323
|
-
const newStateUpdateSink = (): StateUpdateSink => {
|
|
324
|
-
const sink: Invitation[] = [];
|
|
325
|
-
const hasState = (startingIndex: number, state: Invitation.State): boolean => {
|
|
326
|
-
return sink
|
|
327
|
-
.slice(startingIndex)
|
|
328
|
-
.map((i) => i.state)
|
|
329
|
-
.includes(state);
|
|
284
|
+
setup.ctx = new Context();
|
|
285
|
+
onTestFinished(async () => {
|
|
286
|
+
await setup.ctx.dispose();
|
|
287
|
+
});
|
|
288
|
+
const authCodeInput = new Trigger<string>();
|
|
289
|
+
setup.handler.acceptInvitation(setup.ctx, setup.sink, setup.protocol, invitation, authCodeInput);
|
|
290
|
+
return authCodeInput;
|
|
330
291
|
};
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
292
|
+
|
|
293
|
+
const failAuth = async (setup: PeerSetup, invitation: Invitation) => {
|
|
294
|
+
const wrongAuthCodeInput = await acceptInvitation(setup, invitation);
|
|
295
|
+
await setup.sink.waitFor(Invitation.State.READY_FOR_AUTHENTICATION);
|
|
296
|
+
await failCodeInput(setup, wrongAuthCodeInput, invitation);
|
|
297
|
+
return wrongAuthCodeInput;
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const failCodeInput = async (
|
|
301
|
+
setup: PeerSetup,
|
|
302
|
+
codeInput: Trigger<string>,
|
|
303
|
+
invitation: Invitation,
|
|
304
|
+
): Promise<void> => {
|
|
305
|
+
const checkFrom = setup.sink.sink.length;
|
|
306
|
+
while (
|
|
307
|
+
!setup.ctx.disposed &&
|
|
308
|
+
!setup.sink.hasState(checkFrom, Invitation.State.ERROR) &&
|
|
309
|
+
!setup.sink.hasState(checkFrom, Invitation.State.CONNECTED)
|
|
310
|
+
) {
|
|
311
|
+
codeInput.wake(invitation.authCode + '1');
|
|
312
|
+
await sleep(20);
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const createNewHost = async (invitation: Invitation): Promise<PeerSetup> => {
|
|
317
|
+
const newHost = await createPeer(invitation.spaceKey!);
|
|
318
|
+
await performAuth(newHost, invitation);
|
|
319
|
+
await sleep(30);
|
|
320
|
+
await hostInvitation(newHost, invitation);
|
|
321
|
+
return newHost;
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
const performAuth = async (setup: PeerSetup, invitation: Invitation) => {
|
|
325
|
+
const authCodeInput2 = await acceptInvitation(setup, invitation);
|
|
326
|
+
await setup.sink.waitFor(Invitation.State.READY_FOR_AUTHENTICATION);
|
|
327
|
+
authCodeInput2.wake(invitation.authCode!);
|
|
328
|
+
await setup.sink.waitFor(Invitation.State.SUCCESS);
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
const newStateUpdateSink = (): StateUpdateSink => {
|
|
332
|
+
const sink: Invitation[] = [];
|
|
333
|
+
const hasState = (startingIndex: number, state: Invitation.State): boolean => {
|
|
334
|
+
return sink
|
|
335
|
+
.slice(startingIndex)
|
|
336
|
+
.map((i) => i.state)
|
|
337
|
+
.includes(state);
|
|
338
|
+
};
|
|
339
|
+
return {
|
|
340
|
+
sink,
|
|
341
|
+
next: sink.push.bind(sink),
|
|
342
|
+
error: () => {},
|
|
343
|
+
complete: () => {},
|
|
344
|
+
hasState,
|
|
345
|
+
get lastState() {
|
|
346
|
+
return sink[sink.length - 1]?.state;
|
|
347
|
+
},
|
|
348
|
+
waitFor: async (state: Invitation.State): Promise<void> => {
|
|
349
|
+
if (sink[sink.length - 1]?.state === state) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
const sliceStart = sink.length;
|
|
353
|
+
await waitForCondition({
|
|
354
|
+
condition: () => hasState(sliceStart, state),
|
|
355
|
+
});
|
|
356
|
+
},
|
|
357
|
+
} as any;
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
const createInvitation = async (setup: PeerSetup, options?: Partial<Invitation>): Promise<Invitation> => {
|
|
361
|
+
const observable = await setup.peer.invitationsManager.createInvitation({
|
|
362
|
+
type: Invitation.Type.DELEGATED,
|
|
363
|
+
kind: Invitation.Kind.SPACE,
|
|
364
|
+
authMethod: Invitation.AuthMethod.SHARED_SECRET,
|
|
365
|
+
spaceKey: setup.spaceKey,
|
|
366
|
+
multiUse: false,
|
|
367
|
+
...options,
|
|
368
|
+
});
|
|
369
|
+
// cancel to avoid interfering with invitations-handler direct invocations
|
|
370
|
+
const invitation = observable.get();
|
|
371
|
+
await setup.peer.invitationsManager.cancelInvitation(invitation);
|
|
372
|
+
return { ...invitation, swarmKey: PublicKey.random() };
|
|
373
|
+
};
|
|
374
|
+
},
|
|
375
|
+
);
|