Comparing version 1.1.1 to 1.1.2
114
lib/geo.js
"use strict"; | ||
const childProcess = require("child_process"); | ||
function closeArgsToError (code, signal) { | ||
const regKey = "\\Control Panel\\International\\Geo"; | ||
const valueName = "Nation"; | ||
const currGeo = "HKCU" + regKey; | ||
const defaultGeo = "HKU\\.DEFAULT" + regKey; | ||
function closeArgsToError (code, stdout, stderr) { | ||
// code === null when child_process is killed | ||
if (code) { | ||
const err = new Error(`Exited with status ${code}`); | ||
if (stderr) { | ||
stderr = stderr.replace(/^ERROR:\s*/, ""); | ||
} else { | ||
stderr = "Exited with status " + code; | ||
} | ||
const err = new Error(stderr); | ||
err.stdout = stdout; | ||
err.exitStatus = code; | ||
@@ -30,19 +41,18 @@ return err; | ||
const callback = args.pop(); | ||
let cp; | ||
try { | ||
cp = childProcess.spawn.apply(childProcess, args); | ||
} catch (ex) { | ||
return callback(ex); | ||
} | ||
const stderr = prepareStream(cp.stderr); | ||
const cp = childProcess.spawn.apply(childProcess, args); | ||
let error; | ||
cp.on("error", (err) => { | ||
error = err; | ||
callback(error); | ||
}); | ||
let stderr = prepareStream(cp.stderr); | ||
let stdout = prepareStream(cp.stdout); | ||
cp.once("exit", code => { | ||
const error = closeArgsToError(code); | ||
stdout = concatBuffer(stdout); | ||
if (error) { | ||
error.stderr = concatBuffer(stderr); | ||
error.stdout = stdout; | ||
return; | ||
} | ||
callback(error || null, stdout); | ||
stdout = concatBuffer(stdout); | ||
stderr = concatBuffer(stderr); | ||
callback(closeArgsToError(code, stdout, stderr), stdout); | ||
}); | ||
@@ -53,21 +63,27 @@ } | ||
const args = Array.from(arguments); | ||
const callback = args.pop(); | ||
let cp; | ||
try { | ||
cp = childProcess.spawnSync.apply(childProcess, args); | ||
} catch (ex) { | ||
return callback(ex); | ||
} | ||
const stdout = cp.stdout && cp.stdout.toString(); | ||
const error = closeArgsToError(cp.status); | ||
if (error) { | ||
error.stderr = cp.stderr && cp.stderr.toString(); | ||
error.stdout = stdout; | ||
} | ||
callback(error || null, stdout); | ||
const last = args.length - 1; | ||
const callback = args[last]; | ||
args[last] = { | ||
encoding: "utf8", | ||
}; | ||
const cp = childProcess.spawnSync.apply(childProcess, args); | ||
callback(cp.error || closeArgsToError(cp.status, cp.stdout, cp.stderr), cp.stdout); | ||
} | ||
function geo (spawn) { | ||
function regQuery (KeyName, ValueName, callback) { | ||
function queryRegFromPowershell (KeyName, valueName, callback) { | ||
spawn("powershell.exe", [ | ||
"-NoProfile", | ||
"-ExecutionPolicy", | ||
"-Command", | ||
`& {(Get-ItemProperty -Path "Registry::${KeyName}" -Name ${valueName}).${valueName}}`, | ||
], (error, stdout) => { | ||
if (stdout && /^(\d+)(?:\r?\n)*$/.test(stdout)) { | ||
callback(null, RegExp.$1); | ||
} else { | ||
callback(error || stdout); | ||
} | ||
}); | ||
} | ||
function queryRegFromRegExe (KeyName, valueName, callback) { | ||
const args = [ | ||
@@ -77,3 +93,3 @@ "QUERY", | ||
"/v", | ||
ValueName, | ||
valueName, | ||
]; | ||
@@ -92,18 +108,26 @@ if (/64$/.test(process.env.PROCESSOR_ARCHITEW6432 || process.arch)) { | ||
function getNation (parentError, callback) { | ||
regQuery((parentError ? "HKU\\.DEFAULT" : "HKCU") + "\\Control Panel\\International\\Geo", "Nation", (error, nation) => { | ||
if (!error && nation) { | ||
callback(null, nation); | ||
} else if (parentError) { | ||
callback(parentError); | ||
} else { | ||
getNation(error, callback); | ||
} | ||
}); | ||
function getNation (callback) { | ||
const opts = [ | ||
queryRegFromRegExe.bind(this, currGeo), | ||
queryRegFromPowershell.bind(this, currGeo), | ||
queryRegFromRegExe.bind(this, defaultGeo), | ||
queryRegFromPowershell.bind(this, defaultGeo), | ||
]; | ||
function loop () { | ||
opts.shift()(valueName, (error, nation) => { | ||
if (error) { | ||
if (opts.length) { | ||
loop(); | ||
} else { | ||
callback(error); | ||
} | ||
} else { | ||
callback(null, nation); | ||
} | ||
}); | ||
} | ||
loop(); | ||
} | ||
function geo (callback) { | ||
getNation(null, callback); | ||
} | ||
return geo; | ||
return getNation; | ||
} | ||
@@ -110,0 +134,0 @@ |
{ | ||
"name": "in-gfw", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Identify current location is located in mainland China.", | ||
@@ -55,16 +55,16 @@ "main": "lib/index.js", | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-register": "^6.26.0", | ||
"babel-runtime": "^6.26.0", | ||
"ci-info": "^1.1.3", | ||
"codecov": "^3.0.1", | ||
"codecov": "^3.0.2", | ||
"eslint": "^4.19.1", | ||
"eslint-config-standard": "^11.0.0", | ||
"eslint-plugin-import": "^2.11.0", | ||
"eslint-plugin-import": "^2.12.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
"eslint-plugin-promise": "^3.7.0", | ||
"eslint-plugin-promise": "^3.8.0", | ||
"eslint-plugin-standard": "^3.1.0", | ||
"mocha": "^5.1.1", | ||
"mocha": "^5.2.0", | ||
"mock-fs": "^4.5.0", | ||
"nyc": "^11.7.1", | ||
"nyc": "^12.0.2", | ||
"proxyquire": "^2.0.1", | ||
@@ -71,0 +71,0 @@ "util-promisify": "^2.1.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13170
369