Skip to content

Commit 6b38e52

Browse files
committed
Remove the meta dependency from the fetch plugin.
1 parent 95b4694 commit 6b38e52

File tree

3 files changed

+58
-59
lines changed

3 files changed

+58
-59
lines changed

js/load-image-fetch.js

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
'use strict'
1616
if (typeof define === 'function' && define.amd) {
1717
// Register as an anonymous AMD module:
18-
define(['./load-image', './load-image-meta'], factory)
18+
define(['./load-image'], factory)
1919
} else if (typeof module === 'object' && module.exports) {
20-
factory(require('./load-image'), require('./load-image-meta'))
20+
factory(require('./load-image'))
2121
} else {
2222
// Browser globals:
2323
factory(window.loadImage)
@@ -27,49 +27,39 @@
2727

2828
if (typeof fetch !== 'undefined' && typeof Request !== 'undefined') {
2929
loadImage.fetchBlob = function (url, callback, options) {
30-
if (loadImage.hasMetaOption(options)) {
31-
fetch(new Request(url, options))
32-
.then(function (response) {
33-
return response.blob()
34-
})
35-
.then(callback)
36-
.catch(function (err) {
37-
console.log(err) // eslint-disable-line no-console
38-
callback()
39-
})
40-
} else {
41-
callback()
42-
}
30+
fetch(new Request(url, options))
31+
.then(function (response) {
32+
return response.blob()
33+
})
34+
.then(callback)
35+
.catch(function (err) {
36+
callback(null, err)
37+
})
4338
}
4439
} else if (
4540
// Check for XHR2 support:
4641
typeof XMLHttpRequest !== 'undefined' &&
4742
typeof ProgressEvent !== 'undefined'
4843
) {
4944
loadImage.fetchBlob = function (url, callback, options) {
50-
if (loadImage.hasMetaOption(options)) {
51-
// eslint-disable-next-line no-param-reassign
52-
options = options || {}
53-
var req = new XMLHttpRequest()
54-
req.open(options.method || 'GET', url)
55-
if (options.headers) {
56-
Object.keys(options.headers).forEach(function (key) {
57-
req.setRequestHeader(key, options.headers[key])
58-
})
59-
}
60-
req.withCredentials = options.credentials === 'include'
61-
req.responseType = 'blob'
62-
req.onload = function () {
63-
callback(req.response)
64-
}
65-
req.onerror = req.onabort = req.ontimeout = function (e) {
66-
console.log(e) // eslint-disable-line no-console
67-
callback()
68-
}
69-
req.send(options.body)
70-
} else {
71-
callback()
45+
// eslint-disable-next-line no-param-reassign
46+
options = options || {}
47+
var req = new XMLHttpRequest()
48+
req.open(options.method || 'GET', url)
49+
if (options.headers) {
50+
Object.keys(options.headers).forEach(function (key) {
51+
req.setRequestHeader(key, options.headers[key])
52+
})
53+
}
54+
req.withCredentials = options.credentials === 'include'
55+
req.responseType = 'blob'
56+
req.onload = function () {
57+
callback(req.response)
58+
}
59+
req.onerror = req.onabort = req.ontimeout = function (err) {
60+
callback(null, err)
7261
}
62+
req.send(options.body)
7363
}
7464
}
7565
})

js/load-image-meta.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,6 @@
179179
)
180180
}
181181

182-
// Determines if meta data should be loaded automatically:
183-
loadImage.hasMetaOption = function (options) {
184-
return options && options.meta
185-
}
186-
187182
var originalTransform = loadImage.transform
188183
loadImage.transform = function (img, options, callback, file, data) {
189184
if (loadImage.hasMetaOption(options)) {

js/load-image.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,38 @@
2727
function loadImage(file, callback, options) {
2828
var img = document.createElement('img')
2929
var url
30+
/**
31+
* Callback for the fetchBlob call.
32+
*
33+
* @param {Blob} blob Blob object
34+
* @param {Error} err Error object
35+
*/
36+
function fetchBlobCallback(blob, err) {
37+
if (err) console.log(err) // eslint-disable-line no-console
38+
if (blob && loadImage.isInstanceOf('Blob', blob)) {
39+
// eslint-disable-next-line no-param-reassign
40+
file = blob
41+
url = loadImage.createObjectURL(file)
42+
} else {
43+
url = file
44+
if (options && options.crossOrigin) {
45+
img.crossOrigin = options.crossOrigin
46+
}
47+
}
48+
img.src = url
49+
}
3050
img.onerror = function (event) {
3151
return loadImage.onerror(img, event, file, url, callback, options)
3252
}
3353
img.onload = function (event) {
3454
return loadImage.onload(img, event, file, url, callback, options)
3555
}
3656
if (typeof file === 'string') {
37-
loadImage.fetchBlob(
38-
file,
39-
function (blob) {
40-
if (blob && loadImage.isInstanceOf('Blob', blob)) {
41-
// eslint-disable-next-line no-param-reassign
42-
file = blob
43-
url = loadImage.createObjectURL(file)
44-
} else {
45-
url = file
46-
if (options && options.crossOrigin) {
47-
img.crossOrigin = options.crossOrigin
48-
}
49-
}
50-
img.src = url
51-
},
52-
options
53-
)
57+
if (loadImage.hasMetaOption(options)) {
58+
loadImage.fetchBlob(file, fetchBlobCallback, options)
59+
} else {
60+
fetchBlobCallback()
61+
}
5462
return img
5563
} else if (
5664
loadImage.isInstanceOf('Blob', file) ||
@@ -92,6 +100,12 @@
92100
}
93101
}
94102

103+
// Determines if meta data should be loaded automatically.
104+
// Requires the load image meta extension to load meta data.
105+
loadImage.hasMetaOption = function (options) {
106+
return options && options.meta
107+
}
108+
95109
// If the callback given to this function returns a blob, it is used as image
96110
// source instead of the original url and overrides the file argument used in
97111
// the onload and onerror event callbacks:

0 commit comments

Comments
 (0)