-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
426 lines (379 loc) · 10 KB
/
index.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* global XMLHttpRequest */
var configParameters = [
'manifest',
'onDone',
'onProgress',
'onError'
]
var manifestParameters = [
'type',
'src',
'stream',
'credentials',
'parser'
]
var parserParameters = [
'onData',
'onDone'
]
var STATE_ERROR = -1
var STATE_DATA = 0
var STATE_COMPLETE = 1
function raise (message) {
throw new Error('resl: ' + message)
}
function checkType (object, parameters, name) {
Object.keys(object).forEach(function (param) {
if (parameters.indexOf(param) < 0) {
raise('invalid parameter "' + param + '" in ' + name)
}
})
}
function Loader (name, cancel) {
this.state = STATE_DATA
this.ready = false
this.progress = 0
this.name = name
this.cancel = cancel
}
module.exports = function resl (config) {
if (typeof config !== 'object' || !config) {
raise('invalid or missing configuration')
}
checkType(config, configParameters, 'config')
var manifest = config.manifest
if (typeof manifest !== 'object' || !manifest) {
raise('missing manifest')
}
function getFunction (name, dflt) {
if (name in config) {
var func = config[name]
if (typeof func !== 'function') {
raise('invalid callback "' + name + '"')
}
return func
}
return null
}
var onDone = getFunction('onDone')
if (!onDone) {
raise('missing onDone() callback')
}
var onProgress = getFunction('onProgress')
var onError = getFunction('onError')
var assets = {}
var state = STATE_DATA
function loadXHR (request) {
var name = request.name
var stream = request.stream
var binary = request.type === 'binary'
var parser = request.parser
var xhr = new XMLHttpRequest()
var asset = null
var loader = new Loader(name, cancel)
if (stream) {
xhr.onreadystatechange = onReadyStateChange
} else {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
onReadyStateChange()
}
}
}
if (binary) {
xhr.responseType = 'arraybuffer'
}
function onReadyStateChange () {
if (xhr.readyState < 2 ||
loader.state === STATE_COMPLETE ||
loader.state === STATE_ERROR) {
return
}
if (xhr.status !== 200) {
return abort('error loading resource "' + request.name + '"')
}
if (xhr.readyState > 2 && loader.state === STATE_DATA) {
var response
if (request.type === 'binary') {
response = xhr.response
} else {
response = xhr.responseText
}
if (parser.data) {
try {
asset = parser.data(response)
} catch (e) {
return abort(e)
}
} else {
asset = response
}
}
if (xhr.readyState > 3 && loader.state === STATE_DATA) {
if (parser.done) {
try {
asset = parser.done()
} catch (e) {
return abort(e)
}
}
loader.state = STATE_COMPLETE
}
assets[name] = asset
loader.progress = 0.75 * loader.progress + 0.25
loader.ready =
(request.stream && !!asset) ||
loader.state === STATE_COMPLETE
notifyProgress()
}
function cancel () {
if (loader.state === STATE_COMPLETE || loader.state === STATE_ERROR) {
return
}
xhr.onreadystatechange = null
xhr.abort()
loader.state = STATE_ERROR
}
// set up request
if (request.credentials) {
xhr.withCredentials = true
}
xhr.open('GET', request.src, true)
xhr.send()
return loader
}
function loadElement (request, element) {
var name = request.name
var parser = request.parser
var loader = new Loader(name, cancel)
var asset = element
function handleProgress () {
if (loader.state === STATE_DATA) {
if (parser.data) {
try {
asset = parser.data(element)
} catch (e) {
return abort(e)
}
} else {
asset = element
}
}
}
function onProgress (e) {
handleProgress()
assets[name] = asset
if (e.lengthComputable) {
loader.progress = Math.max(loader.progress, e.loaded / e.total)
} else {
loader.progress = 0.75 * loader.progress + 0.25
}
notifyProgress(name)
}
function onComplete () {
handleProgress()
if (loader.state === STATE_DATA) {
if (parser.done) {
try {
asset = parser.done()
} catch (e) {
return abort(e)
}
}
loader.state = STATE_COMPLETE
}
loader.progress = 1
loader.ready = true
assets[name] = asset
removeListeners()
notifyProgress('finish ' + name)
}
function onError () {
abort('error loading asset "' + name + '"')
}
if (request.stream) {
element.addEventListener('progress', onProgress)
}
if (request.type === 'image') {
element.addEventListener('load', onComplete)
} else {
var canPlay = false
var loadedMetaData = false
element.addEventListener('loadedmetadata', function () {
loadedMetaData = true
if (canPlay) {
onComplete()
}
})
element.addEventListener('canplay', function () {
canPlay = true
if (loadedMetaData) {
onComplete()
}
})
}
element.addEventListener('error', onError)
function removeListeners () {
if (request.stream) {
element.removeEventListener('progress', onProgress)
}
if (request.type === 'image') {
element.addEventListener('load', onComplete)
} else {
element.addEventListener('canplay', onComplete)
}
element.removeEventListener('error', onError)
}
function cancel () {
if (loader.state === STATE_COMPLETE || loader.state === STATE_ERROR) {
return
}
loader.state = STATE_ERROR
removeListeners()
element.src = ''
}
// set up request
if (request.credentials) {
element.crossOrigin = 'use-credentials'
} else {
element.crossOrigin = 'anonymous'
}
element.src = request.src
return loader
}
var loaders = {
text: loadXHR,
binary: function (request) {
// TODO use fetch API for streaming if supported
return loadXHR(request)
},
image: function (request) {
return loadElement(request, document.createElement('img'))
},
video: function (request) {
return loadElement(request, document.createElement('video'))
},
audio: function (request) {
return loadElement(request, document.createElement('audio'))
}
}
// First we parse all objects in order to verify that all type information
// is correct
var pending = Object.keys(manifest).map(function (name) {
var request = manifest[name]
if (typeof request === 'string') {
request = {
src: request
}
} else if (typeof request !== 'object' || !request) {
raise('invalid asset definition "' + name + '"')
}
checkType(request, manifestParameters, 'asset "' + name + '"')
function getParameter (prop, accepted, init) {
var value = init
if (prop in request) {
value = request[prop]
}
if (accepted.indexOf(value) < 0) {
raise('invalid ' + prop + ' "' + value + '" for asset "' + name + '", possible values: ' + accepted)
}
return value
}
function getString (prop, required, init) {
var value = init
if (prop in request) {
value = request[prop]
} else if (required) {
raise('missing ' + prop + ' for asset "' + name + '"')
}
if (typeof value !== 'string') {
raise('invalid ' + prop + ' for asset "' + name + '", must be a string')
}
return value
}
function getParseFunc (name, dflt) {
if (name in request.parser) {
var result = request.parser[name]
if (typeof result !== 'function') {
raise('invalid parser callback ' + name + ' for asset "' + name + '"')
}
return result
} else {
return dflt
}
}
var parser = {}
if ('parser' in request) {
if (typeof request.parser === 'function') {
parser = {
data: request.parser
}
} else if (typeof request.parser === 'object' && request.parser) {
checkType(request.parser, parserParameters, 'parser for asset "' + name + '"')
if (!('onData' in request.parser)) {
raise('missing onData callback for parser in asset "' + name + '"')
}
parser = {
data: getParseFunc('onData'),
done: getParseFunc('onDone')
}
} else {
raise('invalid parser for asset "' + name + '"')
}
}
return {
name: name,
type: getParameter('type', Object.keys(loaders), 'text'),
stream: !!request.stream,
credentials: !!request.credentials,
src: getString('src', true, ''),
parser: parser
}
}).map(function (request) {
return (loaders[request.type])(request)
})
function abort (message) {
if (state === STATE_ERROR || state === STATE_COMPLETE) {
return
}
state = STATE_ERROR
pending.forEach(function (loader) {
loader.cancel()
})
if (onError) {
if (typeof message === 'string') {
onError(new Error('resl: ' + message))
} else {
onError(message)
}
} else {
console.error('resl error:', message)
}
}
function notifyProgress (message) {
if (state === STATE_ERROR || state === STATE_COMPLETE) {
return
}
var progress = 0
var numReady = 0
pending.forEach(function (loader) {
if (loader.ready) {
numReady += 1
}
progress += loader.progress
})
if (numReady === pending.length) {
state = STATE_COMPLETE
onDone(assets)
} else {
if (onProgress) {
onProgress(progress / pending.length, message)
}
}
}
if (pending.length === 0) {
setTimeout(function () {
notifyProgress('done')
}, 1)
}
}