Comparing version 1.1.2 to 1.2.0
"use strict"; | ||
const os = require("./os"); | ||
const mem = require("mem"); | ||
const net = mem(require("./net")); | ||
module.exports.os = os.async; | ||
module.exports.osSync = os.sync; | ||
module.exports.net = net; | ||
const net = require("./net"); | ||
function inGFW (blockedHost, cnHost) { | ||
return os.async().catch(() => net.async(blockedHost, cnHost)); | ||
}; | ||
inGFW.sync = function (blockedHost, cnHost) { | ||
try { | ||
return os.sync(); | ||
} catch (ex) { | ||
return net.sync(blockedHost, cnHost); | ||
} | ||
}; | ||
inGFW.os = os.async; | ||
inGFW.osSync = os.sync; | ||
inGFW.net = net.async; | ||
inGFW.netSync = net.sync; | ||
module.exports = inGFW; |
"use strict"; | ||
const childProcess = require("child_process"); | ||
const https = require("https"); | ||
const http = require("http"); | ||
const url = require("url"); | ||
const mem = require("mem"); | ||
const syncArgv = "in-gfw-net-sync-argv"; | ||
module.exports = (blockedHost, cnHost) => { | ||
function async (blockedHost, cnHost) { | ||
let reqs = []; | ||
@@ -22,3 +25,3 @@ function head (config) { | ||
method: "HEAD", | ||
}), res => { | ||
}), () => { | ||
resolve(true); | ||
@@ -52,1 +55,61 @@ abort(); | ||
}; | ||
function sync (blockedHost, cnHost) { | ||
const args = [ | ||
__filename, | ||
syncArgv, | ||
].concat( | ||
[ | ||
blockedHost, | ||
cnHost, | ||
].filter(Boolean).map(JSON.stringify) | ||
); | ||
let result; | ||
try { | ||
result = childProcess.execFileSync( | ||
process.execPath, | ||
args, | ||
{ | ||
stdio: "pipe", | ||
encoding: "utf8", | ||
} | ||
); | ||
} catch (ex) { | ||
const errorInfo = JSON.parse(ex.stderr); | ||
const error = new Error(errorInfo.message); | ||
Object.assign( | ||
error, | ||
errorInfo | ||
); | ||
throw error; | ||
} | ||
return JSON.parse( | ||
result | ||
); | ||
} | ||
if ((!process.mainModule || process.mainModule === module) && process.argv[2] === syncArgv) { | ||
async.apply(this, process.argv.slice(3).map(JSON.parse)).then(result => { | ||
process.stdout.write(JSON.stringify(result)); | ||
return 0; | ||
}, error => { | ||
const errorInfo = {}; | ||
[ | ||
"message", | ||
"stack", | ||
"fileName", | ||
"lineNumber", | ||
"columnNumber", | ||
].forEach(prop => { | ||
errorInfo[prop] = error[prop]; | ||
}); | ||
process.stderr.write(JSON.stringify(Object.assign(errorInfo, error))); | ||
return 1; | ||
}).then(code => { | ||
process.exit(code); | ||
}); | ||
} else { | ||
module.exports = { | ||
async: mem(async), | ||
sync: mem(sync), | ||
}; | ||
} |
{ | ||
"name": "in-gfw", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Identify current location is located in mainland China.", | ||
@@ -8,3 +8,3 @@ "main": "lib/index.js", | ||
"require": [ | ||
"babel-register" | ||
"@babel/register" | ||
], | ||
@@ -20,7 +20,7 @@ "reporter": [ | ||
[ | ||
"env" | ||
"@babel/env" | ||
] | ||
], | ||
"plugins": [ | ||
"transform-runtime" | ||
"@babel/transform-runtime" | ||
] | ||
@@ -30,3 +30,3 @@ }, | ||
"report-coverage": "codecov", | ||
"pretest": "eslint *.js lib test", | ||
"pretest": "eslint lib test", | ||
"test": "node . && nyc mocha --no-timeouts" | ||
@@ -57,11 +57,12 @@ }, | ||
"devDependencies": { | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-register": "^6.26.0", | ||
"babel-runtime": "^6.26.0", | ||
"@babel/core": "^7.0.0-beta.51", | ||
"@babel/plugin-transform-runtime": "^7.0.0-beta.51", | ||
"@babel/preset-env": "^7.0.0-beta.51", | ||
"@babel/register": "^7.0.0-beta.51", | ||
"@babel/runtime": "^7.0.0-beta.51", | ||
"ci-info": "^1.1.3", | ||
"codecov": "^3.0.2", | ||
"eslint": "^4.19.1", | ||
"eslint": "^5.0.1", | ||
"eslint-config-standard": "^11.0.0", | ||
"eslint-plugin-import": "^2.12.0", | ||
"eslint-plugin-import": "^2.13.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
@@ -79,4 +80,4 @@ "eslint-plugin-promise": "^3.8.0", | ||
"is-wsl": "^1.1.0", | ||
"mem": "^3.0.0" | ||
"mem": "^3.0.1" | ||
} | ||
} |
@@ -21,7 +21,4 @@ in-gfw | ||
const inGFW = require("in-gfw"); | ||
inGFW().then(console.log); // `true` for located in mainland China | ||
inGFW.os().then(console.log); // `true` for system located in mainland China | ||
``` | ||
```js | ||
const inGFW = require("in-gfw"); | ||
inGFW.net().then(console.log); // `true` for network located in mainland China | ||
@@ -33,2 +30,8 @@ ``` | ||
```js | ||
inGFW(blockedHost, cnHost); | ||
inGFW.sync(blockedHost, cnHost); // Synchronous version of `inGFW()` | ||
``` | ||
Get result by `inGFW.os()` and fallback to `inGFW.net()` | ||
```js | ||
inGFW.os(); | ||
@@ -44,2 +47,3 @@ inGFW.osSync(); // Synchronous version of `inGFW.os()` | ||
inGFW.net(blockedHost, cnHost); | ||
inGFW.netSync(blockedHost, cnHost); // Synchronous version of `inGFW.net()` | ||
``` | ||
@@ -46,0 +50,0 @@ Based on the speed of network access to identify if current location is located in mainland China. |
15073
442
64
18
4
Updatedmem@^3.0.1