parent
0b03390ae1
commit
872bcbb64e
@ -0,0 +1,8 @@
|
||||
{
|
||||
"flags": [],
|
||||
"linkedModules": [],
|
||||
"topLevelPatters": [],
|
||||
"lockfileEntries": {},
|
||||
"files": [],
|
||||
"artifacts": {}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||
# yarn lockfile v1
|
||||
|
||||
|
@ -0,0 +1,147 @@
|
||||
/******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 0);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
/** global: browser */
|
||||
|
||||
(function () {
|
||||
var c = {
|
||||
init: function () {
|
||||
var html = document.documentElement.outerHTML;
|
||||
|
||||
c.log('Function call: init()');
|
||||
|
||||
if (html.length > 50000) {
|
||||
html = html.substring(0, 25000) + html.substring(html.length - 25000, html.length);
|
||||
}
|
||||
|
||||
browser.runtime.sendMessage({
|
||||
id: 'analyze',
|
||||
subject: { html: html },
|
||||
source: 'content.js'
|
||||
});
|
||||
|
||||
c.getEnvironmentVars();
|
||||
},
|
||||
|
||||
log: function (message) {
|
||||
browser.runtime.sendMessage({
|
||||
id: 'log',
|
||||
message: message,
|
||||
source: 'content.js'
|
||||
});
|
||||
},
|
||||
|
||||
getEnvironmentVars: function () {
|
||||
var container, script;
|
||||
|
||||
c.log('Function call: getEnvironmentVars()');
|
||||
|
||||
if (typeof document.body === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
container = document.createElement('wappalyzerData');
|
||||
|
||||
container.setAttribute('id', 'wappalyzerData');
|
||||
container.setAttribute('style', 'display: none');
|
||||
|
||||
script = document.createElement('script');
|
||||
|
||||
script.setAttribute('id', 'wappalyzerEnvDetection');
|
||||
script.setAttribute('src', browser.extension.getURL('js/inject.js'));
|
||||
|
||||
container.addEventListener('wappalyzerEvent', function (event) {
|
||||
var environmentVars = event.target.childNodes[0].nodeValue;
|
||||
|
||||
document.documentElement.removeChild(container);
|
||||
document.documentElement.removeChild(script);
|
||||
|
||||
environmentVars = environmentVars.split(' ').slice(0, 500);
|
||||
|
||||
browser.runtime.sendMessage({
|
||||
id: 'analyze',
|
||||
subject: { env: environmentVars },
|
||||
source: 'content.js'
|
||||
});
|
||||
}, true);
|
||||
|
||||
document.documentElement.appendChild(container);
|
||||
document.documentElement.appendChild(script);
|
||||
} catch (e) {
|
||||
c.log('Error: ' + e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
c.init();
|
||||
})();
|
||||
|
||||
/***/ })
|
||||
/******/ ]);
|
@ -0,0 +1,144 @@
|
||||
/******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 0);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
/** global: browser */
|
||||
/** global: wappalyzer */
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
var d = document;
|
||||
|
||||
var options = {
|
||||
init: function () {
|
||||
options.load();
|
||||
|
||||
d.querySelector('#github').addEventListener('click', function () {
|
||||
open(wappalyzer.config.githubURL);
|
||||
});
|
||||
|
||||
d.querySelector('#twitter').addEventListener('click', function () {
|
||||
open(wappalyzer.config.twitterURL);
|
||||
});
|
||||
|
||||
d.querySelector('#wappalyzer').addEventListener('click', function () {
|
||||
open(wappalyzer.config.websiteURL);
|
||||
});
|
||||
},
|
||||
|
||||
get: function (name, defaultValue, callback) {
|
||||
browser.storage.local.get(name).then(function (item) {
|
||||
callback(item.hasOwnProperty(name) ? item[name] : defaultValue);
|
||||
});
|
||||
},
|
||||
|
||||
set: function (name, value) {
|
||||
var option = {};
|
||||
|
||||
option[name] = value;
|
||||
|
||||
browser.storage.local.set(option);
|
||||
},
|
||||
|
||||
load: function () {
|
||||
options.get('upgradeMessage', true, function (value) {
|
||||
var el = d.querySelector('#option-upgrade-message');
|
||||
|
||||
el.checked = value;
|
||||
|
||||
el.addEventListener('change', function () {
|
||||
options.set('upgradeMessage', el.checked);
|
||||
});
|
||||
});
|
||||
|
||||
options.get('dynamicIcon', true, function (value) {
|
||||
var el = d.querySelector('#option-dynamic-icon');
|
||||
|
||||
el.checked = value;
|
||||
|
||||
el.addEventListener('change', function () {
|
||||
options.set('dynamicIcon', el.checked);
|
||||
});
|
||||
});
|
||||
|
||||
options.get('tracking', true, function (value) {
|
||||
var el = d.querySelector('#option-tracking');
|
||||
|
||||
el.checked = value;
|
||||
|
||||
el.addEventListener('change', function () {
|
||||
options.set('tracking', el.checked);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
options.init();
|
||||
});
|
||||
|
||||
/***/ })
|
||||
/******/ ]);
|
@ -0,0 +1,168 @@
|
||||
/******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 0);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
/** global: chrome */
|
||||
/** global: browser */
|
||||
|
||||
(function () {
|
||||
var popup = {
|
||||
init: function () {
|
||||
popup.update(['p', {}, ' '], document, {});
|
||||
|
||||
var func = function (tabs) {
|
||||
(chrome || browser).runtime.sendMessage({ id: 'get_apps', tab: tabs[0], source: 'popup.js' }, function (response) {
|
||||
popup.update(popup.appsToDomTemplate(response));
|
||||
});
|
||||
};
|
||||
|
||||
try {
|
||||
// Chrome, Firefox
|
||||
browser.tabs.query({ active: true, currentWindow: true }).then(func);
|
||||
} catch (e) {
|
||||
// Edge
|
||||
browser.tabs.query({ active: true, currentWindow: true }, func);
|
||||
}
|
||||
},
|
||||
|
||||
update: function (dom) {
|
||||
if (/complete|interactive|loaded/.test(document.readyState)) {
|
||||
popup.replaceDom(dom);
|
||||
} else {
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
popup.replaceDom(dom);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
replaceDom: function (domTemplate) {
|
||||
var body = document.body;
|
||||
|
||||
while (body.firstChild) {
|
||||
body.removeChild(body.firstChild);
|
||||
}
|
||||
|
||||
body.appendChild(jsonToDOM(domTemplate, document, {}));
|
||||
},
|
||||
|
||||
appsToDomTemplate: function (response) {
|
||||
var appName,
|
||||
confidence,
|
||||
version,
|
||||
categories = [],
|
||||
template = [];
|
||||
|
||||
if (response.tabCache && response.tabCache.count > 0) {
|
||||
for (appName in response.tabCache.appsDetected) {
|
||||
confidence = response.tabCache.appsDetected[appName].confidenceTotal;
|
||||
version = response.tabCache.appsDetected[appName].version;
|
||||
categories = [];
|
||||
|
||||
response.apps[appName].cats.forEach(function (cat) {
|
||||
categories.push(['a', {
|
||||
target: '_blank',
|
||||
href: 'https://wappalyzer.com/categories/' + popup.slugify(response.categories[cat].name)
|
||||
}, ['span', {
|
||||
class: 'category'
|
||||
}, ['span', {
|
||||
class: 'name'
|
||||
}, browser.i18n.getMessage('categoryName' + cat)]]]);
|
||||
});
|
||||
|
||||
template.push(['div', {
|
||||
class: 'detected-app'
|
||||
}, ['a', {
|
||||
target: '_blank',
|
||||
href: 'https://wappalyzer.com/applications/' + popup.slugify(appName)
|
||||
}, ['img', {
|
||||
src: 'images/icons/' + (response.apps[appName].icon || 'default.svg')
|
||||
}], ['span', {
|
||||
class: 'label'
|
||||
}, ['span', {
|
||||
class: 'name'
|
||||
}, appName], (version ? ' ' + version : '') + (confidence < 100 ? ' (' + confidence + '% sure)' : '')]], categories]);
|
||||
}
|
||||
} else {
|
||||
template = ['div', {
|
||||
class: 'empty'
|
||||
}, browser.i18n.getMessage('noAppsDetected')];
|
||||
}
|
||||
|
||||
return template;
|
||||
},
|
||||
|
||||
slugify: function (string) {
|
||||
return string.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/--+/g, '-').replace(/(?:^-|-$)/, '');
|
||||
}
|
||||
};
|
||||
|
||||
popup.init();
|
||||
})();
|
||||
|
||||
/***/ })
|
||||
/******/ ]);
|
@ -0,0 +1 @@
|
||||
../extract-zip/cli.js
|
@ -0,0 +1 @@
|
||||
../har-validator/bin/har-validator
|
@ -0,0 +1 @@
|
||||
../mkdirp/bin/cmd.js
|
@ -0,0 +1 @@
|
||||
../phantomjs-prebuilt/bin/phantomjs
|
@ -0,0 +1 @@
|
||||
../sshpk/bin/sshpk-conv
|
@ -0,0 +1 @@
|
||||
../sshpk/bin/sshpk-sign
|
@ -0,0 +1 @@
|
||||
../sshpk/bin/sshpk-verify
|
@ -0,0 +1 @@
|
||||
../uuid/bin/uuid
|
@ -0,0 +1 @@
|
||||
../which/bin/which
|
@ -0,0 +1,164 @@
|
||||
{
|
||||
"flags": [],
|
||||
"linkedModules": [],
|
||||
"topLevelPatters": [
|
||||
"phantomjs-prebuilt@*"
|
||||
],
|
||||
"lockfileEntries": {
|
||||
"ansi-regex@^2.0.0": "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-2.1.1.tgz#c3b33ab5ee360d86e0e628f0468ae7ef27d654df",
|
||||
"ansi-styles@^2.2.1": "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe",
|
||||
"asn1@~0.2.3": "https://registry.yarnpkg.com/asn1/-/asn1-0.2.3.tgz#dac8787713c9966849fc8180777ebe9c1ddf3b86",
|
||||
"assert-plus@1.0.0": "https://registry.yarnpkg.com/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525",
|
||||
"assert-plus@^0.2.0": "https://registry.yarnpkg.com/assert-plus/-/assert-plus-0.2.0.tgz#d74e1b87e7affc0db8aadb7021f3fe48101ab234",
|
||||
"assert-plus@^1.0.0": "https://registry.yarnpkg.com/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525",
|
||||
"asynckit@^0.4.0": "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79",
|
||||
"aws-sign2@~0.6.0": "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.6.0.tgz#14342dd38dbcc94d0e5b87d763cd63612c0e794f",
|
||||
"aws4@^1.2.1": "https://registry.yarnpkg.com/aws4/-/aws4-1.6.0.tgz#83ef5ca860b2b32e4a0deedee8c771b9db57471e",
|
||||
"bcrypt-pbkdf@^1.0.0": "https://registry.yarnpkg.com/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.1.tgz#63bc5dcb61331b92bc05fd528953c33462a06f8d",
|
||||
"boom@2.x.x": "https://registry.yarnpkg.com/boom/-/boom-2.10.1.tgz#39c8918ceff5799f83f9492a848f625add0c766f",
|
||||
"caseless@~0.11.0": "https://registry.yarnpkg.com/caseless/-/caseless-0.11.0.tgz#715b96ea9841593cc33067923f5ec60ebda4f7d7",
|
||||
"chalk@^1.1.1": "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98",
|
||||
"combined-stream@^1.0.5": "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.5.tgz#938370a57b4a51dea2c77c15d5c5fdf895164009",
|
||||
"combined-stream@~1.0.5": "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.5.tgz#938370a57b4a51dea2c77c15d5c5fdf895164009",
|
||||
"commander@^2.9.0": "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563",
|
||||
"concat-stream@1.5.0": "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.5.0.tgz#53f7d43c51c5e43f81c8fdd03321c631be68d611",
|
||||
"core-util-is@~1.0.0": "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7",
|
||||
"cryptiles@2.x.x": "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8",
|
||||
"dashdash@^1.12.0": "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0",
|
||||
"debug@0.7.4": "https://registry.yarnpkg.com/debug/-/debug-0.7.4.tgz#06e1ea8082c2cb14e39806e22e2f6f757f92af39",
|
||||
"delayed-stream@~1.0.0": "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619",
|
||||
"ecc-jsbn@~0.1.1": "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.1.tgz#0fc73a9ed5f0d53c38193398523ef7e543777505",
|
||||
"es6-promise@~4.0.3": "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.0.5.tgz#7882f30adde5b240ccfa7f7d78c548330951ae42",
|
||||
"escape-string-regexp@^1.0.2": "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4",
|
||||
"extend@~3.0.0": "https://registry.yarnpkg.com/extend/-/extend-3.0.1.tgz#a755ea7bc1adfcc5a31ce7e762dbaadc5e636444",
|
||||
"extract-zip@~1.5.0": "https://registry.yarnpkg.com/extract-zip/-/extract-zip-1.5.0.tgz#92ccf6d81ef70a9fa4c1747114ccef6d8688a6c4",
|
||||
"extsprintf@1.0.2": "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.0.2.tgz#e1080e0658e300b06294990cc70e1502235fd550",
|
||||
"fd-slicer@~1.0.1": "https://registry.yarnpkg.com/fd-slicer/-/fd-slicer-1.0.1.tgz#8b5bcbd9ec327c5041bf9ab023fd6750f1177e65",
|
||||
"forever-agent@~0.6.1": "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91",
|
||||
"form-data@~2.1.1": "https://registry.yarnpkg.com/form-data/-/form-data-2.1.4.tgz#33c183acf193276ecaa98143a69e94bfee1750d1",
|
||||
"fs-extra@~1.0.0": "https://registry.yarnpkg.com/fs-extra/-/fs-extra-1.0.0.tgz#cd3ce5f7e7cb6145883fcae3191e9877f8587950",
|
||||
"generate-function@^2.0.0": "https://registry.yarnpkg.com/generate-function/-/generate-function-2.0.0.tgz#6858fe7c0969b7d4e9093337647ac79f60dfbe74",
|
||||
"generate-object-property@^1.1.0": "https://registry.yarnpkg.com/generate-object-property/-/generate-object-property-1.2.0.tgz#9c0e1c40308ce804f4783618b937fa88f99d50d0",
|
||||
"getpass@^0.1.1": "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa",
|
||||
"graceful-fs@^4.1.2": "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658",
|
||||
"graceful-fs@^4.1.6": "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658",
|
||||
"graceful-fs@^4.1.9": "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658",
|
||||
"har-validator@~2.0.6": "https://registry.yarnpkg.com/har-validator/-/har-validator-2.0.6.tgz#cdcbc08188265ad119b6a5a7c8ab70eecfb5d27d",
|
||||
"has-ansi@^2.0.0": "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91",
|
||||
"hasha@~2.2.0": "https://registry.yarnpkg.com/hasha/-/hasha-2.2.0.tgz#78d7cbfc1e6d66303fe79837365984517b2f6ee1",
|
||||
"hawk@~3.1.3": "https://registry.yarnpkg.com/hawk/-/hawk-3.1.3.tgz#078444bd7c1640b0fe540d2c9b73d59678e8e1c4",
|
||||
"hoek@2.x.x": "https://registry.yarnpkg.com/hoek/-/hoek-2.16.3.tgz#20bb7403d3cea398e91dc4710a8ff1b8274a25ed",
|
||||
"http-signature@~1.1.0": "https://registry.yarnpkg.com/http-signature/-/http-signature-1.1.1.tgz#df72e267066cd0ac67fb76adf8e134a8fbcf91bf",
|
||||
"inherits@~2.0.1": "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de",
|
||||
"is-my-json-valid@^2.12.4": "https://registry.yarnpkg.com/is-my-json-valid/-/is-my-json-valid-2.16.0.tgz#f079dd9bfdae65ee2038aae8acbc86ab109e3693",
|
||||
"is-property@^1.0.0": "https://registry.yarnpkg.com/is-property/-/is-property-1.0.2.tgz#57fe1c4e48474edd65b09911f26b1cd4095dda84",
|
||||
"is-stream@^1.0.1": "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44",
|
||||
"is-typedarray@~1.0.0": "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a",
|
||||
"isarray@~1.0.0": "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11",
|
||||
"isexe@^2.0.0": "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10",
|
||||
"isstream@~0.1.2": "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a",
|
||||
"jsbn@~0.1.0": "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513",
|
||||
"json-schema@0.2.3": "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13",
|
||||
"json-stringify-safe@~5.0.1": "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb",
|
||||
"jsonfile@^2.1.0": "https://registry.yarnpkg.com/jsonfile/-/jsonfile-2.4.0.tgz#3736a2b428b87bbda0cc83b53fa3d633a35c2ae8",
|
||||
"jsonpointer@^4.0.0": "https://registry.yarnpkg.com/jsonpointer/-/jsonpointer-4.0.1.tgz#4fd92cb34e0e9db3c89c8622ecf51f9b978c6cb9",
|
||||
"jsprim@^1.2.2": "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.0.tgz#a3b87e40298d8c380552d8cc7628a0bb95a22918",
|
||||
"kew@~0.7.0": "https://registry.yarnpkg.com/kew/-/kew-0.7.0.tgz#79d93d2d33363d6fdd2970b335d9141ad591d79b",
|
||||
"klaw@^1.0.0": "https://registry.yarnpkg.com/klaw/-/klaw-1.3.1.tgz#4088433b46b3b1ba259d78785d8e96f73ba02439",
|
||||
"mime-db@~1.27.0": "https://registry.yarnpkg.com/mime-db/-/mime-db-1.27.0.tgz#820f572296bbd20ec25ed55e5b5de869e5436eb1",
|
||||
"mime-types@^2.1.12": "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.15.tgz#a4ebf5064094569237b8cf70046776d09fc92aed",
|
||||
"mime-types@~2.1.7": "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.15.tgz#a4ebf5064094569237b8cf70046776d09fc92aed",
|
||||
"minimist@0.0.8": "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d",
|
||||
"mkdirp@0.5.0": "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.0.tgz#1d73076a6df986cd9344e15e71fcc05a4c9abf12",
|
||||
"oauth-sign@~0.8.1": "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.8.2.tgz#46a6ab7f0aead8deae9ec0565780b7d4efeb9d43",
|
||||
"pend@~1.2.0": "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50",
|
||||
"phantomjs-prebuilt@*": "https://registry.yarnpkg.com/phantomjs-prebuilt/-/phantomjs-prebuilt-2.1.14.tgz#d53d311fcfb7d1d08ddb24014558f1188c516da0",
|
||||
"pinkie-promise@^2.0.0": "https://registry.yarnpkg.com/pinkie-promise/-/pinkie-promise-2.0.1.tgz#2135d6dfa7a358c069ac9b178776288228450ffa",
|
||||
"pinkie@^2.0.0": "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870",
|
||||
"process-nextick-args@~1.0.6": "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3",
|
||||
"progress@~1.1.8": "https://registry.yarnpkg.com/progress/-/progress-1.1.8.tgz#e260c78f6161cdd9b0e56cc3e0a85de17c7a57be",
|
||||
"punycode@^1.4.1": "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e",
|
||||
"qs@~6.3.0": "https://registry.yarnpkg.com/qs/-/qs-6.3.2.tgz#e75bd5f6e268122a2a0e0bda630b2550c166502c",
|
||||
"readable-stream@~2.0.0": "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e",
|
||||
"request-progress@~2.0.1": "https://registry.yarnpkg.com/request-progress/-/request-progress-2.0.1.tgz#5d36bb57961c673aa5b788dbc8141fdf23b44e08",
|
||||
"request@~2.79.0": "https://registry.yarnpkg.com/request/-/request-2.79.0.tgz#4dfe5bf6be8b8cdc37fcf93e04b65577722710de",
|
||||
"sntp@1.x.x": "https://registry.yarnpkg.com/sntp/-/sntp-1.0.9.tgz#6541184cc90aeea6c6e7b35e2659082443c66198",
|
||||
"sshpk@^1.7.0": "https://registry.yarnpkg.com/sshpk/-/sshpk-1.13.1.tgz#512df6da6287144316dc4c18fe1cf1d940739be3",
|
||||
"string_decoder@~0.10.x": "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94",
|
||||
"stringstream@~0.0.4": "https://registry.yarnpkg.com/stringstream/-/stringstream-0.0.5.tgz#4e484cd4de5a0bbbee18e46307710a8a81621878",
|
||||
"strip-ansi@^3.0.0": "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf",
|
||||
"supports-color@^2.0.0": "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7",
|
||||
"throttleit@^1.0.0": "https://registry.yarnpkg.com/throttleit/-/throttleit-1.0.0.tgz#9e785836daf46743145a5984b6268d828528ac6c",
|
||||
"tough-cookie@~2.3.0": "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.2.tgz#f081f76e4c85720e6c37a5faced737150d84072a",
|
||||
"tunnel-agent@~0.4.1": "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.4.3.tgz#6373db76909fe570e08d73583365ed828a74eeeb",
|
||||
"tweetnacl@^0.14.3": "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64",
|
||||
"tweetnacl@~0.14.0": "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64",
|
||||
"typedarray@~0.0.5": "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777",
|
||||
"util-deprecate@~1.0.1": "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf",
|
||||
"uuid@^3.0.0": "https://registry.yarnpkg.com/uuid/-/uuid-3.1.0.tgz#3dd3d3e790abc24d7b0d3a034ffababe28ebbc04",
|
||||
"verror@1.3.6": "https://registry.yarnpkg.com/verror/-/verror-1.3.6.tgz#cff5df12946d297d2baaefaa2689e25be01c005c",
|
||||
"which@~1.2.10": "https://registry.yarnpkg.com/which/-/which-1.2.14.tgz#9a87c4378f03e827cecaf1acdf56c736c01c14e5",
|
||||
"xtend@^4.0.0": "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af",
|
||||
"yauzl@2.4.1": "https://registry.yarnpkg.com/yauzl/-/yauzl-2.4.1.tgz#9528f442dab1b2284e58b4379bb194e22e0c4005"
|
||||
},
|
||||
"files": [],
|
||||
"artifacts": {
|
||||
"phantomjs-prebuilt@2.1.14": [
|
||||
"lib",
|
||||
"lib/location.js",
|
||||
"lib/phantom",
|
||||
"lib/phantom/ChangeLog",
|
||||
"lib/phantom/LICENSE.BSD",
|
||||
"lib/phantom/README.md",
|
||||
"lib/phantom/bin",
|
||||
"lib/phantom/bin/phantomjs",
|
||||
"lib/phantom/examples",
|
||||
"lib/phantom/examples/arguments.js",
|
||||
"lib/phantom/examples/child_process-examples.js",
|
||||
"lib/phantom/examples/colorwheel.js",
|
||||
"lib/phantom/examples/countdown.js",
|
||||
"lib/phantom/examples/detectsniff.js",
|
||||
"lib/phantom/examples/echoToFile.js",
|
||||
"lib/phantom/examples/features.js",
|
||||
"lib/phantom/examples/fibo.js",
|
||||
"lib/phantom/examples/hello.js",
|
||||
"lib/phantom/examples/injectme.js",
|
||||
"lib/phantom/examples/loadspeed.js",
|
||||
"lib/phantom/examples/loadurlwithoutcss.js",
|
||||
"lib/phantom/examples/modernizr.js",
|
||||
"lib/phantom/examples/module.js",
|
||||
"lib/phantom/examples/netlog.js",
|
||||
"lib/phantom/examples/netsniff.js",
|
||||
"lib/phantom/examples/openurlwithproxy.js",
|
||||
"lib/phantom/examples/outputEncoding.js",
|
||||
"lib/phantom/examples/page_events.js",
|
||||
"lib/phantom/examples/pagecallback.js",
|
||||
"lib/phantom/examples/phantomwebintro.js",
|
||||
"lib/phantom/examples/post.js",
|
||||
"lib/phantom/examples/postjson.js",
|
||||
"lib/phantom/examples/postserver.js",
|
||||
"lib/phantom/examples/printenv.js",
|
||||
"lib/phantom/examples/printheaderfooter.js",
|
||||
"lib/phantom/examples/printmargins.js",
|
||||
"lib/phantom/examples/rasterize.js",
|
||||
"lib/phantom/examples/render_multi_url.js",
|
||||
"lib/phantom/examples/responsive-screenshot.js",
|
||||
"lib/phantom/examples/run-jasmine.js",
|
||||
"lib/phantom/examples/run-jasmine2.js",
|
||||
"lib/phantom/examples/run-qunit.js",
|
||||
"lib/phantom/examples/scandir.js",
|
||||
"lib/phantom/examples/server.js",
|
||||
"lib/phantom/examples/serverkeepalive.js",
|
||||
"lib/phantom/examples/simpleserver.js",
|
||||
"lib/phantom/examples/sleepsort.js",
|
||||
"lib/phantom/examples/stdin-stdout-stderr.js",
|
||||
"lib/phantom/examples/universe.js",
|
||||
"lib/phantom/examples/unrandomize.js",
|
||||
"lib/phantom/examples/useragent.js",
|
||||
"lib/phantom/examples/version.js",
|
||||
"lib/phantom/examples/waitfor.js",
|
||||
"lib/phantom/examples/walk_through_frames.js",
|
||||
"lib/phantom/third-party.txt"
|
||||
]
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
'use strict';
|
||||
module.exports = function () {
|
||||
return /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]/g;
|
||||
};
|
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
@ -0,0 +1,64 @@
|
||||
{
|
||||
"name": "ansi-regex",
|
||||
"version": "2.1.1",
|
||||
"description": "Regular expression for matching ANSI escape codes",
|
||||
"license": "MIT",
|
||||
"repository": "chalk/ansi-regex",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
},
|
||||
"maintainers": [
|
||||
"Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)",
|
||||
"Joshua Appelman <jappelman@xebia.com> (jbnicolai.com)",
|
||||
"JD Ballard <i.am.qix@gmail.com> (github.com/qix-)"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava --verbose",
|
||||
"view-supported": "node fixtures/view-codes.js"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"keywords": [
|
||||
"ansi",
|
||||
"styles",
|
||||
"color",
|
||||
"colour",
|
||||
"colors",
|
||||
"terminal",
|
||||
"console",
|
||||
"cli",
|
||||
"string",
|
||||
"tty",
|
||||
"escape",
|
||||
"formatting",
|
||||
"rgb",
|
||||
"256",
|
||||
"shell",
|
||||
"xterm",
|
||||
"command-line",
|
||||
"text",
|
||||
"regex",
|
||||
"regexp",
|
||||
"re",
|
||||
"match",
|
||||
"test",
|
||||
"find",
|
||||
"pattern"
|
||||
],
|
||||
"devDependencies": {
|
||||
"ava": "0.17.0",
|
||||
"xo": "0.16.0"
|
||||
},
|
||||
"xo": {
|
||||
"rules": {
|
||||
"guard-for-in": 0,
|
||||
"no-loop-func": 0
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
# ansi-regex [](https://travis-ci.org/chalk/ansi-regex)
|
||||
|
||||
> Regular expression for matching [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code)
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install --save ansi-regex
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const ansiRegex = require('ansi-regex');
|
||||
|
||||
ansiRegex().test('\u001b[4mcake\u001b[0m');
|
||||
//=> true
|
||||
|
||||
ansiRegex().test('cake');
|
||||
//=> false
|
||||
|
||||
'\u001b[4mcake\u001b[0m'.match(ansiRegex());
|
||||
//=> ['\u001b[4m', '\u001b[0m']
|
||||
```
|
||||
|
||||
## FAQ
|
||||
|
||||
### Why do you test for codes not in the ECMA 48 standard?
|
||||
|
||||
Some of the codes we run as a test are codes that we acquired finding various lists of non-standard or manufacturer specific codes. If I recall correctly, we test for both standard and non-standard codes, as most of them follow the same or similar format and can be safely matched in strings without the risk of removing actual string content. There are a few non-standard control codes that do not follow the traditional format (i.e. they end in numbers) thus forcing us to exclude them from the test because we cannot reliably match them.
|
||||
|
||||
On the historical side, those ECMA standards were established in the early 90's whereas the VT100, for example, was designed in the mid/late 70's. At that point in time, control codes were still pretty ungoverned and engineers used them for a multitude of things, namely to activate hardware ports that may have been proprietary. Somewhere else you see a similar 'anarchy' of codes is in the x86 architecture for processors; there are a ton of "interrupts" that can mean different things on certain brands of processors, most of which have been phased out.
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Sindre Sorhus](http://sindresorhus.com)
|
@ -0,0 +1,65 @@
|
||||
'use strict';
|
||||
|
||||
function assembleStyles () {
|
||||
var styles = {
|
||||
modifiers: {
|
||||
reset: [0, 0],
|
||||
bold: [1, 22], // 21 isn't widely supported and 22 does the same thing
|
||||
dim: [2, 22],
|
||||
italic: [3, 23],
|
||||
underline: [4, 24],
|
||||
inverse: [7, 27],
|
||||
hidden: [8, 28],
|
||||
strikethrough: [9, 29]
|
||||
},
|
||||
colors: {
|
||||
black: [30, 39],
|
||||
red: [31, 39],
|
||||
green: [32, 39],
|
||||
yellow: [33, 39],
|
||||
blue: [34, 39],
|
||||
magenta: [35, 39],
|
||||
cyan: [36, 39],
|
||||
white: [37, 39],
|
||||
gray: [90, 39]
|
||||
},
|
||||
bgColors: {
|
||||
bgBlack: [40, 49],
|
||||
bgRed: [41, 49],
|
||||
bgGreen: [42, 49],
|
||||
bgYellow: [43, 49],
|
||||
bgBlue: [44, 49],
|
||||
bgMagenta: [45, 49],
|
||||
bgCyan: [46, 49],
|
||||
bgWhite: [47, 49]
|
||||
}
|
||||
};
|
||||
|
||||
// fix humans
|
||||
styles.colors.grey = styles.colors.gray;
|
||||
|
||||
Object.keys(styles).forEach(function (groupName) {
|
||||
var group = styles[groupName];
|
||||
|
||||
Object.keys(group).forEach(function (styleName) {
|
||||
var style = group[styleName];
|
||||
|
||||
styles[styleName] = group[styleName] = {
|
||||
open: '\u001b[' + style[0] + 'm',
|
||||
close: '\u001b[' + style[1] + 'm'
|
||||
};
|
||||
});
|
||||
|
||||
Object.defineProperty(styles, groupName, {
|
||||
value: group,
|
||||
enumerable: false
|
||||
});
|
||||
});
|
||||
|
||||
return styles;
|
||||
}
|
||||
|
||||
Object.defineProperty(module, 'exports', {
|
||||
enumerable: true,
|
||||
get: assembleStyles
|
||||
});
|
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
@ -0,0 +1,50 @@
|
||||
{
|
||||
"name": "ansi-styles",
|
||||
"version": "2.2.1",
|
||||
"description": "ANSI escape codes for styling strings in the terminal",
|
||||
"license": "MIT",
|
||||
"repository": "chalk/ansi-styles",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
},
|
||||
"maintainers": [
|
||||
"Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)",
|
||||
"Joshua Appelman <jappelman@xebia.com> (jbnicolai.com)"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"keywords": [
|
||||
"ansi",
|
||||
"styles",
|
||||
"color",
|
||||
"colour",
|
||||
"colors",
|
||||
"terminal",
|
||||
"console",
|
||||
"cli",
|
||||
"string",
|
||||
"tty",
|
||||
"escape",
|
||||
"formatting",
|
||||
"rgb",
|
||||
"256",
|
||||
"shell",
|
||||
"xterm",
|
||||
"log",
|
||||
"logging",
|
||||
"command-line",
|
||||
"text"
|
||||
],
|
||||
"devDependencies": {
|
||||
"mocha": "*"
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
# ansi-styles [](https://travis-ci.org/chalk/ansi-styles)
|
||||
|
||||
> [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) for styling strings in the terminal
|
||||
|
||||
You probably want the higher-level [chalk](https://github.com/chalk/chalk) module for styling your strings.
|
||||
|
||||

|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install --save ansi-styles
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
var ansi = require('ansi-styles');
|
||||
|
||||
console.log(ansi.green.open + 'Hello world!' + ansi.green.close);
|
||||
```
|
||||
|
||||
|
||||
## API
|
||||
|
||||
Each style has an `open` and `close` property.
|
||||
|
||||
|
||||
## Styles
|
||||
|
||||
### Modifiers
|
||||
|
||||
- `reset`
|
||||
- `bold`
|
||||
- `dim`
|
||||
- `italic` *(not widely supported)*
|
||||
- `underline`
|
||||
- `inverse`
|
||||
- `hidden`
|
||||
- `strikethrough` *(not widely supported)*
|
||||
|
||||
### Colors
|
||||
|
||||
- `black`
|
||||
- `red`
|
||||
- `green`
|
||||
- `yellow`
|
||||
- `blue`
|
||||
- `magenta`
|
||||
- `cyan`
|
||||
- `white`
|
||||
- `gray`
|
||||
|
||||
### Background colors
|
||||
|
||||
- `bgBlack`
|
||||
- `bgRed`
|
||||
- `bgGreen`
|
||||
- `bgYellow`
|
||||
- `bgBlue`
|
||||
- `bgMagenta`
|
||||
- `bgCyan`
|
||||
- `bgWhite`
|
||||
|
||||
|
||||
## Advanced usage
|
||||
|
||||
By default you get a map of styles, but the styles are also available as groups. They are non-enumerable so they don't show up unless you access them explicitly. This makes it easier to expose only a subset in a higher-level module.
|
||||
|
||||
- `ansi.modifiers`
|
||||
- `ansi.colors`
|
||||
- `ansi.bgColors`
|
||||
|
||||
|
||||
###### Example
|
||||
|
||||
```js
|
||||
console.log(ansi.colors.green.open);
|
||||
```
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Sindre Sorhus](http://sindresorhus.com)
|
@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
*.log
|
@ -0,0 +1,4 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- 0.8
|
||||
- 0.10
|
@ -0,0 +1,19 @@
|
||||
Copyright (c) 2011 Mark Cavage, All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE
|
@ -0,0 +1,50 @@
|
||||
node-asn1 is a library for encoding and decoding ASN.1 datatypes in pure JS.
|
||||
Currently BER encoding is supported; at some point I'll likely have to do DER.
|
||||
|
||||
## Usage
|
||||
|
||||
Mostly, if you're *actually* needing to read and write ASN.1, you probably don't
|
||||
need this readme to explain what and why. If you have no idea what ASN.1 is,
|
||||
see this: ftp://ftp.rsa.com/pub/pkcs/ascii/layman.asc
|
||||
|
||||
The source is pretty much self-explanatory, and has read/write methods for the
|
||||
common types out there.
|
||||
|
||||
### Decoding
|
||||
|
||||
The following reads an ASN.1 sequence with a boolean.
|
||||
|
||||
var Ber = require('asn1').Ber;
|
||||
|
||||
var reader = new Ber.Reader(new Buffer([0x30, 0x03, 0x01, 0x01, 0xff]));
|
||||
|
||||
reader.readSequence();
|
||||
console.log('Sequence len: ' + reader.length);
|
||||
if (reader.peek() === Ber.Boolean)
|
||||
console.log(reader.readBoolean());
|
||||
|
||||
### Encoding
|
||||
|
||||
The following generates the same payload as above.
|
||||
|
||||
var Ber = require('asn1').Ber;
|
||||
|
||||
var writer = new Ber.Writer();
|
||||
|
||||
writer.startSequence();
|
||||
writer.writeBoolean(true);
|
||||
writer.endSequence();
|
||||
|
||||
console.log(writer.buffer);
|
||||
|
||||
## Installation
|
||||
|
||||
npm install asn1
|
||||
|
||||
## License
|
||||
|
||||
MIT.
|
||||
|
||||
## Bugs
|
||||
|
||||
See <https://github.com/mcavage/node-asn1/issues>.
|
@ -0,0 +1,13 @@
|
||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
|
||||
|
||||
|
||||
module.exports = {
|
||||
|
||||
newInvalidAsn1Error: function(msg) {
|
||||
var e = new Error();
|
||||
e.name = 'InvalidAsn1Error';
|
||||
e.message = msg || '';
|
||||
return e;
|
||||
}
|
||||
|
||||
};
|
@ -0,0 +1,27 @@
|
||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
|
||||
|
||||
var errors = require('./errors');
|
||||
var types = require('./types');
|
||||
|
||||
var Reader = require('./reader');
|
||||
var Writer = require('./writer');
|
||||
|
||||
|
||||
///--- Exports
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reader: Reader,
|
||||
|
||||
Writer: Writer
|
||||
|
||||
};
|
||||
|
||||
for (var t in types) {
|
||||
if (types.hasOwnProperty(t))
|
||||
module.exports[t] = types[t];
|
||||
}
|
||||
for (var e in errors) {
|
||||
if (errors.hasOwnProperty(e))
|
||||
module.exports[e] = errors[e];
|
||||
}
|
@ -0,0 +1,261 @@
|
||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
|
||||
|
||||
var assert = require('assert');
|
||||
|
||||
var ASN1 = require('./types');
|
||||
var errors = require('./errors');
|
||||
|
||||
|
||||
///--- Globals
|
||||
|
||||
var newInvalidAsn1Error = errors.newInvalidAsn1Error;
|
||||
|
||||
|
||||
|
||||
///--- API
|
||||
|
||||
function Reader(data) {
|
||||
if (!data || !Buffer.isBuffer(data))
|
||||
throw new TypeError('data must be a node Buffer');
|
||||
|
||||
this._buf = data;
|
||||
this._size = data.length;
|
||||
|
||||
// These hold the "current" state
|
||||
this._len = 0;
|
||||
this._offset = 0;
|
||||
}
|
||||
|
||||
Object.defineProperty(Reader.prototype, 'length', {
|
||||
enumerable: true,
|
||||
get: function () { return (this._len); }
|
||||
});
|
||||
|
||||
Object.defineProperty(Reader.prototype, 'offset', {
|
||||
enumerable: true,
|
||||
get: function () { return (this._offset); }
|
||||
});
|
||||
|
||||
Object.defineProperty(Reader.prototype, 'remain', {
|
||||
get: function () { return (this._size - this._offset); }
|
||||
});
|
||||
|
||||
Object.defineProperty(Reader.prototype, 'buffer', {
|
||||
get: function () { return (this._buf.slice(this._offset)); }
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Reads a single byte and advances offset; you can pass in `true` to make this
|
||||
* a "peek" operation (i.e., get the byte, but don't advance the offset).
|
||||
*
|
||||
* @param {Boolean} peek true means don't move offset.
|
||||
* @return {Number} the next byte, null if not enough data.
|
||||
*/
|
||||
Reader.prototype.readByte = function(peek) {
|
||||
if (this._size - this._offset < 1)
|
||||
return null;
|
||||
|
||||
var b = this._buf[this._offset] & 0xff;
|
||||
|
||||
if (!peek)
|
||||
this._offset += 1;
|
||||
|
||||
return b;
|
||||
};
|
||||
|
||||
|
||||
Reader.prototype.peek = function() {
|
||||
return this.readByte(true);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Reads a (potentially) variable length off the BER buffer. This call is
|
||||
* not really meant to be called directly, as callers have to manipulate
|
||||
* the internal buffer afterwards.
|
||||
*
|
||||
* As a result of this call, you can call `Reader.length`, until the
|
||||
* next thing called that does a readLength.
|
||||
*
|
||||
* @return {Number} the amount of offset to advance the buffer.
|
||||
* @throws {InvalidAsn1Error} on bad ASN.1
|
||||
*/
|
||||
Reader.prototype.readLength = function(offset) {
|
||||
if (offset === undefined)
|
||||
offset = this._offset;
|
||||
|
||||
if (offset >= this._size)
|
||||
return null;
|
||||
|
||||
var lenB = this._buf[offset++] & 0xff;
|
||||
if (lenB === null)
|
||||
return null;
|
||||
|
||||
if ((lenB & 0x80) == 0x80) {
|
||||
lenB &= 0x7f;
|
||||
|
||||
if (lenB == 0)
|
||||
throw newInvalidAsn1Error('Indefinite length not supported');
|
||||
|
||||
if (lenB > 4)
|
||||
throw newInvalidAsn1Error('encoding too long');
|
||||
|
||||
if (this._size - offset < lenB)
|
||||
return null;
|
||||
|
||||
this._len = 0;
|
||||
for (var i = 0; i < lenB; i++)
|
||||
this._len = (this._len << 8) + (this._buf[offset++] & 0xff);
|
||||
|
||||
} else {
|
||||
// Wasn't a variable length
|
||||
this._len = lenB;
|
||||
}
|
||||
|
||||
return offset;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Parses the next sequence in this BER buffer.
|
||||
*
|
||||
* To get the length of the sequence, call `Reader.length`.
|
||||
*
|
||||
* @return {Number} the sequence's tag.
|
||||
*/
|
||||
Reader.prototype.readSequence = function(tag) {
|
||||
var seq = this.peek();
|
||||
if (seq === null)
|
||||
return null;
|
||||
if (tag !== undefined && tag !== seq)
|
||||
throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
|
||||
': got 0x' + seq.toString(16));
|
||||
|
||||
var o = this.readLength(this._offset + 1); // stored in `length`
|
||||
if (o === null)
|
||||
return null;
|
||||
|
||||
this._offset = o;
|
||||
return seq;
|
||||
};
|
||||
|
||||
|
||||
Reader.prototype.readInt = function() {
|
||||
return this._readTag(ASN1.Integer);
|
||||
};
|
||||
|
||||
|
||||
Reader.prototype.readBoolean = function() {
|
||||
return (this._readTag(ASN1.Boolean) === 0 ? false : true);
|
||||
};
|
||||
|
||||
|
||||
Reader.prototype.readEnumeration = function() {
|
||||
return this._readTag(ASN1.Enumeration);
|
||||
};
|
||||
|
||||
|
||||
Reader.prototype.readString = function(tag, retbuf) {
|
||||
if (!tag)
|
||||
tag = ASN1.OctetString;
|
||||
|
||||
var b = this.peek();
|
||||
if (b === null)
|
||||
return null;
|
||||
|
||||
if (b !== tag)
|
||||
throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
|
||||
': got 0x' + b.toString(16));
|
||||
|
||||
var o = this.readLength(this._offset + 1); // stored in `length`
|
||||
|
||||
if (o === null)
|
||||
return null;
|
||||
|
||||
if (this.length > this._size - o)
|
||||
return null;
|
||||
|
||||
this._offset = o;
|
||||
|
||||
if (this.length === 0)
|
||||
return retbuf ? new Buffer(0) : '';
|
||||
|
||||
var str = this._buf.slice(this._offset, this._offset + this.length);
|
||||
this._offset += this.length;
|
||||
|
||||
return retbuf ? str : str.toString('utf8');
|
||||
};
|
||||
|
||||
Reader.prototype.readOID = function(tag) {
|
||||
if (!tag)
|
||||
tag = ASN1.OID;
|
||||
|
||||
var b = this.readString(tag, true);
|
||||
if (b === null)
|
||||
return null;
|
||||
|
||||
var values = [];
|
||||
var value = 0;
|
||||
|
||||
for (var i = 0; i < b.length; i++) {
|
||||
var byte = b[i] & 0xff;
|
||||
|
||||
value <<= 7;
|
||||
value += byte & 0x7f;
|
||||
if ((byte & 0x80) == 0) {
|
||||
values.push(value);
|
||||
value = 0;
|
||||
}
|
||||
}
|
||||
|
||||
value = values.shift();
|
||||
values.unshift(value % 40);
|
||||
values.unshift((value / 40) >> 0);
|
||||
|
||||
return values.join('.');
|
||||
};
|
||||
|
||||
|
||||
Reader.prototype._readTag = function(tag) {
|
||||
assert.ok(tag !== undefined);
|
||||
|
||||
var b = this.peek();
|
||||
|
||||
if (b === null)
|
||||
return null;
|
||||
|
||||
if (b !== tag)
|
||||
throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
|
||||
': got 0x' + b.toString(16));
|
||||
|
||||
var o = this.readLength(this._offset + 1); // stored in `length`
|
||||
if (o === null)
|
||||
return null;
|
||||
|
||||
if (this.length > 4)
|
||||
throw newInvalidAsn1Error('Integer too long: ' + this.length);
|
||||
|
||||
if (this.length > this._size - o)
|
||||
return null;
|
||||
this._offset = o;
|
||||
|
||||
var fb = this._buf[this._offset];
|
||||
var value = 0;
|
||||
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
value <<= 8;
|
||||
value |= (this._buf[this._offset++] & 0xff);
|
||||
}
|
||||
|
||||
if ((fb & 0x80) == 0x80 && i !== 4)
|
||||
value -= (1 << (i * 8));
|
||||
|
||||
return value >> 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
///--- Exported API
|
||||
|
||||
module.exports = Reader;
|
@ -0,0 +1,36 @@
|
||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
|
||||
|
||||
|
||||
module.exports = {
|
||||
EOC: 0,
|
||||
Boolean: 1,
|
||||
Integer: 2,
|
||||
BitString: 3,
|
||||
OctetString: 4,
|
||||
Null: 5,
|
||||
OID: 6,
|
||||
ObjectDescriptor: 7,
|
||||
External: 8,
|
||||
Real: 9, // float
|
||||
Enumeration: 10,
|
||||
PDV: 11,
|
||||
Utf8String: 12,
|
||||
RelativeOID: 13,
|
||||
Sequence: 16,
|
||||
Set: 17,
|
||||
NumericString: 18,
|
||||
PrintableString: 19,
|
||||
T61String: 20,
|
||||
VideotexString: 21,
|
||||
IA5String: 22,
|
||||
UTCTime: 23,
|
||||
GeneralizedTime: 24,
|
||||
GraphicString: 25,
|
||||
VisibleString: 26,
|
||||
GeneralString: 28,
|
||||
UniversalString: 29,
|
||||
CharacterString: 30,
|
||||
BMPString: 31,
|
||||
Constructor: 32,
|
||||
Context: 128
|
||||
};
|
@ -0,0 +1,316 @@
|
||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
|
||||
|
||||
var assert = require('assert');
|
||||
var ASN1 = require('./types');
|
||||
var errors = require('./errors');
|
||||
|
||||
|
||||
///--- Globals
|
||||
|
||||
var newInvalidAsn1Error = errors.newInvalidAsn1Error;
|
||||
|
||||
var DEFAULT_OPTS = {
|
||||
size: 1024,
|
||||
growthFactor: 8
|
||||
};
|
||||
|
||||
|
||||
///--- Helpers
|
||||
|
||||
function merge(from, to) {
|
||||
assert.ok(from);
|
||||
assert.equal(typeof(from), 'object');
|
||||
assert.ok(to);
|
||||
assert.equal(typeof(to), 'object');
|
||||
|
||||
var keys = Object.getOwnPropertyNames(from);
|
||||
keys.forEach(function(key) {
|
||||
if (to[key])
|
||||
return;
|
||||
|
||||
var value = Object.getOwnPropertyDescriptor(from, key);
|
||||
Object.defineProperty(to, key, value);
|
||||
});
|
||||
|
||||
return to;
|
||||
}
|
||||
|
||||
|
||||
|
||||
///--- API
|
||||
|
||||
function Writer(options) {
|
||||
options = merge(DEFAULT_OPTS, options || {});
|
||||
|
||||
this._buf = new Buffer(options.size || 1024);
|
||||
this._size = this._buf.length;
|
||||
this._offset = 0;
|
||||
this._options = options;
|
||||
|
||||
// A list of offsets in the buffer where we need to insert
|
||||
// sequence tag/len pairs.
|
||||
this._seq = [];
|
||||
}
|
||||
|
||||
Object.defineProperty(Writer.prototype, 'buffer', {
|
||||
get: function () {
|
||||
if (this._seq.length)
|
||||
throw new InvalidAsn1Error(this._seq.length + ' unended sequence(s)');
|
||||
|
||||
return (this._buf.slice(0, this._offset));
|
||||
}
|
||||
});
|
||||
|
||||
Writer.prototype.writeByte = function(b) {
|
||||
if (typeof(b) !== 'number')
|
||||
throw new TypeError('argument must be a Number');
|
||||
|
||||
this._ensure(1);
|
||||
this._buf[this._offset++] = b;
|
||||
};
|
||||
|
||||
|
||||
Writer.prototype.writeInt = function(i, tag) {
|
||||
if (typeof(i) !== 'number')
|
||||
throw new TypeError('argument must be a Number');
|
||||
if (typeof(tag) !== 'number')
|
||||
tag = ASN1.Integer;
|
||||
|
||||
var sz = 4;
|
||||
|
||||
while ((((i & 0xff800000) === 0) || ((i & 0xff800000) === 0xff800000 >> 0)) &&
|
||||
(sz > 1)) {
|
||||
sz--;
|
||||
i <<= 8;
|
||||
}
|
||||
|
||||
if (sz > 4)
|
||||
throw new InvalidAsn1Error('BER ints cannot be > 0xffffffff');
|
||||
|
||||
this._ensure(2 + sz);
|
||||
this._buf[this._offset++] = tag;
|
||||
this._buf[this._offset++] = sz;
|
||||
|
||||
while (sz-- > 0) {
|
||||
this._buf[this._offset++] = ((i & 0xff000000) >>> 24);
|
||||
i <<= 8;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
Writer.prototype.writeNull = function() {
|
||||
this.writeByte(ASN1.Null);
|
||||
this.writeByte(0x00);
|
||||
};
|
||||
|
||||
|
||||
Writer.prototype.writeEnumeration = function(i, tag) {
|
||||
if (typeof(i) !== 'number')
|
||||
throw new TypeError('argument must be a Number');
|
||||
if (typeof(tag) !== 'number')
|
||||
tag = ASN1.Enumeration;
|
||||
|
||||
return this.writeInt(i, tag);
|
||||
};
|
||||
|
||||
|
||||
Writer.prototype.writeBoolean = function(b, tag) {
|
||||
if (typeof(b) !== 'boolean')
|
||||
throw new TypeError('argument must be a Boolean');
|
||||
if (typeof(tag) !== 'number')
|
||||
tag = ASN1.Boolean;
|
||||
|
||||
this._ensure(3);
|
||||
this._buf[this._offset++] = tag;
|
||||
this._buf[this._offset++] = 0x01;
|
||||
this._buf[this._offset++] = b ? 0xff : 0x00;
|
||||
};
|
||||
|
||||
|
||||
Writer.prototype.writeString = function(s, tag) {
|
||||
if (typeof(s) !== 'string')
|
||||
throw new TypeError('argument must be a string (was: ' + typeof(s) + ')');
|
||||
if (typeof(tag) !== 'number')
|
||||
tag = ASN1.OctetString;
|
||||
|
||||
var len = Buffer.byteLength(s);
|
||||
this.writeByte(tag);
|
||||
this.writeLength(len);
|
||||
if (len) {
|
||||
this._ensure(len);
|
||||
this._buf.write(s, this._offset);
|
||||
this._offset += len;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Writer.prototype.writeBuffer = function(buf, tag) {
|
||||
if (typeof(tag) !== 'number')
|
||||
throw new TypeError('tag must be a number');
|
||||
if (!Buffer.isBuffer(buf))
|
||||
throw new TypeError('argument must be a buffer');
|
||||
|
||||
this.writeByte(tag);
|
||||
this.writeLength(buf.length);
|
||||
this._ensure(buf.length);
|
||||
buf.copy(this._buf, this._offset, 0, buf.length);
|
||||
this._offset += buf.length;
|
||||
};
|
||||
|
||||
|
||||
Writer.prototype.writeStringArray = function(strings) {
|
||||
if ((!strings instanceof Array))
|
||||
throw new TypeError('argument must be an Array[String]');
|
||||
|
||||
var self = this;
|
||||
strings.forEach(function(s) {
|
||||
self.writeString(s);
|
||||
});
|
||||
};
|
||||
|
||||
// This is really to solve DER cases, but whatever for now
|
||||
Writer.prototype.writeOID = function(s, tag) {
|
||||
if (typeof(s) !== 'string')
|
||||
throw new TypeError('argument must be a string');
|
||||
if (typeof(tag) !== 'number')
|
||||
tag = ASN1.OID;
|
||||
|
||||
if (!/^([0-9]+\.){3,}[0-9]+$/.test(s))
|
||||
throw new Error('argument is not a valid OID string');
|
||||
|
||||
function encodeOctet(bytes, octet) {
|
||||
if (octet < 128) {
|
||||
bytes.push(octet);
|
||||
} else if (octet < 16384) {
|
||||
bytes.push((octet >>> 7) | 0x80);
|
||||
bytes.push(octet & 0x7F);
|
||||
} else if (octet < 2097152) {
|
||||
bytes.push((octet >>> 14) | 0x80);
|
||||
bytes.push(((octet >>> 7) | 0x80) & 0xFF);
|
||||
bytes.push(octet & 0x7F);
|
||||
} else if (octet < 268435456) {
|
||||
bytes.push((octet >>> 21) | 0x80);
|
||||
bytes.push(((octet >>> 14) | 0x80) & 0xFF);
|
||||
bytes.push(((octet >>> 7) | 0x80) & 0xFF);
|
||||
bytes.push(octet & 0x7F);
|
||||
} else {
|
||||
bytes.push(((octet >>> 28) | 0x80) & 0xFF);
|
||||
bytes.push(((octet >>> 21) | 0x80) & 0xFF);
|
||||
bytes.push(((octet >>> 14) | 0x80) & 0xFF);
|
||||
bytes.push(((octet >>> 7) | 0x80) & 0xFF);
|
||||
bytes.push(octet & 0x7F);
|
||||
}
|
||||
}
|
||||
|
||||
var tmp = s.split('.');
|
||||
var bytes = [];
|
||||
bytes.push(parseInt(tmp[0], 10) * 40 + parseInt(tmp[1], 10));
|
||||
tmp.slice(2).forEach(function(b) {
|
||||
encodeOctet(bytes, parseInt(b, 10));
|
||||
});
|
||||
|
||||
var self = this;
|
||||
this._ensure(2 + bytes.length);
|
||||
this.writeByte(tag);
|
||||
this.writeLength(bytes.length);
|
||||
bytes.forEach(function(b) {
|
||||
self.writeByte(b);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Writer.prototype.writeLength = function(len) {
|
||||
if (typeof(len) !== 'number')
|
||||
throw new TypeError('argument must be a Number');
|
||||
|
||||
this._ensure(4);
|
||||
|
||||
if (len <= 0x7f) {
|
||||
this._buf[this._offset++] = len;
|
||||
} else if (len <= 0xff) {
|
||||
this._buf[this._offset++] = 0x81;
|
||||
this._buf[this._offset++] = len;
|
||||
} else if (len <= 0xffff) {
|
||||
this._buf[this._offset++] = 0x82;
|
||||
this._buf[this._offset++] = len >> 8;
|
||||
this._buf[this._offset++] = len;
|
||||
} else if (len <= 0xffffff) {
|
||||
this._buf[this._offset++] = 0x83;
|
||||
this._buf[this._offset++] = len >> 16;
|
||||
this._buf[this._offset++] = len >> 8;
|
||||
this._buf[this._offset++] = len;
|
||||
} else {
|
||||
throw new InvalidAsn1ERror('Length too long (> 4 bytes)');
|
||||
}
|
||||
};
|
||||
|
||||
Writer.prototype.startSequence = function(tag) {
|
||||
if (typeof(tag) !== 'number')
|
||||
tag = ASN1.Sequence | ASN1.Constructor;
|
||||
|
||||
this.writeByte(tag);
|
||||
this._seq.push(this._offset);
|
||||
this._ensure(3);
|
||||
this._offset += 3;
|
||||
};
|
||||
|
||||
|
||||
Writer.prototype.endSequence = function() {
|
||||
var seq = this._seq.pop();
|
||||
var start = seq + 3;
|
||||
var len = this._offset - start;
|
||||
|
||||
if (len <= 0x7f) {
|
||||
this._shift(start, len, -2);
|
||||
this._buf[seq] = len;
|
||||
} else if (len <= 0xff) {
|
||||
this._shift(start, len, -1);
|
||||
this._buf[seq] = 0x81;
|
||||
this._buf[seq + 1] = len;
|
||||
} else if (len <= 0xffff) {
|
||||
this._buf[seq] = 0x82;
|
||||
this._buf[seq + 1] = len >> 8;
|
||||
this._buf[seq + 2] = len;
|
||||
} else if (len <= 0xffffff) {
|
||||
this._shift(start, len, 1);
|
||||
this._buf[seq] = 0x83;
|
||||
this._buf[seq + 1] = len >> 16;
|
||||
this._buf[seq + 2] = len >> 8;
|
||||
this._buf[seq + 3] = len;
|
||||
} else {
|
||||
throw new InvalidAsn1Error('Sequence too long');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Writer.prototype._shift = function(start, len, shift) {
|
||||
assert.ok(start !== undefined);
|
||||
assert.ok(len !== undefined);
|
||||
assert.ok(shift);
|
||||
|
||||
this._buf.copy(this._buf, start + shift, start, start + len);
|
||||
this._offset += shift;
|
||||
};
|
||||
|
||||
Writer.prototype._ensure = function(len) {
|
||||
assert.ok(len);
|
||||
|
||||
if (this._size - this._offset < len) {
|
||||
var sz = this._size * this._options.growthFactor;
|
||||
if (sz - this._offset < len)
|
||||
sz += len;
|
||||
|
||||
var buf = new Buffer(sz);
|
||||
|
||||
this._buf.copy(buf, 0, 0, this._offset);
|
||||
this._buf = buf;
|
||||
this._size = sz;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
///--- Exported API
|
||||
|
||||
module.exports = Writer;
|
@ -0,0 +1,20 @@
|
||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
|
||||
|
||||
// If you have no idea what ASN.1 or BER is, see this:
|
||||
// ftp://ftp.rsa.com/pub/pkcs/ascii/layman.asc
|
||||
|
||||
var Ber = require('./ber/index');
|
||||
|
||||
|
||||
|
||||
///--- Exported API
|
||||
|
||||
module.exports = {
|
||||
|
||||
Ber: Ber,
|
||||
|
||||
BerReader: Ber.Reader,
|
||||
|
||||
BerWriter: Ber.Writer
|
||||
|
||||
};
|
@ -0,0 +1,24 @@
|
||||
{
|
||||
"author": "Mark Cavage <mcavage@gmail.com>",
|
||||
"contributors": [
|
||||
"David Gwynne <loki@animata.net>",
|
||||
"Yunong Xiao <yunong@joyent.com>",
|
||||
"Alex Wilson <alex.wilson@joyent.com>"
|
||||
],
|
||||
"name": "asn1",
|
||||
"description": "Contains parsers and serializers for ASN.1 (currently BER only)",
|
||||
"version": "0.2.3",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/mcavage/node-asn1.git"
|
||||
},
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"tap": "0.4.8"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "./node_modules/.bin/tap ./tst"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
@ -0,0 +1,208 @@
|
||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
|
||||
|
||||
var test = require('tap').test;
|
||||
|
||||
|
||||
|
||||
///--- Globals
|
||||
|
||||
var BerReader;
|
||||
|
||||
|
||||
|
||||
///--- Tests
|
||||
|
||||
test('load library', function(t) {
|
||||
BerReader = require('../../lib/index').BerReader;
|
||||
t.ok(BerReader);
|
||||
try {
|
||||
new BerReader();
|
||||
t.fail('Should have thrown');
|
||||
} catch (e) {
|
||||
t.ok(e instanceof TypeError, 'Should have been a type error');
|
||||
}
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read byte', function(t) {
|
||||
var reader = new BerReader(new Buffer([0xde]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readByte(), 0xde, 'wrong value');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read 1 byte int', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x02, 0x01, 0x03]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readInt(), 0x03, 'wrong value');
|
||||
t.equal(reader.length, 0x01, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read 2 byte int', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x02, 0x02, 0x7e, 0xde]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readInt(), 0x7ede, 'wrong value');
|
||||
t.equal(reader.length, 0x02, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read 3 byte int', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x02, 0x03, 0x7e, 0xde, 0x03]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readInt(), 0x7ede03, 'wrong value');
|
||||
t.equal(reader.length, 0x03, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read 4 byte int', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x02, 0x04, 0x7e, 0xde, 0x03, 0x01]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readInt(), 0x7ede0301, 'wrong value');
|
||||
t.equal(reader.length, 0x04, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read 1 byte negative int', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x02, 0x01, 0xdc]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readInt(), -36, 'wrong value');
|
||||
t.equal(reader.length, 0x01, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read 2 byte negative int', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x02, 0x02, 0xc0, 0x4e]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readInt(), -16306, 'wrong value');
|
||||
t.equal(reader.length, 0x02, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read 3 byte negative int', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x02, 0x03, 0xff, 0x00, 0x19]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readInt(), -65511, 'wrong value');
|
||||
t.equal(reader.length, 0x03, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read 4 byte negative int', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x02, 0x04, 0x91, 0x7c, 0x22, 0x1f]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readInt(), -1854135777, 'wrong value');
|
||||
t.equal(reader.length, 0x04, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read boolean true', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x01, 0x01, 0xff]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readBoolean(), true, 'wrong value');
|
||||
t.equal(reader.length, 0x01, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read boolean false', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x01, 0x01, 0x00]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readBoolean(), false, 'wrong value');
|
||||
t.equal(reader.length, 0x01, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read enumeration', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x0a, 0x01, 0x20]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readEnumeration(), 0x20, 'wrong value');
|
||||
t.equal(reader.length, 0x01, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read string', function(t) {
|
||||
var dn = 'cn=foo,ou=unit,o=test';
|
||||
var buf = new Buffer(dn.length + 2);
|
||||
buf[0] = 0x04;
|
||||
buf[1] = Buffer.byteLength(dn);
|
||||
buf.write(dn, 2);
|
||||
var reader = new BerReader(buf);
|
||||
t.ok(reader);
|
||||
t.equal(reader.readString(), dn, 'wrong value');
|
||||
t.equal(reader.length, dn.length, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('read sequence', function(t) {
|
||||
var reader = new BerReader(new Buffer([0x30, 0x03, 0x01, 0x01, 0xff]));
|
||||
t.ok(reader);
|
||||
t.equal(reader.readSequence(), 0x30, 'wrong value');
|
||||
t.equal(reader.length, 0x03, 'wrong length');
|
||||
t.equal(reader.readBoolean(), true, 'wrong value');
|
||||
t.equal(reader.length, 0x01, 'wrong length');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('anonymous LDAPv3 bind', function(t) {
|
||||
var BIND = new Buffer(14);
|
||||
BIND[0] = 0x30; // Sequence
|
||||
BIND[1] = 12; // len
|
||||
BIND[2] = 0x02; // ASN.1 Integer
|
||||
BIND[3] = 1; // len
|
||||
BIND[4] = 0x04; // msgid (make up 4)
|
||||
BIND[5] = 0x60; // Bind Request
|
||||
BIND[6] = 7; // len
|
||||
BIND[7] = 0x02; // ASN.1 Integer
|
||||
BIND[8] = 1; // len
|
||||
BIND[9] = 0x03; // v3
|
||||
BIND[10] = 0x04; // String (bind dn)
|
||||
BIND[11] = 0; // len
|
||||
BIND[12] = 0x80; // ContextSpecific (choice)
|
||||
BIND[13] = 0; // simple bind
|
||||
|
||||
// Start testing ^^
|
||||
var ber = new BerReader(BIND);
|
||||
t.equal(ber.readSequence(), 48, 'Not an ASN.1 Sequence');
|
||||
t.equal(ber.length, 12, 'Message length should be 12');
|
||||
t.equal(ber.readInt(), 4, 'Message id should have been 4');
|
||||
t.equal(ber.readSequence(), 96, 'Bind Request should have been 96');
|
||||
t.equal(ber.length, 7, 'Bind length should have been 7');
|
||||
t.equal(ber.readInt(), 3, 'LDAP version should have been 3');
|
||||
t.equal(ber.readString(), '', 'Bind DN should have been empty');
|
||||
t.equal(ber.length, 0, 'string length should have been 0');
|
||||
t.equal(ber.readByte(), 0x80, 'Should have been ContextSpecific (choice)');
|
||||
t.equal(ber.readByte(), 0, 'Should have been simple bind');
|
||||
t.equal(null, ber.readByte(), 'Should be out of data');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('long string', function(t) {
|
||||
var buf = new Buffer(256);
|
||||
var o;
|
||||
var s =
|
||||
'2;649;CN=Red Hat CS 71GA Demo,O=Red Hat CS 71GA Demo,C=US;' +
|
||||
'CN=RHCS Agent - admin01,UID=admin01,O=redhat,C=US [1] This is ' +
|
||||
'Teena Vradmin\'s description.';
|
||||
buf[0] = 0x04;
|
||||
buf[1] = 0x81;
|
||||
buf[2] = 0x94;
|
||||
buf.write(s, 3);
|
||||
var ber = new BerReader(buf.slice(0, 3 + s.length));
|
||||
t.equal(ber.readString(), s);
|
||||
t.end();
|
||||
});
|
@ -0,0 +1,370 @@
|
||||
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
|
||||
|
||||
var test = require('tap').test;
|
||||
var sys = require('sys');
|
||||
|
||||
///--- Globals
|
||||
|
||||
var BerWriter;
|
||||
|
||||
var BerReader;
|
||||
|
||||
|
||||
///--- Tests
|
||||
|
||||
test('load library', function(t) {
|
||||
BerWriter = require('../../lib/index').BerWriter;
|
||||
t.ok(BerWriter);
|
||||
t.ok(new BerWriter());
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('write byte', function(t) {
|
||||
var writer = new BerWriter();
|
||||
|
||||
writer.writeByte(0xC2);
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
t.equal(ber.length, 1, 'Wrong length');
|
||||
t.equal(ber[0], 0xC2, 'value wrong');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('write 1 byte int', function(t) {
|
||||
var writer = new BerWriter();
|
||||
|
||||
writer.writeInt(0x7f);
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
t.equal(ber.length, 3, 'Wrong length for an int: ' + ber.length);
|
||||
t.equal(ber[0], 0x02, 'ASN.1 tag wrong (2) -> ' + ber[0]);
|
||||
t.equal(ber[1], 0x01, 'length wrong(1) -> ' + ber[1]);
|
||||
t.equal(ber[2], 0x7f, 'value wrong(3) -> ' + ber[2]);
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('write 2 byte int', function(t) {
|
||||
var writer = new BerWriter();
|
||||
|
||||
writer.writeInt(0x7ffe);
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
t.equal(ber.length, 4, 'Wrong length for an int');
|
||||
t.equal(ber[0], 0x02, 'ASN.1 tag wrong');
|
||||
t.equal(ber[1], 0x02, 'length wrong');
|
||||
t.equal(ber[2], 0x7f, 'value wrong (byte 1)');
|
||||
t.equal(ber[3], 0xfe, 'value wrong (byte 2)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('write 3 byte int', function(t) {
|
||||
var writer = new BerWriter();
|
||||
|
||||
writer.writeInt(0x7ffffe);
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
t.equal(ber.length, 5, 'Wrong length for an int');
|
||||
t.equal(ber[0], 0x02, 'ASN.1 tag wrong');
|
||||
t.equal(ber[1], 0x03, 'length wrong');
|
||||
t.equal(ber[2], 0x7f, 'value wrong (byte 1)');
|
||||
t.equal(ber[3], 0xff, 'value wrong (byte 2)');
|
||||
t.equal(ber[4], 0xfe, 'value wrong (byte 3)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('write 4 byte int', function(t) {
|
||||
var writer = new BerWriter();
|
||||
|
||||
writer.writeInt(0x7ffffffe);
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
|
||||
t.equal(ber.length, 6, 'Wrong length for an int');
|
||||
t.equal(ber[0], 0x02, 'ASN.1 tag wrong');
|
||||
t.equal(ber[1], 0x04, 'length wrong');
|
||||
t.equal(ber[2], 0x7f, 'value wrong (byte 1)');
|
||||
t.equal(ber[3], 0xff, 'value wrong (byte 2)');
|
||||
t.equal(ber[4], 0xff, 'value wrong (byte 3)');
|
||||
t.equal(ber[5], 0xfe, 'value wrong (byte 4)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('write 1 byte negative int', function(t) {
|
||||
var writer = new BerWriter();
|
||||
|
||||
writer.writeInt(-128);
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
|
||||
t.equal(ber.length, 3, 'Wrong length for an int');
|
||||
t.equal(ber[0], 0x02, 'ASN.1 tag wrong');
|
||||
t.equal(ber[1], 0x01, 'length wrong');
|
||||
t.equal(ber[2], 0x80, 'value wrong (byte 1)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('write 2 byte negative int', function(t) {
|
||||
var writer = new BerWriter();
|
||||
|
||||
writer.writeInt(-22400);
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
|
||||
t.equal(ber.length, 4, 'Wrong length for an int');
|
||||
t.equal(ber[0], 0x02, 'ASN.1 tag wrong');
|
||||
t.equal(ber[1], 0x02, 'length wrong');
|
||||
t.equal(ber[2], 0xa8, 'value wrong (byte 1)');
|
||||
t.equal(ber[3], 0x80, 'value wrong (byte 2)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('write 3 byte negative int', function(t) {
|
||||
var writer = new BerWriter();
|
||||
|
||||
writer.writeInt(-481653);
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
|
||||
t.equal(ber.length, 5, 'Wrong length for an int');
|
||||
t.equal(ber[0], 0x02, 'ASN.1 tag wrong');
|
||||
t.equal(ber[1], 0x03, 'length wrong');
|
||||
t.equal(ber[2], 0xf8, 'value wrong (byte 1)');
|
||||
t.equal(ber[3], 0xa6, 'value wrong (byte 2)');
|
||||
t.equal(ber[4], 0x8b, 'value wrong (byte 3)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('write 4 byte negative int', function(t) {
|
||||
var writer = new BerWriter();
|
||||
|
||||
writer.writeInt(-1522904131);
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
|
||||
t.equal(ber.length, 6, 'Wrong length for an int');
|
||||
t.equal(ber[0], 0x02, 'ASN.1 tag wrong');
|
||||
t.equal(ber[1], 0x04, 'length wrong');
|
||||
t.equal(ber[2], 0xa5, 'value wrong (byte 1)');
|
||||
t.equal(ber[3], 0x3a, 'value wrong (byte 2)');
|
||||
t.equal(ber[4], 0x53, 'value wrong (byte 3)');
|
||||
t.equal(ber[5], 0xbd, 'value wrong (byte 4)');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('write boolean', function(t) {
|
||||
var writer = new BerWriter();
|
||||
|
||||
writer.writeBoolean(true);
|
||||
writer.writeBoolean(false);
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
t.equal(ber.length, 6, 'Wrong length');
|
||||
t.equal(ber[0], 0x01, 'tag wrong');
|
||||
t.equal(ber[1], 0x01, 'length wrong');
|
||||
t.equal(ber[2], 0xff, 'value wrong');
|
||||
t.equal(ber[3], 0x01, 'tag wrong');
|
||||
t.equal(ber[4], 0x01, 'length wrong');
|
||||
t.equal(ber[5], 0x00, 'value wrong');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('write string', function(t) {
|
||||
var writer = new BerWriter();
|
||||
writer.writeString('hello world');
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
t.equal(ber.length, 13, 'wrong length');
|
||||
t.equal(ber[0], 0x04, 'wrong tag');
|
||||
t.equal(ber[1], 11, 'wrong length');
|
||||
t.equal(ber.slice(2).toString('utf8'), 'hello world', 'wrong value');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('write buffer', function(t) {
|
||||
var writer = new BerWriter();
|
||||
// write some stuff to start with
|
||||
writer.writeString('hello world');
|
||||
var ber = writer.buffer;
|
||||
var buf = new Buffer([0x04, 0x0b, 0x30, 0x09, 0x02, 0x01, 0x0f, 0x01, 0x01,
|
||||
0xff, 0x01, 0x01, 0xff]);
|
||||
writer.writeBuffer(buf.slice(2, buf.length), 0x04);
|
||||
ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
t.equal(ber.length, 26, 'wrong length');
|
||||
t.equal(ber[0], 0x04, 'wrong tag');
|
||||
t.equal(ber[1], 11, 'wrong length');
|
||||
t.equal(ber.slice(2, 13).toString('utf8'), 'hello world', 'wrong value');
|
||||
t.equal(ber[13], buf[0], 'wrong tag');
|
||||
t.equal(ber[14], buf[1], 'wrong length');
|
||||
for (var i = 13, j = 0; i < ber.length && j < buf.length; i++, j++) {
|
||||
t.equal(ber[i], buf[j], 'buffer contents not identical');
|
||||
}
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('write string array', function(t) {
|
||||
var writer = new BerWriter();
|
||||
writer.writeStringArray(['hello world', 'fubar!']);
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
|
||||
t.equal(ber.length, 21, 'wrong length');
|
||||
t.equal(ber[0], 0x04, 'wrong tag');
|
||||
t.equal(ber[1], 11, 'wrong length');
|
||||
t.equal(ber.slice(2, 13).toString('utf8'), 'hello world', 'wrong value');
|
||||
|
||||
t.equal(ber[13], 0x04, 'wrong tag');
|
||||
t.equal(ber[14], 6, 'wrong length');
|
||||
t.equal(ber.slice(15).toString('utf8'), 'fubar!', 'wrong value');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('resize internal buffer', function(t) {
|
||||
var writer = new BerWriter({size: 2});
|
||||
writer.writeString('hello world');
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
t.equal(ber.length, 13, 'wrong length');
|
||||
t.equal(ber[0], 0x04, 'wrong tag');
|
||||
t.equal(ber[1], 11, 'wrong length');
|
||||
t.equal(ber.slice(2).toString('utf8'), 'hello world', 'wrong value');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('sequence', function(t) {
|
||||
var writer = new BerWriter({size: 25});
|
||||
writer.startSequence();
|
||||
writer.writeString('hello world');
|
||||
writer.endSequence();
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
console.log(ber);
|
||||
t.equal(ber.length, 15, 'wrong length');
|
||||
t.equal(ber[0], 0x30, 'wrong tag');
|
||||
t.equal(ber[1], 13, 'wrong length');
|
||||
t.equal(ber[2], 0x04, 'wrong tag');
|
||||
t.equal(ber[3], 11, 'wrong length');
|
||||
t.equal(ber.slice(4).toString('utf8'), 'hello world', 'wrong value');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('nested sequence', function(t) {
|
||||
var writer = new BerWriter({size: 25});
|
||||
writer.startSequence();
|
||||
writer.writeString('hello world');
|
||||
writer.startSequence();
|
||||
writer.writeString('hello world');
|
||||
writer.endSequence();
|
||||
writer.endSequence();
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
t.equal(ber.length, 30, 'wrong length');
|
||||
t.equal(ber[0], 0x30, 'wrong tag');
|
||||
t.equal(ber[1], 28, 'wrong length');
|
||||
t.equal(ber[2], 0x04, 'wrong tag');
|
||||
t.equal(ber[3], 11, 'wrong length');
|
||||
t.equal(ber.slice(4, 15).toString('utf8'), 'hello world', 'wrong value');
|
||||
t.equal(ber[15], 0x30, 'wrong tag');
|
||||
t.equal(ber[16], 13, 'wrong length');
|
||||
t.equal(ber[17], 0x04, 'wrong tag');
|
||||
t.equal(ber[18], 11, 'wrong length');
|
||||
t.equal(ber.slice(19, 30).toString('utf8'), 'hello world', 'wrong value');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('LDAP bind message', function(t) {
|
||||
var dn = 'cn=foo,ou=unit,o=test';
|
||||
var writer = new BerWriter();
|
||||
writer.startSequence();
|
||||
writer.writeInt(3); // msgid = 3
|
||||
writer.startSequence(0x60); // ldap bind
|
||||
writer.writeInt(3); // ldap v3
|
||||
writer.writeString(dn);
|
||||
writer.writeByte(0x80);
|
||||
writer.writeByte(0x00);
|
||||
writer.endSequence();
|
||||
writer.endSequence();
|
||||
var ber = writer.buffer;
|
||||
|
||||
t.ok(ber);
|
||||
t.equal(ber.length, 35, 'wrong length (buffer)');
|
||||
t.equal(ber[0], 0x30, 'wrong tag');
|
||||
t.equal(ber[1], 33, 'wrong length');
|
||||
t.equal(ber[2], 0x02, 'wrong tag');
|
||||
t.equal(ber[3], 1, 'wrong length');
|
||||
t.equal(ber[4], 0x03, 'wrong value');
|
||||
t.equal(ber[5], 0x60, 'wrong tag');
|
||||
t.equal(ber[6], 28, 'wrong length');
|
||||
t.equal(ber[7], 0x02, 'wrong tag');
|
||||
t.equal(ber[8], 1, 'wrong length');
|
||||
t.equal(ber[9], 0x03, 'wrong value');
|
||||
t.equal(ber[10], 0x04, 'wrong tag');
|
||||
t.equal(ber[11], dn.length, 'wrong length');
|
||||
t.equal(ber.slice(12, 33).toString('utf8'), dn, 'wrong value');
|
||||
t.equal(ber[33], 0x80, 'wrong tag');
|
||||
t.equal(ber[34], 0x00, 'wrong len');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
||||
test('Write OID', function(t) {
|
||||
var oid = '1.2.840.113549.1.1.1';
|
||||
var writer = new BerWriter();
|
||||
writer.writeOID(oid);
|
||||
|
||||
var ber = writer.buffer;
|
||||
t.ok(ber);
|
||||
console.log(require('util').inspect(ber));
|
||||
console.log(require('util').inspect(new Buffer([0x06, 0x09, 0x2a, 0x86,
|
||||
0x48, 0x86, 0xf7, 0x0d,
|
||||
0x01, 0x01, 0x01])));
|
||||
|
||||
t.end();
|
||||
});
|
@ -0,0 +1,6 @@
|
||||
Dave Eddy <dave@daveeddy.com>
|
||||
Fred Kuo <fred.kuo@joyent.com>
|
||||
Lars-Magnus Skog <ralphtheninja@riseup.net>
|
||||
Mark Cavage <mcavage@gmail.com>
|
||||
Patrick Mooney <pmooney@pfmooney.com>
|
||||
Rob Gulewich <robert.gulewich@joyent.com>
|
@ -0,0 +1,14 @@
|
||||
# assert-plus Changelog
|
||||
|
||||
## 1.0.0
|
||||
|
||||
- *BREAKING* assert.number (and derivatives) now accept Infinity as valid input
|
||||
- Add assert.finite check. Previous assert.number callers should use this if
|
||||
they expect Infinity inputs to throw.
|
||||
|
||||
## 0.2.0
|
||||
|
||||
- Fix `assert.object(null)` so it throws
|
||||
- Fix optional/arrayOf exports for non-type-of asserts
|
||||
- Add optiona/arrayOf exports for Stream/Date/Regex/uuid
|
||||
- Add basic unit test coverage
|
@ -0,0 +1,162 @@
|
||||
# assert-plus
|
||||
|
||||
This library is a super small wrapper over node's assert module that has two
|
||||
things: (1) the ability to disable assertions with the environment variable
|
||||
NODE\_NDEBUG, and (2) some API wrappers for argument testing. Like
|
||||
`assert.string(myArg, 'myArg')`. As a simple example, most of my code looks
|
||||
like this:
|
||||
|
||||
```javascript
|
||||
var assert = require('assert-plus');
|
||||
|
||||
function fooAccount(options, callback) {
|
||||
assert.object(options, 'options');
|
||||
assert.number(options.id, 'options.id');
|
||||
assert.bool(options.isManager, 'options.isManager');
|
||||
assert.string(options.name, 'options.name');
|
||||
assert.arrayOfString(options.email, 'options.email');
|
||||
assert.func(callback, 'callback');
|
||||
|
||||
// Do stuff
|
||||
callback(null, {});
|
||||
}
|
||||
```
|
||||
|
||||
# API
|
||||
|
||||
All methods that *aren't* part of node's core assert API are simply assumed to
|
||||
take an argument, and then a string 'name' that's not a message; `AssertionError`
|
||||
will be thrown if the assertion fails with a message like:
|
||||
|
||||
AssertionError: foo (string) is required
|
||||
at test (/home/mark/work/foo/foo.js:3:9)
|
||||
at Object.<anonymous> (/home/mark/work/foo/foo.js:15:1)
|
||||
at Module._compile (module.js:446:26)
|
||||
at Object..js (module.js:464:10)
|
||||
at Module.load (module.js:353:31)
|
||||
at Function._load (module.js:311:12)
|
||||
at Array.0 (module.js:484:10)
|
||||
at EventEmitter._tickCallback (node.js:190:38)
|
||||
|
||||
from:
|
||||
|
||||
```javascript
|
||||
function test(foo) {
|
||||
assert.string(foo, 'foo');
|
||||
}
|
||||
```
|
||||
|
||||
There you go. You can check that arrays are of a homogeneous type with `Arrayof$Type`:
|
||||
|
||||
```javascript
|
||||
function test(foo) {
|
||||
assert.arrayOfString(foo, 'foo');
|
||||
}
|
||||
```
|
||||
|
||||
You can assert IFF an argument is not `undefined` (i.e., an optional arg):
|
||||
|
||||
```javascript
|
||||
assert.optionalString(foo, 'foo');
|
||||
```
|
||||
|
||||
Lastly, you can opt-out of assertion checking altogether by setting the
|
||||
environment variable `NODE_NDEBUG=1`. This is pseudo-useful if you have
|
||||
lots of assertions, and don't want to pay `typeof ()` taxes to v8 in
|
||||
production. Be advised: The standard functions re-exported from `assert` are
|
||||
also disabled in assert-plus if NDEBUG is specified. Using them directly from
|
||||
the `assert` module avoids this behavior.
|
||||
|
||||
The complete list of APIs is:
|
||||
|
||||
* assert.array
|
||||
* assert.bool
|
||||
* assert.buffer
|
||||
* assert.func
|
||||
* assert.number
|
||||
* assert.finite
|
||||
* assert.object
|
||||
* assert.string
|
||||
* assert.stream
|
||||
* assert.date
|
||||
* assert.regexp
|
||||
* assert.uuid
|
||||
* assert.arrayOfArray
|
||||
* assert.arrayOfBool
|
||||
* assert.arrayOfBuffer
|
||||
* assert.arrayOfFunc
|
||||
* assert.arrayOfNumber
|
||||
* assert.arrayOfFinite
|
||||
* assert.arrayOfObject
|
||||
* assert.arrayOfString
|
||||
* assert.arrayOfStream
|
||||
* assert.arrayOfDate
|
||||
* assert.arrayOfRegexp
|
||||
* assert.arrayOfUuid
|
||||
* assert.optionalArray
|
||||
* assert.optionalBool
|
||||
* assert.optionalBuffer
|
||||
* assert.optionalFunc
|
||||
* assert.optionalNumber
|
||||
* assert.optionalFinite
|
||||
* assert.optionalObject
|
||||
* assert.optionalString
|
||||
* assert.optionalStream
|
||||
* assert.optionalDate
|
||||
* assert.optionalRegexp
|
||||
* assert.optionalUuid
|
||||
* assert.optionalArrayOfArray
|
||||
* assert.optionalArrayOfBool
|
||||
* assert.optionalArrayOfBuffer
|
||||
* assert.optionalArrayOfFunc
|
||||
* assert.optionalArrayOfNumber
|
||||
* assert.optionalArrayOfFinite
|
||||
* assert.optionalArrayOfObject
|
||||
* assert.optionalArrayOfString
|
||||
* assert.optionalArrayOfStream
|
||||
* assert.optionalArrayOfDate
|
||||
* assert.optionalArrayOfRegexp
|
||||
* assert.optionalArrayOfUuid
|
||||
* assert.AssertionError
|
||||
* assert.fail
|
||||
* assert.ok
|
||||
* assert.equal
|
||||
* assert.notEqual
|
||||
* assert.deepEqual
|
||||
* assert.notDeepEqual
|
||||
* assert.strictEqual
|
||||
* assert.notStrictEqual
|
||||
* assert.throws
|
||||
* assert.doesNotThrow
|
||||
* assert.ifError
|
||||
|
||||
# Installation
|
||||
|
||||
npm install assert-plus
|
||||
|
||||
## License
|
||||
|
||||
The MIT License (MIT)
|
||||
Copyright (c) 2012 Mark Cavage
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
## Bugs
|
||||
|
||||
See <https://github.com/mcavage/node-assert-plus/issues>.
|
@ -0,0 +1,211 @@
|
||||
// Copyright (c) 2012, Mark Cavage. All rights reserved.
|
||||
// Copyright 2015 Joyent, Inc.
|
||||
|
||||
var assert = require('assert');
|
||||
var Stream = require('stream').Stream;
|
||||
var util = require('util');
|
||||
|
||||
|
||||
///--- Globals
|
||||
|
||||
/* JSSTYLED */
|
||||
var UUID_REGEXP = /^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/;
|
||||
|
||||
|
||||
///--- Internal
|
||||
|
||||
function _capitalize(str) {
|
||||
return (str.charAt(0).toUpperCase() + str.slice(1));
|
||||
}
|
||||
|
||||
function _toss(name, expected, oper, arg, actual) {
|
||||
throw new assert.AssertionError({
|
||||
message: util.format('%s (%s) is required', name, expected),
|
||||
actual: (actual === undefined) ? typeof (arg) : actual(arg),
|
||||
expected: expected,
|
||||
operator: oper || '===',
|
||||
stackStartFunction: _toss.caller
|
||||
});
|
||||
}
|
||||
|
||||
function _getClass(arg) {
|
||||
return (Object.prototype.toString.call(arg).slice(8, -1));
|
||||
}
|
||||
|
||||
function noop() {
|
||||
// Why even bother with asserts?
|
||||
}
|
||||
|
||||
|
||||
///--- Exports
|
||||
|
||||
var types = {
|
||||
bool: {
|
||||
check: function (arg) { return typeof (arg) === 'boolean'; }
|
||||
},
|
||||
func: {
|
||||
check: function (arg) { return typeof (arg) === 'function'; }
|
||||
},
|
||||
string: {
|
||||
check: function (arg) { return typeof (arg) === 'string'; }
|
||||
},
|
||||
object: {
|
||||
check: function (arg) {
|
||||
return typeof (arg) === 'object' && arg !== null;
|
||||
}
|
||||
},
|
||||
number: {
|
||||
check: function (arg) {
|
||||
return typeof (arg) === 'number' && !isNaN(arg);
|
||||
}
|
||||
},
|
||||
finite: {
|
||||
check: function (arg) {
|
||||
return typeof (arg) === 'number' && !isNaN(arg) && isFinite(arg);
|
||||
}
|
||||
},
|
||||
buffer: {
|
||||
check: function (arg) { return Buffer.isBuffer(arg); },
|
||||
operator: 'Buffer.isBuffer'
|
||||
},
|
||||
array: {
|
||||
check: function (arg) { return Array.isArray(arg); },
|
||||
operator: 'Array.isArray'
|
||||
},
|
||||
stream: {
|
||||
check: function (arg) { return arg instanceof Stream; },
|
||||
operator: 'instanceof',
|
||||
actual: _getClass
|
||||
},
|
||||
date: {
|
||||
check: function (arg) { return arg instanceof Date; },
|
||||
operator: 'instanceof',
|
||||
actual: _getClass
|
||||
},
|
||||
regexp: {
|
||||
check: function (arg) { return arg instanceof RegExp; },
|
||||
operator: 'instanceof',
|
||||
actual: _getClass
|
||||
},
|
||||
uuid: {
|
||||
check: function (arg) {
|
||||
return typeof (arg) === 'string' && UUID_REGEXP.test(arg);
|
||||
},
|
||||
operator: 'isUUID'
|
||||
}
|
||||
};
|
||||
|
||||
function _setExports(ndebug) {
|
||||
var keys = Object.keys(types);
|
||||
var out;
|
||||
|
||||
/* re-export standard assert */
|
||||
if (process.env.NODE_NDEBUG) {
|
||||
out = noop;
|
||||
} else {
|
||||
out = function (arg, msg) {
|
||||
if (!arg) {
|
||||
_toss(msg, 'true', arg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/* standard checks */
|
||||
keys.forEach(function (k) {
|
||||
if (ndebug) {
|
||||
out[k] = noop;
|
||||
return;
|
||||
}
|
||||
var type = types[k];
|
||||
out[k] = function (arg, msg) {
|
||||
if (!type.check(arg)) {
|
||||
_toss(msg, k, type.operator, arg, type.actual);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
/* optional checks */
|
||||
keys.forEach(function (k) {
|
||||
var name = 'optional' + _capitalize(k);
|
||||
if (ndebug) {
|
||||
out[name] = noop;
|
||||
return;
|
||||
}
|
||||
var type = types[k];
|
||||
out[name] = function (arg, msg) {
|
||||
if (arg === undefined || arg === null) {
|
||||
return;
|
||||
}
|
||||
if (!type.check(arg)) {
|
||||
_toss(msg, k, type.operator, arg, type.actual);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
/* arrayOf checks */
|
||||
keys.forEach(function (k) {
|
||||
var name = 'arrayOf' + _capitalize(k);
|
||||
if (ndebug) {
|
||||
out[name] = noop;
|
||||
return;
|
||||
}
|
||||
var type = types[k];
|
||||
var expected = '[' + k + ']';
|
||||
out[name] = function (arg, msg) {
|
||||
if (!Array.isArray(arg)) {
|
||||
_toss(msg, expected, type.operator, arg, type.actual);
|
||||
}
|
||||
var i;
|
||||
for (i = 0; i < arg.length; i++) {
|
||||
if (!type.check(arg[i])) {
|
||||
_toss(msg, expected, type.operator, arg, type.actual);
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
/* optionalArrayOf checks */
|
||||
keys.forEach(function (k) {
|
||||
var name = 'optionalArrayOf' + _capitalize(k);
|
||||
if (ndebug) {
|
||||
out[name] = noop;
|
||||
return;
|
||||
}
|
||||
var type = types[k];
|
||||
var expected = '[' + k + ']';
|
||||
out[name] = function (arg, msg) {
|
||||
if (arg === undefined || arg === null) {
|
||||
return;
|
||||
}
|
||||
if (!Array.isArray(arg)) {
|
||||
_toss(msg, expected, type.operator, arg, type.actual);
|
||||
}
|
||||
var i;
|
||||
for (i = 0; i < arg.length; i++) {
|
||||
if (!type.check(arg[i])) {
|
||||
_toss(msg, expected, type.operator, arg, type.actual);
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
/* re-export built-in assertions */
|
||||
Object.keys(assert).forEach(function (k) {
|
||||
if (k === 'AssertionError') {
|
||||
out[k] = assert[k];
|
||||
return;
|
||||
}
|
||||
if (ndebug) {
|
||||
out[k] = noop;
|
||||
return;
|
||||
}
|
||||
out[k] = assert[k];
|
||||
});
|
||||
|
||||
/* export ourselves (for unit tests _only_) */
|
||||
out._setExports = _setExports;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
module.exports = _setExports(process.env.NODE_NDEBUG);
|
@ -0,0 +1,23 @@
|
||||
{
|
||||
"author": "Mark Cavage <mcavage@gmail.com>",
|
||||
"name": "assert-plus",
|
||||
"description": "Extra assertions on top of node's assert module",
|
||||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
"main": "./assert.js",
|
||||
"devDependencies": {
|
||||
"tape": "4.2.2",
|
||||
"faucet": "0.0.1"
|
||||
},
|
||||
"optionalDependencies": {},
|
||||
"scripts": {
|
||||
"test": "./node_modules/.bin/tape tests/*.js | ./node_modules/.bin/faucet"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mcavage/node-assert-plus.git"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.8"
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Alex Indigo
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,233 @@
|
||||
# asynckit [](https://www.npmjs.com/package/asynckit)
|
||||
|
||||
Minimal async jobs utility library, with streams support.
|
||||
|
||||
[](https://travis-ci.org/alexindigo/asynckit)
|
||||
[](https://travis-ci.org/alexindigo/asynckit)
|
||||
[](https://ci.appveyor.com/project/alexindigo/asynckit)
|
||||
|
||||
[](https://coveralls.io/github/alexindigo/asynckit?branch=master)
|
||||
[](https://david-dm.org/alexindigo/asynckit)
|
||||
[](https://www.bithound.io/github/alexindigo/asynckit)
|
||||
|
||||
<!-- [](https://www.npmjs.com/package/reamde) -->
|
||||
|
||||
AsyncKit provides harness for `parallel` and `serial` iterators over list of items represented by arrays or objects.
|
||||
Optionally it accepts abort function (should be synchronously return by iterator for each item), and terminates left over jobs upon an error event. For specific iteration order built-in (`ascending` and `descending`) and custom sort helpers also supported, via `asynckit.serialOrdered` method.
|
||||
|
||||
It ensures async operations to keep behavior more stable and prevent `Maximum call stack size exceeded` errors, from sync iterators.
|
||||
|
||||
| compression | size |
|
||||
| :----------------- | -------: |
|
||||
| asynckit.js | 12.34 kB |
|
||||
| asynckit.min.js | 4.11 kB |
|
||||
| asynckit.min.js.gz | 1.47 kB |
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
$ npm install --save asynckit
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Parallel Jobs
|
||||
|
||||
Runs iterator over provided array in parallel. Stores output in the `result` array,
|
||||
on the matching positions. In unlikely event of an error from one of the jobs,
|
||||
will terminate rest of the active jobs (if abort function is provided)
|
||||
and return error along with salvaged data to the main callback function.
|
||||
|
||||
#### Input Array
|
||||
|
||||
```javascript
|
||||
var parallel = require('asynckit').parallel
|
||||
, assert = require('assert')
|
||||
;
|
||||
|
||||
var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ]
|
||||
, expectedResult = [ 2, 2, 8, 32, 128, 64, 16, 4 ]
|
||||
, expectedTarget = [ 1, 1, 2, 4, 8, 16, 32, 64 ]
|
||||
, target = []
|
||||
;
|
||||
|
||||
parallel(source, asyncJob, function(err, result)
|
||||
{
|
||||
assert.deepEqual(result, expectedResult);
|
||||
assert.deepEqual(target, expectedTarget);
|
||||
});
|
||||
|
||||
// async job accepts one element from the array
|
||||
// and a callback function
|
||||
function asyncJob(item, cb)
|
||||
{
|
||||
// different delays (in ms) per item
|
||||
var delay = item * 25;
|
||||
|
||||
// pretend different jobs take different time to finish
|
||||
// and not in consequential order
|
||||
var timeoutId = setTimeout(function() {
|
||||
target.push(item);
|
||||
cb(null, item * 2);
|
||||
}, delay);
|
||||
|
||||
// allow to cancel "leftover" jobs upon error
|
||||
// return function, invoking of which will abort this job
|
||||
return clearTimeout.bind(null, timeoutId);
|
||||
}
|
||||
```
|
||||
|
||||
More examples could be found in [test/test-parallel-array.js](test/test-parallel-array.js).
|
||||
|
||||
#### Input Object
|
||||
|
||||
Also it supports named jobs, listed via object.
|
||||
|
||||
```javascript
|
||||
var parallel = require('asynckit/parallel')
|
||||
, assert = require('assert')
|
||||
;
|
||||
|
||||
var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 }
|
||||
, expectedResult = { first: 2, one: 2, four: 8, sixteen: 32, sixtyFour: 128, thirtyTwo: 64, eight: 16, two: 4 }
|
||||
, expectedTarget = [ 1, 1, 2, 4, 8, 16, 32, 64 ]
|
||||
, expectedKeys = [ 'first', 'one', 'two', 'four', 'eight', 'sixteen', 'thirtyTwo', 'sixtyFour' ]
|
||||
, target = []
|
||||
, keys = []
|
||||
;
|
||||
|
||||
parallel(source, asyncJob, function(err, result)
|
||||
{
|
||||
assert.deepEqual(result, expectedResult);
|
||||
assert.deepEqual(target, expectedTarget);
|
||||
assert.deepEqual(keys, expectedKeys);
|
||||
});
|
||||
|
||||
// supports full value, key, callback (shortcut) interface
|
||||
function asyncJob(item, key, cb)
|
||||
{
|
||||
// different delays (in ms) per item
|
||||
var delay = item * 25;
|
||||
|
||||
// pretend different jobs take different time to finish
|
||||
// and not in consequential order
|
||||
var timeoutId = setTimeout(function() {
|
||||
keys.push(key);
|
||||
target.push(item);
|
||||
cb(null, item * 2);
|
||||
}, delay);
|
||||
|
||||
// allow to cancel "leftover" jobs upon error
|
||||
// return function, invoking of which will abort this job
|
||||
return clearTimeout.bind(null, timeoutId);
|
||||
}
|
||||
```
|
||||
|
||||
More examples could be found in [test/test-parallel-object.js](test/test-parallel-object.js).
|
||||
|
||||
### Serial Jobs
|
||||
|
||||
Runs iterator over provided array sequentially. Stores output in the `result` array,
|
||||
on the matching positions. In unlikely event of an error from one of the jobs,
|
||||
will not proceed to the rest of the items in the list
|
||||
and return error along with salvaged data to the main callback function.
|
||||
|
||||
#### Input Array
|
||||
|
||||
```javascript
|
||||
var serial = require('asynckit/serial')
|
||||
, assert = require('assert')
|
||||
;
|
||||
|
||||
var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ]
|
||||
, expectedResult = [ 2, 2, 8, 32, 128, 64, 16, 4 ]
|
||||
, expectedTarget = [ 0, 1, 2, 3, 4, 5, 6, 7 ]
|
||||
, target = []
|
||||
;
|
||||
|
||||
serial(source, asyncJob, function(err, result)
|
||||
{
|
||||
assert.deepEqual(result, expectedResult);
|
||||
assert.deepEqual(target, expectedTarget);
|
||||
});
|
||||
|
||||
// extended interface (item, key, callback)
|
||||
// also supported for arrays
|
||||
function asyncJob(item, key, cb)
|
||||
{
|
||||
target.push(key);
|
||||
|
||||
// it will be automatically made async
|
||||
// even it iterator "returns" in the same event loop
|
||||
cb(null, item * 2);
|
||||
}
|
||||
```
|
||||
|
||||
More examples could be found in [test/test-serial-array.js](test/test-serial-array.js).
|
||||
|
||||
#### Input Object
|
||||
|
||||
Also it supports named jobs, listed via object.
|
||||
|
||||
```javascript
|
||||
var serial = require('asynckit').serial
|
||||
, assert = require('assert')
|
||||
;
|
||||
|
||||
var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ]
|
||||
, expectedResult = [ 2, 2, 8, 32, 128, 64, 16, 4 ]
|
||||
, expectedTarget = [ 0, 1, 2, 3, 4, 5, 6, 7 ]
|
||||
, target = []
|
||||
;
|
||||
|
||||
var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 }
|
||||
, expectedResult = { first: 2, one: 2, four: 8, sixteen: 32, sixtyFour: 128, thirtyTwo: 64, eight: 16, two: 4 }
|
||||
, expectedTarget = [ 1, 1, 4, 16, 64, 32, 8, 2 ]
|
||||
, target = []
|
||||
;
|
||||
|
||||
|
||||
serial(source, asyncJob, function(err, result)
|
||||
{
|
||||
assert.deepEqual(result, expectedResult);
|
||||
assert.deepEqual(target, expectedTarget);
|
||||
});
|
||||
|
||||
// shortcut interface (item, callback)
|
||||
// works for object as well as for the arrays
|
||||
function asyncJob(item, cb)
|
||||
{
|
||||
target.push(item);
|
||||
|
||||
// it will be automatically made async
|
||||
// even it iterator "returns" in the same event loop
|
||||
cb(null, item * 2);
|
||||
}
|
||||
```
|
||||
|
||||
More examples could be found in [test/test-serial-object.js](test/test-serial-object.js).
|
||||
|
||||
_Note: Since _object_ is an _unordered_ collection of properties,
|
||||
it may produce unexpected results with sequential iterations.
|
||||
Whenever order of the jobs' execution is important please use `serialOrdered` method._
|
||||
|
||||
### Ordered Serial Iterations
|
||||
|
||||
TBD
|
||||
|
||||
For example [compare-property](compare-property) package.
|
||||
|
||||
### Streaming interface
|
||||
|
||||
TBD
|
||||
|
||||
## Want to Know More?
|
||||
|
||||
More examples can be found in [test folder](test/).
|
||||
|
||||
Or open an [issue](https://github.com/alexindigo/asynckit/issues) with questions and/or suggestions.
|
||||
|
||||
## License
|
||||
|
||||
AsyncKit is licensed under the MIT license.
|
@ -0,0 +1,76 @@
|
||||
/* eslint no-console: "off" */
|
||||
|
||||
var asynckit = require('./')
|
||||
, async = require('async')
|
||||
, assert = require('assert')
|
||||
, expected = 0
|
||||
;
|
||||
|
||||
var Benchmark = require('benchmark');
|
||||
var suite = new Benchmark.Suite;
|
||||
|
||||
var source = [];
|
||||
for (var z = 1; z < 100; z++)
|
||||
{
|
||||
source.push(z);
|
||||
expected += z;
|
||||
}
|
||||
|
||||
suite
|
||||
// add tests
|
||||
|
||||
.add('async.map', function(deferred)
|
||||
{
|
||||
var total = 0;
|
||||
|
||||
async.map(source,
|
||||
function(i, cb)
|
||||
{
|
||||
setImmediate(function()
|
||||
{
|
||||
total += i;
|
||||
cb(null, total);
|
||||
});
|
||||
},
|
||||
function(err, result)
|
||||
{
|
||||
assert.ifError(err);
|
||||
assert.equal(result[result.length - 1], expected);
|
||||
deferred.resolve();
|
||||
});
|
||||
}, {'defer': true})
|
||||
|
||||
|
||||
.add('asynckit.parallel', function(deferred)
|
||||
{
|
||||
var total = 0;
|
||||
|
||||
asynckit.parallel(source,
|
||||
function(i, cb)
|
||||
{
|
||||
setImmediate(function()
|
||||
{
|
||||
total += i;
|
||||
cb(null, total);
|
||||
});
|
||||
},
|
||||
function(err, result)
|
||||
{
|
||||
assert.ifError(err);
|
||||
assert.equal(result[result.length - 1], expected);
|
||||
deferred.resolve();
|
||||
});
|
||||
}, {'defer': true})
|
||||
|
||||
|
||||
// add listeners
|
||||
.on('cycle', function(ev)
|
||||
{
|
||||
console.log(String(ev.target));
|
||||
})
|
||||
.on('complete', function()
|
||||
{
|
||||
console.log('Fastest is ' + this.filter('fastest').map('name'));
|
||||
})
|
||||
// run async
|
||||
.run({ 'async': true });
|
@ -0,0 +1,6 @@
|
||||
module.exports =
|
||||
{
|
||||
parallel : require('./parallel.js'),
|
||||
serial : require('./serial.js'),
|
||||
serialOrdered : require('./serialOrdered.js')
|
||||
};
|
@ -0,0 +1,29 @@
|
||||
// API
|
||||
module.exports = abort;
|
||||
|
||||
/**
|
||||
* Aborts leftover active jobs
|
||||
*
|
||||
* @param {object} state - current state object
|
||||
*/
|
||||
function abort(state)
|
||||
{
|
||||
Object.keys(state.jobs).forEach(clean.bind(state));
|
||||
|
||||
// reset leftover jobs
|
||||
state.jobs = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans up leftover job by invoking abort function for the provided job id
|
||||
*
|
||||
* @this state
|
||||
* @param {string|number} key - job id to abort
|
||||
*/
|
||||
function clean(key)
|
||||
{
|
||||
if (typeof this.jobs[key] == 'function')
|
||||
{
|
||||
this.jobs[key]();
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
var defer = require('./defer.js');
|
||||
|
||||
// API
|
||||
module.exports = async;
|
||||
|
||||
/**
|
||||
* Runs provided callback asynchronously
|
||||
* even if callback itself is not
|
||||
*
|
||||
* @param {function} callback - callback to invoke
|
||||
* @returns {function} - augmented callback
|
||||
*/
|
||||
function async(callback)
|
||||
{
|
||||
var isAsync = false;
|
||||
|
||||
// check if async happened
|
||||
defer(function() { isAsync = true; });
|
||||
|
||||
return function async_callback(err, result)
|
||||
{
|
||||
if (isAsync)
|
||||
{
|
||||
callback(err, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
defer(function nextTick_callback()
|
||||
{
|
||||
callback(err, result);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
module.exports = defer;
|
||||
|
||||
/**
|
||||
* Runs provided function on next iteration of the event loop
|
||||
*
|
||||
* @param {function} fn - function to run
|
||||
*/
|
||||
function defer(fn)
|
||||
{
|
||||
var nextTick = typeof setImmediate == 'function'
|
||||
? setImmediate
|
||||
: (
|
||||
typeof process == 'object' && typeof process.nextTick == 'function'
|
||||
? process.nextTick
|
||||
: null
|
||||
);
|
||||
|
||||
if (nextTick)
|
||||
{
|
||||
nextTick(fn);
|
||||
}
|
||||
else
|
||||
{
|
||||
setTimeout(fn, 0);
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
var async = require('./async.js')
|
||||
, abort = require('./abort.js')
|
||||
;
|
||||
|
||||
// API
|
||||
module.exports = iterate;
|
||||
|
||||
/**
|
||||
* Iterates over each job object
|
||||
*
|
||||
* @param {array|object} list - array or object (named list) to iterate over
|
||||
* @param {function} iterator - iterator to run
|
||||
* @param {object} state - current job status
|
||||
* @param {function} callback - invoked when all elements processed
|
||||
*/
|
||||
function iterate(list, iterator, state, callback)
|
||||
{
|
||||
// store current index
|
||||
var key = state['keyedList'] ? state['keyedList'][state.index] : state.index;
|
||||
|
||||
state.jobs[key] = runJob(iterator, key, list[key], function(error, output)
|
||||
{
|
||||
// don't repeat yourself
|
||||
// skip secondary callbacks
|
||||
if (!(key in state.jobs))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// clean up jobs
|
||||
delete state.jobs[key];
|
||||
|
||||
if (error)
|
||||
{
|
||||
// don't process rest of the results
|
||||
// stop still active jobs
|
||||
// and reset the list
|
||||
abort(state);
|
||||
}
|
||||
else
|
||||
{
|
||||
state.results[key] = output;
|
||||
}
|
||||
|
||||
// return salvaged results
|
||||
callback(error, state.results);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs iterator over provided job element
|
||||
*
|
||||
* @param {function} iterator - iterator to invoke
|
||||
* @param {string|number} key - key/index of the element in the list of jobs
|
||||
* @param {mixed} item - job description
|
||||
* @param {function} callback - invoked after iterator is done with the job
|
||||
* @returns {function|mixed} - job abort function or something else
|
||||
*/
|
||||
function runJob(iterator, key, item, callback)
|
||||
{
|
||||
var aborter;
|
||||
|
||||
// allow shortcut if iterator expects only two arguments
|
||||
if (iterator.length == 2)
|
||||
{
|
||||
aborter = iterator(item, async(callback));
|
||||
}
|
||||
// otherwise go with full three arguments
|
||||
else
|
||||
{
|
||||
aborter = iterator(item, key, async(callback));
|
||||
}
|
||||
|
||||
return aborter;
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
var parallel = require('../parallel.js');
|
||||
|
||||
// API
|
||||
module.exports = ReadableParallel;
|
||||
|
||||
/**
|
||||
* Streaming wrapper to `asynckit.parallel`
|
||||
*
|
||||
* @param {array|object} list - array or object (named list) to iterate over
|
||||
* @param {function} iterator - iterator to run
|
||||
* @param {function} callback - invoked when all elements processed
|
||||
* @returns {stream.Readable#}
|
||||
*/
|
||||
function ReadableParallel(list, iterator, callback)
|
||||
{
|
||||
if (!(this instanceof ReadableParallel))
|
||||
{
|
||||
return new ReadableParallel(list, iterator, callback);
|
||||
}
|
||||
|
||||
// turn on object mode
|
||||
ReadableParallel.super_.call(this, {objectMode: true});
|
||||
|
||||
this._start(parallel, list, iterator, callback);
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
var serial = require('../serial.js');
|
||||
|
||||
// API
|
||||
module.exports = ReadableSerial;
|
||||
|
||||
/**
|
||||
* Streaming wrapper to `asynckit.serial`
|
||||
*
|
||||
* @param {array|object} list - array or object (named list) to iterate over
|
||||
* @param {function} iterator - iterator to run
|
||||
* @param {function} callback - invoked when all elements processed
|
||||
* @returns {stream.Readable#}
|
||||
*/
|
||||
function ReadableSerial(list, iterator, callback)
|
||||
{
|
||||
if (!(this instanceof ReadableSerial))
|
||||
{
|
||||
return new ReadableSerial(list, iterator, callback);
|
||||
}
|
||||
|
||||
// turn on object mode
|
||||
ReadableSerial.super_.call(this, {objectMode: true});
|
||||
|
||||
this._start(serial, list, iterator, callback);
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
var serialOrdered = require('../serialOrdered.js');
|
||||
|
||||
// API
|
||||
module.exports = ReadableSerialOrdered;
|
||||
// expose sort helpers
|
||||
module.exports.ascending = serialOrdered.ascending;
|
||||
module.exports.descending = serialOrdered.descending;
|
||||
|
||||
/**
|
||||
* Streaming wrapper to `asynckit.serialOrdered`
|
||||
*
|
||||
* @param {array|object} list - array or object (named list) to iterate over
|
||||
* @param {function} iterator - iterator to run
|
||||
* @param {function} sortMethod - custom sort function
|
||||
* @param {function} callback - invoked when all elements processed
|
||||
* @returns {stream.Readable#}
|
||||
*/
|
||||
function ReadableSerialOrdered(list, iterator, sortMethod, callback)
|
||||
{
|
||||
if (!(this instanceof ReadableSerialOrdered))
|
||||
{
|
||||
return new ReadableSerialOrdered(list, iterator, sortMethod, callback);
|
||||
}
|
||||
|
||||
// turn on object mode
|
||||
ReadableSerialOrdered.super_.call(this, {objectMode: true});
|
||||
|
||||
this._start(serialOrdered, list, iterator, sortMethod, callback);
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
// API
|
||||
module.exports = state;
|
||||
|
||||
/**
|
||||
* Creates initial state object
|
||||
* for iteration over list
|
||||
*
|
||||
* @param {array|object} list - list to iterate over
|
||||
* @param {function|null} sortMethod - function to use for keys sort,
|
||||
* or `null` to keep them as is
|
||||
* @returns {object} - initial state object
|
||||
*/
|
||||
function state(list, sortMethod)
|
||||
{
|
||||
var isNamedList = !Array.isArray(list)
|
||||
, initState =
|
||||
{
|
||||
index : 0,
|
||||
keyedList: isNamedList || sortMethod ? Object.keys(list) : null,
|
||||
jobs : {},
|
||||
results : isNamedList ? {} : [],
|
||||
size : isNamedList ? Object.keys(list).length : list.length
|
||||
}
|
||||
;
|
||||
|
||||
if (sortMethod)
|
||||
{
|
||||
// sort array keys based on it's values
|
||||
// sort object's keys just on own merit
|
||||
initState.keyedList.sort(isNamedList ? sortMethod : function(a, b)
|
||||
{
|
||||
return sortMethod(list[a], list[b]);
|
||||
});
|
||||
}
|
||||
|
||||
return initState;
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
var async = require('./async.js');
|
||||
|
||||
// API
|
||||
module.exports = {
|
||||
iterator: wrapIterator,
|
||||
callback: wrapCallback
|
||||
};
|
||||
|
||||
/**
|
||||
* Wraps iterators with long signature
|
||||
*
|
||||
* @this ReadableAsyncKit#
|
||||
* @param {function} iterator - function to wrap
|
||||
* @returns {function} - wrapped function
|
||||
*/
|
||||
function wrapIterator(iterator)
|
||||
{
|
||||
var stream = this;
|
||||
|
||||
return function(item, key, cb)
|
||||
{
|
||||
var aborter
|
||||
, wrappedCb = async(wrapIteratorCallback.call(stream, cb, key))
|
||||
;
|
||||
|
||||
stream.jobs[key] = wrappedCb;
|
||||
|
||||
// it's either shortcut (item, cb)
|
||||
if (iterator.length == 2)
|
||||
{
|
||||
aborter = iterator(item, wrappedCb);
|
||||
}
|
||||
// or long format (item, key, cb)
|
||||
else
|
||||
{
|
||||
aborter = iterator(item, key, wrappedCb);
|
||||
}
|
||||
|
||||
return aborter;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps provided callback function
|
||||
* allowing to execute snitch function before
|
||||
* real callback
|
||||
*
|
||||
* @this ReadableAsyncKit#
|
||||
* @param {function} callback - function to wrap
|
||||
* @returns {function} - wrapped function
|
||||
*/
|
||||
function wrapCallback(callback)
|
||||
{
|
||||
var stream = this;
|
||||
|
||||
var wrapped = function(error, result)
|
||||
{
|
||||
return finisher.call(stream, error, result, callback);
|
||||
};
|
||||
|
||||
return wrapped;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps provided iterator callback function
|
||||
* makes sure snitch only called once,
|
||||
* but passes secondary calls to the original callback
|
||||
*
|
||||
* @this ReadableAsyncKit#
|
||||
* @param {function} callback - callback to wrap
|
||||
* @param {number|string} key - iteration key
|
||||
* @returns {function} wrapped callback
|
||||
*/
|
||||
function wrapIteratorCallback(callback, key)
|
||||
{
|
||||
var stream = this;
|
||||
|
||||
return function(error, output)
|
||||
{
|
||||
// don't repeat yourself
|
||||
if (!(key in stream.jobs))
|
||||
{
|
||||
callback(error, output);
|
||||
return;
|
||||
}
|
||||
|
||||
// clean up jobs
|
||||
delete stream.jobs[key];
|
||||
|
||||
return streamer.call(stream, error, {key: key, value: output}, callback);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream wrapper for iterator callback
|
||||
*
|
||||
* @this ReadableAsyncKit#
|
||||
* @param {mixed} error - error response
|
||||
* @param {mixed} output - iterator output
|
||||
* @param {function} callback - callback that expects iterator results
|
||||
*/
|
||||
function streamer(error, output, callback)
|
||||
{
|
||||
if (error && !this.error)
|
||||
{
|
||||
this.error = error;
|
||||
this.pause();
|
||||
this.emit('error', error);
|
||||
// send back value only, as expected
|
||||
callback(error, output && output.value);
|
||||
return;
|
||||
}
|
||||
|
||||
// stream stuff
|
||||
this.push(output);
|
||||
|
||||
// back to original track
|
||||
// send back value only, as expected
|
||||
callback(error, output && output.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream wrapper for finishing callback
|
||||
*
|
||||
* @this ReadableAsyncKit#
|
||||
* @param {mixed} error - error response
|
||||
* @param {mixed} output - iterator output
|
||||
* @param {function} callback - callback that expects final results
|
||||
*/
|
||||
function finisher(error, output, callback)
|
||||
{
|
||||
// signal end of the stream
|
||||
// only for successfully finished streams
|
||||
if (!error)
|
||||
{
|
||||
this.push(null);
|
||||
}
|
||||
|
||||
// back to original track
|
||||
callback(error, output);
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
var abort = require('./abort.js')
|
||||
, async = require('./async.js')
|
||||
;
|
||||
|
||||
// API
|
||||
module.exports = terminator;
|
||||
|
||||
/**
|
||||
* Terminates jobs in the attached state context
|
||||
*
|
||||
* @this AsyncKitState#
|
||||
* @param {function} callback - final callback to invoke after termination
|
||||
*/
|
||||
function terminator(callback)
|
||||
{
|
||||
if (!Object.keys(this.jobs).length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// fast forward iteration index
|
||||
this.index = this.size;
|
||||
|
||||
// abort jobs
|
||||
abort(this);
|
||||
|
||||
// send back results we have so far
|
||||
async(callback)(null, this.results);
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
{
|
||||
"name": "asynckit",
|
||||
"version": "0.4.0",
|
||||
"description": "Minimal async jobs utility library, with streams support",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"clean": "rimraf coverage",
|
||||
"lint": "eslint *.js lib/*.js test/*.js",
|
||||
"test": "istanbul cover --reporter=json tape -- 'test/test-*.js' | tap-spec",
|
||||
"win-test": "tape test/test-*.js",
|
||||
"browser": "browserify -t browserify-istanbul test/lib/browserify_adjustment.js test/test-*.js | obake --coverage | tap-spec",
|
||||
"report": "istanbul report",
|
||||
"size": "browserify index.js | size-table asynckit",
|
||||
"debug": "tape test/test-*.js"
|
||||
},
|
||||
"pre-commit": [
|
||||
"clean",
|
||||
"lint",
|
||||
"test",
|
||||
"browser",
|
||||
"report",
|
||||
"size"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/alexindigo/asynckit.git"
|
||||
},
|
||||
"keywords": [
|
||||
"async",
|
||||
"jobs",
|
||||
"parallel",
|
||||
"serial",
|
||||
"iterator",
|
||||
"array",
|
||||
"object",
|
||||
"stream",
|
||||
"destroy",
|
||||
"terminate",
|
||||
"abort"
|
||||
],
|
||||
"author": "Alex Indigo <iam@alexindigo.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/alexindigo/asynckit/issues"
|
||||
},
|
||||
"homepage": "https://github.com/alexindigo/asynckit#readme",
|
||||
"devDependencies": {
|
||||
"browserify": "^13.0.0",
|
||||
"browserify-istanbul": "^2.0.0",
|
||||
"coveralls": "^2.11.9",
|
||||
"eslint": "^2.9.0",
|
||||
"istanbul": "^0.4.3",
|
||||
"obake": "^0.1.2",
|
||||
"phantomjs-prebuilt": "^2.1.7",
|
||||
"pre-commit": "^1.1.3",
|
||||
"reamde": "^1.1.0",
|
||||
"rimraf": "^2.5.2",
|
||||
"size-table": "^0.2.0",
|
||||
"tap-spec": "^4.1.1",
|
||||
"tape": "^4.5.1"
|
||||
},
|
||||
"dependencies": {}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
var iterate = require('./lib/iterate.js')
|
||||
, initState = require('./lib/state.js')
|
||||
, terminator = require('./lib/terminator.js')
|
||||
;
|
||||
|
||||
// Public API
|
||||
module.exports = parallel;
|
||||
|
||||
/**
|
||||
* Runs iterator over provided array elements in parallel
|
||||
*
|
||||
* @param {array|object} list - array or object (named list) to iterate over
|
||||
* @param {function} iterator - iterator to run
|
||||
* @param {function} callback - invoked when all elements processed
|
||||
* @returns {function} - jobs terminator
|
||||
*/
|
||||
function parallel(list, iterator, callback)
|
||||
{
|
||||
var state = initState(list);
|
||||
|
||||
while (state.index < (state['keyedList'] || list).length)
|
||||
{
|
||||
iterate(list, iterator, state, function(error, result)
|
||||
{
|
||||
if (error)
|
||||
{
|
||||
callback(error, result);
|
||||
return;
|
||||
}
|
||||
|
||||
// looks like it's the last one
|
||||
if (Object.keys(state.jobs).length === 0)
|
||||
{
|
||||
callback(null, state.results);
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
state.index++;
|
||||
}
|
||||
|
||||
return terminator.bind(state, callback);
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
var serialOrdered = require('./serialOrdered.js');
|
||||
|
||||
// Public API
|
||||
module.exports = serial;
|
||||
|
||||
/**
|
||||
* Runs iterator over provided array elements in series
|
||||
*
|
||||
* @param {array|object} list - array or object (named list) to iterate over
|
||||
* @param {function} iterator - iterator to run
|
||||
* @param {function} callback - invoked when all elements processed
|
||||
* @returns {function} - jobs terminator
|
||||
*/
|
||||
function serial(list, iterator, callback)
|
||||
{
|
||||
return serialOrdered(list, iterator, null, callback);
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
var iterate = require('./lib/iterate.js')
|
||||
, initState = require('./lib/state.js')
|
||||
, terminator = require('./lib/terminator.js')
|
||||
;
|
||||
|
||||
// Public API
|
||||
module.exports = serialOrdered;
|
||||
// sorting helpers
|
||||
module.exports.ascending = ascending;
|
||||
module.exports.descending = descending;
|
||||
|
||||
/**
|
||||
* Runs iterator over provided sorted array elements in series
|
||||
*
|
||||
* @param {array|object} list - array or object (named list) to iterate over
|
||||
* @param {function} iterator - iterator to run
|
||||
* @param {function} sortMethod - custom sort function
|
||||
* @param {function} callback - invoked when all elements processed
|
||||
* @returns {function} - jobs terminator
|
||||
*/
|
||||
function serialOrdered(list, iterator, sortMethod, callback)
|
||||
{
|
||||
var state = initState(list, sortMethod);
|
||||
|
||||
iterate(list, iterator, state, function iteratorHandler(error, result)
|
||||
{
|
||||
if (error)
|
||||
{
|
||||
callback(error, result);
|
||||
return;
|
||||
}
|
||||
|
||||
state.index++;
|
||||
|
||||
// are we there yet?
|
||||
if (state.index < (state['keyedList'] || list).length)
|
||||
{
|
||||
iterate(list, iterator, state, iteratorHandler);
|
||||
return;
|
||||
}
|
||||
|
||||
// done here
|
||||
callback(null, state.results);
|
||||
});
|
||||
|
||||
return terminator.bind(state, callback);
|
||||
}
|
||||
|
||||
/*
|
||||
* -- Sort methods
|
||||
*/
|
||||
|
||||
/**
|
||||
* sort helper to sort array elements in ascending order
|
||||
*
|
||||
* @param {mixed} a - an item to compare
|
||||
* @param {mixed} b - an item to compare
|
||||
* @returns {number} - comparison result
|
||||
*/
|
||||
function ascending(a, b)
|
||||
{
|
||||
return a < b ? -1 : a > b ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* sort helper to sort array elements in descending order
|
||||
*
|
||||
* @param {mixed} a - an item to compare
|
||||
* @param {mixed} b - an item to compare
|
||||
* @returns {number} - comparison result
|
||||
*/
|
||||
function descending(a, b)
|
||||
{
|
||||
return -1 * ascending(a, b);
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
var inherits = require('util').inherits
|
||||
, Readable = require('stream').Readable
|
||||
, ReadableAsyncKit = require('./lib/readable_asynckit.js')
|
||||
, ReadableParallel = require('./lib/readable_parallel.js')
|
||||
, ReadableSerial = require('./lib/readable_serial.js')
|
||||
, ReadableSerialOrdered = require('./lib/readable_serial_ordered.js')
|
||||
;
|
||||
|
||||
// API
|
||||
module.exports =
|
||||
{
|
||||
parallel : ReadableParallel,
|
||||
serial : ReadableSerial,
|
||||
serialOrdered : ReadableSerialOrdered,
|
||||
};
|
||||
|
||||
inherits(ReadableAsyncKit, Readable);
|
||||
|
||||
inherits(ReadableParallel, ReadableAsyncKit);
|
||||
inherits(ReadableSerial, ReadableAsyncKit);
|
||||
inherits(ReadableSerialOrdered, ReadableAsyncKit);
|
@ -0,0 +1,55 @@
|
||||
Apache License
|
||||
|
||||
Version 2.0, January 2004
|
||||
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
@ -0,0 +1,4 @@
|
||||
aws-sign
|
||||
========
|
||||
|
||||
AWS signing. Originally pulled from LearnBoost/knox, maintained as vendor in request, now a standalone module.
|
@ -0,0 +1,212 @@
|
||||
|
||||
/*!
|
||||
* Copyright 2010 LearnBoost <dev@learnboost.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var crypto = require('crypto')
|
||||
, parse = require('url').parse
|
||||
;
|
||||
|
||||
/**
|
||||
* Valid keys.
|
||||
*/
|
||||
|
||||
var keys =
|
||||
[ 'acl'
|
||||
, 'location'
|
||||
, 'logging'
|
||||
, 'notification'
|
||||
, 'partNumber'
|
||||
, 'policy'
|
||||
, 'requestPayment'
|
||||
, 'torrent'
|
||||
, 'uploadId'
|
||||
, 'uploads'
|
||||
, 'versionId'
|
||||
, 'versioning'
|
||||
, 'versions'
|
||||
, 'website'
|
||||
]
|
||||
|
||||
/**
|
||||
* Return an "Authorization" header value with the given `options`
|
||||
* in the form of "AWS <key>:<signature>"
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {String}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function authorization (options) {
|
||||
return 'AWS ' + options.key + ':' + sign(options)
|
||||
}
|
||||
|
||||
module.exports = authorization
|
||||
module.exports.authorization = authorization
|
||||
|
||||
/**
|
||||
* Simple HMAC-SHA1 Wrapper
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {String}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function hmacSha1 (options) {
|
||||
return crypto.createHmac('sha1', options.secret).update(options.message).digest('base64')
|
||||
}
|
||||
|
||||
module.exports.hmacSha1 = hmacSha1
|
||||
|
||||
/**
|
||||
* Create a base64 sha1 HMAC for `options`.
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {String}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function sign (options) {
|
||||
options.message = stringToSign(options)
|
||||
return hmacSha1(options)
|
||||
}
|
||||
module.exports.sign = sign
|
||||
|
||||
/**
|
||||
* Create a base64 sha1 HMAC for `options`.
|
||||
*
|
||||
* Specifically to be used with S3 presigned URLs
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {String}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function signQuery (options) {
|
||||
options.message = queryStringToSign(options)
|
||||
return hmacSha1(options)
|
||||
}
|
||||
module.exports.signQuery= signQuery
|
||||
|
||||
/**
|
||||
* Return a string for sign() with the given `options`.
|
||||
*
|
||||
* Spec:
|
||||
*
|
||||
* <verb>\n
|
||||
* <md5>\n
|
||||
* <content-type>\n
|
||||
* <date>\n
|
||||
* [headers\n]
|
||||
* <resource>
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {String}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function stringToSign (options) {
|
||||
var headers = options.amazonHeaders || ''
|
||||
if (headers) headers += '\n'
|
||||
var r =
|
||||
[ options.verb
|
||||
, options.md5
|
||||
, options.contentType
|
||||
, options.date ? options.date.toUTCString() : ''
|
||||
, headers + options.resource
|
||||
]
|
||||
return r.join('\n')
|
||||
}
|
||||
module.exports.queryStringToSign = stringToSign
|
||||
|
||||
/**
|
||||
* Return a string for sign() with the given `options`, but is meant exclusively
|
||||
* for S3 presigned URLs
|
||||
*
|
||||
* Spec:
|
||||
*
|
||||
* <date>\n
|
||||
* <resource>
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {String}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function queryStringToSign (options){
|
||||
return 'GET\n\n\n' + options.date + '\n' + options.resource
|
||||
}
|
||||
module.exports.queryStringToSign = queryStringToSign
|
||||
|
||||
/**
|
||||
* Perform the following:
|
||||
*
|
||||
* - ignore non-amazon headers
|
||||
* - lowercase fields
|
||||
* - sort lexicographically
|
||||
* - trim whitespace between ":"
|
||||
* - join with newline
|
||||
*
|
||||
* @param {Object} headers
|
||||
* @return {String}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function canonicalizeHeaders (headers) {
|
||||
var buf = []
|
||||
, fields = Object.keys(headers)
|
||||
;
|
||||
for (var i = 0, len = fields.length; i < len; ++i) {
|
||||
var field = fields[i]
|
||||
, val = headers[field]
|
||||
, field = field.toLowerCase()
|
||||
;
|
||||
if (0 !== field.indexOf('x-amz')) continue
|
||||
buf.push(field + ':' + val)
|
||||
}
|
||||
return buf.sort().join('\n')
|
||||
}
|
||||
module.exports.canonicalizeHeaders = canonicalizeHeaders
|
||||
|
||||
/**
|
||||
* Perform the following:
|
||||
*
|
||||
* - ignore non sub-resources
|
||||
* - sort lexicographically
|
||||
*
|
||||
* @param {String} resource
|
||||
* @return {String}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function canonicalizeResource (resource) {
|
||||
var url = parse(resource, true)
|
||||
, path = url.pathname
|
||||
, buf = []
|
||||
;
|
||||
|
||||
Object.keys(url.query).forEach(function(key){
|
||||
if (!~keys.indexOf(key)) return
|
||||
var val = '' == url.query[key] ? '' : '=' + encodeURIComponent(url.query[key])
|
||||
buf.push(key + val)
|
||||
})
|
||||
|
||||
return path + (buf.length ? '?' + buf.sort().join('&') : '')
|
||||
}
|
||||
module.exports.canonicalizeResource = canonicalizeResource
|
@ -0,0 +1,17 @@
|
||||
{
|
||||
"author": "Mikeal Rogers <mikeal.rogers@gmail.com> (http://www.futurealoof.com)",
|
||||
"name": "aws-sign2",
|
||||
"description": "AWS signing. Originally pulled from LearnBoost/knox, maintained as vendor in request, now a standalone module.",
|
||||
"version": "0.6.0",
|
||||
"repository": {
|
||||
"url": "https://github.com/mikeal/aws-sign"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"main": "index.js",
|
||||
"dependencies": {},
|
||||
"devDependencies": {},
|
||||
"optionalDependencies": {},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
test
|
||||
examples
|
||||
example.js
|
||||
browser
|
@ -0,0 +1 @@
|
||||
62638
|
@ -0,0 +1,5 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.10"
|
||||
- "0.12"
|
||||
- "4.2"
|
@ -0,0 +1,19 @@
|
||||
Copyright 2013 Michael Hart (michael.hart.au@gmail.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,523 @@
|
||||
aws4
|
||||
----
|
||||
|
||||
[](http://travis-ci.org/mhart/aws4)
|
||||
|
||||
A small utility to sign vanilla node.js http(s) request options using Amazon's
|
||||
[AWS Signature Version 4](http://docs.amazonwebservices.com/general/latest/gr/signature-version-4.html).
|
||||
|
||||
Can also be used [in the browser](./browser).
|
||||
|
||||
This signature is supported by nearly all Amazon services, including
|
||||
[S3](http://docs.aws.amazon.com/AmazonS3/latest/API/),
|
||||
[EC2](http://docs.aws.amazon.com/AWSEC2/latest/APIReference/),
|
||||
[DynamoDB](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/API.html),
|
||||
[Kinesis](http://docs.aws.amazon.com/kinesis/latest/APIReference/),
|
||||
[Lambda](http://docs.aws.amazon.com/lambda/latest/dg/API_Reference.html),
|
||||
[SQS](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/),
|
||||
[SNS](http://docs.aws.amazon.com/sns/latest/api/),
|
||||
[IAM](http://docs.aws.amazon.com/IAM/latest/APIReference/),
|
||||
[STS](http://docs.aws.amazon.com/STS/latest/APIReference/),
|
||||
[RDS](http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/),
|
||||
[CloudWatch](http://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/),
|
||||
[CloudWatch Logs](http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/),
|
||||
[CodeDeploy](http://docs.aws.amazon.com/codedeploy/latest/APIReference/),
|
||||
[CloudFront](http://docs.aws.amazon.com/AmazonCloudFront/latest/APIReference/),
|
||||
[CloudTrail](http://docs.aws.amazon.com/awscloudtrail/latest/APIReference/),
|
||||
[ElastiCache](http://docs.aws.amazon.com/AmazonElastiCache/latest/APIReference/),
|
||||
[EMR](http://docs.aws.amazon.com/ElasticMapReduce/latest/API/),
|
||||
[Glacier](http://docs.aws.amazon.com/amazonglacier/latest/dev/amazon-glacier-api.html),
|
||||
[CloudSearch](http://docs.aws.amazon.com/cloudsearch/latest/developerguide/APIReq.html),
|
||||
[Elastic Load Balancing](http://docs.aws.amazon.com/ElasticLoadBalancing/latest/APIReference/),
|
||||
[Elastic Transcoder](http://docs.aws.amazon.com/elastictranscoder/latest/developerguide/api-reference.html),
|
||||
[CloudFormation](http://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/),
|
||||
[Elastic Beanstalk](http://docs.aws.amazon.com/elasticbeanstalk/latest/api/),
|
||||
[Storage Gateway](http://docs.aws.amazon.com/storagegateway/latest/userguide/AWSStorageGatewayAPI.html),
|
||||
[Data Pipeline](http://docs.aws.amazon.com/datapipeline/latest/APIReference/),
|
||||
[Direct Connect](http://docs.aws.amazon.com/directconnect/latest/APIReference/),
|
||||
[Redshift](http://docs.aws.amazon.com/redshift/latest/APIReference/),
|
||||
[OpsWorks](http://docs.aws.amazon.com/opsworks/latest/APIReference/),
|
||||
[SES](http://docs.aws.amazon.com/ses/latest/APIReference/),
|
||||
[SWF](http://docs.aws.amazon.com/amazonswf/latest/apireference/),
|
||||
[AutoScaling](http://docs.aws.amazon.com/AutoScaling/latest/APIReference/),
|
||||
[Mobile Analytics](http://docs.aws.amazon.com/mobileanalytics/latest/ug/server-reference.html),
|
||||
[Cognito Identity](http://docs.aws.amazon.com/cognitoidentity/latest/APIReference/),
|
||||
[Cognito Sync](http://docs.aws.amazon.com/cognitosync/latest/APIReference/),
|
||||
[Container Service](http://docs.aws.amazon.com/AmazonECS/latest/APIReference/),
|
||||
[AppStream](http://docs.aws.amazon.com/appstream/latest/developerguide/appstream-api-rest.html),
|
||||
[Key Management Service](http://docs.aws.amazon.com/kms/latest/APIReference/),
|
||||
[Config](http://docs.aws.amazon.com/config/latest/APIReference/),
|
||||
[CloudHSM](http://docs.aws.amazon.com/cloudhsm/latest/dg/api-ref.html),
|
||||
[Route53](http://docs.aws.amazon.com/Route53/latest/APIReference/requests-rest.html) and
|
||||
[Route53 Domains](http://docs.aws.amazon.com/Route53/latest/APIReference/requests-rpc.html).
|
||||
|
||||
Indeed, the only AWS services that *don't* support v4 as of 2014-12-30 are
|
||||
[Import/Export](http://docs.aws.amazon.com/AWSImportExport/latest/DG/api-reference.html) and
|
||||
[SimpleDB](http://docs.aws.amazon.com/AmazonSimpleDB/latest/DeveloperGuide/SDB_API.html)
|
||||
(they only support [AWS Signature Version 2](https://github.com/mhart/aws2)).
|
||||
|
||||
It also provides defaults for a number of core AWS headers and
|
||||
request parameters, making it very easy to query AWS services, or
|
||||
build out a fully-featured AWS library.
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
```javascript
|
||||
var http = require('http'),
|
||||
https = require('https'),
|
||||
aws4 = require('aws4')
|
||||
|
||||
// given an options object you could pass to http.request
|
||||
var opts = {host: 'sqs.us-east-1.amazonaws.com', path: '/?Action=ListQueues'}
|
||||
|
||||
// alternatively (as aws4 can infer the host):
|
||||
opts = {service: 'sqs', region: 'us-east-1', path: '/?Action=ListQueues'}
|
||||
|
||||
// alternatively (as us-east-1 is default):
|
||||
opts = {service: 'sqs', path: '/?Action=ListQueues'}
|
||||
|
||||
aws4.sign(opts) // assumes AWS credentials are available in process.env
|
||||
|
||||
console.log(opts)
|
||||
/*
|
||||
{
|
||||
host: 'sqs.us-east-1.amazonaws.com',
|
||||
path: '/?Action=ListQueues',
|
||||
headers: {
|
||||
Host: 'sqs.us-east-1.amazonaws.com',
|
||||
'X-Amz-Date': '20121226T061030Z',
|
||||
Authorization: 'AWS4-HMAC-SHA256 Credential=ABCDEF/20121226/us-east-1/sqs/aws4_request, ...'
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// we can now use this to query AWS using the standard node.js http API
|
||||
http.request(opts, function(res) { res.pipe(process.stdout) }).end()
|
||||
/*
|
||||
<?xml version="1.0"?>
|
||||
<ListQueuesResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/">
|
||||
...
|
||||
*/
|
||||
```
|
||||
|
||||
More options
|
||||
------------
|
||||
|
||||
```javascript
|
||||
// you can also pass AWS credentials in explicitly (otherwise taken from process.env)
|
||||
aws4.sign(opts, {accessKeyId: '', secretAccessKey: ''})
|
||||
|
||||
// can also add the signature to query strings
|
||||
aws4.sign({service: 's3', path: '/my-bucket?X-Amz-Expires=12345', signQuery: true})
|
||||
|
||||
// create a utility function to pipe to stdout (with https this time)
|
||||
function request(o) { https.request(o, function(res) { res.pipe(process.stdout) }).end(o.body || '') }
|
||||
|
||||
// aws4 can infer the HTTP method if a body is passed in
|
||||
// method will be POST and Content-Type: 'application/x-www-form-urlencoded; charset=utf-8'
|
||||
request(aws4.sign({service: 'iam', body: 'Action=ListGroups&Version=2010-05-08'}))
|
||||
/*
|
||||
<ListGroupsResponse xmlns="https://iam.amazonaws.com/doc/2010-05-08/">
|
||||
...
|
||||
*/
|
||||
|
||||
// can specify any custom option or header as per usual
|
||||
request(aws4.sign({
|
||||
service: 'dynamodb',
|
||||
region: 'ap-southeast-2',
|
||||
method: 'POST',
|
||||
path: '/',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.0',
|
||||
'X-Amz-Target': 'DynamoDB_20120810.ListTables'
|
||||
},
|
||||
body: '{}'
|
||||
}))
|
||||
/*
|
||||
{"TableNames":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
// works with all other services that support Signature Version 4
|
||||
|
||||
request(aws4.sign({service: 's3', path: '/', signQuery: true}))
|
||||
/*
|
||||
<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'ec2', path: '/?Action=DescribeRegions&Version=2014-06-15'}))
|
||||
/*
|
||||
<DescribeRegionsResponse xmlns="http://ec2.amazonaws.com/doc/2014-06-15/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'sns', path: '/?Action=ListTopics&Version=2010-03-31'}))
|
||||
/*
|
||||
<ListTopicsResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'sts', path: '/?Action=GetSessionToken&Version=2011-06-15'}))
|
||||
/*
|
||||
<GetSessionTokenResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'cloudsearch', path: '/?Action=ListDomainNames&Version=2013-01-01'}))
|
||||
/*
|
||||
<ListDomainNamesResponse xmlns="http://cloudsearch.amazonaws.com/doc/2013-01-01/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'ses', path: '/?Action=ListIdentities&Version=2010-12-01'}))
|
||||
/*
|
||||
<ListIdentitiesResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'autoscaling', path: '/?Action=DescribeAutoScalingInstances&Version=2011-01-01'}))
|
||||
/*
|
||||
<DescribeAutoScalingInstancesResponse xmlns="http://autoscaling.amazonaws.com/doc/2011-01-01/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'elasticloadbalancing', path: '/?Action=DescribeLoadBalancers&Version=2012-06-01'}))
|
||||
/*
|
||||
<DescribeLoadBalancersResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'cloudformation', path: '/?Action=ListStacks&Version=2010-05-15'}))
|
||||
/*
|
||||
<ListStacksResponse xmlns="http://cloudformation.amazonaws.com/doc/2010-05-15/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'elasticbeanstalk', path: '/?Action=ListAvailableSolutionStacks&Version=2010-12-01'}))
|
||||
/*
|
||||
<ListAvailableSolutionStacksResponse xmlns="http://elasticbeanstalk.amazonaws.com/docs/2010-12-01/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'rds', path: '/?Action=DescribeDBInstances&Version=2012-09-17'}))
|
||||
/*
|
||||
<DescribeDBInstancesResponse xmlns="http://rds.amazonaws.com/doc/2012-09-17/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'monitoring', path: '/?Action=ListMetrics&Version=2010-08-01'}))
|
||||
/*
|
||||
<ListMetricsResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'redshift', path: '/?Action=DescribeClusters&Version=2012-12-01'}))
|
||||
/*
|
||||
<DescribeClustersResponse xmlns="http://redshift.amazonaws.com/doc/2012-12-01/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'cloudfront', path: '/2014-05-31/distribution'}))
|
||||
/*
|
||||
<DistributionList xmlns="http://cloudfront.amazonaws.com/doc/2014-05-31/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'elasticache', path: '/?Action=DescribeCacheClusters&Version=2014-07-15'}))
|
||||
/*
|
||||
<DescribeCacheClustersResponse xmlns="http://elasticache.amazonaws.com/doc/2014-07-15/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'elasticmapreduce', path: '/?Action=DescribeJobFlows&Version=2009-03-31'}))
|
||||
/*
|
||||
<DescribeJobFlowsResponse xmlns="http://elasticmapreduce.amazonaws.com/doc/2009-03-31">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'route53', path: '/2013-04-01/hostedzone'}))
|
||||
/*
|
||||
<ListHostedZonesResponse xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'appstream', path: '/applications'}))
|
||||
/*
|
||||
{"_links":{"curie":[{"href":"http://docs.aws.amazon.com/appstream/latest/...
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'cognito-sync', path: '/identitypools'}))
|
||||
/*
|
||||
{"Count":0,"IdentityPoolUsages":[],"MaxResults":16,"NextToken":null}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'elastictranscoder', path: '/2012-09-25/pipelines'}))
|
||||
/*
|
||||
{"NextPageToken":null,"Pipelines":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'lambda', path: '/2014-11-13/functions/'}))
|
||||
/*
|
||||
{"Functions":[],"NextMarker":null}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'ecs', path: '/?Action=ListClusters&Version=2014-11-13'}))
|
||||
/*
|
||||
<ListClustersResponse xmlns="http://ecs.amazonaws.com/doc/2014-11-13/">
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'glacier', path: '/-/vaults', headers: {'X-Amz-Glacier-Version': '2012-06-01'}}))
|
||||
/*
|
||||
{"Marker":null,"VaultList":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'storagegateway', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'StorageGateway_20120630.ListGateways'
|
||||
}}))
|
||||
/*
|
||||
{"Gateways":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'datapipeline', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'DataPipeline.ListPipelines'
|
||||
}}))
|
||||
/*
|
||||
{"hasMoreResults":false,"pipelineIdList":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'opsworks', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'OpsWorks_20130218.DescribeStacks'
|
||||
}}))
|
||||
/*
|
||||
{"Stacks":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'route53domains', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'Route53Domains_v20140515.ListDomains'
|
||||
}}))
|
||||
/*
|
||||
{"Domains":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'kinesis', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'Kinesis_20131202.ListStreams'
|
||||
}}))
|
||||
/*
|
||||
{"HasMoreStreams":false,"StreamNames":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'cloudtrail', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'CloudTrail_20131101.DescribeTrails'
|
||||
}}))
|
||||
/*
|
||||
{"trailList":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'logs', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'Logs_20140328.DescribeLogGroups'
|
||||
}}))
|
||||
/*
|
||||
{"logGroups":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'codedeploy', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'CodeDeploy_20141006.ListApplications'
|
||||
}}))
|
||||
/*
|
||||
{"applications":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'directconnect', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'OvertureService.DescribeConnections'
|
||||
}}))
|
||||
/*
|
||||
{"connections":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'kms', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'TrentService.ListKeys'
|
||||
}}))
|
||||
/*
|
||||
{"Keys":[],"Truncated":false}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'config', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'StarlingDoveService.DescribeDeliveryChannels'
|
||||
}}))
|
||||
/*
|
||||
{"DeliveryChannels":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({service: 'cloudhsm', body: '{}', headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'CloudHsmFrontendService.ListAvailableZones'
|
||||
}}))
|
||||
/*
|
||||
{"AZList":["us-east-1a","us-east-1b","us-east-1c"]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({
|
||||
service: 'swf',
|
||||
body: '{"registrationStatus":"REGISTERED"}',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.0',
|
||||
'X-Amz-Target': 'SimpleWorkflowService.ListDomains'
|
||||
}
|
||||
}))
|
||||
/*
|
||||
{"domainInfos":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({
|
||||
service: 'cognito-identity',
|
||||
body: '{"MaxResults": 1}',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-amz-json-1.1',
|
||||
'X-Amz-Target': 'AWSCognitoIdentityService.ListIdentityPools'
|
||||
}
|
||||
}))
|
||||
/*
|
||||
{"IdentityPools":[]}
|
||||
...
|
||||
*/
|
||||
|
||||
request(aws4.sign({
|
||||
service: 'mobileanalytics',
|
||||
path: '/2014-06-05/events',
|
||||
body: JSON.stringify({events:[{
|
||||
eventType: 'a',
|
||||
timestamp: new Date().toISOString(),
|
||||
session: {},
|
||||
}]}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Amz-Client-Context': JSON.stringify({
|
||||
client: {client_id: 'a', app_title: 'a'},
|
||||
custom: {},
|
||||
env: {platform: 'a'},
|
||||
services: {},
|
||||
}),
|
||||
}
|
||||
}))
|
||||
/*
|
||||
(HTTP 202, empty response)
|
||||
*/
|
||||
|
||||
// Generate CodeCommit Git access password
|
||||
var signer = new aws4.RequestSigner({
|
||||
service: 'codecommit',
|
||||
host: 'git-codecommit.us-east-1.amazonaws.com',
|
||||
method: 'GIT',
|
||||
path: '/v1/repos/MyAwesomeRepo',
|
||||
})
|
||||
var password = signer.getDateTime() + 'Z' + signer.signature()
|
||||
```
|
||||
|
||||
API
|
||||
---
|
||||
|
||||
### aws4.sign(requestOptions, [credentials])
|
||||
|
||||
This calculates and populates the `Authorization` header of
|
||||
`requestOptions`, and any other necessary AWS headers and/or request
|
||||
options. Returns `requestOptions` as a convenience for chaining.
|
||||
|
||||
`requestOptions` is an object holding the same options that the node.js
|
||||
[http.request](http://nodejs.org/docs/latest/api/http.html#http_http_request_options_callback)
|
||||
function takes.
|
||||
|
||||
The following properties of `requestOptions` are used in the signing or
|
||||
populated if they don't already exist:
|
||||
|
||||
- `hostname` or `host` (will be determined from `service` and `region` if not given)
|
||||
- `method` (will use `'GET'` if not given or `'POST'` if there is a `body`)
|
||||
- `path` (will use `'/'` if not given)
|
||||
- `body` (will use `''` if not given)
|
||||
- `service` (will be calculated from `hostname` or `host` if not given)
|
||||
- `region` (will be calculated from `hostname` or `host` or use `'us-east-1'` if not given)
|
||||
- `headers['Host']` (will use `hostname` or `host` or be calculated if not given)
|
||||
- `headers['Content-Type']` (will use `'application/x-www-form-urlencoded; charset=utf-8'`
|
||||
if not given and there is a `body`)
|
||||
- `headers['Date']` (used to calculate the signature date if given, otherwise `new Date` is used)
|
||||
|
||||
Your AWS credentials (which can be found in your
|
||||
[AWS console](https://portal.aws.amazon.com/gp/aws/securityCredentials))
|
||||
can be specified in one of two ways:
|
||||
|
||||
- As the second argument, like this:
|
||||
|
||||
```javascript
|
||||
aws4.sign(requestOptions, {
|
||||
secretAccessKey: "<your-secret-access-key>",
|
||||
accessKeyId: "<your-access-key-id>",
|
||||
sessionToken: "<your-session-token>"
|
||||
})
|
||||
```
|
||||
|
||||
- From `process.env`, such as this:
|
||||
|
||||
```
|
||||
export AWS_SECRET_ACCESS_KEY="<your-secret-access-key>"
|
||||
export AWS_ACCESS_KEY_ID="<your-access-key-id>"
|
||||
export AWS_SESSION_TOKEN="<your-session-token>"
|
||||
```
|
||||
|
||||
(will also use `AWS_ACCESS_KEY` and `AWS_SECRET_KEY` if available)
|
||||
|
||||
The `sessionToken` property and `AWS_SESSION_TOKEN` environment variable are optional for signing
|
||||
with [IAM STS temporary credentials](http://docs.aws.amazon.com/STS/latest/UsingSTS/using-temp-creds.html).
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
With [npm](http://npmjs.org/) do:
|
||||
|
||||
```
|
||||
npm install aws4
|
||||
```
|
||||
|
||||
Can also be used [in the browser](./browser).
|
||||
|
||||
Thanks
|
||||
------
|
||||
|
||||
Thanks to [@jed](https://github.com/jed) for his
|
||||
[dynamo-client](https://github.com/jed/dynamo-client) lib where I first
|
||||
committed and subsequently extracted this code.
|
||||
|
||||
Also thanks to the
|
||||
[official node.js AWS SDK](https://github.com/aws/aws-sdk-js) for giving
|
||||
me a start on implementing the v4 signature.
|
||||
|
@ -0,0 +1,96 @@
|
||||
module.exports = function(size) {
|
||||
return new LruCache(size)
|
||||
}
|
||||
|
||||
function LruCache(size) {
|
||||
this.capacity = size | 0
|
||||
this.map = Object.create(null)
|
||||
this.list = new DoublyLinkedList()
|
||||
}
|
||||
|
||||
LruCache.prototype.get = function(key) {
|
||||
var node = this.map[key]
|
||||
if (node == null) return undefined
|
||||
this.used(node)
|
||||
return node.val
|
||||
}
|
||||
|
||||
LruCache.prototype.set = function(key, val) {
|
||||
var node = this.map[key]
|
||||
if (node != null) {
|
||||
node.val = val
|
||||
} else {
|
||||
if (!this.capacity) this.prune()
|
||||
if (!this.capacity) return false
|
||||
node = new DoublyLinkedNode(key, val)
|
||||
this.map[key] = node
|
||||
this.capacity--
|
||||
}
|
||||
this.used(node)
|
||||
return true
|
||||
}
|
||||
|
||||
LruCache.prototype.used = function(node) {
|
||||
this.list.moveToFront(node)
|
||||
}
|
||||
|
||||
LruCache.prototype.prune = function() {
|
||||
var node = this.list.pop()
|
||||
if (node != null) {
|
||||
delete this.map[node.key]
|
||||
this.capacity++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function DoublyLinkedList() {
|
||||
this.firstNode = null
|
||||
this.lastNode = null
|
||||
}
|
||||
|
||||
DoublyLinkedList.prototype.moveToFront = function(node) {
|
||||
if (this.firstNode == node) return
|
||||
|
||||
this.remove(node)
|
||||
|
||||
if (this.firstNode == null) {
|
||||
this.firstNode = node
|
||||
this.lastNode = node
|
||||
node.prev = null
|
||||
node.next = null
|
||||
} else {
|
||||
node.prev = null
|
||||
node.next = this.firstNode
|
||||
node.next.prev = node
|
||||
this.firstNode = node
|
||||
}
|
||||
}
|
||||
|
||||
DoublyLinkedList.prototype.pop = function() {
|
||||
var lastNode = this.lastNode
|
||||
if (lastNode != null) {
|
||||
this.remove(lastNode)
|
||||
}
|
||||
return lastNode
|
||||
}
|
||||
|
||||
DoublyLinkedList.prototype.remove = function(node) {
|
||||
if (this.firstNode == node) {
|
||||
this.firstNode = node.next
|
||||
} else if (node.prev != null) {
|
||||
node.prev.next = node.next
|
||||
}
|
||||
if (this.lastNode == node) {
|
||||
this.lastNode = node.prev
|
||||
} else if (node.next != null) {
|
||||
node.next.prev = node.prev
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function DoublyLinkedNode(key, val) {
|
||||
this.key = key
|
||||
this.val = val
|
||||
this.prev = null
|
||||
this.next = null
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
{
|
||||
"name": "aws4",
|
||||
"version": "1.6.0",
|
||||
"description": "Signs and prepares requests using AWS Signature Version 4",
|
||||
"author": "Michael Hart <michael.hart.au@gmail.com> (http://github.com/mhart)",
|
||||
"main": "aws4.js",
|
||||
"keywords": [
|
||||
"amazon",
|
||||
"aws",
|
||||
"signature",
|
||||
"s3",
|
||||
"ec2",
|
||||
"autoscaling",
|
||||
"cloudformation",
|
||||
"elasticloadbalancing",
|
||||
"elb",
|
||||
"elasticbeanstalk",
|
||||
"cloudsearch",
|
||||
"dynamodb",
|
||||
"kinesis",
|
||||
"lambda",
|
||||
"glacier",
|
||||
"sqs",
|
||||
"sns",
|
||||
"iam",
|
||||
"sts",
|
||||
"ses",
|
||||
"swf",
|
||||
"storagegateway",
|
||||
"datapipeline",
|
||||
"directconnect",
|
||||
"redshift",
|
||||
"opsworks",
|
||||
"rds",
|
||||
"monitoring",
|
||||
"cloudtrail",
|
||||
"cloudfront",
|
||||
"codedeploy",
|
||||
"elasticache",
|
||||
"elasticmapreduce",
|
||||
"elastictranscoder",
|
||||
"emr",
|
||||
"cloudwatch",
|
||||
"mobileanalytics",
|
||||
"cognitoidentity",
|
||||
"cognitosync",
|
||||
"cognito",
|
||||
"containerservice",
|
||||
"ecs",
|
||||
"appstream",
|
||||
"keymanagementservice",
|
||||
"kms",
|
||||
"config",
|
||||
"cloudhsm",
|
||||
"route53",
|
||||
"route53domains",
|
||||
"logs"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mhart/aws4.git"
|
||||
},
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"mocha": "^2.4.5",
|
||||
"should": "^8.2.2"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha ./test/fast.js ./test/slow.js -b -t 100s -R list"
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
Port of the OpenBSD `bcrypt_pbkdf` function to pure Javascript. `npm`-ified
|
||||
version of [Devi Mandiri's port]
|
||||
(https://github.com/devi/tmp/blob/master/js/bcrypt_pbkdf.js),
|
||||
with some minor performance improvements. The code is copied verbatim (and
|
||||
un-styled) from Devi's work.
|
||||
|
||||
This product includes software developed by Niels Provos.
|
||||
|
||||
## API
|
||||
|
||||
### `bcrypt_pbkdf.pbkdf(pass, passlen, salt, saltlen, key, keylen, rounds)`
|
||||
|
||||
Derive a cryptographic key of arbitrary length from a given password and salt,
|
||||
using the OpenBSD `bcrypt_pbkdf` function. This is a combination of Blowfish and
|
||||
SHA-512.
|
||||
|
||||
See [this article](http://www.tedunangst.com/flak/post/bcrypt-pbkdf) for
|
||||
further information.
|
||||
|
||||
Parameters:
|
||||
|
||||
* `pass`, a Uint8Array of length `passlen`
|
||||
* `passlen`, an integer Number
|
||||
* `salt`, a Uint8Array of length `saltlen`
|
||||
* `saltlen`, an integer Number
|
||||
* `key`, a Uint8Array of length `keylen`, will be filled with output
|
||||
* `keylen`, an integer Number
|
||||
* `rounds`, an integer Number, number of rounds of the PBKDF to run
|
||||
|
||||
### `bcrypt_pbkdf.hash(sha2pass, sha2salt, out)`
|
||||
|
||||
Calculate a Blowfish hash, given SHA2-512 output of a password and salt. Used as
|
||||
part of the inner round function in the PBKDF.
|
||||
|
||||
Parameters:
|
||||
|
||||
* `sha2pass`, a Uint8Array of length 64
|
||||
* `sha2salt`, a Uint8Array of length 64
|
||||
* `out`, a Uint8Array of length 32, will be filled with output
|
@ -0,0 +1,556 @@
|
||||
'use strict';
|
||||
|
||||
var crypto_hash_sha512 = require('tweetnacl').lowlevel.crypto_hash;
|
||||
|
||||
/*
|
||||
* This file is a 1:1 port from the OpenBSD blowfish.c and bcrypt_pbkdf.c. As a
|
||||
* result, it retains the original copyright and license. The two files are
|
||||
* under slightly different (but compatible) licenses, and are here combined in
|
||||
* one file.
|
||||
*
|
||||
* Credit for the actual porting work goes to:
|
||||
* Devi Mandiri <me@devi.web.id>
|
||||
*/
|
||||
|
||||
/*
|
||||
* The Blowfish portions are under the following license:
|
||||
*
|
||||
* Blowfish block cipher for OpenBSD
|
||||
* Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Implementation advice by David Mazieres <dm@lcs.mit.edu>.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The bcrypt_pbkdf portions are under the following license:
|
||||
*
|
||||
* Copyright (c) 2013 Ted Unangst <tedu@openbsd.org>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Performance improvements (Javascript-specific):
|
||||
*
|
||||
* Copyright 2016, Joyent Inc
|
||||
* Author: Alex Wilson <alex.wilson@joyent.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
// Ported from OpenBSD bcrypt_pbkdf.c v1.9
|
||||
|
||||
var BLF_J = 0;
|
||||
|
||||
var Blowfish = function() {
|
||||
this.S = [
|
||||
new Uint32Array([
|
||||
0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7,
|
||||
0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99,
|
||||
0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
|
||||
0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e,
|
||||
0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee,
|
||||
0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013,
|
||||
0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef,
|
||||
0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e,
|
||||
0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60,
|
||||
0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440,
|
||||
0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce,
|
||||
0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a,
|
||||
0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e,
|
||||
0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677,
|
||||
0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193,
|
||||
0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032,
|
||||
0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88,
|
||||
0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239,
|
||||
0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e,
|
||||
0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0,
|
||||
0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3,
|
||||
0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98,
|
||||
0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88,
|
||||
0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe,
|
||||
0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6,
|
||||
0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d,
|
||||
0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b,
|
||||
0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7,
|
||||
0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba,
|
||||
0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463,
|
||||
0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f,
|
||||
0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09,
|
||||
0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3,
|
||||
0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb,
|
||||
0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279,
|
||||
0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8,
|
||||
0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab,
|
||||
0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82,
|
||||
0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db,
|
||||
0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573,
|
||||
0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0,
|
||||
0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b,
|
||||
0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790,
|
||||
0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8,
|
||||
0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4,
|
||||
0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0,
|
||||
0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7,
|
||||
0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c,
|
||||
0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad,
|
||||
0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1,
|
||||
0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299,
|
||||
0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9,
|
||||
0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477,
|
||||
0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf,
|
||||
0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49,
|
||||
0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af,
|
||||
0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa,
|
||||
0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5,
|
||||
0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41,
|
||||
0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915,
|
||||
0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400,
|
||||
0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915,
|
||||
0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664,
|
||||
0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a]),
|
||||
new Uint32Array([
|
||||
0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623,
|
||||
0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266,
|
||||
0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1,
|
||||
0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e,
|
||||
0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6,
|
||||
0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1,
|
||||
0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e,
|
||||
0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1,
|
||||
0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737,
|
||||
0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8,
|
||||
0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff,
|
||||
0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd,
|
||||
0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701,
|
||||
0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7,
|
||||
0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41,
|
||||
0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331,
|
||||
0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf,
|
||||
0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af,
|
||||
0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e,
|
||||
0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87,
|
||||
0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c,
|
||||
0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2,
|
||||
0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16,
|
||||
0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd,
|
||||
0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b,
|
||||
0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509,
|
||||
0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e,
|
||||
0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3,
|
||||
0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f,
|
||||
0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a,
|
||||
0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4,
|
||||
0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960,
|
||||
0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66,
|
||||
0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28,
|
||||
0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802,
|
||||
0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84,
|
||||
0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510,
|
||||
0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf,
|
||||
0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14,
|
||||
0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e,
|
||||
0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50,
|
||||
0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7,
|
||||
0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8,
|
||||
0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281,
|
||||
0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99,
|
||||
0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696,
|
||||
0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128,
|
||||
0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73,
|
||||
0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0,
|
||||
0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0,
|
||||
0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105,
|
||||
0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250,
|
||||
0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3,
|
||||
0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285,
|
||||
0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00,
|
||||
0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061,
|
||||
0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb,
|
||||
0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e,
|
||||
0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735,
|
||||
0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc,
|
||||
0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9,
|
||||
0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340,
|
||||
0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20,
|
||||
0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7]),
|
||||
new Uint32Array([
|
||||
0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934,
|
||||
0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068,
|
||||
0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af,
|
||||
0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840,
|
||||
0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45,
|
||||
0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504,
|
||||
0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a,
|
||||
0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb,
|
||||
0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee,
|
||||
0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6,
|
||||
0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42,
|
||||
0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b,
|
||||
0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2,
|
||||
0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb,
|
||||
0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527,
|
||||
0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b,
|
||||
0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33,
|
||||
0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c,
|
||||
0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3,
|
||||
0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc,
|
||||
0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17,
|
||||
0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564,
|
||||
0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b,
|
||||
0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115,
|
||||
0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922,
|
||||
0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728,
|
||||
0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0,
|
||||
0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e,
|
||||
0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37,
|
||||
0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d,
|
||||
0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804,
|
||||
0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b,
|
||||
0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3,
|
||||
0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb,
|
||||
0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d,
|
||||
0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c,
|
||||
0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350,
|
||||
0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9,
|
||||
0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a,
|
||||
0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe,
|
||||
0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d,
|
||||
0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc,
|
||||
0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f,
|
||||
0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61,
|
||||
0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2,
|
||||
0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9,
|
||||
0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2,
|
||||
0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c,
|
||||
0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e,
|
||||
0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633,
|
||||
0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10,
|
||||
0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169,
|
||||
0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52,
|
||||
0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027,
|
||||
0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5,
|
||||
0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62,
|
||||
0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634,
|
||||
0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76,
|
||||
0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24,
|
||||
0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc,
|
||||
0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4,
|
||||
0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c,
|
||||
0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837,
|
||||
0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0]),
|
||||
new Uint32Array([
|
||||
0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b,
|
||||
0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe,
|
||||
0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b,
|
||||
0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4,
|
||||
0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8,
|
||||
0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6,
|
||||
0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304,
|
||||
0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22,
|
||||
0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4,
|
||||
0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6,
|
||||
0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9,
|
||||
0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59,
|
||||
0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593,
|
||||
0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51,
|
||||
0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28,
|
||||
0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c,
|
||||
0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b,
|
||||
0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28,
|
||||
0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c,
|
||||
0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd,
|
||||
0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a,
|
||||
0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319,
|
||||
0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb,
|
||||
0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f,
|
||||
0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991,
|
||||
0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32,
|
||||
0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680,
|
||||
0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166,
|
||||
0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae,
|
||||
0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb,
|
||||
0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5,
|
||||
0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47,
|
||||
0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370,
|
||||
0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d,
|
||||
0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84,
|
||||
0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048,
|
||||
0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8,
|
||||
0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd,
|
||||
0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9,
|
||||
0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7,
|
||||
0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38,
|
||||
0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f,
|
||||
0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c,
|
||||
0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525,
|
||||
0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1,
|
||||
0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442,
|
||||
0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964,
|
||||
0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e,
|
||||
0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8,
|
||||
0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d,
|
||||
0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f,
|
||||
0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299,
|
||||
0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02,
|
||||
0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc,
|
||||
0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614,
|
||||
0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a,
|
||||
0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6,
|
||||
0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b,
|
||||
0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0,
|
||||
0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060,
|
||||
0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e,
|
||||
0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9,
|
||||
0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f,
|
||||
0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6])
|
||||
];
|
||||
this.P = new Uint32Array([
|
||||
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
|
||||
0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
|
||||
0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
|
||||
0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
|
||||
0x9216d5d9, 0x8979fb1b]);
|
||||
};
|
||||
|
||||
function F(S, x8, i) {
|
||||
return (((S[0][x8[i+3]] +
|
||||
S[1][x8[i+2]]) ^
|
||||
S[2][x8[i+1]]) +
|
||||
S[3][x8[i]]);
|
||||
};
|
||||
|
||||
Blowfish.prototype.encipher = function(x, x8) {
|
||||
if (x8 === undefined) {
|
||||
x8 = new Uint8Array(x.buffer);
|
||||
if (x.byteOffset !== 0)
|
||||
x8 = x8.subarray(x.byteOffset);
|
||||
}
|
||||
x[0] ^= this.P[0];
|
||||
for (var i = 1; i < 16; i += 2) {
|
||||
x[1] ^= F(this.S, x8, 0) ^ this.P[i];
|
||||
x[0] ^= F(this.S, x8, 4) ^ this.P[i+1];
|
||||
}
|
||||
var t = x[0];
|
||||
x[0] = x[1] ^ this.P[17];
|
||||
x[1] = t;
|
||||
};
|
||||
|
||||
Blowfish.prototype.decipher = function(x) {
|
||||
var x8 = new Uint8Array(x.buffer);
|
||||
if (x.byteOffset !== 0)
|
||||
x8 = x8.subarray(x.byteOffset);
|
||||
x[0] ^= this.P[17];
|
||||
for (var i = 16; i > 0; i -= 2) {
|
||||
x[1] ^= F(this.S, x8, 0) ^ this.P[i];
|
||||
x[0] ^= F(this.S, x8, 4) ^ this.P[i-1];
|
||||
}
|
||||
var t = x[0];
|
||||
x[0] = x[1] ^ this.P[0];
|
||||
x[1] = t;
|
||||
};
|
||||
|
||||
function stream2word(data, databytes){
|
||||
var i, temp = 0;
|
||||
for (i = 0; i < 4; i++, BLF_J++) {
|
||||
if (BLF_J >= databytes) BLF_J = 0;
|
||||
temp = (temp << 8) | data[BLF_J];
|
||||
}
|
||||
return temp;
|
||||
};
|
||||
|
||||
Blowfish.prototype.expand0state = function(key, keybytes) {
|
||||
var d = new Uint32Array(2), i, k;
|
||||
var d8 = new Uint8Array(d.buffer);
|
||||
|
||||
for (i = 0, BLF_J = 0; i < 18; i++) {
|
||||
this.P[i] ^= stream2word(key, keybytes);
|
||||
}
|
||||
BLF_J = 0;
|
||||
|
||||
for (i = 0; i < 18; i += 2) {
|
||||
this.encipher(d, d8);
|
||||
this.P[i] = d[0];
|
||||
this.P[i+1] = d[1];
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (k = 0; k < 256; k += 2) {
|
||||
this.encipher(d, d8);
|
||||
this.S[i][k] = d[0];
|
||||
this.S[i][k+1] = d[1];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Blowfish.prototype.expandstate = function(data, databytes, key, keybytes) {
|
||||
var d = new Uint32Array(2), i, k;
|
||||
|
||||
for (i = 0, BLF_J = 0; i < 18; i++) {
|
||||
this.P[i] ^= stream2word(key, keybytes);
|
||||
}
|
||||
|
||||
for (i = 0, BLF_J = 0; i < 18; i += 2) {
|
||||
d[0] ^= stream2word(data, databytes);
|
||||
d[1] ^= stream2word(data, databytes);
|
||||
this.encipher(d);
|
||||
this.P[i] = d[0];
|
||||
this.P[i+1] = d[1];
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (k = 0; k < 256; k += 2) {
|
||||
d[0] ^= stream2word(data, databytes);
|
||||
d[1] ^= stream2word(data, databytes);
|
||||
this.encipher(d);
|
||||
this.S[i][k] = d[0];
|
||||
this.S[i][k+1] = d[1];
|
||||
}
|
||||
}
|
||||
BLF_J = 0;
|
||||
};
|
||||
|
||||
Blowfish.prototype.enc = function(data, blocks) {
|
||||
for (var i = 0; i < blocks; i++) {
|
||||
this.encipher(data.subarray(i*2));
|
||||
}
|
||||
};
|
||||
|
||||
Blowfish.prototype.dec = function(data, blocks) {
|
||||
for (var i = 0; i < blocks; i++) {
|
||||
this.decipher(data.subarray(i*2));
|
||||
}
|
||||
};
|
||||
|
||||
var BCRYPT_BLOCKS = 8,
|
||||
BCRYPT_HASHSIZE = 32;
|
||||
|
||||
function bcrypt_hash(sha2pass, sha2salt, out) {
|
||||
var state = new Blowfish(),
|
||||
cdata = new Uint32Array(BCRYPT_BLOCKS), i,
|
||||
ciphertext = new Uint8Array([79,120,121,99,104,114,111,109,97,116,105,
|
||||
99,66,108,111,119,102,105,115,104,83,119,97,116,68,121,110,97,109,
|
||||
105,116,101]); //"OxychromaticBlowfishSwatDynamite"
|
||||
|
||||
state.expandstate(sha2salt, 64, sha2pass, 64);
|
||||
for (i = 0; i < 64; i++) {
|
||||
state.expand0state(sha2salt, 64);
|
||||
state.expand0state(sha2pass, 64);
|
||||
}
|
||||
|
||||
for (i = 0; i < BCRYPT_BLOCKS; i++)
|
||||
cdata[i] = stream2word(ciphertext, ciphertext.byteLength);
|
||||
for (i = 0; i < 64; i++)
|
||||
state.enc(cdata, cdata.byteLength / 8);
|
||||
|
||||
for (i = 0; i < BCRYPT_BLOCKS; i++) {
|
||||
out[4*i+3] = cdata[i] >>> 24;
|
||||
out[4*i+2] = cdata[i] >>> 16;
|
||||
out[4*i+1] = cdata[i] >>> 8;
|
||||
out[4*i+0] = cdata[i];
|
||||
}
|
||||
};
|
||||
|
||||
function bcrypt_pbkdf(pass, passlen, salt, saltlen, key, keylen, rounds) {
|
||||
var sha2pass = new Uint8Array(64),
|
||||
sha2salt = new Uint8Array(64),
|
||||
out = new Uint8Array(BCRYPT_HASHSIZE),
|
||||
tmpout = new Uint8Array(BCRYPT_HASHSIZE),
|
||||
countsalt = new Uint8Array(saltlen+4),
|
||||
i, j, amt, stride, dest, count,
|
||||
origkeylen = keylen;
|
||||
|
||||
if (rounds < 1)
|
||||
return -1;
|
||||
if (passlen === 0 || saltlen === 0 || keylen === 0 ||
|
||||
keylen > (out.byteLength * out.byteLength) || saltlen > (1<<20))
|
||||
return -1;
|
||||
|
||||
stride = Math.floor((keylen + out.byteLength - 1) / out.byteLength);
|
||||
amt = Math.floor((keylen + stride - 1) / stride);
|
||||
|
||||
for (i = 0; i < saltlen; i++)
|
||||
countsalt[i] = salt[i];
|
||||
|
||||
crypto_hash_sha512(sha2pass, pass, passlen);
|
||||
|
||||
for (count = 1; keylen > 0; count++) {
|
||||
countsalt[saltlen+0] = count >>> 24;
|
||||
countsalt[saltlen+1] = count >>> 16;
|
||||
countsalt[saltlen+2] = count >>> 8;
|
||||
countsalt[saltlen+3] = count;
|
||||
|
||||
crypto_hash_sha512(sha2salt, countsalt, saltlen + 4);
|
||||
bcrypt_hash(sha2pass, sha2salt, tmpout);
|
||||
for (i = out.byteLength; i--;)
|
||||
out[i] = tmpout[i];
|
||||
|
||||
for (i = 1; i < rounds; i++) {
|
||||
crypto_hash_sha512(sha2salt, tmpout, tmpout.byteLength);
|
||||
bcrypt_hash(sha2pass, sha2salt, tmpout);
|
||||
for (j = 0; j < out.byteLength; j++)
|
||||
out[j] ^= tmpout[j];
|
||||
}
|
||||
|
||||
amt = Math.min(amt, keylen);
|
||||
for (i = 0; i < amt; i++) {
|
||||
dest = i * stride + (count - 1);
|
||||
if (dest >= origkeylen)
|
||||
break;
|
||||
key[dest] = out[i];
|
||||
}
|
||||
keylen -= i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
BLOCKS: BCRYPT_BLOCKS,
|
||||
HASHSIZE: BCRYPT_HASHSIZE,
|
||||
hash: bcrypt_hash,
|
||||
pbkdf: bcrypt_pbkdf
|
||||
};
|
@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "bcrypt-pbkdf",
|
||||
"version": "1.0.1",
|
||||
"description": "Port of the OpenBSD bcrypt_pbkdf function to pure JS",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"tweetnacl": "^0.14.3"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"license": "BSD-3-Clause"
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
.idea
|
||||
*.iml
|
||||
npm-debug.log
|
||||
dump.rdb
|
||||
node_modules
|
||||
results.tap
|
||||
results.xml
|
||||
npm-shrinkwrap.json
|
||||
config.json
|
||||
.DS_Store
|
||||
*/.DS_Store
|
||||
*/*/.DS_Store
|
||||
._*
|
||||
*/._*
|
||||
*/*/._*
|
||||
coverage.*
|
||||
lib-cov
|
||||
|
@ -0,0 +1,8 @@
|
||||
language: node_js
|
||||
|
||||
node_js:
|
||||
- 0.10
|
||||
- 4.0
|
||||
|
||||
sudo: false
|
||||
|
@ -0,0 +1 @@
|
||||
Please view our [hapijs contributing guide](https://github.com/hapijs/hapi/blob/master/CONTRIBUTING.md).
|
@ -0,0 +1,28 @@
|
||||
Copyright (c) 2012-2014, Walmart and other contributors.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* The names of any contributors may not be used to endorse or promote
|
||||
products derived from this software without specific prior written
|
||||
permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
* * *
|
||||
|
||||
The complete list of contributors can be found at: https://github.com/hapijs/boom/graphs/contributors
|
@ -0,0 +1,652 @@
|
||||

|
||||
|
||||
HTTP-friendly error objects
|
||||
|
||||
[](http://travis-ci.org/hapijs/boom)
|
||||
[](https://www.npmjs.com/package/boom)
|
||||
|
||||
Lead Maintainer: [Adam Bretz](https://github.com/arb)
|
||||
|
||||
**boom** provides a set of utilities for returning HTTP errors. Each utility returns a `Boom` error response
|
||||
object (instance of `Error`) which includes the following properties:
|
||||
- `isBoom` - if `true`, indicates this is a `Boom` object instance.
|
||||
- `isServer` - convenience bool indicating status code >= 500.
|
||||
- `message` - the error message.
|
||||
- `output` - the formatted response. Can be directly manipulated after object construction to return a custom
|
||||
error response. Allowed root keys:
|
||||
- `statusCode` - the HTTP status code (typically 4xx or 5xx).
|
||||
- `headers` - an object containing any HTTP headers where each key is a header name and value is the header content.
|
||||
- `payload` - the formatted object used as the response payload (stringified). Can be directly manipulated but any
|
||||
changes will be lost
|
||||
if `reformat()` is called. Any content allowed and by default includes the following content:
|
||||
- `statusCode` - the HTTP status code, derived from `error.output.statusCode`.
|
||||
- `error` - the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from `statusCode`.
|
||||
- `message` - the error message derived from `error.message`.
|
||||
- inherited `Error` properties.
|
||||
|
||||
The `Boom` object also supports the following method:
|
||||
- `reformat()` - rebuilds `error.output` using the other object properties.
|
||||
|
||||
## Overview
|
||||
|
||||
- Helper methods
|
||||
- [`wrap(error, [statusCode], [message])`](#wraperror-statuscode-message)
|
||||
- [`create(statusCode, [message], [data])`](#createstatuscode-message-data)
|
||||
- HTTP 4xx Errors
|
||||
- 400: [`Boom.badRequest([message], [data])`](#boombadrequestmessage-data)
|
||||
- 401: [`Boom.unauthorized([message], [scheme], [attributes])`](#boomunauthorizedmessage-scheme-attributes)
|
||||
- 403: [`Boom.forbidden([message], [data])`](#boomforbiddenmessage-data)
|
||||
- 404: [`Boom.notFound([message], [data])`](#boomnotfoundmessage-data)
|
||||
- 405: [`Boom.methodNotAllowed([message], [data])`](#boommethodnotallowedmessage-data)
|
||||
- 406: [`Boom.notAcceptable([message], [data])`](#boomnotacceptablemessage-data)
|
||||
- 407: [`Boom.proxyAuthRequired([message], [data])`](#boomproxyauthrequiredmessage-data)
|
||||
- 408: [`Boom.clientTimeout([message], [data])`](#boomclienttimeoutmessage-data)
|
||||
- 409: [`Boom.conflict([message], [data])`](#boomconflictmessage-data)
|
||||
- 410: [`Boom.resourceGone([message], [data])`](#boomresourcegonemessage-data)
|
||||
- 411: [`Boom.lengthRequired([message], [data])`](#boomlengthrequiredmessage-data)
|
||||
- 412: [`Boom.preconditionFailed([message], [data])`](#boompreconditionfailedmessage-data)
|
||||
- 413: [`Boom.entityTooLarge([message], [data])`](#boomentitytoolargemessage-data)
|
||||
- 414: [`Boom.uriTooLong([message], [data])`](#boomuritoolongmessage-data)
|
||||
- 415: [`Boom.unsupportedMediaType([message], [data])`](#boomunsupportedmediatypemessage-data)
|
||||
- 416: [`Boom.rangeNotSatisfiable([message], [data])`](#boomrangenotsatisfiablemessage-data)
|
||||
- 417: [`Boom.expectationFailed([message], [data])`](#boomexpectationfailedmessage-data)
|
||||
- 422: [`Boom.badData([message], [data])`](#boombaddatamessage-data)
|
||||
- 428: [`Boom.preconditionRequired([message], [data])`](#boompreconditionrequiredmessage-data)
|
||||
- 429: [`Boom.tooManyRequests([message], [data])`](#boomtoomanyrequestsmessage-data)
|
||||
- HTTP 5xx Errors
|
||||
- 500: [`Boom.badImplementation([message], [data])`](#boombadimplementationmessage-data)
|
||||
- 501: [`Boom.notImplemented([message], [data])`](#boomnotimplementedmessage-data)
|
||||
- 502: [`Boom.badGateway([message], [data])`](#boombadgatewaymessage-data)
|
||||
- 503: [`Boom.serverTimeout([message], [data])`](#boomservertimeoutmessage-data)
|
||||
- 504: [`Boom.gatewayTimeout([message], [data])`](#boomgatewaytimeoutmessage-data)
|
||||
- [FAQ](#faq)
|
||||
|
||||
|
||||
## Helper Methods
|
||||
|
||||
### `wrap(error, [statusCode], [message])`
|
||||
|
||||
Decorates an error with the **boom** properties where:
|
||||
- `error` - the error object to wrap. If `error` is already a **boom** object, returns back the same object.
|
||||
- `statusCode` - optional HTTP status code. Defaults to `500`.
|
||||
- `message` - optional message string. If the error already has a message, it adds the message as a prefix.
|
||||
Defaults to no message.
|
||||
|
||||
```js
|
||||
var error = new Error('Unexpected input');
|
||||
Boom.wrap(error, 400);
|
||||
```
|
||||
|
||||
### `create(statusCode, [message], [data])`
|
||||
|
||||
Generates an `Error` object with the **boom** decorations where:
|
||||
- `statusCode` - an HTTP error code number. Must be greater or equal 400.
|
||||
- `message` - optional message string.
|
||||
- `data` - additional error data set to `error.data` property.
|
||||
|
||||
```js
|
||||
var error = Boom.create(400, 'Bad request', { timestamp: Date.now() });
|
||||
```
|
||||
|
||||
## HTTP 4xx Errors
|
||||
|
||||
### `Boom.badRequest([message], [data])`
|
||||
|
||||
Returns a 400 Bad Request error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.badRequest('invalid query');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 400,
|
||||
"error": "Bad Request",
|
||||
"message": "invalid query"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.unauthorized([message], [scheme], [attributes])`
|
||||
|
||||
Returns a 401 Unauthorized error where:
|
||||
- `message` - optional message.
|
||||
- `scheme` can be one of the following:
|
||||
- an authentication scheme name
|
||||
- an array of string values. These values will be separated by ', ' and set to the 'WWW-Authenticate' header.
|
||||
- `attributes` - an object of values to use while setting the 'WWW-Authenticate' header. This value is only used
|
||||
when `schema` is a string, otherwise it is ignored. Every key/value pair will be included in the
|
||||
'WWW-Authenticate' in the format of 'key="value"' as well as in the response payload under the `attributes` key.
|
||||
`null` and `undefined` will be replaced with an empty string. If `attributes` is set, `message` will be used as
|
||||
the 'error' segment of the 'WWW-Authenticate' header. If `message` is unset, the 'error' segment of the header
|
||||
will not be present and `isMissing` will be true on the error object.
|
||||
|
||||
If either `scheme` or `attributes` are set, the resultant `Boom` object will have the 'WWW-Authenticate' header set for the response.
|
||||
|
||||
```js
|
||||
Boom.unauthorized('invalid password');
|
||||
```
|
||||
|
||||
Generates the following response:
|
||||
|
||||
```json
|
||||
"payload": {
|
||||
"statusCode": 401,
|
||||
"error": "Unauthorized",
|
||||
"message": "invalid password"
|
||||
},
|
||||
"headers" {}
|
||||
```
|
||||
|
||||
```js
|
||||
Boom.unauthorized('invalid password', 'sample');
|
||||
```
|
||||
|
||||
Generates the following response:
|
||||
|
||||
```json
|
||||
"payload": {
|
||||
"statusCode": 401,
|
||||
"error": "Unauthorized",
|
||||
"message": "invalid password",
|
||||
"attributes": {
|
||||
"error": "invalid password"
|
||||
}
|
||||
},
|
||||
"headers" {
|
||||
"WWW-Authenticate": "sample error=\"invalid password\""
|
||||
}
|
||||
```
|
||||
|
||||
```js
|
||||
Boom.unauthorized('invalid password', 'sample', { ttl: 0, cache: null, foo: 'bar' });
|
||||
```
|
||||
|
||||
Generates the following response:
|
||||
|
||||
```json
|
||||
"payload": {
|
||||
"statusCode": 401,
|
||||
"error": "Unauthorized",
|
||||
"message": "invalid password",
|
||||
"attributes": {
|
||||
"error": "invalid password",
|
||||
"ttl": 0,
|
||||
"cache": "",
|
||||
"foo": "bar"
|
||||
}
|
||||
},
|
||||
"headers" {
|
||||
"WWW-Authenticate": "sample ttl=\"0\", cache=\"\", foo=\"bar\", error=\"invalid password\""
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.forbidden([message], [data])`
|
||||
|
||||
Returns a 403 Forbidden error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.forbidden('try again some time');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 403,
|
||||
"error": "Forbidden",
|
||||
"message": "try again some time"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.notFound([message], [data])`
|
||||
|
||||
Returns a 404 Not Found error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.notFound('missing');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 404,
|
||||
"error": "Not Found",
|
||||
"message": "missing"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.methodNotAllowed([message], [data])`
|
||||
|
||||
Returns a 405 Method Not Allowed error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.methodNotAllowed('that method is not allowed');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 405,
|
||||
"error": "Method Not Allowed",
|
||||
"message": "that method is not allowed"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.notAcceptable([message], [data])`
|
||||
|
||||
Returns a 406 Not Acceptable error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.notAcceptable('unacceptable');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 406,
|
||||
"error": "Not Acceptable",
|
||||
"message": "unacceptable"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.proxyAuthRequired([message], [data])`
|
||||
|
||||
Returns a 407 Proxy Authentication Required error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.proxyAuthRequired('auth missing');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 407,
|
||||
"error": "Proxy Authentication Required",
|
||||
"message": "auth missing"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.clientTimeout([message], [data])`
|
||||
|
||||
Returns a 408 Request Time-out error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.clientTimeout('timed out');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 408,
|
||||
"error": "Request Time-out",
|
||||
"message": "timed out"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.conflict([message], [data])`
|
||||
|
||||
Returns a 409 Conflict error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.conflict('there was a conflict');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 409,
|
||||
"error": "Conflict",
|
||||
"message": "there was a conflict"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.resourceGone([message], [data])`
|
||||
|
||||
Returns a 410 Gone error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.resourceGone('it is gone');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 410,
|
||||
"error": "Gone",
|
||||
"message": "it is gone"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.lengthRequired([message], [data])`
|
||||
|
||||
Returns a 411 Length Required error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.lengthRequired('length needed');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 411,
|
||||
"error": "Length Required",
|
||||
"message": "length needed"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.preconditionFailed([message], [data])`
|
||||
|
||||
Returns a 412 Precondition Failed error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.preconditionFailed();
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 412,
|
||||
"error": "Precondition Failed"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.entityTooLarge([message], [data])`
|
||||
|
||||
Returns a 413 Request Entity Too Large error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.entityTooLarge('too big');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 413,
|
||||
"error": "Request Entity Too Large",
|
||||
"message": "too big"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.uriTooLong([message], [data])`
|
||||
|
||||
Returns a 414 Request-URI Too Large error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.uriTooLong('uri is too long');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 414,
|
||||
"error": "Request-URI Too Large",
|
||||
"message": "uri is too long"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.unsupportedMediaType([message], [data])`
|
||||
|
||||
Returns a 415 Unsupported Media Type error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.unsupportedMediaType('that media is not supported');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 415,
|
||||
"error": "Unsupported Media Type",
|
||||
"message": "that media is not supported"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.rangeNotSatisfiable([message], [data])`
|
||||
|
||||
Returns a 416 Requested Range Not Satisfiable error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.rangeNotSatisfiable();
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 416,
|
||||
"error": "Requested Range Not Satisfiable"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.expectationFailed([message], [data])`
|
||||
|
||||
Returns a 417 Expectation Failed error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.expectationFailed('expected this to work');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 417,
|
||||
"error": "Expectation Failed",
|
||||
"message": "expected this to work"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.badData([message], [data])`
|
||||
|
||||
Returns a 422 Unprocessable Entity error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.badData('your data is bad and you should feel bad');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 422,
|
||||
"error": "Unprocessable Entity",
|
||||
"message": "your data is bad and you should feel bad"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.preconditionRequired([message], [data])`
|
||||
|
||||
Returns a 428 Precondition Required error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.preconditionRequired('you must supply an If-Match header');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 428,
|
||||
"error": "Precondition Required",
|
||||
"message": "you must supply an If-Match header"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.tooManyRequests([message], [data])`
|
||||
|
||||
Returns a 429 Too Many Requests error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.tooManyRequests('you have exceeded your request limit');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 429,
|
||||
"error": "Too Many Requests",
|
||||
"message": "you have exceeded your request limit"
|
||||
}
|
||||
```
|
||||
|
||||
## HTTP 5xx Errors
|
||||
|
||||
All 500 errors hide your message from the end user. Your message is recorded in the server log.
|
||||
|
||||
### `Boom.badImplementation([message], [data])`
|
||||
|
||||
Returns a 500 Internal Server Error error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.badImplementation('terrible implementation');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 500,
|
||||
"error": "Internal Server Error",
|
||||
"message": "An internal server error occurred"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.notImplemented([message], [data])`
|
||||
|
||||
Returns a 501 Not Implemented error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.notImplemented('method not implemented');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 501,
|
||||
"error": "Not Implemented",
|
||||
"message": "method not implemented"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.badGateway([message], [data])`
|
||||
|
||||
Returns a 502 Bad Gateway error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.badGateway('that is a bad gateway');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 502,
|
||||
"error": "Bad Gateway",
|
||||
"message": "that is a bad gateway"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.serverTimeout([message], [data])`
|
||||
|
||||
Returns a 503 Service Unavailable error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.serverTimeout('unavailable');
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 503,
|
||||
"error": "Service Unavailable",
|
||||
"message": "unavailable"
|
||||
}
|
||||
```
|
||||
|
||||
### `Boom.gatewayTimeout([message], [data])`
|
||||
|
||||
Returns a 504 Gateway Time-out error where:
|
||||
- `message` - optional message.
|
||||
- `data` - optional additional error data.
|
||||
|
||||
```js
|
||||
Boom.gatewayTimeout();
|
||||
```
|
||||
|
||||
Generates the following response payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"statusCode": 504,
|
||||
"error": "Gateway Time-out"
|
||||
}
|
||||
```
|
||||
|
||||
## F.A.Q.
|
||||
|
||||
###### How do I include extra information in my responses? `output.payload` is missing `data`, what gives?
|
||||
|
||||
There is a reason the values passed back in the response payloads are pretty locked down. It's mostly for security and to not leak any important information back to the client. This means you will need to put in a little more effort to include extra information about your custom error. Check out the ["Error transformation"](https://github.com/hapijs/hapi/blob/master/API.md#error-transformation) section in the hapi documentation.
|
After Width: | Height: | Size: 29 KiB |
@ -0,0 +1,318 @@
|
||||
// Load modules
|
||||
|
||||
var Http = require('http');
|
||||
var Hoek = require('hoek');
|
||||
|
||||
|
||||
// Declare internals
|
||||
|
||||
var internals = {};
|
||||
|
||||
exports.wrap = function (error, statusCode, message) {
|
||||
|
||||
Hoek.assert(error instanceof Error, 'Cannot wrap non-Error object');
|
||||
return (error.isBoom ? error : internals.initialize(error, statusCode || 500, message));
|
||||
};
|
||||
|
||||
|
||||
exports.create = function (statusCode, message, data) {
|
||||
|
||||
return internals.create(statusCode, message, data, exports.create);
|
||||
};
|
||||
|
||||
internals.create = function (statusCode, message, data, ctor) {
|
||||
|
||||
var error = new Error(message ? message : undefined); // Avoids settings null message
|
||||
Error.captureStackTrace(error, ctor); // Filter the stack to our external API
|
||||
error.data = data || null;
|
||||
internals.initialize(error, statusCode);
|
||||
return error;
|
||||
};
|
||||
|
||||
internals.initialize = function (error, statusCode, message) {
|
||||
|
||||
var numberCode = parseInt(statusCode, 10);
|
||||
Hoek.assert(!isNaN(numberCode) && numberCode >= 400, 'First argument must be a number (400+):', statusCode);
|
||||
|
||||
error.isBoom = true;
|
||||
error.isServer = numberCode >= 500;
|
||||
|
||||
if (!error.hasOwnProperty('data')) {
|
||||
error.data = null;
|
||||
}
|
||||
|
||||
error.output = {
|
||||
statusCode: numberCode,
|
||||
payload: {},
|
||||
headers: {}
|
||||
};
|
||||
|
||||
error.reformat = internals.reformat;
|
||||
error.reformat();
|
||||
|
||||
if (!message &&
|
||||
!error.message) {
|
||||
|
||||
message = error.output.payload.error;
|
||||
}
|
||||
|
||||
if (message) {
|
||||
error.message = (message + (error.message ? ': ' + error.message : ''));
|
||||
}
|
||||
|
||||
return error;
|
||||
};
|
||||
|
||||
|
||||
internals.reformat = function () {
|
||||
|
||||
this.output.payload.statusCode = this.output.statusCode;
|
||||
this.output.payload.error = Http.STATUS_CODES[this.output.statusCode] || 'Unknown';
|
||||
|
||||
if (this.output.statusCode === 500) {
|
||||
this.output.payload.message = 'An internal server error occurred'; // Hide actual error from user
|
||||
}
|
||||
else if (this.message) {
|
||||
this.output.payload.message = this.message;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// 4xx Client Errors
|
||||
|
||||
exports.badRequest = function (message, data) {
|
||||
|
||||
return internals.create(400, message, data, exports.badRequest);
|
||||
};
|
||||
|
||||
|
||||
exports.unauthorized = function (message, scheme, attributes) { // Or function (message, wwwAuthenticate[])
|
||||
|
||||
var err = internals.create(401, message, undefined, exports.unauthorized);
|
||||
|
||||
if (!scheme) {
|
||||
return err;
|
||||
}
|
||||
|
||||
var wwwAuthenticate = '';
|
||||
var i = 0;
|
||||
var il = 0;
|
||||
|
||||
if (typeof scheme === 'string') {
|
||||
|
||||
// function (message, scheme, attributes)
|
||||
|
||||
wwwAuthenticate = scheme;
|
||||
|
||||
if (attributes || message) {
|
||||
err.output.payload.attributes = {};
|
||||
}
|
||||
|
||||
if (attributes) {
|
||||
var names = Object.keys(attributes);
|
||||
for (i = 0, il = names.length; i < il; ++i) {
|
||||
var name = names[i];
|
||||
if (i) {
|
||||
wwwAuthenticate += ',';
|
||||
}
|
||||
|
||||
var value = attributes[name];
|
||||
if (value === null ||
|
||||
value === undefined) { // Value can be zero
|
||||
|
||||
value = '';
|
||||
}
|
||||
wwwAuthenticate += ' ' + name + '="' + Hoek.escapeHeaderAttribute(value.toString()) + '"';
|
||||
err.output.payload.attributes[name] = value;
|
||||
}
|
||||
}
|
||||
|
||||
if (message) {
|
||||
if (attributes) {
|
||||
wwwAuthenticate += ',';
|
||||
}
|
||||
wwwAuthenticate += ' error="' + Hoek.escapeHeaderAttribute(message) + '"';
|
||||
err.output.payload.attributes.error = message;
|
||||
}
|
||||
else {
|
||||
err.isMissing = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
// function (message, wwwAuthenticate[])
|
||||
|
||||
var wwwArray = scheme;
|
||||
for (i = 0, il = wwwArray.length; i < il; ++i) {
|
||||
if (i) {
|
||||
wwwAuthenticate += ', ';
|
||||
}
|
||||
|
||||
wwwAuthenticate += wwwArray[i];
|
||||
}
|
||||
}
|
||||
|
||||
err.output.headers['WWW-Authenticate'] = wwwAuthenticate;
|
||||
|
||||
return err;
|
||||
};
|
||||
|
||||
|
||||
exports.forbidden = function (message, data) {
|
||||
|
||||
return internals.create(403, message, data, exports.forbidden);
|
||||
};
|
||||
|
||||
|
||||
exports.notFound = function (message, data) {
|
||||
|
||||
return internals.create(404, message, data, exports.notFound);
|
||||
};
|
||||
|
||||
|
||||
exports.methodNotAllowed = function (message, data) {
|
||||
|
||||
return internals.create(405, message, data, exports.methodNotAllowed);
|
||||
};
|
||||
|
||||
|
||||
exports.notAcceptable = function (message, data) {
|
||||
|
||||
return internals.create(406, message, data, exports.notAcceptable);
|
||||
};
|
||||
|
||||
|
||||
exports.proxyAuthRequired = function (message, data) {
|
||||
|
||||
return internals.create(407, message, data, exports.proxyAuthRequired);
|
||||
};
|
||||
|
||||
|
||||
exports.clientTimeout = function (message, data) {
|
||||
|
||||
return internals.create(408, message, data, exports.clientTimeout);
|
||||
};
|
||||
|
||||
|
||||
exports.conflict = function (message, data) {
|
||||
|
||||
return internals.create(409, message, data, exports.conflict);
|
||||
};
|
||||
|
||||
|
||||
exports.resourceGone = function (message, data) {
|
||||
|
||||
return internals.create(410, message, data, exports.resourceGone);
|
||||
};
|
||||
|
||||
|
||||
exports.lengthRequired = function (message, data) {
|
||||
|
||||
return internals.create(411, message, data, exports.lengthRequired);
|
||||
};
|
||||
|
||||
|
||||
exports.preconditionFailed = function (message, data) {
|
||||
|
||||
return internals.create(412, message, data, exports.preconditionFailed);
|
||||
};
|
||||
|
||||
|
||||
exports.entityTooLarge = function (message, data) {
|
||||
|
||||
return internals.create(413, message, data, exports.entityTooLarge);
|
||||
};
|
||||
|
||||
|
||||
exports.uriTooLong = function (message, data) {
|
||||
|
||||
return internals.create(414, message, data, exports.uriTooLong);
|
||||
};
|
||||
|
||||
|
||||
exports.unsupportedMediaType = function (message, data) {
|
||||
|
||||
return internals.create(415, message, data, exports.unsupportedMediaType);
|
||||
};
|
||||
|
||||
|
||||
exports.rangeNotSatisfiable = function (message, data) {
|
||||
|
||||
return internals.create(416, message, data, exports.rangeNotSatisfiable);
|
||||
};
|
||||
|
||||
|
||||
exports.expectationFailed = function (message, data) {
|
||||
|
||||
return internals.create(417, message, data, exports.expectationFailed);
|
||||
};
|
||||
|
||||
exports.badData = function (message, data) {
|
||||
|
||||
return internals.create(422, message, data, exports.badData);
|
||||
};
|
||||
|
||||
|
||||
exports.preconditionRequired = function (message, data) {
|
||||
|
||||
return internals.create(428, message, data, exports.preconditionRequired);
|
||||
};
|
||||
|
||||
|
||||
exports.tooManyRequests = function (message, data) {
|
||||
|
||||
return internals.create(429, message, data, exports.tooManyRequests);
|
||||
};
|
||||
|
||||
|
||||
// 5xx Server Errors
|
||||
|
||||
exports.internal = function (message, data, statusCode) {
|
||||
|
||||
return internals.serverError(message, data, statusCode, exports.internal);
|
||||
};
|
||||
|
||||
internals.serverError = function (message, data, statusCode, ctor) {
|
||||
|
||||
var error;
|
||||
if (data instanceof Error) {
|
||||
error = exports.wrap(data, statusCode, message);
|
||||
} else {
|
||||
error = internals.create(statusCode || 500, message, undefined, ctor);
|
||||
error.data = data;
|
||||
}
|
||||
|
||||
return error;
|
||||
};
|
||||
|
||||
|
||||
exports.notImplemented = function (message, data) {
|
||||
|
||||
return internals.serverError(message, data, 501, exports.notImplemented);
|
||||
};
|
||||
|
||||
|
||||
exports.badGateway = function (message, data) {
|
||||
|
||||
return internals.serverError(message, data, 502, exports.badGateway);
|
||||
};
|
||||
|
||||
|
||||
exports.serverTimeout = function (message, data) {
|
||||
|
||||
return internals.serverError(message, data, 503, exports.serverTimeout);
|
||||
};
|
||||
|
||||
|
||||
exports.gatewayTimeout = function (message, data) {
|
||||
|
||||
return internals.serverError(message, data, 504, exports.gatewayTimeout);
|
||||
};
|
||||
|
||||
|
||||
exports.badImplementation = function (message, data) {
|
||||
|
||||
var err = internals.serverError(message, data, 500, exports.badImplementation);
|
||||
err.isDeveloperError = true;
|
||||
return err;
|
||||
};
|
@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "boom",
|
||||
"description": "HTTP-friendly error objects",
|
||||
"version": "2.10.1",
|
||||
"repository": "git://github.com/hapijs/boom",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"error",
|
||||
"http"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=0.10.40"
|
||||
},
|
||||
"dependencies": {
|
||||
"hoek": "2.x.x"
|
||||
},
|
||||
"devDependencies": {
|
||||
"code": "1.x.x",
|
||||
"lab": "7.x.x"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "lab -a code -t 100 -L",
|
||||
"test-cov-html": "lab -a code -r html -o coverage.html -L"
|
||||
},
|
||||
"license": "BSD-3-Clause"
|
||||
}
|
@ -0,0 +1,654 @@
|
||||
// Load modules
|
||||
|
||||
var Code = require('code');
|
||||
var Boom = require('../lib');
|
||||
var Lab = require('lab');
|
||||
|
||||
|
||||
// Declare internals
|
||||
|
||||
var internals = {};
|
||||
|
||||
|
||||
// Test shortcuts
|
||||
|
||||
var lab = exports.lab = Lab.script();
|
||||
var describe = lab.describe;
|
||||
var it = lab.it;
|
||||
var expect = Code.expect;
|
||||
|
||||
|
||||
it('returns the same object when already boom', function (done) {
|
||||
|
||||
var error = Boom.badRequest();
|
||||
var wrapped = Boom.wrap(error);
|
||||
expect(error).to.equal(wrapped);
|
||||
done();
|
||||
});
|
||||
|
||||
it('returns an error with info when constructed using another error', function (done) {
|
||||
|
||||
var error = new Error('ka-boom');
|
||||
error.xyz = 123;
|
||||
var err = Boom.wrap(error);
|
||||
expect(err.xyz).to.equal(123);
|
||||
expect(err.message).to.equal('ka-boom');
|
||||
expect(err.output).to.deep.equal({
|
||||
statusCode: 500,
|
||||
payload: {
|
||||
statusCode: 500,
|
||||
error: 'Internal Server Error',
|
||||
message: 'An internal server error occurred'
|
||||
},
|
||||
headers: {}
|
||||
});
|
||||
expect(err.data).to.equal(null);
|
||||
done();
|
||||
});
|
||||
|
||||
it('does not override data when constructed using another error', function (done) {
|
||||
|
||||
var error = new Error('ka-boom');
|
||||
error.data = { useful: 'data' };
|
||||
var err = Boom.wrap(error);
|
||||
expect(err.data).to.equal(error.data);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets new message when none exists', function (done) {
|
||||
|
||||
var error = new Error();
|
||||
var wrapped = Boom.wrap(error, 400, 'something bad');
|
||||
expect(wrapped.message).to.equal('something bad');
|
||||
done();
|
||||
});
|
||||
|
||||
it('throws when statusCode is not a number', function (done) {
|
||||
|
||||
expect(function () {
|
||||
|
||||
Boom.create('x');
|
||||
}).to.throw('First argument must be a number (400+): x');
|
||||
done();
|
||||
});
|
||||
|
||||
it('will cast a number-string to an integer', function (done) {
|
||||
|
||||
var codes = [
|
||||
{ input: '404', result: 404 },
|
||||
{ input: '404.1', result: 404 },
|
||||
{ input: 400, result: 400 },
|
||||
{ input: 400.123, result: 400 }];
|
||||
for (var i = 0, il = codes.length; i < il; ++i) {
|
||||
var code = codes[i];
|
||||
var err = Boom.create(code.input);
|
||||
expect(err.output.statusCode).to.equal(code.result);
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it('throws when statusCode is not finite', function (done) {
|
||||
|
||||
expect(function () {
|
||||
|
||||
Boom.create(1 / 0);
|
||||
}).to.throw('First argument must be a number (400+): null');
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets error code to unknown', function (done) {
|
||||
|
||||
var err = Boom.create(999);
|
||||
expect(err.output.payload.error).to.equal('Unknown');
|
||||
done();
|
||||
});
|
||||
|
||||
describe('create()', function () {
|
||||
|
||||
it('does not sets null message', function (done) {
|
||||
|
||||
var error = Boom.unauthorized(null);
|
||||
expect(error.output.payload.message).to.not.exist();
|
||||
expect(error.isServer).to.be.false();
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets message and data', function (done) {
|
||||
|
||||
var error = Boom.badRequest('Missing data', { type: 'user' });
|
||||
expect(error.data.type).to.equal('user');
|
||||
expect(error.output.payload.message).to.equal('Missing data');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('isBoom()', function () {
|
||||
|
||||
it('returns true for Boom object', function (done) {
|
||||
|
||||
expect(Boom.badRequest().isBoom).to.equal(true);
|
||||
done();
|
||||
});
|
||||
|
||||
it('returns false for Error object', function (done) {
|
||||
|
||||
expect((new Error()).isBoom).to.not.exist();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('badRequest()', function () {
|
||||
|
||||
it('returns a 400 error statusCode', function (done) {
|
||||
|
||||
var error = Boom.badRequest();
|
||||
|
||||
expect(error.output.statusCode).to.equal(400);
|
||||
expect(error.isServer).to.be.false();
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.badRequest('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message to HTTP status if none provided', function (done) {
|
||||
|
||||
expect(Boom.badRequest().message).to.equal('Bad Request');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('unauthorized()', function () {
|
||||
|
||||
it('returns a 401 error statusCode', function (done) {
|
||||
|
||||
var err = Boom.unauthorized();
|
||||
expect(err.output.statusCode).to.equal(401);
|
||||
expect(err.output.headers).to.deep.equal({});
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.unauthorized('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
|
||||
it('returns a WWW-Authenticate header when passed a scheme', function (done) {
|
||||
|
||||
var err = Boom.unauthorized('boom', 'Test');
|
||||
expect(err.output.statusCode).to.equal(401);
|
||||
expect(err.output.headers['WWW-Authenticate']).to.equal('Test error="boom"');
|
||||
done();
|
||||
});
|
||||
|
||||
it('returns a WWW-Authenticate header set to the schema array value', function (done) {
|
||||
|
||||
var err = Boom.unauthorized(null, ['Test','one','two']);
|
||||
expect(err.output.statusCode).to.equal(401);
|
||||
expect(err.output.headers['WWW-Authenticate']).to.equal('Test, one, two');
|
||||
done();
|
||||
});
|
||||
|
||||
it('returns a WWW-Authenticate header when passed a scheme and attributes', function (done) {
|
||||
|
||||
var err = Boom.unauthorized('boom', 'Test', { a: 1, b: 'something', c: null, d: 0 });
|
||||
expect(err.output.statusCode).to.equal(401);
|
||||
expect(err.output.headers['WWW-Authenticate']).to.equal('Test a="1", b="something", c="", d="0", error="boom"');
|
||||
expect(err.output.payload.attributes).to.deep.equal({ a: 1, b: 'something', c: '', d: 0, error: 'boom' });
|
||||
done();
|
||||
});
|
||||
|
||||
it('returns a WWW-Authenticate header when passed attributes, missing error', function (done) {
|
||||
|
||||
var err = Boom.unauthorized(null, 'Test', { a: 1, b: 'something', c: null, d: 0 });
|
||||
expect(err.output.statusCode).to.equal(401);
|
||||
expect(err.output.headers['WWW-Authenticate']).to.equal('Test a="1", b="something", c="", d="0"');
|
||||
expect(err.isMissing).to.equal(true);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the isMissing flag when error message is empty', function (done) {
|
||||
|
||||
var err = Boom.unauthorized('', 'Basic');
|
||||
expect(err.isMissing).to.equal(true);
|
||||
done();
|
||||
});
|
||||
|
||||
it('does not set the isMissing flag when error message is not empty', function (done) {
|
||||
|
||||
var err = Boom.unauthorized('message', 'Basic');
|
||||
expect(err.isMissing).to.equal(undefined);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets a WWW-Authenticate when passed as an array', function (done) {
|
||||
|
||||
var err = Boom.unauthorized('message', ['Basic', 'Example e="1"', 'Another x="3", y="4"']);
|
||||
expect(err.output.headers['WWW-Authenticate']).to.equal('Basic, Example e="1", Another x="3", y="4"');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('methodNotAllowed()', function () {
|
||||
|
||||
it('returns a 405 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.methodNotAllowed().output.statusCode).to.equal(405);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.methodNotAllowed('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('notAcceptable()', function () {
|
||||
|
||||
it('returns a 406 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.notAcceptable().output.statusCode).to.equal(406);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.notAcceptable('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('proxyAuthRequired()', function () {
|
||||
|
||||
it('returns a 407 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.proxyAuthRequired().output.statusCode).to.equal(407);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.proxyAuthRequired('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('clientTimeout()', function () {
|
||||
|
||||
it('returns a 408 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.clientTimeout().output.statusCode).to.equal(408);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.clientTimeout('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('conflict()', function () {
|
||||
|
||||
it('returns a 409 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.conflict().output.statusCode).to.equal(409);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.conflict('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('resourceGone()', function () {
|
||||
|
||||
it('returns a 410 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.resourceGone().output.statusCode).to.equal(410);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.resourceGone('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('lengthRequired()', function () {
|
||||
|
||||
it('returns a 411 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.lengthRequired().output.statusCode).to.equal(411);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.lengthRequired('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('preconditionFailed()', function () {
|
||||
|
||||
it('returns a 412 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.preconditionFailed().output.statusCode).to.equal(412);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.preconditionFailed('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('entityTooLarge()', function () {
|
||||
|
||||
it('returns a 413 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.entityTooLarge().output.statusCode).to.equal(413);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.entityTooLarge('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('uriTooLong()', function () {
|
||||
|
||||
it('returns a 414 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.uriTooLong().output.statusCode).to.equal(414);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.uriTooLong('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('unsupportedMediaType()', function () {
|
||||
|
||||
it('returns a 415 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.unsupportedMediaType().output.statusCode).to.equal(415);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.unsupportedMediaType('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('rangeNotSatisfiable()', function () {
|
||||
|
||||
it('returns a 416 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.rangeNotSatisfiable().output.statusCode).to.equal(416);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.rangeNotSatisfiable('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('expectationFailed()', function () {
|
||||
|
||||
it('returns a 417 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.expectationFailed().output.statusCode).to.equal(417);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.expectationFailed('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('badData()', function () {
|
||||
|
||||
it('returns a 422 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.badData().output.statusCode).to.equal(422);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.badData('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('preconditionRequired()', function () {
|
||||
|
||||
it('returns a 428 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.preconditionRequired().output.statusCode).to.equal(428);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.preconditionRequired('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('tooManyRequests()', function () {
|
||||
|
||||
it('returns a 429 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.tooManyRequests().output.statusCode).to.equal(429);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed-in message', function (done) {
|
||||
|
||||
expect(Boom.tooManyRequests('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('serverTimeout()', function () {
|
||||
|
||||
it('returns a 503 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.serverTimeout().output.statusCode).to.equal(503);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.serverTimeout('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('forbidden()', function () {
|
||||
|
||||
it('returns a 403 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.forbidden().output.statusCode).to.equal(403);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.forbidden('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('notFound()', function () {
|
||||
|
||||
it('returns a 404 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.notFound().output.statusCode).to.equal(404);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.notFound('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('internal()', function () {
|
||||
|
||||
it('returns a 500 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.internal().output.statusCode).to.equal(500);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
var err = Boom.internal('my message');
|
||||
expect(err.message).to.equal('my message');
|
||||
expect(err.isServer).to.true();
|
||||
expect(err.output.payload.message).to.equal('An internal server error occurred');
|
||||
done();
|
||||
});
|
||||
|
||||
it('passes data on the callback if its passed in', function (done) {
|
||||
|
||||
expect(Boom.internal('my message', { my: 'data' }).data.my).to.equal('data');
|
||||
done();
|
||||
});
|
||||
|
||||
it('returns an error with composite message', function (done) {
|
||||
|
||||
try {
|
||||
JSON.parse('{');
|
||||
}
|
||||
catch (err) {
|
||||
var boom = Boom.internal('Someting bad', err);
|
||||
expect(boom.message).to.equal('Someting bad: Unexpected end of input');
|
||||
expect(boom.isServer).to.be.true();
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('notImplemented()', function () {
|
||||
|
||||
it('returns a 501 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.notImplemented().output.statusCode).to.equal(501);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.notImplemented('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('badGateway()', function () {
|
||||
|
||||
it('returns a 502 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.badGateway().output.statusCode).to.equal(502);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.badGateway('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('gatewayTimeout()', function () {
|
||||
|
||||
it('returns a 504 error statusCode', function (done) {
|
||||
|
||||
expect(Boom.gatewayTimeout().output.statusCode).to.equal(504);
|
||||
done();
|
||||
});
|
||||
|
||||
it('sets the message with the passed in message', function (done) {
|
||||
|
||||
expect(Boom.gatewayTimeout('my message').message).to.equal('my message');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('badImplementation()', function () {
|
||||
|
||||
it('returns a 500 error statusCode', function (done) {
|
||||
|
||||
var err = Boom.badImplementation();
|
||||
expect(err.output.statusCode).to.equal(500);
|
||||
expect(err.isDeveloperError).to.equal(true);
|
||||
expect(err.isServer).to.be.true();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('stack trace', function () {
|
||||
|
||||
it('should omit lib', function (done) {
|
||||
|
||||
['badRequest', 'unauthorized', 'forbidden', 'notFound', 'methodNotAllowed',
|
||||
'notAcceptable', 'proxyAuthRequired', 'clientTimeout', 'conflict',
|
||||
'resourceGone', 'lengthRequired', 'preconditionFailed', 'entityTooLarge',
|
||||
'uriTooLong', 'unsupportedMediaType', 'rangeNotSatisfiable', 'expectationFailed',
|
||||
'badData', 'preconditionRequired', 'tooManyRequests',
|
||||
|
||||
// 500s
|
||||
'internal', 'notImplemented', 'badGateway', 'serverTimeout', 'gatewayTimeout',
|
||||
'badImplementation'
|
||||
].forEach(function (name) {
|
||||
|
||||
var err = Boom[name]();
|
||||
expect(err.stack).to.not.match(/\/lib\/index\.js/);
|
||||
});
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
@ -0,0 +1,28 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
1. Definitions.
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
END OF TERMS AND CONDITIONS
|
@ -0,0 +1,45 @@
|
||||
## Caseless -- wrap an object to set and get property with caseless semantics but also preserve caseing.
|
||||
|
||||
This library is incredibly useful when working with HTTP headers. It allows you to get/set/check for headers in a caseless manner while also preserving the caseing of headers the first time they are set.
|
||||
|
||||
## Usage
|
||||
|
||||
```javascript
|
||||
var headers = {}
|
||||
, c = caseless(headers)
|
||||
;
|
||||
c.set('a-Header', 'asdf')
|
||||
c.get('a-header') === 'asdf'
|
||||
```
|
||||
|
||||
## has(key)
|
||||
|
||||
Has takes a name and if it finds a matching header will return that header name with the preserved caseing it was set with.
|
||||
|
||||
```javascript
|
||||
c.has('a-header') === 'a-Header'
|
||||
```
|
||||
|
||||
## set(key, value[, clobber=true])
|
||||
|
||||
Set is fairly straight forward except that if the header exists and clobber is disabled it will add `','+value` to the existing header.
|
||||
|
||||
```javascript
|
||||
c.set('a-Header', 'fdas')
|
||||
c.set('a-HEADER', 'more', false)
|
||||
c.get('a-header') === 'fdsa,more'
|
||||
```
|
||||
|
||||
## swap(key)
|
||||
|
||||
Swaps the casing of a header with the new one that is passed in.
|
||||
|
||||
```javascript
|
||||
var headers = {}
|
||||
, c = caseless(headers)
|
||||
;
|
||||
c.set('a-Header', 'fdas')
|
||||
c.swap('a-HEADER')
|
||||
c.has('a-header') === 'a-HEADER'
|
||||
headers === {'a-HEADER': 'fdas'}
|
||||
```
|
@ -0,0 +1,66 @@
|
||||
function Caseless (dict) {
|
||||
this.dict = dict || {}
|
||||
}
|
||||
Caseless.prototype.set = function (name, value, clobber) {
|
||||
if (typeof name === 'object') {
|
||||
for (var i in name) {
|
||||
this.set(i, name[i], value)
|
||||
}
|
||||
} else {
|
||||
if (typeof clobber === 'undefined') clobber = true
|
||||
var has = this.has(name)
|
||||
|
||||
if (!clobber && has) this.dict[has] = this.dict[has] + ',' + value
|
||||
else this.dict[has || name] = value
|
||||
return has
|
||||
}
|
||||
}
|
||||
Caseless.prototype.has = function (name) {
|
||||
var keys = Object.keys(this.dict)
|
||||
, name = name.toLowerCase()
|
||||
;
|
||||
for (var i=0;i<keys.length;i++) {
|
||||
if (keys[i].toLowerCase() === name) return keys[i]
|
||||
}
|
||||
return false
|
||||
}
|
||||
Caseless.prototype.get = function (name) {
|
||||
name = name.toLowerCase()
|
||||
var result, _key
|
||||
var headers = this.dict
|
||||
Object.keys(headers).forEach(function (key) {
|
||||
_key = key.toLowerCase()
|
||||
if (name === _key) result = headers[key]
|
||||
})
|
||||
return result
|
||||
}
|
||||
Caseless.prototype.swap = function (name) {
|
||||
var has = this.has(name)
|
||||
if (!has) throw new Error('There is no header than matches "'+name+'"')
|
||||
this.dict[name] = this.dict[has]
|
||||
delete this.dict[has]
|
||||
}
|
||||
Caseless.prototype.del = function (name) {
|
||||
var has = this.has(name)
|
||||
return delete this.dict[has || name]
|
||||
}
|
||||
|
||||
module.exports = function (dict) {return new Caseless(dict)}
|
||||
module.exports.httpify = function (resp, headers) {
|
||||
var c = new Caseless(headers)
|
||||
resp.setHeader = function (key, value, clobber) {
|
||||
if (typeof value === 'undefined') return
|
||||
return c.set(key, value, clobber)
|
||||
}
|
||||
resp.hasHeader = function (key) {
|
||||
return c.has(key)
|
||||
}
|
||||
resp.getHeader = function (key) {
|
||||
return c.get(key)
|
||||
}
|
||||
resp.removeHeader = function (key) {
|
||||
return c.del(key)
|
||||
}
|
||||
resp.headers = c.dict
|
||||
return c
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "caseless",
|
||||
"version": "0.11.0",
|
||||
"description": "Caseless object set/get/has, very useful when working with HTTP headers.",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "node test.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mikeal/caseless"
|
||||
},
|
||||
"keywords": [
|
||||
"headers",
|
||||
"http",
|
||||
"caseless"
|
||||
],
|
||||
"test": "node test.js",
|
||||
"author": "Mikeal Rogers <mikeal.rogers@gmail.com>",
|
||||
"license": "Apache-2.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/mikeal/caseless/issues"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tape": "^2.10.2"
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
var tape = require('tape')
|
||||
, caseless = require('./')
|
||||
;
|
||||
|
||||
tape('set get has', function (t) {
|
||||
var headers = {}
|
||||
, c = caseless(headers)
|
||||
;
|
||||
t.plan(17)
|
||||
c.set('a-Header', 'asdf')
|
||||
t.equal(c.get('a-header'), 'asdf')
|
||||
t.equal(c.has('a-header'), 'a-Header')
|
||||
t.ok(!c.has('nothing'))
|
||||
// old bug where we used the wrong regex
|
||||
t.ok(!c.has('a-hea'))
|
||||
c.set('a-header', 'fdsa')
|
||||
t.equal(c.get('a-header'), 'fdsa')
|
||||
t.equal(c.get('a-Header'), 'fdsa')
|
||||
c.set('a-HEADER', 'more', false)
|
||||
t.equal(c.get('a-header'), 'fdsa,more')
|
||||
|
||||
t.deepEqual(headers, {'a-Header': 'fdsa,more'})
|
||||
c.swap('a-HEADER')
|
||||
t.deepEqual(headers, {'a-HEADER': 'fdsa,more'})
|
||||
|
||||
c.set('deleteme', 'foobar')
|
||||
t.ok(c.has('deleteme'))
|
||||
t.ok(c.del('deleteme'))
|
||||
t.notOk(c.has('deleteme'))
|
||||
t.notOk(c.has('idonotexist'))
|
||||
t.ok(c.del('idonotexist'))
|
||||
|
||||
c.set('tva', 'test1')
|
||||
c.set('tva-header', 'test2')
|
||||
t.equal(c.has('tva'), 'tva')
|
||||
t.notOk(c.has('header'))
|
||||
|
||||
t.equal(c.get('tva'), 'test1')
|
||||
|
||||
})
|
@ -0,0 +1,116 @@
|
||||
'use strict';
|
||||
var escapeStringRegexp = require('escape-string-regexp');
|
||||
var ansiStyles = require('ansi-styles');
|
||||
var stripAnsi = require('strip-ansi');
|
||||
var hasAnsi = require('has-ansi');
|
||||
var supportsColor = require('supports-color');
|
||||
var defineProps = Object.defineProperties;
|
||||
var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM);
|
||||
|
||||
function Chalk(options) {
|
||||
// detect mode if not set manually
|
||||
this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
|
||||
}
|
||||
|
||||
// use bright blue on Windows as the normal blue color is illegible
|
||||
if (isSimpleWindowsTerm) {
|
||||
ansiStyles.blue.open = '\u001b[94m';
|
||||
}
|
||||
|
||||
var styles = (function () {
|
||||
var ret = {};
|
||||
|
||||
Object.keys(ansiStyles).forEach(function (key) {
|
||||
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
||||
|
||||
ret[key] = {
|
||||
get: function () {
|
||||
return build.call(this, this._styles.concat(key));
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return ret;
|
||||
})();
|
||||
|
||||
var proto = defineProps(function chalk() {}, styles);
|
||||
|
||||
function build(_styles) {
|
||||
var builder = function () {
|
||||
return applyStyle.apply(builder, arguments);
|
||||
};
|
||||
|
||||
builder._styles = _styles;
|
||||
builder.enabled = this.enabled;
|
||||
// __proto__ is used because we must return a function, but there is
|
||||
// no way to create a function with a different prototype.
|
||||
/* eslint-disable no-proto */
|
||||
builder.__proto__ = proto;
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
function applyStyle() {
|
||||
// support varags, but simply cast to string in case there's only one arg
|
||||
var args = arguments;
|
||||
var argsLen = args.length;
|
||||
var str = argsLen !== 0 && String(arguments[0]);
|
||||
|
||||
if (argsLen > 1) {
|
||||
// don't slice `arguments`, it prevents v8 optimizations
|
||||
for (var a = 1; a < argsLen; a++) {
|
||||
str += ' ' + args[a];
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.enabled || !str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
var nestedStyles = this._styles;
|
||||
var i = nestedStyles.length;
|
||||
|
||||
// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
|
||||
// see https://github.com/chalk/chalk/issues/58
|
||||
// If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
|
||||
var originalDim = ansiStyles.dim.open;
|
||||
if (isSimpleWindowsTerm && (nestedStyles.indexOf('gray') !== -1 || nestedStyles.indexOf('grey') !== -1)) {
|
||||
ansiStyles.dim.open = '';
|
||||
}
|
||||
|
||||
while (i--) {
|
||||
var code = ansiStyles[nestedStyles[i]];
|
||||
|
||||
// Replace any instances already present with a re-opening code
|
||||
// otherwise only the part of the string until said closing code
|
||||
// will be colored, and the rest will simply be 'plain'.
|
||||
str = code.open + str.replace(code.closeRe, code.open) + code.close;
|
||||
}
|
||||
|
||||
// Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue.
|
||||
ansiStyles.dim.open = originalDim;
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
function init() {
|
||||
var ret = {};
|
||||
|
||||
Object.keys(styles).forEach(function (name) {
|
||||
ret[name] = {
|
||||
get: function () {
|
||||
return build.call(this, [name]);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
defineProps(Chalk.prototype, init());
|
||||
|
||||
module.exports = new Chalk();
|
||||
module.exports.styles = ansiStyles;
|
||||
module.exports.hasColor = hasAnsi;
|
||||
module.exports.stripColor = stripAnsi;
|
||||
module.exports.supportsColor = supportsColor;
|
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
Some files were not shown because too many files have changed in this diff Show More
Reference in new issue