Skip to content

Commit c5e5455

Browse files
committed
Document file transfer javascript.
1 parent dcb3706 commit c5e5455

File tree

1 file changed

+107
-24
lines changed

1 file changed

+107
-24
lines changed

static/protocol.js

+107-24
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,18 @@ function ServerConnection() {
203203
*/
204204
this.onusermessage = null;
205205
/**
206+
* The set of files currently being transferred.
207+
*
206208
* @type {Object<string,TransferredFile>}
207209
*/
208210
this.transferredFiles = {};
209211
/**
212+
* onfiletransfer is called whenever a peer offers a file transfer.
213+
*
214+
* If the transfer is accepted, it should set up the file transfer
215+
* callbacks and return immediately. It may also throw an exception
216+
* in order to reject the file transfer.
217+
*
210218
* @type {(this: ServerConnection, f: TransferredFile) => void}
211219
*/
212220
this.onfiletransfer = null;
@@ -1466,6 +1474,12 @@ Stream.prototype.setStatsInterval = function(ms) {
14661474
* A file in the process of being transferred.
14671475
* These are stored in the ServerConnection.transferredFiles dictionary.
14681476
*
1477+
* State transitions:
1478+
* @example
1479+
* '' -> inviting -> connecting -> connected -> done -> closed
1480+
* any -> cancelled -> closed
1481+
*
1482+
*
14691483
* @parm {ServerConnection} sc
14701484
* @parm {string} userid
14711485
* @parm {string} rid
@@ -1474,47 +1488,109 @@ Stream.prototype.setStatsInterval = function(ms) {
14741488
* @parm {string} mimetype
14751489
* @parm {number} size
14761490
* @constructor
1477-
*
1478-
* State transitions:
1479-
*
1480-
* '' -> inviting -> connecting -> connected -> done -> closed
1481-
* any -> cancelled -> closed
1482-
*
14831491
*/
14841492
function TransferredFile(sc, userid, id, up, username, name, mimetype, size) {
1485-
/** @type {ServerConnection} */
1493+
/**
1494+
* The server connection this file is associated with.
1495+
*
1496+
* @type {ServerConnection}
1497+
*/
14861498
this.sc = sc;
1487-
/** @type {string} */
1499+
/** The id of the remote peer.
1500+
*
1501+
* @type {string}
1502+
*/
14881503
this.userid = userid;
1489-
/** @type {string} */
1504+
/**
1505+
* The id of this file transfer.
1506+
*
1507+
* @type {string}
1508+
*/
14901509
this.id = id;
1491-
/** @type {boolean} */
1510+
/**
1511+
* True if this is an upload.
1512+
*
1513+
* @type {boolean}
1514+
*/
14921515
this.up = up;
1493-
/** @type {string} */
1516+
/**
1517+
* The state of this file transfer. See the description of the
1518+
* constructor for possible state transitions.
1519+
*
1520+
* @type {string}
1521+
*/
14941522
this.state = '';
1495-
/** @type {string} */
1523+
/**
1524+
* The username of the remote peer.
1525+
*
1526+
* @type {string}
1527+
*/
14961528
this.username = username;
1497-
/** @type {string} */
1529+
/**
1530+
* The name of the file being transferred.
1531+
*
1532+
* @type {string}
1533+
*/
14981534
this.name = name;
1499-
/** @type {string} */
1535+
/**
1536+
* The MIME type of the file being transferred.
1537+
*
1538+
* @type {string}
1539+
*/
15001540
this.mimetype = mimetype;
1501-
/** @type {number} */
1541+
/**
1542+
* The size in bytes of the file being transferred.
1543+
*
1544+
* @type {number}
1545+
*/
15021546
this.size = size;
1503-
/** @type {File} */
1547+
/**
1548+
* The file being uploaded. Unused for downloads.
1549+
*
1550+
* @type {File}
1551+
*/
15041552
this.file = null;
1505-
/** @type {boolean} */
1506-
this.closed = false;
1507-
/** @type {RTCPeerConnection} */
1553+
/**
1554+
* The peer connection used for the transfer.
1555+
*
1556+
* @type {RTCPeerConnection}
1557+
*/
15081558
this.pc = null;
1509-
/** @type {RTCDataChannel} */
1559+
/**
1560+
* The datachannel used for the transfer.
1561+
*
1562+
* @type {RTCDataChannel}
1563+
*/
15101564
this.dc = null;
1511-
/** @type {Array<RTCIceCandidateInit>} */
1565+
/**
1566+
* Buffered remote ICE candidates.
1567+
*
1568+
* @type {Array<RTCIceCandidateInit>}
1569+
*/
15121570
this.candidates = [];
1513-
/** @type {Array<Blob|ArrayBuffer>} */
1571+
/**
1572+
* The data received to date, stored as a list of blobs or array buffers,
1573+
* depending on what the browser supports.
1574+
*
1575+
* @type {Array<Blob|ArrayBuffer>}
1576+
*/
15141577
this.data = [];
1515-
/** @type {number} */
1578+
/**
1579+
* The total size of the data received to date.
1580+
*
1581+
* @type {number}
1582+
*/
15161583
this.datalen = 0;
1517-
/** @type {(this: TransferredFile, type: string, [data]: string) => void} */
1584+
/**
1585+
* The main filetransfer callback.
1586+
*
1587+
* This is called whenever the state of the transfer changes,
1588+
* but may also be called multiple times in a single state, for example
1589+
* in order to display a progress bar. Call this.cancel in order
1590+
* to cancel the transfer.
1591+
*
1592+
* @type {(this: TransferredFile, type: string, [data]: string) => void}
1593+
*/
15181594
this.onevent = null;
15191595
}
15201596

@@ -1903,6 +1979,8 @@ TransferredFile.prototype.send = async function() {
19031979
}
19041980

19051981
/**
1982+
* Called after we receive an answer. Don't call this.
1983+
*
19061984
* @param {string} sdp
19071985
*/
19081986
TransferredFile.prototype.receiveFile = async function(sdp) {
@@ -1917,6 +1995,8 @@ TransferredFile.prototype.receiveFile = async function(sdp) {
19171995
}
19181996

19191997
/**
1998+
* Called whenever we receive a chunk of data. Don't call this.
1999+
*
19202000
* @param {Blob|ArrayBuffer} data
19212001
*/
19222002
TransferredFile.prototype.receiveData = async function(data) {
@@ -1957,6 +2037,9 @@ TransferredFile.prototype.receiveData = async function(data) {
19572037
}
19582038

19592039
/**
2040+
* fileTransfer handles a usermessage of kind 'filetransfer'. Don't call
2041+
* this, it is called automatically as needed.
2042+
*
19602043
* @param {string} id
19612044
* @param {string} username
19622045
* @param {object} message

0 commit comments

Comments
 (0)