yandex-speller
Advanced tools
Comparing version 2.1.0 to 2.1.1
108
lib/post.js
'use strict'; | ||
const querystring = require('querystring'); | ||
const https = require('https'); | ||
const http = require('http'); | ||
@@ -9,69 +9,75 @@ const ETIMEDOUT = 'Request timed out.'; | ||
function post(params, callback) { | ||
const form = querystring.stringify(params.form); | ||
const options = { | ||
host: params.host, | ||
path: params.path, | ||
port: 443, | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': Buffer.byteLength(form) | ||
} | ||
}; | ||
const form = querystring.stringify(params.form); | ||
const options = { | ||
host: params.host, | ||
path: params.path, | ||
port: 80, | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': Buffer.byteLength(form) | ||
} | ||
}; | ||
let req = https.request(options, function(res) { | ||
let textBuffer = ''; | ||
let req = http.request(options, function(res) { | ||
let textBuffer = ''; | ||
res.setEncoding('utf8'); | ||
res.setEncoding('utf8'); | ||
res.on('data', function(chunk) { | ||
textBuffer += chunk; | ||
res.on('data', function(chunk) { | ||
textBuffer += chunk; | ||
}); | ||
res.on('end', function() { | ||
let json; | ||
try { | ||
json = JSON.parse(textBuffer); | ||
} catch (e) { | ||
callback( | ||
'Yandex.Speller API:\n-------------------\n' + | ||
'Status code: ' + res.statusCode + '\n' + | ||
'Response: ' + textBuffer + '\n' + | ||
'JSON.parse(Response): ' + e + '\n' | ||
); | ||
return; | ||
} | ||
callback(null, res, json); | ||
}); | ||
}); | ||
res.on('end', function() { | ||
let json; | ||
try { | ||
json = JSON.parse(textBuffer); | ||
} catch (e) { | ||
req.on('error', function(e) { | ||
callback(e); | ||
return; | ||
} | ||
}); | ||
callback(null, res, json); | ||
req.on('timeout', function() { | ||
req.abort(); | ||
callback(Error(ETIMEDOUT)); | ||
}); | ||
}); | ||
req.on('error', function(e) { | ||
callback(e); | ||
}); | ||
req.on('timeout', function() { | ||
req.abort(); | ||
callback(Error(ETIMEDOUT)); | ||
}); | ||
req.write(form); | ||
req.end(); | ||
req.write(form); | ||
req.end(); | ||
} | ||
function postRetryingDecorator(params, callback) { | ||
let tryNumber = 1; | ||
let callbackOrigin = callback; | ||
let paramsOrigin = params; | ||
let requestLimit = params.requestLimit || 2; | ||
let timeout = params.timeout || 500; | ||
let tryNumber = 1; | ||
let callbackOrigin = callback; | ||
let paramsOrigin = params; | ||
let requestLimit = params.requestLimit || 2; | ||
let timeout = params.timeout || 500; | ||
let callbackMock = function(err) { | ||
if (err && err.message == ETIMEDOUT && tryNumber <= requestLimit) { | ||
tryNumber++; | ||
setTimeout(function() { post(paramsOrigin, callbackMock); }, timeout); | ||
return; | ||
} | ||
let callbackMock = function(err) { | ||
if (err && err.message == ETIMEDOUT && tryNumber <= requestLimit) { | ||
tryNumber++; | ||
setTimeout(function() { post(paramsOrigin, callbackMock); }, timeout); | ||
return; | ||
} | ||
callbackOrigin.apply(this, arguments); | ||
}; | ||
callbackOrigin.apply(this, arguments); | ||
}; | ||
post(params, callbackMock); | ||
post(params, callbackMock); | ||
} | ||
module.exports = postRetryingDecorator; |
@@ -17,22 +17,22 @@ 'use strict'; | ||
function checkText(text, callback, settings) { | ||
const form = prepareSettings(settings); | ||
form.text = text; | ||
const form = prepareSettings(settings); | ||
form.text = text; | ||
post({ | ||
host: YASPELLER_HOST, | ||
path: YASPELLER_PATH + 'checkText', | ||
form: form, | ||
post({ | ||
host: YASPELLER_HOST, | ||
path: YASPELLER_PATH + 'checkText', | ||
form: form, | ||
}, function(error, response, body) { | ||
if (error) { | ||
callback(error, null); | ||
} else { | ||
if (response.statusCode === 200) { | ||
callback(null, body); | ||
if (error) { | ||
callback(error, null); | ||
} else { | ||
callback( | ||
Error('Yandex.Speller API returns status code is ' + | ||
if (response.statusCode === 200) { | ||
callback(null, body); | ||
} else { | ||
callback( | ||
Error('Yandex.Speller API returns status code is ' + | ||
response.statusCode, null) | ||
); | ||
); | ||
} | ||
} | ||
} | ||
}); | ||
@@ -50,62 +50,60 @@ } | ||
function checkTexts(texts, callback, settings) { | ||
const form = prepareSettings(settings); | ||
form.text = texts; | ||
const form = prepareSettings(settings); | ||
form.text = texts; | ||
post({ | ||
host: YASPELLER_HOST, | ||
path: YASPELLER_PATH + 'checkTexts', | ||
form: form, | ||
post({ | ||
host: YASPELLER_HOST, | ||
path: YASPELLER_PATH + 'checkTexts', | ||
form: form, | ||
}, function(error, response, body) { | ||
if (error) { | ||
callback(error, null); | ||
} else { | ||
if (response.statusCode === 200) { | ||
callback(null, body); | ||
if (error) { | ||
callback(error, null); | ||
} else { | ||
callback( | ||
Error('Yandex.Speller API returns status code is ' + | ||
if (response.statusCode === 200) { | ||
callback(null, body); | ||
} else { | ||
callback( | ||
Error('Yandex.Speller API returns status code is ' + | ||
response.statusCode, null) | ||
); | ||
); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
/** | ||
* @see {@link https://tech.yandex.ru/speller/doc/dg/reference/speller-options-docpage/} | ||
*/ | ||
function prepareOptions(options) { | ||
let result = 0; | ||
const standartOptions = { | ||
IGNORE_UPPERCASE: 1, | ||
IGNORE_DIGITS: 2, | ||
IGNORE_URLS: 4, | ||
FIND_REPEAT_WORDS: 8, | ||
IGNORE_LATIN: 16, | ||
NO_SUGGEST: 32, | ||
FLAG_LATIN: 128, | ||
BY_WORDS: 256, | ||
IGNORE_CAPITALIZATION: 512, | ||
IGNORE_ROMAN_NUMERALS: 2048, | ||
let result = 0; | ||
// https://tech.yandex.ru/speller/doc/dg/reference/speller-options-docpage/ | ||
const standartOptions = { | ||
IGNORE_UPPERCASE: 1, | ||
IGNORE_DIGITS: 2, | ||
IGNORE_URLS: 4, | ||
FIND_REPEAT_WORDS: 8, | ||
IGNORE_LATIN: 16, | ||
NO_SUGGEST: 32, | ||
FLAG_LATIN: 128, | ||
BY_WORDS: 256, | ||
IGNORE_CAPITALIZATION: 512, | ||
IGNORE_ROMAN_NUMERALS: 2048, | ||
}; | ||
Object.keys(options || {}).forEach(function(key) { | ||
const upperCaseKey = key.replace(/([A-Z])/g, '_$1').toUpperCase(); | ||
if (standartOptions[upperCaseKey] && options[key]) { | ||
result |= standartOptions[upperCaseKey]; | ||
} | ||
Object.keys(options || {}).forEach(function(key) { | ||
const upperCaseKey = key.replace(/([A-Z])/g, '_$1').toUpperCase(); | ||
if (standartOptions[upperCaseKey] && options[key]) { | ||
result |= standartOptions[upperCaseKey]; | ||
} | ||
}); | ||
return result; | ||
return result; | ||
} | ||
function prepareSettings(settings) { | ||
settings = settings || {}; | ||
settings = settings || {}; | ||
return { | ||
format: settings.format || 'plain', | ||
lang: settings.lang || 'en,ru', | ||
options: prepareOptions(settings.options), | ||
requestLimit: settings.requestLimit || 2, | ||
timeout: settings.timeout || 500 | ||
return { | ||
format: settings.format || 'plain', | ||
lang: settings.lang || 'en,ru', | ||
options: prepareOptions(settings.options), | ||
requestLimit: settings.requestLimit || 2, | ||
timeout: settings.timeout || 500 | ||
}; | ||
@@ -133,29 +131,29 @@ } | ||
module.exports = { | ||
checkText: checkText, | ||
checkTexts: checkTexts, | ||
/** | ||
checkText: checkText, | ||
checkTexts: checkTexts, | ||
/** | ||
* @see {@link https://tech.yandex.ru/speller/doc/dg/reference/error-codes-docpage/} | ||
*/ | ||
errorCodes: [ | ||
{ | ||
name: 'ERROR_UNKNOWN_WORD', | ||
text: 'Typos', | ||
code: 1, | ||
}, | ||
{ | ||
name: 'ERROR_REPEAT_WORD', | ||
text: 'Repeat words', | ||
code: 2, | ||
}, | ||
{ | ||
name: 'ERROR_CAPITALIZATION', | ||
text: 'Capitalization', | ||
code: 3, | ||
}, | ||
{ | ||
name: 'ERROR_TOO_MANY_ERRORS', | ||
text: 'Too many errors', | ||
code: 4, | ||
} | ||
] | ||
errorCodes: [ | ||
{ | ||
name: 'ERROR_UNKNOWN_WORD', | ||
text: 'Typos', | ||
code: 1, | ||
}, | ||
{ | ||
name: 'ERROR_REPEAT_WORD', | ||
text: 'Repeat words', | ||
code: 2, | ||
}, | ||
{ | ||
name: 'ERROR_CAPITALIZATION', | ||
text: 'Capitalization', | ||
code: 3, | ||
}, | ||
{ | ||
name: 'ERROR_TOO_MANY_ERRORS', | ||
text: 'Too many errors', | ||
code: 4, | ||
} | ||
] | ||
}; |
@@ -10,3 +10,3 @@ { | ||
"description": "Yandex Speller API for Node.js", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"license": "MIT", | ||
@@ -33,8 +33,7 @@ "homepage": "https://github.com/hcodes/yandex-speller", | ||
"devDependencies": { | ||
"chai": "4.x", | ||
"chai": "^4.1.2", | ||
"eslint": "^4.10.0", | ||
"istanbul": "0.4.x", | ||
"eslint": "4.x", | ||
"jscs": "3.x", | ||
"mocha": "3.x", | ||
"sinon": "3.x" | ||
"mocha": "^4.0.1", | ||
"sinon": "^4.1.1" | ||
}, | ||
@@ -45,4 +44,3 @@ "engines": { | ||
"scripts": { | ||
"test": "npm run-script eslint && npm run-script jscs && npm run-script unit-test-coverage", | ||
"jscs": "./node_modules/.bin/jscs .", | ||
"test": "npm run-script eslint && npm run-script unit-test-coverage", | ||
"eslint": "./node_modules/.bin/eslint .", | ||
@@ -49,0 +47,0 @@ "unit-test": "./node_modules/.bin/mocha -u bdd -R spec --recursive test", |
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
Network access
Supply chain riskThis module accesses the network.
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
10787
5
209