load-image-fetch.js
1.8 KB
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
/*
* JavaScript Load Image Fetch
* https://github.com/blueimp/JavaScript-Load-Image
*
* Copyright 2017, Sebastian Tschan
* https://blueimp.net
*
* Licensed under the MIT license:
* https://opensource.org/licenses/MIT
*/
/* global define, module, require */
;(function (factory) {
'use strict'
if (typeof define === 'function' && define.amd) {
// Register as an anonymous AMD module:
define(['./load-image'], factory)
} else if (typeof module === 'object' && module.exports) {
factory(require('./load-image'))
} else {
// Browser globals:
factory(window.loadImage)
}
})(function (loadImage) {
'use strict'
if (typeof fetch !== 'undefined' && typeof Request !== 'undefined') {
loadImage.fetchBlob = function (url, callback, options) {
fetch(new Request(url, options))
.then(function (response) {
return response.blob()
})
.then(callback)
.catch(function (err) {
callback(null, err)
})
}
} else if (
// Check for XHR2 support:
typeof XMLHttpRequest !== 'undefined' &&
typeof ProgressEvent !== 'undefined'
) {
loadImage.fetchBlob = function (url, callback, options) {
// eslint-disable-next-line no-param-reassign
options = options || {}
var req = new XMLHttpRequest()
req.open(options.method || 'GET', url)
if (options.headers) {
Object.keys(options.headers).forEach(function (key) {
req.setRequestHeader(key, options.headers[key])
})
}
req.withCredentials = options.credentials === 'include'
req.responseType = 'blob'
req.onload = function () {
callback(req.response)
}
req.onerror = req.onabort = req.ontimeout = function (err) {
callback(null, err)
}
req.send(options.body)
}
}
})