-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
46 lines (42 loc) · 1.18 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
const canvas = require('canvas');
const imageType = require('image-type');
function overrideWindow() {
const doc = {};
doc.createElement = function createElement(tag) {
return (tag !== 'canvas')
? false
: new canvas();
};
return {
document: doc
};
}
function overrideFileReader() {
const FileReader = function FileReader() { };
FileReader.prototype.readAsDataURL = function readAsDataURL(buffer) {
this.onload({
target: {
result: "data:image/" + imageType(buffer) + ";base64," + buffer.toString('base64') + ""
}
});
};
return FileReader;
}
function overrideImage() {
const Image = canvas.Image;
Image.prototype.setAttribute = function setAttribute() { };
return Image;
}
module.exports = function exports() {
if (typeof window === 'undefined') {
global.window = overrideWindow();
}
if (typeof FileReader === 'undefined') {
global.FileReader = overrideFileReader();
}
if (typeof Image === 'undefined') {
global.Image = overrideImage();
}
const resemblejs = require('resemblejs'); // eslint-disable-line global-require
return resemblejs.apply(this, arguments); // eslint-disable-line prefer-rest-params
};