uncloud-p2p 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +36 -2
- package/dist/uncloudproto.d.ts +1 -0
- package/dist/uncloudproto.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export interface IceCandidate {
|
|
|
7
7
|
export interface IceExchangePayload {
|
|
8
8
|
sdp: string;
|
|
9
9
|
candidates: IceCandidate[];
|
|
10
|
+
token: string;
|
|
10
11
|
}
|
|
11
12
|
export type ConnectionState = "idle" | "gathering" | "signaling" | "connecting" | "connected" | "disconnected" | "error";
|
|
12
13
|
interface P2POptions {
|
|
@@ -24,6 +25,7 @@ export declare class UncloudP2P {
|
|
|
24
25
|
private options;
|
|
25
26
|
private pendingRequests;
|
|
26
27
|
private pendingDownloads;
|
|
28
|
+
private token;
|
|
27
29
|
constructor(options: P2POptions);
|
|
28
30
|
private log;
|
|
29
31
|
/**
|
|
@@ -48,4 +50,5 @@ export declare class UncloudP2P {
|
|
|
48
50
|
private handleError;
|
|
49
51
|
disconnect(): void;
|
|
50
52
|
}
|
|
51
|
-
export
|
|
53
|
+
export * from "./uncloudproto";
|
|
54
|
+
export * from "./files";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
17
|
exports.UncloudP2P = void 0;
|
|
4
18
|
const uncloudproto_1 = require("./uncloudproto");
|
|
@@ -9,6 +23,7 @@ class UncloudP2P {
|
|
|
9
23
|
this.localCandidates = [];
|
|
10
24
|
this.pendingRequests = new Map();
|
|
11
25
|
this.pendingDownloads = new Map();
|
|
26
|
+
this.token = "";
|
|
12
27
|
this.options = {
|
|
13
28
|
baseUrl: "https://uncloud.mpsoftware.io",
|
|
14
29
|
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
|
|
@@ -56,6 +71,11 @@ class UncloudP2P {
|
|
|
56
71
|
};
|
|
57
72
|
const offer = await this.pc.createOffer();
|
|
58
73
|
await this.pc.setLocalDescription(offer);
|
|
74
|
+
await this.waitForOpen();
|
|
75
|
+
// finally, set our token so the node knows what grants we were given from the p2p public link
|
|
76
|
+
const resp = await this.sendCommand(uncloudproto_1.Actions.SET_TOKEN, {
|
|
77
|
+
token: this.token,
|
|
78
|
+
});
|
|
59
79
|
}
|
|
60
80
|
catch (err) {
|
|
61
81
|
this.handleError(err);
|
|
@@ -71,6 +91,7 @@ class UncloudP2P {
|
|
|
71
91
|
const payload = {
|
|
72
92
|
sdp: this.pc.localDescription.sdp,
|
|
73
93
|
candidates: this.localCandidates,
|
|
94
|
+
token: "", // we get this back from the node
|
|
74
95
|
};
|
|
75
96
|
try {
|
|
76
97
|
const endpoint = `${this.options.baseUrl}/api/public/node/${this.options.nodeId}/exchangeCandidates`;
|
|
@@ -82,6 +103,7 @@ class UncloudP2P {
|
|
|
82
103
|
if (!response.ok)
|
|
83
104
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
84
105
|
const nodeResponse = await response.json();
|
|
106
|
+
this.token = nodeResponse.token;
|
|
85
107
|
await this.pc.setRemoteDescription(new RTCSessionDescription({ type: "answer", sdp: nodeResponse.sdp }));
|
|
86
108
|
for (const cand of nodeResponse.candidates) {
|
|
87
109
|
await this.pc.addIceCandidate(cand);
|
|
@@ -127,7 +149,16 @@ class UncloudP2P {
|
|
|
127
149
|
pending.reject(response.error);
|
|
128
150
|
}
|
|
129
151
|
else {
|
|
130
|
-
|
|
152
|
+
// check if body is 1 field and is an error field
|
|
153
|
+
if (response.body &&
|
|
154
|
+
typeof response.body === "object" &&
|
|
155
|
+
"error" in response.body &&
|
|
156
|
+
Object.keys(response.body).length === 1) {
|
|
157
|
+
pending.reject(response.body.error);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
pending.resolve(response.body);
|
|
161
|
+
}
|
|
131
162
|
}
|
|
132
163
|
return;
|
|
133
164
|
}
|
|
@@ -201,7 +232,7 @@ class UncloudP2P {
|
|
|
201
232
|
this.pendingDownloads.set(fileName, { resolve, reject });
|
|
202
233
|
try {
|
|
203
234
|
// 2. Trigger the request
|
|
204
|
-
await this.sendCommand(uncloudproto_1.Actions.NODE_TO_CLIENT_FILE_TRANSFER, {
|
|
235
|
+
const resp = await this.sendCommand(uncloudproto_1.Actions.NODE_TO_CLIENT_FILE_TRANSFER, {
|
|
205
236
|
name: fileName,
|
|
206
237
|
});
|
|
207
238
|
}
|
|
@@ -255,3 +286,6 @@ class UncloudP2P {
|
|
|
255
286
|
}
|
|
256
287
|
}
|
|
257
288
|
exports.UncloudP2P = UncloudP2P;
|
|
289
|
+
// src/index.ts
|
|
290
|
+
__exportStar(require("./uncloudproto"), exports);
|
|
291
|
+
__exportStar(require("./files"), exports);
|
package/dist/uncloudproto.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export declare const Actions: {
|
|
|
24
24
|
CLIENT_TO_NODE_FILE_TRANSFER: string;
|
|
25
25
|
NODE_TO_CLIENT_FILE_TRANSFER: string;
|
|
26
26
|
CLIENT_TO_NODE_LIST_FILES: string;
|
|
27
|
+
SET_TOKEN: string;
|
|
27
28
|
};
|
|
28
29
|
export type Action = (typeof Actions)[keyof typeof Actions];
|
|
29
30
|
export type PendingRequest = {
|
package/dist/uncloudproto.js
CHANGED