-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapi.js
263 lines (229 loc) · 7.91 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
var peer5_options =
{
downloadMode:”hybrid” or "p2p" or "http". default “hybrid”;
}
*/
peer5.Request = function(peer5_options){
peer5.Request.prototype = {
/* -- Attributes -- */
/*
The readyState attribute must return the current state, which must be one of the following:
UNSENT = 0 - Object has been constructed
OPENED = 1 - The method open() has been properly envoked
HEADERS_RECEIVED = 2 - All http headers and p2p fileInfo has been received
LOADING = 3 - The response is being received
DONE = 4 - The data transfer has been completed
*/
this.readyState;
/*
The response attribute returns a reference to the response entity body
if responseType is "blobUrl"
return the blobUrl of the blob response entity body
*/
this.response;
/*
Returns the response type
Values are: empty string (default) - which responds with text,"text","arraybuffer","blob","blobUrl".
*/
this.responseType;
/*
Warning:Not implemented
*/
this.timeout;
/*
Returns the current status of the request
If an error occured returns:
peer5.Request.INVALID_RESPONSETYPE = 669;
peer5.Request.SWARMID_NOT_FOUND_ERR = 650;
peer5.Request.FILE_SIZE_ERR = 640;
peer5.Request.FIREFOX_ONLY_SWARM_ERR = 641;
peer5.Request.CHROME_ONLY_SWARM_ERR = 642;
peer5.Request.BROWSER_SWARM_COMPAT_ERR = 643;
peer5.Request.OUT_OF_SPACE_ERR = 644;
peer5.Request.WRITE_ERR = 645;
peer5.Request.VERIFICATION_ERR = 646;
or HTTP status codes
*/
this.status;
/* -- Methods -- */
/*
id - either http url for uploading/downloading to/from a server,
or a swarmId for p2p only case
method: “GET” / “POST”
e.g: peer5Request.open(“GET”,”images.google.com/img1.jpg”)
*/
open: function(method, id) {},
/*
Warning: Not implemented
e.g: (“range”,”5-10”)
*/
setRequestHeader:function(header, value) {},
/*
sends the request and activates the peer5 process
e.g: peer5Request.send()
*/
send: function() {},
/*
Available only when state >= 2
content-length,content-range, content-disposition,last-modified
see http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses
*/
getResponseHeader:function(){},
/*
Available only when state >=2
content-length,content-range, content-disposition,last-modified
see http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses
*/
getAllResponseHeaders:function(){},
/*
Available only when state >=2
returns: fileInfo object
*/
getFileInfo:function(){},
/*
This method cancels the download, clears all connections, memory and objects
offline storage remains
options =
{
leaveSwarm:true or false. default false;
}
For example:
to pause: keeps the connections, keeps the memory/objects, keeps offline storage call abort()
to stop: terminates connections, memory/objects call abort({leaveSwarm:true})
to resume in either case create a new request for the same swarmId/url.
This method triggers the events onabort(),onloadend()
*/
abort: function() {},
/* -- EVENTS --
These events are null and the user needs to set (“listen” to) them.
*/
/*
Adds an event listener by the name specified in eventName with value cb.
The event that will be triggered will be of the type listened to. e.g: 'load' event will be triggered with addEventListener:function('load',onloadevent)
*/
addEventListener: function(eventName,cb),
/*
Dispatched when the readyState attribute is changed
input: event
*/
onreadystatechange: function(e) {},
/*
Dispatched once when the request starts
input: progress event
*/
onloadstart: function(e) {},
/*
Dispatched while sending and loading data: each time a p2p block is received and verified or bubbles up xhr’s onprogress
input: progress event
*/
onprogress: function(e) {},
/*
Dispatched when request was aborted, e.g: envoking abort()
input: progress event
*/
onabort: function(e) {},
/*
Dispatched when there was an error in the request which prevents it to continue.
e.g: size of resource is too large, browser unsupported, CORS error, HTTP errors
The status attribute returns the error number
input: progress event
*/
onerror:function(e) {},
/*
Dispatched when request was successfully completed
input: progress event
*/
onload:function(e) {},
/*
Warning: not implemented
When a ‘timeout attribute’ amount of time has passed before the request could complete
*/
ontimeout:function(e) {},
/*
Dispatched when request was completed (with or without success)
input: progress event
*/
onloadend:function(e) {},
/*
Dispatched when one of the parameters described in the event are changed
input: swarm state event
*/
onswarmstatechange:function(e){}
}
}
/* -- Objects -- */
event =
{
currentTarget:peer5RequestObject
}
/*
Inherits from event
*/
progressEvent =
{
loaded:123, //number of bytes transfered
loadedHTTP:23, //number of bytes transfered via HTTP
loadedP2P:100, //number of bytes transfered via WebRTC
loadedFS:39, //number of bytes loaded from offline storage
lengthComputable:true, //If the length of the content is known, attribute is set to true
total:1234, //If lengthComputable is true, total is set to content length
}
swarmStateEvent =
{
numOfPeers:7 //number of peers connected to the client
uploaded:15290 //amount of bytes uploaded by the peer in this swarm
}
fileInfo = {
swarmId:"9t7e8dc1" //An id that uniquely identifies the content in the tracker
}
/* -- Advanced options and methods -- */
/*
peer5.DATACHANNELS_ERROR = 700;
peer5.WEBSOCKETS_ERROR = 701;
peer5.FILESYSTEM_ERROR = 702;
calls the cb with an array of peer5 errors
e.g cb([peer5.DATACHANNELS_ERROR,peer5.FILESYSTEM_ERROR]);
*/
peer5.getCompatabilityStatus = function(cb){};
/*
Warning: not impelemented
only the options that are specified will be overwritten
options:
{
inMemory:true or false; if set to true, Peer5 will not use any available disk but only RAM. Suitable for small files mostly. default false.
spaceReqPrompt:true or false; - if set to true, when needed will request authorization to use diskspace, else size limit, firefox = 250MB, chrome = 10% free diskspace. default true.
downLinkLimit:#; in bytes/seconds
upLinkLimit:#; in bytes/seconds
}
*/
peer5.setGlobalOptions = function(options) {};
/*
Warning: not implemented
Resource =
{
fileSize
swarmId
name?
numOfBlocks
numOfBlocksDownloaded
status? (play,pause,stop)
}
resourceId - Either swarmId or url used when creating the resource
*/
peer5.removeResource(resourceId)
/*
Warning: not implemented
resources =
{
resourceId:Resource
}
cb(resources)
*/
peer5.getAllLocalResources(cb)
/*
Warning: not implemented
resourceId - Either swarmId or url used when creating the resource
cb(resources) - only 1 resourceId in the resources object
*/
peer5.getLocalResource(resourceId,cb)