uncloud-p2p 1.0.2 → 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 CHANGED
@@ -1 +1,3 @@
1
- # uncloud-p2p
1
+ # uncloud-p2p
2
+
3
+ https://uncloud.mpsoftware.io/docs/uncloud-p2p coming soon
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
  /**
package/dist/index.js CHANGED
@@ -23,6 +23,7 @@ class UncloudP2P {
23
23
  this.localCandidates = [];
24
24
  this.pendingRequests = new Map();
25
25
  this.pendingDownloads = new Map();
26
+ this.token = "";
26
27
  this.options = {
27
28
  baseUrl: "https://uncloud.mpsoftware.io",
28
29
  iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
@@ -70,6 +71,11 @@ class UncloudP2P {
70
71
  };
71
72
  const offer = await this.pc.createOffer();
72
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
+ });
73
79
  }
74
80
  catch (err) {
75
81
  this.handleError(err);
@@ -85,6 +91,7 @@ class UncloudP2P {
85
91
  const payload = {
86
92
  sdp: this.pc.localDescription.sdp,
87
93
  candidates: this.localCandidates,
94
+ token: "", // we get this back from the node
88
95
  };
89
96
  try {
90
97
  const endpoint = `${this.options.baseUrl}/api/public/node/${this.options.nodeId}/exchangeCandidates`;
@@ -96,6 +103,7 @@ class UncloudP2P {
96
103
  if (!response.ok)
97
104
  throw new Error(`HTTP error! status: ${response.status}`);
98
105
  const nodeResponse = await response.json();
106
+ this.token = nodeResponse.token;
99
107
  await this.pc.setRemoteDescription(new RTCSessionDescription({ type: "answer", sdp: nodeResponse.sdp }));
100
108
  for (const cand of nodeResponse.candidates) {
101
109
  await this.pc.addIceCandidate(cand);
@@ -141,7 +149,16 @@ class UncloudP2P {
141
149
  pending.reject(response.error);
142
150
  }
143
151
  else {
144
- pending.resolve(response.body);
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
+ }
145
162
  }
146
163
  return;
147
164
  }
@@ -215,7 +232,7 @@ class UncloudP2P {
215
232
  this.pendingDownloads.set(fileName, { resolve, reject });
216
233
  try {
217
234
  // 2. Trigger the request
218
- 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, {
219
236
  name: fileName,
220
237
  });
221
238
  }
@@ -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 = {
@@ -41,4 +41,5 @@ exports.Actions = {
41
41
  CLIENT_TO_NODE_FILE_TRANSFER: "client_to_node_file_transfer",
42
42
  NODE_TO_CLIENT_FILE_TRANSFER: "node_to_client_file_transfer",
43
43
  CLIENT_TO_NODE_LIST_FILES: "client_to_node_list_files",
44
+ SET_TOKEN: "set_token",
44
45
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uncloud-p2p",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Public Peer to Peer File Reader",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",