forked from zhangyuanwei/node-images
-
Notifications
You must be signed in to change notification settings - Fork 5
/
binding.js
61 lines (47 loc) · 1.54 KB
/
binding.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
'use strict';
var fs = require('fs');
var buildModule = __dirname + '/build/Release/binding.node';
if (fs.existsSync(buildModule)) {
try {
module.exports = require(buildModule);
} catch (e) {
console.log('Cant\'t load `.node` module ' + buildModule);
throw e;
}
return;
}
function compiler(a, b) {
if (!/^(?:\d+.?)+$/.test(a) || !/^(?:\d+.?)+/.test(b)) {
return a > b;
}
var aArr = a.split('.');
var bArr = b.split('.');
var max = Math.max(aArr.length, bArr.length);
for (var i = 0; i < max; i++) {
if ((aArr[i] && !bArr[i]) || aArr[i] > bArr[i]) {
return 1;
} else if ((!aArr[i] && bArr[i]) || aArr[i] < bArr[i]) {
return -1;
}
}
return 0;
}
var pkgInf = require('./package.json');
var bindingMap = pkgInf.bindingMap;
var bugUrl = pkgInf['bugs'] ? (pkgInf['bugs']['url'] || '') : '';
for ( var i in bindingMap) {
if (bindingMap.hasOwnProperty(i)) {
var target = i;
var versions = bindingMap[i];
var cur = process.versions['node'];
if (compiler(versions[0], cur) <= 0 && compiler(versions[1], cur) >= 0) {
try {
module.exports = require('./bindings/'+ process.platform + '/' + process.arch + '/' + target + '/binding.node');
return;
} catch ( e ) {
throw new Error('Can\'t load the addon. Issue to: ' + bugUrl + ' ' + e.stack);
}
}
}
}
throw new Error('Can\'t load the addon. Issue to: ' + bugUrl);