Template Upload

This commit is contained in:
SOUTHERNCO\x2mjbyrn
2017-05-17 13:45:25 -04:00
parent 415b9c25f3
commit 7efe7605b8
11476 changed files with 2170865 additions and 34 deletions

6
node_modules/http-signature/.dir-locals.el generated vendored Normal file
View File

@ -0,0 +1,6 @@
((nil . ((indent-tabs-mode . nil)
(tab-width . 8)
(fill-column . 80)))
(js-mode . ((js-indent-level . 2)
(indent-tabs-mode . nil)
)))

7
node_modules/http-signature/.npmignore generated vendored Normal file
View File

@ -0,0 +1,7 @@
.gitmodules
deps
docs
Makefile
node_modules
test
tools

46
node_modules/http-signature/CHANGES.md generated vendored Normal file
View File

@ -0,0 +1,46 @@
# node-http-signature changelog
## 1.1.1
- Version of dependency `assert-plus` updated: old version was missing
some license information
- Corrected examples in `http_signing.md`, added auto-tests to
automatically validate these examples
## 1.1.0
- Bump version of `sshpk` dependency, remove peerDependency on it since
it now supports exchanging objects between multiple versions of itself
where possible
## 1.0.2
- Bump min version of `jsprim` dependency, to include fixes for using
http-signature with `browserify`
## 1.0.1
- Bump minimum version of `sshpk` dependency, to include fixes for
whitespace tolerance in key parsing.
## 1.0.0
- First semver release.
- #36: Ensure verifySignature does not leak useful timing information
- #42: Bring the library up to the latest version of the spec (including the
request-target changes)
- Support for ECDSA keys and signatures.
- Now uses `sshpk` for key parsing, validation and conversion.
- Fixes for #21, #47, #39 and compatibility with node 0.8
## 0.11.0
- Split up HMAC and Signature verification to avoid vulnerabilities where a
key intended for use with one can be validated against the other method
instead.
## 0.10.2
- Updated versions of most dependencies.
- Utility functions exported for PEM => SSH-RSA conversion.
- Improvements to tests and examples.

18
node_modules/http-signature/LICENSE generated vendored Normal file
View File

@ -0,0 +1,18 @@
Copyright Joyent, Inc. 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.

79
node_modules/http-signature/README.md generated vendored Normal file
View File

@ -0,0 +1,79 @@
# node-http-signature
node-http-signature is a node.js library that has client and server components
for Joyent's [HTTP Signature Scheme](http_signing.md).
## Usage
Note the example below signs a request with the same key/cert used to start an
HTTP server. This is almost certainly not what you actually want, but is just
used to illustrate the API calls; you will need to provide your own key
management in addition to this library.
### Client
```js
var fs = require('fs');
var https = require('https');
var httpSignature = require('http-signature');
var key = fs.readFileSync('./key.pem', 'ascii');
var options = {
host: 'localhost',
port: 8443,
path: '/',
method: 'GET',
headers: {}
};
// Adds a 'Date' header in, signs it, and adds the
// 'Authorization' header in.
var req = https.request(options, function(res) {
console.log(res.statusCode);
});
httpSignature.sign(req, {
key: key,
keyId: './cert.pem'
});
req.end();
```
### Server
```js
var fs = require('fs');
var https = require('https');
var httpSignature = require('http-signature');
var options = {
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./cert.pem')
};
https.createServer(options, function (req, res) {
var rc = 200;
var parsed = httpSignature.parseRequest(req);
var pub = fs.readFileSync(parsed.keyId, 'ascii');
if (!httpSignature.verifySignature(parsed, pub))
rc = 401;
res.writeHead(rc);
res.end();
}).listen(8443);
```
## Installation
npm install http-signature
## License
MIT.
## Bugs
See <https://github.com/joyent/node-http-signature/issues>.

363
node_modules/http-signature/http_signing.md generated vendored Normal file
View File

@ -0,0 +1,363 @@
# Abstract
This document describes a way to add origin authentication, message integrity,
and replay resistance to HTTP REST requests. It is intended to be used over
the HTTPS protocol.
# Copyright Notice
Copyright (c) 2011 Joyent, Inc. and the persons identified as document authors.
All rights reserved.
Code Components extracted from this document must include MIT License text.
# Introduction
This protocol is intended to provide a standard way for clients to sign HTTP
requests. RFC2617 (HTTP Authentication) defines Basic and Digest authentication
mechanisms, and RFC5246 (TLS 1.2) defines client-auth, both of which are widely
employed on the Internet today. However, it is common place that the burdens of
PKI prevent web service operators from deploying that methodology, and so many
fall back to Basic authentication, which has poor security characteristics.
Additionally, OAuth provides a fully-specified alternative for authorization
of web service requests, but is not (always) ideal for machine to machine
communication, as the key acquisition steps (generally) imply a fixed
infrastructure that may not make sense to a service provider (e.g., symmetric
keys).
Several web service providers have invented their own schemes for signing
HTTP requests, but to date, none have been placed in the public domain as a
standard. This document serves that purpose. There are no techniques in this
proposal that are novel beyond previous art, however, this aims to be a simple
mechanism for signing these requests.
# Signature Authentication Scheme
The "signature" authentication scheme is based on the model that the client must
authenticate itself with a digital signature produced by either a private
asymmetric key (e.g., RSA) or a shared symmetric key (e.g., HMAC). The scheme
is parameterized enough such that it is not bound to any particular key type or
signing algorithm. However, it does explicitly assume that clients can send an
HTTP `Date` header.
## Authorization Header
The client is expected to send an Authorization header (as defined in RFC 2617)
with the following parameterization:
credentials := "Signature" params
params := 1#(keyId | algorithm | [headers] | [ext] | signature)
digitalSignature := plain-string
keyId := "keyId" "=" <"> plain-string <">
algorithm := "algorithm" "=" <"> plain-string <">
headers := "headers" "=" <"> 1#headers-value <">
ext := "ext" "=" <"> plain-string <">
signature := "signature" "=" <"> plain-string <">
headers-value := plain-string
plain-string = 1*( %x20-21 / %x23-5B / %x5D-7E )
### Signature Parameters
#### keyId
REQUIRED. The `keyId` field is an opaque string that the server can use to look
up the component they need to validate the signature. It could be an SSH key
fingerprint, an LDAP DN, etc. Management of keys and assignment of `keyId` is
out of scope for this document.
#### algorithm
REQUIRED. The `algorithm` parameter is used if the client and server agree on a
non-standard digital signature algorithm. The full list of supported signature
mechanisms is listed below.
#### headers
OPTIONAL. The `headers` parameter is used to specify the list of HTTP headers
used to sign the request. If specified, it should be a quoted list of HTTP
header names, separated by a single space character. By default, only one
HTTP header is signed, which is the `Date` header. Note that the list MUST be
specified in the order the values are concatenated together during signing. To
include the HTTP request line in the signature calculation, use the special
`request-line` value. While this is overloading the definition of `headers` in
HTTP linguism, the request-line is defined in RFC 2616, and as the outlier from
headers in useful signature calculation, it is deemed simpler to simply use
`request-line` than to add a separate parameter for it.
#### extensions
OPTIONAL. The `extensions` parameter is used to include additional information
which is covered by the request. The content and format of the string is out of
scope for this document, and expected to be specified by implementors.
#### signature
REQUIRED. The `signature` parameter is a `Base64` encoded digital signature
generated by the client. The client uses the `algorithm` and `headers` request
parameters to form a canonicalized `signing string`. This `signing string` is
then signed with the key associated with `keyId` and the algorithm
corresponding to `algorithm`. The `signature` parameter is then set to the
`Base64` encoding of the signature.
### Signing String Composition
In order to generate the string that is signed with a key, the client MUST take
the values of each HTTP header specified by `headers` in the order they appear.
1. If the header name is not `request-line` then append the lowercased header
name followed with an ASCII colon `:` and an ASCII space ` `.
2. If the header name is `request-line` then append the HTTP request line,
otherwise append the header value.
3. If value is not the last value then append an ASCII newline `\n`. The string
MUST NOT include a trailing ASCII newline.
# Example Requests
All requests refer to the following request (body omitted):
POST /foo HTTP/1.1
Host: example.org
Date: Tue, 07 Jun 2014 20:51:35 GMT
Content-Type: application/json
Digest: SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=
Content-Length: 18
The "rsa-key-1" keyId refers to a private key known to the client and a public
key known to the server. The "hmac-key-1" keyId refers to key known to the
client and server.
## Default parameterization
The authorization header and signature would be generated as:
Authorization: Signature keyId="rsa-key-1",algorithm="rsa-sha256",signature="Base64(RSA-SHA256(signing string))"
The client would compose the signing string as:
date: Tue, 07 Jun 2014 20:51:35 GMT
## Header List
The authorization header and signature would be generated as:
Authorization: Signature keyId="rsa-key-1",algorithm="rsa-sha256",headers="(request-target) date content-type digest",signature="Base64(RSA-SHA256(signing string))"
The client would compose the signing string as (`+ "\n"` inserted for
readability):
(request-target) post /foo + "\n"
date: Tue, 07 Jun 2011 20:51:35 GMT + "\n"
content-type: application/json + "\n"
digest: SHA-256=Base64(SHA256(Body))
## Algorithm
The authorization header and signature would be generated as:
Authorization: Signature keyId="hmac-key-1",algorithm="hmac-sha1",signature="Base64(HMAC-SHA1(signing string))"
The client would compose the signing string as:
date: Tue, 07 Jun 2011 20:51:35 GMT
# Signing Algorithms
Currently supported algorithm names are:
* rsa-sha1
* rsa-sha256
* rsa-sha512
* dsa-sha1
* hmac-sha1
* hmac-sha256
* hmac-sha512
# Security Considerations
## Default Parameters
Note the default parameterization of the `Signature` scheme is only safe if all
requests are carried over a secure transport (i.e., TLS). Sending the default
scheme over a non-secure transport will leave the request vulnerable to
spoofing, tampering, replay/repudiation, and integrity violations (if using the
STRIDE threat-modeling methodology).
## Insecure Transports
If sending the request over plain HTTP, service providers SHOULD require clients
to sign ALL HTTP headers, and the `request-line`. Additionally, service
providers SHOULD require `Content-MD5` calculations to be performed to ensure
against any tampering from clients.
## Nonces
Nonces are out of scope for this document simply because many service providers
fail to implement them correctly, or do not adopt security specifications
because of the infrastructure complexity. Given the `header` parameterization,
a service provider is fully enabled to add nonce semantics into this scheme by
using something like an `x-request-nonce` header, and ensuring it is signed
with the `Date` header.
## Clock Skew
As the default scheme is to sign the `Date` header, service providers SHOULD
protect against logged replay attacks by enforcing a clock skew. The server
SHOULD be synchronized with NTP, and the recommendation in this specification
is to allow 300s of clock skew (in either direction).
## Required Headers to Sign
It is out of scope for this document to dictate what headers a service provider
will want to enforce, but service providers SHOULD at minimum include the
`Date` header.
# References
## Normative References
* [RFC2616] Hypertext Transfer Protocol -- HTTP/1.1
* [RFC2617] HTTP Authentication: Basic and Digest Access Authentication
* [RFC5246] The Transport Layer Security (TLS) Protocol Version 1.2
## Informative References
Name: Mark Cavage (editor)
Company: Joyent, Inc.
Email: mark.cavage@joyent.com
URI: http://www.joyent.com
# Appendix A - Test Values
The following test data uses the RSA (1024b) keys, which we will refer
to as `keyId=Test` in the following samples:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCFENGw33yGihy92pDjZQhl0C3
6rPJj+CvfSC8+q28hxA161QFNUd13wuCTUcq0Qd2qsBe/2hFyc2DCJJg0h1L78+6
Z4UMR7EOcpfdUE9Hf3m/hs+FUR45uBJeDK1HSFHD8bHKD6kv8FPGfJTotc+2xjJw
oYi+1hqp1fIekaxsyQIDAQAB
-----END PUBLIC KEY-----
-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDCFENGw33yGihy92pDjZQhl0C36rPJj+CvfSC8+q28hxA161QF
NUd13wuCTUcq0Qd2qsBe/2hFyc2DCJJg0h1L78+6Z4UMR7EOcpfdUE9Hf3m/hs+F
UR45uBJeDK1HSFHD8bHKD6kv8FPGfJTotc+2xjJwoYi+1hqp1fIekaxsyQIDAQAB
AoGBAJR8ZkCUvx5kzv+utdl7T5MnordT1TvoXXJGXK7ZZ+UuvMNUCdN2QPc4sBiA
QWvLw1cSKt5DsKZ8UETpYPy8pPYnnDEz2dDYiaew9+xEpubyeW2oH4Zx71wqBtOK
kqwrXa/pzdpiucRRjk6vE6YY7EBBs/g7uanVpGibOVAEsqH1AkEA7DkjVH28WDUg
f1nqvfn2Kj6CT7nIcE3jGJsZZ7zlZmBmHFDONMLUrXR/Zm3pR5m0tCmBqa5RK95u
412jt1dPIwJBANJT3v8pnkth48bQo/fKel6uEYyboRtA5/uHuHkZ6FQF7OUkGogc
mSJluOdc5t6hI1VsLn0QZEjQZMEOWr+wKSMCQQCC4kXJEsHAve77oP6HtG/IiEn7
kpyUXRNvFsDE0czpJJBvL/aRFUJxuRK91jhjC68sA7NsKMGg5OXb5I5Jj36xAkEA
gIT7aFOYBFwGgQAQkWNKLvySgKbAZRTeLBacpHMuQdl1DfdntvAyqpAZ0lY0RKmW
G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI
7U1yQXnTAEFYM560yJlzUpOb1V4cScGd365tiSMvxLOvTA==
-----END RSA PRIVATE KEY-----
And all examples use this request:
<!-- httpreq -->
POST /foo?param=value&pet=dog HTTP/1.1
Host: example.com
Date: Thu, 05 Jan 2014 21:31:40 GMT
Content-Type: application/json
Digest: SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=
Content-Length: 18
{"hello": "world"}
<!-- /httpreq -->
### Default
The string to sign would be:
<!-- sign {"name": "Default", "options": {"keyId":"Test", "algorithm": "rsa-sha256"}} -->
<!-- signstring -->
date: Thu, 05 Jan 2014 21:31:40 GMT
<!-- /signstring -->
The Authorization header would be:
<!-- authz -->
Authorization: Signature keyId="Test",algorithm="rsa-sha256",headers="date",signature="jKyvPcxB4JbmYY4mByyBY7cZfNl4OW9HpFQlG7N4YcJPteKTu4MWCLyk+gIr0wDgqtLWf9NLpMAMimdfsH7FSWGfbMFSrsVTHNTk0rK3usrfFnti1dxsM4jl0kYJCKTGI/UWkqiaxwNiKqGcdlEDrTcUhhsFsOIo8VhddmZTZ8w="
<!-- /authz -->
### All Headers
Parameterized to include all headers, the string to sign would be (`+ "\n"`
inserted for readability):
<!-- sign {"name": "All Headers", "options": {"keyId":"Test", "algorithm": "rsa-sha256", "headers": ["(request-target)", "host", "date", "content-type", "digest", "content-length"]}} -->
<!-- signstring -->
(request-target): post /foo?param=value&pet=dog
host: example.com
date: Thu, 05 Jan 2014 21:31:40 GMT
content-type: application/json
digest: SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=
content-length: 18
<!-- /signstring -->
The Authorization header would be:
<!-- authz -->
Authorization: Signature keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date content-type digest content-length",signature="Ef7MlxLXoBovhil3AlyjtBwAL9g4TN3tibLj7uuNB3CROat/9KaeQ4hW2NiJ+pZ6HQEOx9vYZAyi+7cmIkmJszJCut5kQLAwuX+Ms/mUFvpKlSo9StS2bMXDBNjOh4Auj774GFj4gwjS+3NhFeoqyr/MuN6HsEnkvn6zdgfE2i0="
<!-- /authz -->
## Generating and verifying signatures using `openssl`
The `openssl` commandline tool can be used to generate or verify the signatures listed above.
Compose the signing string as usual, and pipe it into the the `openssl dgst` command, then into `openssl enc -base64`, as follows:
$ printf 'date: Thu, 05 Jan 2014 21:31:40 GMT' | \
openssl dgst -binary -sign /path/to/private.pem -sha256 | \
openssl enc -base64
jKyvPcxB4JbmYY4mByyBY7cZfNl4OW9Hp...
$
The `-sha256` option is necessary to produce an `rsa-sha256` signature. You can select other hash algorithms such as `sha1` by changing this argument.
To verify a signature, first save the signature data, Base64-decoded, into a file, then use `openssl dgst` again with the `-verify` option:
$ echo 'jKyvPcxB4JbmYY4mByy...' | openssl enc -A -d -base64 > signature
$ printf 'date: Thu, 05 Jan 2014 21:31:40 GMT' | \
openssl dgst -sha256 -verify /path/to/public.pem -signature ./signature
Verified OK
$
## Generating and verifying signatures using `sshpk-sign`
You can also generate and check signatures using the `sshpk-sign` tool which is
included with the `sshpk` package in `npm`.
Compose the signing string as above, and pipe it into `sshpk-sign` as follows:
$ printf 'date: Thu, 05 Jan 2014 21:31:40 GMT' | \
sshpk-sign -i /path/to/private.pem
jKyvPcxB4JbmYY4mByyBY7cZfNl4OW9Hp...
$
This will produce an `rsa-sha256` signature by default, as you can see using
the `-v` option:
sshpk-sign: using rsa-sha256 with a 1024 bit key
You can also use `sshpk-verify` in a similar manner:
$ printf 'date: Thu, 05 Jan 2014 21:31:40 GMT' | \
sshpk-verify -i ./public.pem -s 'jKyvPcxB4JbmYY...'
OK
$

29
node_modules/http-signature/lib/index.js generated vendored Normal file
View File

@ -0,0 +1,29 @@
// Copyright 2015 Joyent, Inc.
var parser = require('./parser');
var signer = require('./signer');
var verify = require('./verify');
var utils = require('./utils');
///--- API
module.exports = {
parse: parser.parseRequest,
parseRequest: parser.parseRequest,
sign: signer.signRequest,
signRequest: signer.signRequest,
createSigner: signer.createSigner,
isSigner: signer.isSigner,
sshKeyToPEM: utils.sshKeyToPEM,
sshKeyFingerprint: utils.fingerprint,
pemToRsaSSHKey: utils.pemToRsaSSHKey,
verify: verify.verifySignature,
verifySignature: verify.verifySignature,
verifyHMAC: verify.verifyHMAC
};

318
node_modules/http-signature/lib/parser.js generated vendored Normal file
View File

@ -0,0 +1,318 @@
// Copyright 2012 Joyent, Inc. All rights reserved.
var assert = require('assert-plus');
var util = require('util');
var utils = require('./utils');
///--- Globals
var HASH_ALGOS = utils.HASH_ALGOS;
var PK_ALGOS = utils.PK_ALGOS;
var HttpSignatureError = utils.HttpSignatureError;
var InvalidAlgorithmError = utils.InvalidAlgorithmError;
var validateAlgorithm = utils.validateAlgorithm;
var State = {
New: 0,
Params: 1
};
var ParamsState = {
Name: 0,
Quote: 1,
Value: 2,
Comma: 3
};
///--- Specific Errors
function ExpiredRequestError(message) {
HttpSignatureError.call(this, message, ExpiredRequestError);
}
util.inherits(ExpiredRequestError, HttpSignatureError);
function InvalidHeaderError(message) {
HttpSignatureError.call(this, message, InvalidHeaderError);
}
util.inherits(InvalidHeaderError, HttpSignatureError);
function InvalidParamsError(message) {
HttpSignatureError.call(this, message, InvalidParamsError);
}
util.inherits(InvalidParamsError, HttpSignatureError);
function MissingHeaderError(message) {
HttpSignatureError.call(this, message, MissingHeaderError);
}
util.inherits(MissingHeaderError, HttpSignatureError);
function StrictParsingError(message) {
HttpSignatureError.call(this, message, StrictParsingError);
}
util.inherits(StrictParsingError, HttpSignatureError);
///--- Exported API
module.exports = {
/**
* Parses the 'Authorization' header out of an http.ServerRequest object.
*
* Note that this API will fully validate the Authorization header, and throw
* on any error. It will not however check the signature, or the keyId format
* as those are specific to your environment. You can use the options object
* to pass in extra constraints.
*
* As a response object you can expect this:
*
* {
* "scheme": "Signature",
* "params": {
* "keyId": "foo",
* "algorithm": "rsa-sha256",
* "headers": [
* "date" or "x-date",
* "digest"
* ],
* "signature": "base64"
* },
* "signingString": "ready to be passed to crypto.verify()"
* }
*
* @param {Object} request an http.ServerRequest.
* @param {Object} options an optional options object with:
* - clockSkew: allowed clock skew in seconds (default 300).
* - headers: required header names (def: date or x-date)
* - algorithms: algorithms to support (default: all).
* - strict: should enforce latest spec parsing
* (default: false).
* @return {Object} parsed out object (see above).
* @throws {TypeError} on invalid input.
* @throws {InvalidHeaderError} on an invalid Authorization header error.
* @throws {InvalidParamsError} if the params in the scheme are invalid.
* @throws {MissingHeaderError} if the params indicate a header not present,
* either in the request headers from the params,
* or not in the params from a required header
* in options.
* @throws {StrictParsingError} if old attributes are used in strict parsing
* mode.
* @throws {ExpiredRequestError} if the value of date or x-date exceeds skew.
*/
parseRequest: function parseRequest(request, options) {
assert.object(request, 'request');
assert.object(request.headers, 'request.headers');
if (options === undefined) {
options = {};
}
if (options.headers === undefined) {
options.headers = [request.headers['x-date'] ? 'x-date' : 'date'];
}
assert.object(options, 'options');
assert.arrayOfString(options.headers, 'options.headers');
assert.optionalNumber(options.clockSkew, 'options.clockSkew');
if (!request.headers.authorization)
throw new MissingHeaderError('no authorization header present in ' +
'the request');
options.clockSkew = options.clockSkew || 300;
var i = 0;
var state = State.New;
var substate = ParamsState.Name;
var tmpName = '';
var tmpValue = '';
var parsed = {
scheme: '',
params: {},
signingString: '',
get algorithm() {
return this.params.algorithm.toUpperCase();
},
get keyId() {
return this.params.keyId;
}
};
var authz = request.headers.authorization;
for (i = 0; i < authz.length; i++) {
var c = authz.charAt(i);
switch (Number(state)) {
case State.New:
if (c !== ' ') parsed.scheme += c;
else state = State.Params;
break;
case State.Params:
switch (Number(substate)) {
case ParamsState.Name:
var code = c.charCodeAt(0);
// restricted name of A-Z / a-z
if ((code >= 0x41 && code <= 0x5a) || // A-Z
(code >= 0x61 && code <= 0x7a)) { // a-z
tmpName += c;
} else if (c === '=') {
if (tmpName.length === 0)
throw new InvalidHeaderError('bad param format');
substate = ParamsState.Quote;
} else {
throw new InvalidHeaderError('bad param format');
}
break;
case ParamsState.Quote:
if (c === '"') {
tmpValue = '';
substate = ParamsState.Value;
} else {
throw new InvalidHeaderError('bad param format');
}
break;
case ParamsState.Value:
if (c === '"') {
parsed.params[tmpName] = tmpValue;
substate = ParamsState.Comma;
} else {
tmpValue += c;
}
break;
case ParamsState.Comma:
if (c === ',') {
tmpName = '';
substate = ParamsState.Name;
} else {
throw new InvalidHeaderError('bad param format');
}
break;
default:
throw new Error('Invalid substate');
}
break;
default:
throw new Error('Invalid substate');
}
}
if (!parsed.params.headers || parsed.params.headers === '') {
if (request.headers['x-date']) {
parsed.params.headers = ['x-date'];
} else {
parsed.params.headers = ['date'];
}
} else {
parsed.params.headers = parsed.params.headers.split(' ');
}
// Minimally validate the parsed object
if (!parsed.scheme || parsed.scheme !== 'Signature')
throw new InvalidHeaderError('scheme was not "Signature"');
if (!parsed.params.keyId)
throw new InvalidHeaderError('keyId was not specified');
if (!parsed.params.algorithm)
throw new InvalidHeaderError('algorithm was not specified');
if (!parsed.params.signature)
throw new InvalidHeaderError('signature was not specified');
// Check the algorithm against the official list
parsed.params.algorithm = parsed.params.algorithm.toLowerCase();
try {
validateAlgorithm(parsed.params.algorithm);
} catch (e) {
if (e instanceof InvalidAlgorithmError)
throw (new InvalidParamsError(parsed.params.algorithm + ' is not ' +
'supported'));
else
throw (e);
}
// Build the signingString
for (i = 0; i < parsed.params.headers.length; i++) {
var h = parsed.params.headers[i].toLowerCase();
parsed.params.headers[i] = h;
if (h === 'request-line') {
if (!options.strict) {
/*
* We allow headers from the older spec drafts if strict parsing isn't
* specified in options.
*/
parsed.signingString +=
request.method + ' ' + request.url + ' HTTP/' + request.httpVersion;
} else {
/* Strict parsing doesn't allow older draft headers. */
throw (new StrictParsingError('request-line is not a valid header ' +
'with strict parsing enabled.'));
}
} else if (h === '(request-target)') {
parsed.signingString +=
'(request-target): ' + request.method.toLowerCase() + ' ' +
request.url;
} else {
var value = request.headers[h];
if (value === undefined)
throw new MissingHeaderError(h + ' was not in the request');
parsed.signingString += h + ': ' + value;
}
if ((i + 1) < parsed.params.headers.length)
parsed.signingString += '\n';
}
// Check against the constraints
var date;
if (request.headers.date || request.headers['x-date']) {
if (request.headers['x-date']) {
date = new Date(request.headers['x-date']);
} else {
date = new Date(request.headers.date);
}
var now = new Date();
var skew = Math.abs(now.getTime() - date.getTime());
if (skew > options.clockSkew * 1000) {
throw new ExpiredRequestError('clock skew of ' +
(skew / 1000) +
's was greater than ' +
options.clockSkew + 's');
}
}
options.headers.forEach(function (hdr) {
// Remember that we already checked any headers in the params
// were in the request, so if this passes we're good.
if (parsed.params.headers.indexOf(hdr) < 0)
throw new MissingHeaderError(hdr + ' was not a signed header');
});
if (options.algorithms) {
if (options.algorithms.indexOf(parsed.params.algorithm) === -1)
throw new InvalidParamsError(parsed.params.algorithm +
' is not a supported algorithm');
}
return parsed;
}
};

399
node_modules/http-signature/lib/signer.js generated vendored Normal file
View File

@ -0,0 +1,399 @@
// Copyright 2012 Joyent, Inc. All rights reserved.
var assert = require('assert-plus');
var crypto = require('crypto');
var http = require('http');
var util = require('util');
var sshpk = require('sshpk');
var jsprim = require('jsprim');
var utils = require('./utils');
var sprintf = require('util').format;
var HASH_ALGOS = utils.HASH_ALGOS;
var PK_ALGOS = utils.PK_ALGOS;
var InvalidAlgorithmError = utils.InvalidAlgorithmError;
var HttpSignatureError = utils.HttpSignatureError;
var validateAlgorithm = utils.validateAlgorithm;
///--- Globals
var AUTHZ_FMT =
'Signature keyId="%s",algorithm="%s",headers="%s",signature="%s"';
///--- Specific Errors
function MissingHeaderError(message) {
HttpSignatureError.call(this, message, MissingHeaderError);
}
util.inherits(MissingHeaderError, HttpSignatureError);
function StrictParsingError(message) {
HttpSignatureError.call(this, message, StrictParsingError);
}
util.inherits(StrictParsingError, HttpSignatureError);
/* See createSigner() */
function RequestSigner(options) {
assert.object(options, 'options');
var alg = [];
if (options.algorithm !== undefined) {
assert.string(options.algorithm, 'options.algorithm');
alg = validateAlgorithm(options.algorithm);
}
this.rs_alg = alg;
/*
* RequestSigners come in two varieties: ones with an rs_signFunc, and ones
* with an rs_signer.
*
* rs_signFunc-based RequestSigners have to build up their entire signing
* string within the rs_lines array and give it to rs_signFunc as a single
* concat'd blob. rs_signer-based RequestSigners can add a line at a time to
* their signing state by using rs_signer.update(), thus only needing to
* buffer the hash function state and one line at a time.
*/
if (options.sign !== undefined) {
assert.func(options.sign, 'options.sign');
this.rs_signFunc = options.sign;
} else if (alg[0] === 'hmac' && options.key !== undefined) {
assert.string(options.keyId, 'options.keyId');
this.rs_keyId = options.keyId;
if (typeof (options.key) !== 'string' && !Buffer.isBuffer(options.key))
throw (new TypeError('options.key for HMAC must be a string or Buffer'));
/*
* Make an rs_signer for HMACs, not a rs_signFunc -- HMACs digest their
* data in chunks rather than requiring it all to be given in one go
* at the end, so they are more similar to signers than signFuncs.
*/
this.rs_signer = crypto.createHmac(alg[1].toUpperCase(), options.key);
this.rs_signer.sign = function () {
var digest = this.digest('base64');
return ({
hashAlgorithm: alg[1],
toString: function () { return (digest); }
});
};
} else if (options.key !== undefined) {
var key = options.key;
if (typeof (key) === 'string' || Buffer.isBuffer(key))
key = sshpk.parsePrivateKey(key);
assert.ok(sshpk.PrivateKey.isPrivateKey(key, [1, 2]),
'options.key must be a sshpk.PrivateKey');
this.rs_key = key;
assert.string(options.keyId, 'options.keyId');
this.rs_keyId = options.keyId;
if (!PK_ALGOS[key.type]) {
throw (new InvalidAlgorithmError(key.type.toUpperCase() + ' type ' +
'keys are not supported'));
}
if (alg[0] !== undefined && key.type !== alg[0]) {
throw (new InvalidAlgorithmError('options.key must be a ' +
alg[0].toUpperCase() + ' key, was given a ' +
key.type.toUpperCase() + ' key instead'));
}
this.rs_signer = key.createSign(alg[1]);
} else {
throw (new TypeError('options.sign (func) or options.key is required'));
}
this.rs_headers = [];
this.rs_lines = [];
}
/**
* Adds a header to be signed, with its value, into this signer.
*
* @param {String} header
* @param {String} value
* @return {String} value written
*/
RequestSigner.prototype.writeHeader = function (header, value) {
assert.string(header, 'header');
header = header.toLowerCase();
assert.string(value, 'value');
this.rs_headers.push(header);
if (this.rs_signFunc) {
this.rs_lines.push(header + ': ' + value);
} else {
var line = header + ': ' + value;
if (this.rs_headers.length > 0)
line = '\n' + line;
this.rs_signer.update(line);
}
return (value);
};
/**
* Adds a default Date header, returning its value.
*
* @return {String}
*/
RequestSigner.prototype.writeDateHeader = function () {
return (this.writeHeader('date', jsprim.rfc1123(new Date())));
};
/**
* Adds the request target line to be signed.
*
* @param {String} method, HTTP method (e.g. 'get', 'post', 'put')
* @param {String} path
*/
RequestSigner.prototype.writeTarget = function (method, path) {
assert.string(method, 'method');
assert.string(path, 'path');
method = method.toLowerCase();
this.writeHeader('(request-target)', method + ' ' + path);
};
/**
* Calculate the value for the Authorization header on this request
* asynchronously.
*
* @param {Func} callback (err, authz)
*/
RequestSigner.prototype.sign = function (cb) {
assert.func(cb, 'callback');
if (this.rs_headers.length < 1)
throw (new Error('At least one header must be signed'));
var alg, authz;
if (this.rs_signFunc) {
var data = this.rs_lines.join('\n');
var self = this;
this.rs_signFunc(data, function (err, sig) {
if (err) {
cb(err);
return;
}
try {
assert.object(sig, 'signature');
assert.string(sig.keyId, 'signature.keyId');
assert.string(sig.algorithm, 'signature.algorithm');
assert.string(sig.signature, 'signature.signature');
alg = validateAlgorithm(sig.algorithm);
authz = sprintf(AUTHZ_FMT,
sig.keyId,
sig.algorithm,
self.rs_headers.join(' '),
sig.signature);
} catch (e) {
cb(e);
return;
}
cb(null, authz);
});
} else {
try {
var sigObj = this.rs_signer.sign();
} catch (e) {
cb(e);
return;
}
alg = (this.rs_alg[0] || this.rs_key.type) + '-' + sigObj.hashAlgorithm;
var signature = sigObj.toString();
authz = sprintf(AUTHZ_FMT,
this.rs_keyId,
alg,
this.rs_headers.join(' '),
signature);
cb(null, authz);
}
};
///--- Exported API
module.exports = {
/**
* Identifies whether a given object is a request signer or not.
*
* @param {Object} object, the object to identify
* @returns {Boolean}
*/
isSigner: function (obj) {
if (typeof (obj) === 'object' && obj instanceof RequestSigner)
return (true);
return (false);
},
/**
* Creates a request signer, used to asynchronously build a signature
* for a request (does not have to be an http.ClientRequest).
*
* @param {Object} options, either:
* - {String} keyId
* - {String|Buffer} key
* - {String} algorithm (optional, required for HMAC)
* or:
* - {Func} sign (data, cb)
* @return {RequestSigner}
*/
createSigner: function createSigner(options) {
return (new RequestSigner(options));
},
/**
* Adds an 'Authorization' header to an http.ClientRequest object.
*
* Note that this API will add a Date header if it's not already set. Any
* other headers in the options.headers array MUST be present, or this
* will throw.
*
* You shouldn't need to check the return type; it's just there if you want
* to be pedantic.
*
* The optional flag indicates whether parsing should use strict enforcement
* of the version draft-cavage-http-signatures-04 of the spec or beyond.
* The default is to be loose and support
* older versions for compatibility.
*
* @param {Object} request an instance of http.ClientRequest.
* @param {Object} options signing parameters object:
* - {String} keyId required.
* - {String} key required (either a PEM or HMAC key).
* - {Array} headers optional; defaults to ['date'].
* - {String} algorithm optional (unless key is HMAC);
* default is the same as the sshpk default
* signing algorithm for the type of key given
* - {String} httpVersion optional; defaults to '1.1'.
* - {Boolean} strict optional; defaults to 'false'.
* @return {Boolean} true if Authorization (and optionally Date) were added.
* @throws {TypeError} on bad parameter types (input).
* @throws {InvalidAlgorithmError} if algorithm was bad or incompatible with
* the given key.
* @throws {sshpk.KeyParseError} if key was bad.
* @throws {MissingHeaderError} if a header to be signed was specified but
* was not present.
*/
signRequest: function signRequest(request, options) {
assert.object(request, 'request');
assert.object(options, 'options');
assert.optionalString(options.algorithm, 'options.algorithm');
assert.string(options.keyId, 'options.keyId');
assert.optionalArrayOfString(options.headers, 'options.headers');
assert.optionalString(options.httpVersion, 'options.httpVersion');
if (!request.getHeader('Date'))
request.setHeader('Date', jsprim.rfc1123(new Date()));
if (!options.headers)
options.headers = ['date'];
if (!options.httpVersion)
options.httpVersion = '1.1';
var alg = [];
if (options.algorithm) {
options.algorithm = options.algorithm.toLowerCase();
alg = validateAlgorithm(options.algorithm);
}
var i;
var stringToSign = '';
for (i = 0; i < options.headers.length; i++) {
if (typeof (options.headers[i]) !== 'string')
throw new TypeError('options.headers must be an array of Strings');
var h = options.headers[i].toLowerCase();
if (h === 'request-line') {
if (!options.strict) {
/**
* We allow headers from the older spec drafts if strict parsing isn't
* specified in options.
*/
stringToSign +=
request.method + ' ' + request.path + ' HTTP/' +
options.httpVersion;
} else {
/* Strict parsing doesn't allow older draft headers. */
throw (new StrictParsingError('request-line is not a valid header ' +
'with strict parsing enabled.'));
}
} else if (h === '(request-target)') {
stringToSign +=
'(request-target): ' + request.method.toLowerCase() + ' ' +
request.path;
} else {
var value = request.getHeader(h);
if (value === undefined || value === '') {
throw new MissingHeaderError(h + ' was not in the request');
}
stringToSign += h + ': ' + value;
}
if ((i + 1) < options.headers.length)
stringToSign += '\n';
}
/* This is just for unit tests. */
if (request.hasOwnProperty('_stringToSign')) {
request._stringToSign = stringToSign;
}
var signature;
if (alg[0] === 'hmac') {
if (typeof (options.key) !== 'string' && !Buffer.isBuffer(options.key))
throw (new TypeError('options.key must be a string or Buffer'));
var hmac = crypto.createHmac(alg[1].toUpperCase(), options.key);
hmac.update(stringToSign);
signature = hmac.digest('base64');
} else {
var key = options.key;
if (typeof (key) === 'string' || Buffer.isBuffer(key))
key = sshpk.parsePrivateKey(options.key);
assert.ok(sshpk.PrivateKey.isPrivateKey(key, [1, 2]),
'options.key must be a sshpk.PrivateKey');
if (!PK_ALGOS[key.type]) {
throw (new InvalidAlgorithmError(key.type.toUpperCase() + ' type ' +
'keys are not supported'));
}
if (alg[0] !== undefined && key.type !== alg[0]) {
throw (new InvalidAlgorithmError('options.key must be a ' +
alg[0].toUpperCase() + ' key, was given a ' +
key.type.toUpperCase() + ' key instead'));
}
var signer = key.createSign(alg[1]);
signer.update(stringToSign);
var sigObj = signer.sign();
if (!HASH_ALGOS[sigObj.hashAlgorithm]) {
throw (new InvalidAlgorithmError(sigObj.hashAlgorithm.toUpperCase() +
' is not a supported hash algorithm'));
}
options.algorithm = key.type + '-' + sigObj.hashAlgorithm;
signature = sigObj.toString();
assert.notStrictEqual(signature, '', 'empty signature produced');
}
request.setHeader('Authorization', sprintf(AUTHZ_FMT,
options.keyId,
options.algorithm,
options.headers.join(' '),
signature));
return true;
}
};

112
node_modules/http-signature/lib/utils.js generated vendored Normal file
View File

@ -0,0 +1,112 @@
// Copyright 2012 Joyent, Inc. All rights reserved.
var assert = require('assert-plus');
var sshpk = require('sshpk');
var util = require('util');
var HASH_ALGOS = {
'sha1': true,
'sha256': true,
'sha512': true
};
var PK_ALGOS = {
'rsa': true,
'dsa': true,
'ecdsa': true
};
function HttpSignatureError(message, caller) {
if (Error.captureStackTrace)
Error.captureStackTrace(this, caller || HttpSignatureError);
this.message = message;
this.name = caller.name;
}
util.inherits(HttpSignatureError, Error);
function InvalidAlgorithmError(message) {
HttpSignatureError.call(this, message, InvalidAlgorithmError);
}
util.inherits(InvalidAlgorithmError, HttpSignatureError);
function validateAlgorithm(algorithm) {
var alg = algorithm.toLowerCase().split('-');
if (alg.length !== 2) {
throw (new InvalidAlgorithmError(alg[0].toUpperCase() + ' is not a ' +
'valid algorithm'));
}
if (alg[0] !== 'hmac' && !PK_ALGOS[alg[0]]) {
throw (new InvalidAlgorithmError(alg[0].toUpperCase() + ' type keys ' +
'are not supported'));
}
if (!HASH_ALGOS[alg[1]]) {
throw (new InvalidAlgorithmError(alg[1].toUpperCase() + ' is not a ' +
'supported hash algorithm'));
}
return (alg);
}
///--- API
module.exports = {
HASH_ALGOS: HASH_ALGOS,
PK_ALGOS: PK_ALGOS,
HttpSignatureError: HttpSignatureError,
InvalidAlgorithmError: InvalidAlgorithmError,
validateAlgorithm: validateAlgorithm,
/**
* Converts an OpenSSH public key (rsa only) to a PKCS#8 PEM file.
*
* The intent of this module is to interoperate with OpenSSL only,
* specifically the node crypto module's `verify` method.
*
* @param {String} key an OpenSSH public key.
* @return {String} PEM encoded form of the RSA public key.
* @throws {TypeError} on bad input.
* @throws {Error} on invalid ssh key formatted data.
*/
sshKeyToPEM: function sshKeyToPEM(key) {
assert.string(key, 'ssh_key');
var k = sshpk.parseKey(key, 'ssh');
return (k.toString('pem'));
},
/**
* Generates an OpenSSH fingerprint from an ssh public key.
*
* @param {String} key an OpenSSH public key.
* @return {String} key fingerprint.
* @throws {TypeError} on bad input.
* @throws {Error} if what you passed doesn't look like an ssh public key.
*/
fingerprint: function fingerprint(key) {
assert.string(key, 'ssh_key');
var k = sshpk.parseKey(key, 'ssh');
return (k.fingerprint('md5').toString('hex'));
},
/**
* Converts a PKGCS#8 PEM file to an OpenSSH public key (rsa)
*
* The reverse of the above function.
*/
pemToRsaSSHKey: function pemToRsaSSHKey(pem, comment) {
assert.equal('string', typeof (pem), 'typeof pem');
var k = sshpk.parseKey(pem, 'pem');
k.comment = comment;
return (k.toString('ssh'));
}
};

88
node_modules/http-signature/lib/verify.js generated vendored Normal file
View File

@ -0,0 +1,88 @@
// Copyright 2015 Joyent, Inc.
var assert = require('assert-plus');
var crypto = require('crypto');
var sshpk = require('sshpk');
var utils = require('./utils');
var HASH_ALGOS = utils.HASH_ALGOS;
var PK_ALGOS = utils.PK_ALGOS;
var InvalidAlgorithmError = utils.InvalidAlgorithmError;
var HttpSignatureError = utils.HttpSignatureError;
var validateAlgorithm = utils.validateAlgorithm;
///--- Exported API
module.exports = {
/**
* Verify RSA/DSA signature against public key. You are expected to pass in
* an object that was returned from `parse()`.
*
* @param {Object} parsedSignature the object you got from `parse`.
* @param {String} pubkey RSA/DSA private key PEM.
* @return {Boolean} true if valid, false otherwise.
* @throws {TypeError} if you pass in bad arguments.
* @throws {InvalidAlgorithmError}
*/
verifySignature: function verifySignature(parsedSignature, pubkey) {
assert.object(parsedSignature, 'parsedSignature');
if (typeof (pubkey) === 'string' || Buffer.isBuffer(pubkey))
pubkey = sshpk.parseKey(pubkey);
assert.ok(sshpk.Key.isKey(pubkey, [1, 1]), 'pubkey must be a sshpk.Key');
var alg = validateAlgorithm(parsedSignature.algorithm);
if (alg[0] === 'hmac' || alg[0] !== pubkey.type)
return (false);
var v = pubkey.createVerify(alg[1]);
v.update(parsedSignature.signingString);
return (v.verify(parsedSignature.params.signature, 'base64'));
},
/**
* Verify HMAC against shared secret. You are expected to pass in an object
* that was returned from `parse()`.
*
* @param {Object} parsedSignature the object you got from `parse`.
* @param {String} secret HMAC shared secret.
* @return {Boolean} true if valid, false otherwise.
* @throws {TypeError} if you pass in bad arguments.
* @throws {InvalidAlgorithmError}
*/
verifyHMAC: function verifyHMAC(parsedSignature, secret) {
assert.object(parsedSignature, 'parsedHMAC');
assert.string(secret, 'secret');
var alg = validateAlgorithm(parsedSignature.algorithm);
if (alg[0] !== 'hmac')
return (false);
var hashAlg = alg[1].toUpperCase();
var hmac = crypto.createHmac(hashAlg, secret);
hmac.update(parsedSignature.signingString);
/*
* Now double-hash to avoid leaking timing information - there's
* no easy constant-time compare in JS, so we use this approach
* instead. See for more info:
* https://www.isecpartners.com/blog/2011/february/double-hmac-
* verification.aspx
*/
var h1 = crypto.createHmac(hashAlg, secret);
h1.update(hmac.digest());
h1 = h1.digest();
var h2 = crypto.createHmac(hashAlg, secret);
h2.update(new Buffer(parsedSignature.params.signature, 'base64'));
h2 = h2.digest();
/* Node 0.8 returns strings from .digest(). */
if (typeof (h1) === 'string')
return (h1 === h2);
/* And node 0.10 lacks the .equals() method on Buffers. */
if (Buffer.isBuffer(h1) && !h1.equals)
return (h1.toString('binary') === h2.toString('binary'));
return (h1.equals(h2));
}
};

View File

@ -0,0 +1,43 @@
# Changelog
## not yet released
None yet.
## v1.4.0 (2017-03-13)
* #7 Add parseInteger() function for safer number parsing
## v1.3.1 (2016-09-12)
* #13 Incompatible with webpack
## v1.3.0 (2016-06-22)
* #14 add safer version of hasOwnProperty()
* #15 forEachKey() should ignore inherited properties
## v1.2.2 (2015-10-15)
* #11 NPM package shouldn't include any code that does `require('JSV')`
* #12 jsl.node.conf missing definition for "module"
## v1.2.1 (2015-10-14)
* #8 odd date parsing behaviour
## v1.2.0 (2015-10-13)
* #9 want function for returning RFC1123 dates
## v1.1.0 (2015-09-02)
* #6 a new suite of hrtime manipulation routines: `hrtimeAdd()`,
`hrtimeAccum()`, `hrtimeNanosec()`, `hrtimeMicrosec()` and
`hrtimeMillisec()`.
## v1.0.0 (2015-09-01)
First tracked release. Includes everything in previous releases, plus:
* #4 want function for merging objects

View File

@ -0,0 +1,19 @@
Copyright (c) 2012, Joyent, Inc. 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

View File

@ -0,0 +1,292 @@
# jsprim: utilities for primitive JavaScript types
This module provides miscellaneous facilities for working with strings,
numbers, dates, and objects and arrays of these basic types.
### deepCopy(obj)
Creates a deep copy of a primitive type, object, or array of primitive types.
### deepEqual(obj1, obj2)
Returns whether two objects are equal.
### isEmpty(obj)
Returns true if the given object has no properties and false otherwise. This
is O(1) (unlike `Object.keys(obj).length === 0`, which is O(N)).
### hasKey(obj, key)
Returns true if the given object has an enumerable, non-inherited property
called `key`. [For information on enumerability and ownership of properties, see
the MDN
documentation.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties)
### forEachKey(obj, callback)
Like Array.forEach, but iterates enumerable, owned properties of an object
rather than elements of an array. Equivalent to:
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
callback(key, obj[key]);
}
}
### flattenObject(obj, depth)
Flattens an object up to a given level of nesting, returning an array of arrays
of length "depth + 1", where the first "depth" elements correspond to flattened
columns and the last element contains the remaining object . For example:
flattenObject({
'I': {
'A': {
'i': {
'datum1': [ 1, 2 ],
'datum2': [ 3, 4 ]
},
'ii': {
'datum1': [ 3, 4 ]
}
},
'B': {
'i': {
'datum1': [ 5, 6 ]
},
'ii': {
'datum1': [ 7, 8 ],
'datum2': [ 3, 4 ],
},
'iii': {
}
}
},
'II': {
'A': {
'i': {
'datum1': [ 1, 2 ],
'datum2': [ 3, 4 ]
}
}
}
}, 3)
becomes:
[
[ 'I', 'A', 'i', { 'datum1': [ 1, 2 ], 'datum2': [ 3, 4 ] } ],
[ 'I', 'A', 'ii', { 'datum1': [ 3, 4 ] } ],
[ 'I', 'B', 'i', { 'datum1': [ 5, 6 ] } ],
[ 'I', 'B', 'ii', { 'datum1': [ 7, 8 ], 'datum2': [ 3, 4 ] } ],
[ 'I', 'B', 'iii', {} ],
[ 'II', 'A', 'i', { 'datum1': [ 1, 2 ], 'datum2': [ 3, 4 ] } ]
]
This function is strict: "depth" must be a non-negative integer and "obj" must
be a non-null object with at least "depth" levels of nesting under all keys.
### flattenIter(obj, depth, func)
This is similar to `flattenObject` except that instead of returning an array,
this function invokes `func(entry)` for each `entry` in the array that
`flattenObject` would return. `flattenIter(obj, depth, func)` is logically
equivalent to `flattenObject(obj, depth).forEach(func)`. Importantly, this
version never constructs the full array. Its memory usage is O(depth) rather
than O(n) (where `n` is the number of flattened elements).
There's another difference between `flattenObject` and `flattenIter` that's
related to the special case where `depth === 0`. In this case, `flattenObject`
omits the array wrapping `obj` (which is regrettable).
### pluck(obj, key)
Fetch nested property "key" from object "obj", traversing objects as needed.
For example, `pluck(obj, "foo.bar.baz")` is roughly equivalent to
`obj.foo.bar.baz`, except that:
1. If traversal fails, the resulting value is undefined, and no error is
thrown. For example, `pluck({}, "foo.bar")` is just undefined.
2. If "obj" has property "key" directly (without traversing), the
corresponding property is returned. For example,
`pluck({ 'foo.bar': 1 }, 'foo.bar')` is 1, not undefined. This is also
true recursively, so `pluck({ 'a': { 'foo.bar': 1 } }, 'a.foo.bar')` is
also 1, not undefined.
### randElt(array)
Returns an element from "array" selected uniformly at random. If "array" is
empty, throws an Error.
### startsWith(str, prefix)
Returns true if the given string starts with the given prefix and false
otherwise.
### endsWith(str, suffix)
Returns true if the given string ends with the given suffix and false
otherwise.
### parseInteger(str, options)
Parses the contents of `str` (a string) as an integer. On success, the integer
value is returned (as a number). On failure, an error is **returned** describing
why parsing failed.
By default, leading and trailing whitespace characters are not allowed, nor are
trailing characters that are not part of the numeric representation. This
behaviour can be toggled by using the options below. The empty string (`''`) is
not considered valid input. If the return value cannot be precisely represented
as a number (i.e., is smaller than `Number.MIN_SAFE_INTEGER` or larger than
`Number.MAX_SAFE_INTEGER`), an error is returned. Additionally, the string
`'-0'` will be parsed as the integer `0`, instead of as the IEEE floating point
value `-0`.
This function accepts both upper and lowercase characters for digits, similar to
`parseInt()`, `Number()`, and [strtol(3C)](https://illumos.org/man/3C/strtol).
The following may be specified in `options`:
Option | Type | Default | Meaning
------------------ | ------- | ------- | ---------------------------
base | number | 10 | numeric base (radix) to use, in the range 2 to 36
allowSign | boolean | true | whether to interpret any leading `+` (positive) and `-` (negative) characters
allowImprecise | boolean | false | whether to accept values that may have lost precision (past `MAX_SAFE_INTEGER` or below `MIN_SAFE_INTEGER`)
allowPrefix | boolean | false | whether to interpret the prefixes `0b` (base 2), `0o` (base 8), `0t` (base 10), or `0x` (base 16)
allowTrailing | boolean | false | whether to ignore trailing characters
trimWhitespace | boolean | false | whether to trim any leading or trailing whitespace/line terminators
leadingZeroIsOctal | boolean | false | whether a leading zero indicates octal
Note that if `base` is unspecified, and `allowPrefix` or `leadingZeroIsOctal`
are, then the leading characters can change the default base from 10. If `base`
is explicitly specified and `allowPrefix` is true, then the prefix will only be
accepted if it matches the specified base. `base` and `leadingZeroIsOctal`
cannot be used together.
**Context:** It's tricky to parse integers with JavaScript's built-in facilities
for several reasons:
- `parseInt()` and `Number()` by default allow the base to be specified in the
input string by a prefix (e.g., `0x` for hex).
- `parseInt()` allows trailing nonnumeric characters.
- `Number(str)` returns 0 when `str` is the empty string (`''`).
- Both functions return incorrect values when the input string represents a
valid integer outside the range of integers that can be represented precisely.
Specifically, `parseInt('9007199254740993')` returns 9007199254740992.
- Both functions always accept `-` and `+` signs before the digit.
- Some older JavaScript engines always interpret a leading 0 as indicating
octal, which can be surprising when parsing input from users who expect a
leading zero to be insignificant.
While each of these may be desirable in some contexts, there are also times when
none of them are wanted. `parseInteger()` grants greater control over what
input's permissible.
### iso8601(date)
Converts a Date object to an ISO8601 date string of the form
"YYYY-MM-DDTHH:MM:SS.sssZ". This format is not customizable.
### parseDateTime(str)
Parses a date expressed as a string, as either a number of milliseconds since
the epoch or any string format that Date accepts, giving preference to the
former where these two sets overlap (e.g., strings containing small numbers).
### hrtimeDiff(timeA, timeB)
Given two hrtime readings (as from Node's `process.hrtime()`), where timeA is
later than timeB, compute the difference and return that as an hrtime. It is
illegal to invoke this for a pair of times where timeB is newer than timeA.
### hrtimeAdd(timeA, timeB)
Add two hrtime intervals (as from Node's `process.hrtime()`), returning a new
hrtime interval array. This function does not modify either input argument.
### hrtimeAccum(timeA, timeB)
Add two hrtime intervals (as from Node's `process.hrtime()`), storing the
result in `timeA`. This function overwrites (and returns) the first argument
passed in.
### hrtimeNanosec(timeA), hrtimeMicrosec(timeA), hrtimeMillisec(timeA)
This suite of functions converts a hrtime interval (as from Node's
`process.hrtime()`) into a scalar number of nanoseconds, microseconds or
milliseconds. Results are truncated, as with `Math.floor()`.
### validateJsonObject(schema, object)
Uses JSON validation (via JSV) to validate the given object against the given
schema. On success, returns null. On failure, *returns* (does not throw) a
useful Error object.
### extraProperties(object, allowed)
Check an object for unexpected properties. Accepts the object to check, and an
array of allowed property name strings. If extra properties are detected, an
array of extra property names is returned. If no properties other than those
in the allowed list are present on the object, the returned array will be of
zero length.
### mergeObjects(provided, overrides, defaults)
Merge properties from objects "provided", "overrides", and "defaults". The
intended use case is for functions that accept named arguments in an "args"
object, but want to provide some default values and override other values. In
that case, "provided" is what the caller specified, "overrides" are what the
function wants to override, and "defaults" contains default values.
The function starts with the values in "defaults", overrides them with the
values in "provided", and then overrides those with the values in "overrides".
For convenience, any of these objects may be falsey, in which case they will be
ignored. The input objects are never modified, but properties in the returned
object are not deep-copied.
For example:
mergeObjects(undefined, { 'objectMode': true }, { 'highWaterMark': 0 })
returns:
{ 'objectMode': true, 'highWaterMark': 0 }
For another example:
mergeObjects(
{ 'highWaterMark': 16, 'objectMode': 7 }, /* from caller */
{ 'objectMode': true }, /* overrides */
{ 'highWaterMark': 0 }); /* default */
returns:
{ 'objectMode': true, 'highWaterMark': 16 }
# Contributing
Code should be "make check" clean. This target assumes that
[jsl](http://github.com/davepacheco/javascriptlint) and
[jsstyle](http://github.com/davepacheco/jsstyle) are on your path.
New tests should generally accompany new functions and bug fixes. The tests
should pass cleanly (run tests/basic.js).

View File

@ -0,0 +1,735 @@
/*
* lib/jsprim.js: utilities for primitive JavaScript types
*/
var mod_assert = require('assert-plus');
var mod_util = require('util');
var mod_extsprintf = require('extsprintf');
var mod_verror = require('verror');
var mod_jsonschema = require('json-schema');
/*
* Public interface
*/
exports.deepCopy = deepCopy;
exports.deepEqual = deepEqual;
exports.isEmpty = isEmpty;
exports.hasKey = hasKey;
exports.forEachKey = forEachKey;
exports.pluck = pluck;
exports.flattenObject = flattenObject;
exports.flattenIter = flattenIter;
exports.validateJsonObject = validateJsonObjectJS;
exports.validateJsonObjectJS = validateJsonObjectJS;
exports.randElt = randElt;
exports.extraProperties = extraProperties;
exports.mergeObjects = mergeObjects;
exports.startsWith = startsWith;
exports.endsWith = endsWith;
exports.parseInteger = parseInteger;
exports.iso8601 = iso8601;
exports.rfc1123 = rfc1123;
exports.parseDateTime = parseDateTime;
exports.hrtimediff = hrtimeDiff;
exports.hrtimeDiff = hrtimeDiff;
exports.hrtimeAccum = hrtimeAccum;
exports.hrtimeAdd = hrtimeAdd;
exports.hrtimeNanosec = hrtimeNanosec;
exports.hrtimeMicrosec = hrtimeMicrosec;
exports.hrtimeMillisec = hrtimeMillisec;
/*
* Deep copy an acyclic *basic* Javascript object. This only handles basic
* scalars (strings, numbers, booleans) and arbitrarily deep arrays and objects
* containing these. This does *not* handle instances of other classes.
*/
function deepCopy(obj)
{
var ret, key;
var marker = '__deepCopy';
if (obj && obj[marker])
throw (new Error('attempted deep copy of cyclic object'));
if (obj && obj.constructor == Object) {
ret = {};
obj[marker] = true;
for (key in obj) {
if (key == marker)
continue;
ret[key] = deepCopy(obj[key]);
}
delete (obj[marker]);
return (ret);
}
if (obj && obj.constructor == Array) {
ret = [];
obj[marker] = true;
for (key = 0; key < obj.length; key++)
ret.push(deepCopy(obj[key]));
delete (obj[marker]);
return (ret);
}
/*
* It must be a primitive type -- just return it.
*/
return (obj);
}
function deepEqual(obj1, obj2)
{
if (typeof (obj1) != typeof (obj2))
return (false);
if (obj1 === null || obj2 === null || typeof (obj1) != 'object')
return (obj1 === obj2);
if (obj1.constructor != obj2.constructor)
return (false);
var k;
for (k in obj1) {
if (!obj2.hasOwnProperty(k))
return (false);
if (!deepEqual(obj1[k], obj2[k]))
return (false);
}
for (k in obj2) {
if (!obj1.hasOwnProperty(k))
return (false);
}
return (true);
}
function isEmpty(obj)
{
var key;
for (key in obj)
return (false);
return (true);
}
function hasKey(obj, key)
{
mod_assert.equal(typeof (key), 'string');
return (Object.prototype.hasOwnProperty.call(obj, key));
}
function forEachKey(obj, callback)
{
for (var key in obj) {
if (hasKey(obj, key)) {
callback(key, obj[key]);
}
}
}
function pluck(obj, key)
{
mod_assert.equal(typeof (key), 'string');
return (pluckv(obj, key));
}
function pluckv(obj, key)
{
if (obj === null || typeof (obj) !== 'object')
return (undefined);
if (obj.hasOwnProperty(key))
return (obj[key]);
var i = key.indexOf('.');
if (i == -1)
return (undefined);
var key1 = key.substr(0, i);
if (!obj.hasOwnProperty(key1))
return (undefined);
return (pluckv(obj[key1], key.substr(i + 1)));
}
/*
* Invoke callback(row) for each entry in the array that would be returned by
* flattenObject(data, depth). This is just like flattenObject(data,
* depth).forEach(callback), except that the intermediate array is never
* created.
*/
function flattenIter(data, depth, callback)
{
doFlattenIter(data, depth, [], callback);
}
function doFlattenIter(data, depth, accum, callback)
{
var each;
var key;
if (depth === 0) {
each = accum.slice(0);
each.push(data);
callback(each);
return;
}
mod_assert.ok(data !== null);
mod_assert.equal(typeof (data), 'object');
mod_assert.equal(typeof (depth), 'number');
mod_assert.ok(depth >= 0);
for (key in data) {
each = accum.slice(0);
each.push(key);
doFlattenIter(data[key], depth - 1, each, callback);
}
}
function flattenObject(data, depth)
{
if (depth === 0)
return ([ data ]);
mod_assert.ok(data !== null);
mod_assert.equal(typeof (data), 'object');
mod_assert.equal(typeof (depth), 'number');
mod_assert.ok(depth >= 0);
var rv = [];
var key;
for (key in data) {
flattenObject(data[key], depth - 1).forEach(function (p) {
rv.push([ key ].concat(p));
});
}
return (rv);
}
function startsWith(str, prefix)
{
return (str.substr(0, prefix.length) == prefix);
}
function endsWith(str, suffix)
{
return (str.substr(
str.length - suffix.length, suffix.length) == suffix);
}
function iso8601(d)
{
if (typeof (d) == 'number')
d = new Date(d);
mod_assert.ok(d.constructor === Date);
return (mod_extsprintf.sprintf('%4d-%02d-%02dT%02d:%02d:%02d.%03dZ',
d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(),
d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(),
d.getUTCMilliseconds()));
}
var RFC1123_MONTHS = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var RFC1123_DAYS = [
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
function rfc1123(date) {
return (mod_extsprintf.sprintf('%s, %02d %s %04d %02d:%02d:%02d GMT',
RFC1123_DAYS[date.getUTCDay()], date.getUTCDate(),
RFC1123_MONTHS[date.getUTCMonth()], date.getUTCFullYear(),
date.getUTCHours(), date.getUTCMinutes(),
date.getUTCSeconds()));
}
/*
* Parses a date expressed as a string, as either a number of milliseconds since
* the epoch or any string format that Date accepts, giving preference to the
* former where these two sets overlap (e.g., small numbers).
*/
function parseDateTime(str)
{
/*
* This is irritatingly implicit, but significantly more concise than
* alternatives. The "+str" will convert a string containing only a
* number directly to a Number, or NaN for other strings. Thus, if the
* conversion succeeds, we use it (this is the milliseconds-since-epoch
* case). Otherwise, we pass the string directly to the Date
* constructor to parse.
*/
var numeric = +str;
if (!isNaN(numeric)) {
return (new Date(numeric));
} else {
return (new Date(str));
}
}
/*
* Number.*_SAFE_INTEGER isn't present before node v0.12, so we hardcode
* the ES6 definitions here, while allowing for them to someday be higher.
*/
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
var MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER || -9007199254740991;
/*
* Default options for parseInteger().
*/
var PI_DEFAULTS = {
base: 10,
allowSign: true,
allowPrefix: false,
allowTrailing: false,
allowImprecise: false,
trimWhitespace: false,
leadingZeroIsOctal: false
};
var CP_0 = 0x30;
var CP_9 = 0x39;
var CP_A = 0x41;
var CP_B = 0x42;
var CP_O = 0x4f;
var CP_T = 0x54;
var CP_X = 0x58;
var CP_Z = 0x5a;
var CP_a = 0x61;
var CP_b = 0x62;
var CP_o = 0x6f;
var CP_t = 0x74;
var CP_x = 0x78;
var CP_z = 0x7a;
var PI_CONV_DEC = 0x30;
var PI_CONV_UC = 0x37;
var PI_CONV_LC = 0x57;
/*
* A stricter version of parseInt() that provides options for changing what
* is an acceptable string (for example, disallowing trailing characters).
*/
function parseInteger(str, uopts)
{
mod_assert.string(str, 'str');
mod_assert.optionalObject(uopts, 'options');
var baseOverride = false;
var options = PI_DEFAULTS;
if (uopts) {
baseOverride = hasKey(uopts, 'base');
options = mergeObjects(options, uopts);
mod_assert.number(options.base, 'options.base');
mod_assert.ok(options.base >= 2, 'options.base >= 2');
mod_assert.ok(options.base <= 36, 'options.base <= 36');
mod_assert.bool(options.allowSign, 'options.allowSign');
mod_assert.bool(options.allowPrefix, 'options.allowPrefix');
mod_assert.bool(options.allowTrailing,
'options.allowTrailing');
mod_assert.bool(options.allowImprecise,
'options.allowImprecise');
mod_assert.bool(options.trimWhitespace,
'options.trimWhitespace');
mod_assert.bool(options.leadingZeroIsOctal,
'options.leadingZeroIsOctal');
if (options.leadingZeroIsOctal) {
mod_assert.ok(!baseOverride,
'"base" and "leadingZeroIsOctal" are ' +
'mutually exclusive');
}
}
var c;
var pbase = -1;
var base = options.base;
var start;
var mult = 1;
var value = 0;
var idx = 0;
var len = str.length;
/* Trim any whitespace on the left side. */
if (options.trimWhitespace) {
while (idx < len && isSpace(str.charCodeAt(idx))) {
++idx;
}
}
/* Check the number for a leading sign. */
if (options.allowSign) {
if (str[idx] === '-') {
idx += 1;
mult = -1;
} else if (str[idx] === '+') {
idx += 1;
}
}
/* Parse the base-indicating prefix if there is one. */
if (str[idx] === '0') {
if (options.allowPrefix) {
pbase = prefixToBase(str.charCodeAt(idx + 1));
if (pbase !== -1 && (!baseOverride || pbase === base)) {
base = pbase;
idx += 2;
}
}
if (pbase === -1 && options.leadingZeroIsOctal) {
base = 8;
}
}
/* Parse the actual digits. */
for (start = idx; idx < len; ++idx) {
c = translateDigit(str.charCodeAt(idx));
if (c !== -1 && c < base) {
value *= base;
value += c;
} else {
break;
}
}
/* If we didn't parse any digits, we have an invalid number. */
if (start === idx) {
return (new Error('invalid number: ' + JSON.stringify(str)));
}
/* Trim any whitespace on the right side. */
if (options.trimWhitespace) {
while (idx < len && isSpace(str.charCodeAt(idx))) {
++idx;
}
}
/* Check for trailing characters. */
if (idx < len && !options.allowTrailing) {
return (new Error('trailing characters after number: ' +
JSON.stringify(str.slice(idx))));
}
/* If our value is 0, we return now, to avoid returning -0. */
if (value === 0) {
return (0);
}
/* Calculate our final value. */
var result = value * mult;
/*
* If the string represents a value that cannot be precisely represented
* by JavaScript, then we want to check that:
*
* - We never increased the value past MAX_SAFE_INTEGER
* - We don't make the result negative and below MIN_SAFE_INTEGER
*
* Because we only ever increment the value during parsing, there's no
* chance of moving past MAX_SAFE_INTEGER and then dropping below it
* again, losing precision in the process. This means that we only need
* to do our checks here, at the end.
*/
if (!options.allowImprecise &&
(value > MAX_SAFE_INTEGER || result < MIN_SAFE_INTEGER)) {
return (new Error('number is outside of the supported range: ' +
JSON.stringify(str.slice(start, idx))));
}
return (result);
}
/*
* Interpret a character code as a base-36 digit.
*/
function translateDigit(d)
{
if (d >= CP_0 && d <= CP_9) {
/* '0' to '9' -> 0 to 9 */
return (d - PI_CONV_DEC);
} else if (d >= CP_A && d <= CP_Z) {
/* 'A' - 'Z' -> 10 to 35 */
return (d - PI_CONV_UC);
} else if (d >= CP_a && d <= CP_z) {
/* 'a' - 'z' -> 10 to 35 */
return (d - PI_CONV_LC);
} else {
/* Invalid character code */
return (-1);
}
}
/*
* Test if a value matches the ECMAScript definition of trimmable whitespace.
*/
function isSpace(c)
{
return (c === 0x20) ||
(c >= 0x0009 && c <= 0x000d) ||
(c === 0x00a0) ||
(c === 0x1680) ||
(c === 0x180e) ||
(c >= 0x2000 && c <= 0x200a) ||
(c === 0x2028) ||
(c === 0x2029) ||
(c === 0x202f) ||
(c === 0x205f) ||
(c === 0x3000) ||
(c === 0xfeff);
}
/*
* Determine which base a character indicates (e.g., 'x' indicates hex).
*/
function prefixToBase(c)
{
if (c === CP_b || c === CP_B) {
/* 0b/0B (binary) */
return (2);
} else if (c === CP_o || c === CP_O) {
/* 0o/0O (octal) */
return (8);
} else if (c === CP_t || c === CP_T) {
/* 0t/0T (decimal) */
return (10);
} else if (c === CP_x || c === CP_X) {
/* 0x/0X (hexadecimal) */
return (16);
} else {
/* Not a meaningful character */
return (-1);
}
}
function validateJsonObjectJS(schema, input)
{
var report = mod_jsonschema.validate(input, schema);
if (report.errors.length === 0)
return (null);
/* Currently, we only do anything useful with the first error. */
var error = report.errors[0];
/* The failed property is given by a URI with an irrelevant prefix. */
var propname = error['property'];
var reason = error['message'].toLowerCase();
var i, j;
/*
* There's at least one case where the property error message is
* confusing at best. We work around this here.
*/
if ((i = reason.indexOf('the property ')) != -1 &&
(j = reason.indexOf(' is not defined in the schema and the ' +
'schema does not allow additional properties')) != -1) {
i += 'the property '.length;
if (propname === '')
propname = reason.substr(i, j - i);
else
propname = propname + '.' + reason.substr(i, j - i);
reason = 'unsupported property';
}
var rv = new mod_verror.VError('property "%s": %s', propname, reason);
rv.jsv_details = error;
return (rv);
}
function randElt(arr)
{
mod_assert.ok(Array.isArray(arr) && arr.length > 0,
'randElt argument must be a non-empty array');
return (arr[Math.floor(Math.random() * arr.length)]);
}
function assertHrtime(a)
{
mod_assert.ok(a[0] >= 0 && a[1] >= 0,
'negative numbers not allowed in hrtimes');
mod_assert.ok(a[1] < 1e9, 'nanoseconds column overflow');
}
/*
* Compute the time elapsed between hrtime readings A and B, where A is later
* than B. hrtime readings come from Node's process.hrtime(). There is no
* defined way to represent negative deltas, so it's illegal to diff B from A
* where the time denoted by B is later than the time denoted by A. If this
* becomes valuable, we can define a representation and extend the
* implementation to support it.
*/
function hrtimeDiff(a, b)
{
assertHrtime(a);
assertHrtime(b);
mod_assert.ok(a[0] > b[0] || (a[0] == b[0] && a[1] >= b[1]),
'negative differences not allowed');
var rv = [ a[0] - b[0], 0 ];
if (a[1] >= b[1]) {
rv[1] = a[1] - b[1];
} else {
rv[0]--;
rv[1] = 1e9 - (b[1] - a[1]);
}
return (rv);
}
/*
* Convert a hrtime reading from the array format returned by Node's
* process.hrtime() into a scalar number of nanoseconds.
*/
function hrtimeNanosec(a)
{
assertHrtime(a);
return (Math.floor(a[0] * 1e9 + a[1]));
}
/*
* Convert a hrtime reading from the array format returned by Node's
* process.hrtime() into a scalar number of microseconds.
*/
function hrtimeMicrosec(a)
{
assertHrtime(a);
return (Math.floor(a[0] * 1e6 + a[1] / 1e3));
}
/*
* Convert a hrtime reading from the array format returned by Node's
* process.hrtime() into a scalar number of milliseconds.
*/
function hrtimeMillisec(a)
{
assertHrtime(a);
return (Math.floor(a[0] * 1e3 + a[1] / 1e6));
}
/*
* Add two hrtime readings A and B, overwriting A with the result of the
* addition. This function is useful for accumulating several hrtime intervals
* into a counter. Returns A.
*/
function hrtimeAccum(a, b)
{
assertHrtime(a);
assertHrtime(b);
/*
* Accumulate the nanosecond component.
*/
a[1] += b[1];
if (a[1] >= 1e9) {
/*
* The nanosecond component overflowed, so carry to the seconds
* field.
*/
a[0]++;
a[1] -= 1e9;
}
/*
* Accumulate the seconds component.
*/
a[0] += b[0];
return (a);
}
/*
* Add two hrtime readings A and B, returning the result as a new hrtime array.
* Does not modify either input argument.
*/
function hrtimeAdd(a, b)
{
assertHrtime(a);
var rv = [ a[0], a[1] ];
return (hrtimeAccum(rv, b));
}
/*
* Check an object for unexpected properties. Accepts the object to check, and
* an array of allowed property names (strings). Returns an array of key names
* that were found on the object, but did not appear in the list of allowed
* properties. If no properties were found, the returned array will be of
* zero length.
*/
function extraProperties(obj, allowed)
{
mod_assert.ok(typeof (obj) === 'object' && obj !== null,
'obj argument must be a non-null object');
mod_assert.ok(Array.isArray(allowed),
'allowed argument must be an array of strings');
for (var i = 0; i < allowed.length; i++) {
mod_assert.ok(typeof (allowed[i]) === 'string',
'allowed argument must be an array of strings');
}
return (Object.keys(obj).filter(function (key) {
return (allowed.indexOf(key) === -1);
}));
}
/*
* Given three sets of properties "provided" (may be undefined), "overrides"
* (required), and "defaults" (may be undefined), construct an object containing
* the union of these sets with "overrides" overriding "provided", and
* "provided" overriding "defaults". None of the input objects are modified.
*/
function mergeObjects(provided, overrides, defaults)
{
var rv, k;
rv = {};
if (defaults) {
for (k in defaults)
rv[k] = defaults[k];
}
if (provided) {
for (k in provided)
rv[k] = provided[k];
}
if (overrides) {
for (k in overrides)
rv[k] = overrides[k];
}
return (rv);
}

View File

@ -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>

View File

@ -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

View File

@ -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>.

View File

@ -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);

View File

@ -0,0 +1,115 @@
{
"_args": [
[
{
"raw": "assert-plus@1.0.0",
"scope": null,
"escapedName": "assert-plus",
"name": "assert-plus",
"rawSpec": "1.0.0",
"spec": "1.0.0",
"type": "version"
},
"C:\\Users\\x2mjbyrn\\Source\\Repos\\Skeleton\\node_modules\\http-signature\\node_modules\\jsprim"
]
],
"_from": "assert-plus@1.0.0",
"_id": "assert-plus@1.0.0",
"_inCache": true,
"_location": "/http-signature/jsprim/assert-plus",
"_nodeVersion": "0.10.40",
"_npmUser": {
"name": "pfmooney",
"email": "patrick.f.mooney@gmail.com"
},
"_npmVersion": "3.3.9",
"_phantomChildren": {},
"_requested": {
"raw": "assert-plus@1.0.0",
"scope": null,
"escapedName": "assert-plus",
"name": "assert-plus",
"rawSpec": "1.0.0",
"spec": "1.0.0",
"type": "version"
},
"_requiredBy": [
"/http-signature/jsprim"
],
"_resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz",
"_shasum": "f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525",
"_shrinkwrap": null,
"_spec": "assert-plus@1.0.0",
"_where": "C:\\Users\\x2mjbyrn\\Source\\Repos\\Skeleton\\node_modules\\http-signature\\node_modules\\jsprim",
"author": {
"name": "Mark Cavage",
"email": "mcavage@gmail.com"
},
"bugs": {
"url": "https://github.com/mcavage/node-assert-plus/issues"
},
"contributors": [
{
"name": "Dave Eddy",
"email": "dave@daveeddy.com"
},
{
"name": "Fred Kuo",
"email": "fred.kuo@joyent.com"
},
{
"name": "Lars-Magnus Skog",
"email": "ralphtheninja@riseup.net"
},
{
"name": "Mark Cavage",
"email": "mcavage@gmail.com"
},
{
"name": "Patrick Mooney",
"email": "pmooney@pfmooney.com"
},
{
"name": "Rob Gulewich",
"email": "robert.gulewich@joyent.com"
}
],
"dependencies": {},
"description": "Extra assertions on top of node's assert module",
"devDependencies": {
"faucet": "0.0.1",
"tape": "4.2.2"
},
"directories": {},
"dist": {
"shasum": "f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525",
"tarball": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz"
},
"engines": {
"node": ">=0.8"
},
"homepage": "https://github.com/mcavage/node-assert-plus#readme",
"license": "MIT",
"main": "./assert.js",
"maintainers": [
{
"name": "mcavage",
"email": "mcavage@gmail.com"
},
{
"name": "pfmooney",
"email": "patrick.f.mooney@gmail.com"
}
],
"name": "assert-plus",
"optionalDependencies": {},
"readme": "ERROR: No README data found!",
"repository": {
"type": "git",
"url": "git+https://github.com/mcavage/node-assert-plus.git"
},
"scripts": {
"test": "tape tests/*.js | ./node_modules/.bin/faucet"
},
"version": "1.0.0"
}

View File

@ -0,0 +1,86 @@
{
"_args": [
[
{
"raw": "jsprim@^1.2.2",
"scope": null,
"escapedName": "jsprim",
"name": "jsprim",
"rawSpec": "^1.2.2",
"spec": ">=1.2.2 <2.0.0",
"type": "range"
},
"C:\\Users\\x2mjbyrn\\Source\\Repos\\Skeleton\\node_modules\\http-signature"
]
],
"_from": "jsprim@>=1.2.2 <2.0.0",
"_id": "jsprim@1.4.0",
"_inCache": true,
"_location": "/http-signature/jsprim",
"_nodeVersion": "0.10.43",
"_npmOperationalInternal": {
"host": "packages-12-west.internal.npmjs.com",
"tmp": "tmp/jsprim-1.4.0.tgz_1489453554649_0.9609751487150788"
},
"_npmUser": {
"name": "dap",
"email": "dap@cs.brown.edu"
},
"_npmVersion": "2.15.9",
"_phantomChildren": {},
"_requested": {
"raw": "jsprim@^1.2.2",
"scope": null,
"escapedName": "jsprim",
"name": "jsprim",
"rawSpec": "^1.2.2",
"spec": ">=1.2.2 <2.0.0",
"type": "range"
},
"_requiredBy": [
"/http-signature"
],
"_resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.0.tgz",
"_shasum": "a3b87e40298d8c380552d8cc7628a0bb95a22918",
"_shrinkwrap": null,
"_spec": "jsprim@^1.2.2",
"_where": "C:\\Users\\x2mjbyrn\\Source\\Repos\\Skeleton\\node_modules\\http-signature",
"bugs": {
"url": "https://github.com/joyent/node-jsprim/issues"
},
"dependencies": {
"assert-plus": "1.0.0",
"extsprintf": "1.0.2",
"json-schema": "0.2.3",
"verror": "1.3.6"
},
"description": "utilities for primitive JavaScript types",
"devDependencies": {},
"directories": {},
"dist": {
"shasum": "a3b87e40298d8c380552d8cc7628a0bb95a22918",
"tarball": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.0.tgz"
},
"engines": [
"node >=0.6.0"
],
"gitHead": "6ea6cb4f3d2d889d87c336487f4940eb4aa11980",
"homepage": "https://github.com/joyent/node-jsprim#readme",
"license": "MIT",
"main": "./lib/jsprim.js",
"maintainers": [
{
"name": "dap",
"email": "dap@cs.brown.edu"
}
],
"name": "jsprim",
"optionalDependencies": {},
"readme": "ERROR: No README data found!",
"repository": {
"type": "git",
"url": "git://github.com/joyent/node-jsprim.git"
},
"scripts": {},
"version": "1.4.0"
}

107
node_modules/http-signature/package.json generated vendored Normal file
View File

@ -0,0 +1,107 @@
{
"_args": [
[
"http-signature@~1.1.0",
"C:\\Users\\x2mjbyrn\\Source\\Repos\\Skeleton\\node_modules\\request"
]
],
"_from": "http-signature@>=1.1.0-0 <1.2.0-0",
"_id": "http-signature@1.1.1",
"_inCache": true,
"_location": "/http-signature",
"_nodeVersion": "0.12.9",
"_npmUser": {
"email": "alex@cooperi.net",
"name": "arekinath"
},
"_npmVersion": "2.14.9",
"_phantomChildren": {},
"_requested": {
"name": "http-signature",
"raw": "http-signature@~1.1.0",
"rawSpec": "~1.1.0",
"scope": null,
"spec": ">=1.1.0-0 <1.2.0-0",
"type": "range"
},
"_requiredBy": [
"/request"
],
"_resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.1.1.tgz",
"_shasum": "df72e267066cd0ac67fb76adf8e134a8fbcf91bf",
"_shrinkwrap": null,
"_spec": "http-signature@~1.1.0",
"_where": "C:\\Users\\x2mjbyrn\\Source\\Repos\\Skeleton\\node_modules\\request",
"author": {
"name": "Joyent, Inc"
},
"bugs": {
"url": "https://github.com/joyent/node-http-signature/issues"
},
"contributors": [
{
"name": "Mark Cavage",
"email": "mcavage@gmail.com"
},
{
"name": "David I. Lehn",
"email": "dil@lehn.org"
},
{
"name": "Patrick Mooney",
"email": "patrick.f.mooney@gmail.com"
}
],
"dependencies": {
"assert-plus": "^0.2.0",
"jsprim": "^1.2.2",
"sshpk": "^1.7.0"
},
"description": "Reference implementation of Joyent's HTTP Signature scheme.",
"devDependencies": {
"node-uuid": "^1.4.1",
"tap": "0.4.2"
},
"directories": {},
"dist": {
"shasum": "df72e267066cd0ac67fb76adf8e134a8fbcf91bf",
"tarball": "https://registry.npmjs.org/http-signature/-/http-signature-1.1.1.tgz"
},
"engines": {
"node": ">=0.8",
"npm": ">=1.3.7"
},
"gitHead": "74d3f35e3aa436d83723c53b01e266f448e8149a",
"homepage": "https://github.com/joyent/node-http-signature/",
"installable": true,
"keywords": [
"https",
"request"
],
"license": "MIT",
"main": "lib/index.js",
"maintainers": [
{
"name": "arekinath",
"email": "alex@cooperi.net"
},
{
"name": "mcavage",
"email": "mcavage@gmail.com"
},
{
"name": "pfmooney",
"email": "patrick.f.mooney@gmail.com"
}
],
"name": "http-signature",
"optionalDependencies": {},
"repository": {
"type": "git",
"url": "git://github.com/joyent/node-http-signature.git"
},
"scripts": {
"test": "tap test/*.js"
},
"version": "1.1.1"
}