Commit dd256bd1 by zCaesar

Initial commit

parents
This diff is collapsed. Click to expand it.
module.exports = class DataUsage {
constructor(devapi_request, devapi_response, feed_read, mqtt_online, ds_byteres, mqtt_connect, mqtt_deliver, mqtt_publish, mqtt_subscribe, shadow_expression, shadow_read, shadow_write, shadow_publish, feed_store, devicereg_trigger, shadow_trigger) {
this["devapi.request"] = devapi_request // Number
this["devapi.response"] = devapi_response // Number
this["feed.read"] = feed_read // Number
this["mqtt.online"] = mqtt_online // Number
this["ds.byteres"] = ds_byteres // Number
this["mqtt.connect"] = mqtt_connect // Number
this["mqtt.deliver"] = mqtt_deliver // Number
this["mqtt.publish"] = mqtt_publish // Number
this["mqtt.subscribe"] = mqtt_subscribe // Number
this["shadow.expression"] = shadow_expression // Number
this["shadow.read"] = shadow_read // Number
this["shadow.write"] = shadow_write // Number
this["shadow.publish"] = shadow_publish // Number
this["feed.store"] = feed_store // Number
this["devicereg.trigger"] = devicereg_trigger // Number
this["shadow.trigger"] = shadow_trigger // Number
}
}
\ No newline at end of file
{
"privateKey": {
"ms:billingregistry": "MS_BILLINGREGISTRY_PRIKEY"
},
"trustedIssuer": {
},
"config": {
},
"dependency": {
"projectstat_service_uri": "PROJECTSTAT_SERVICE_URI"
}
}
\ No newline at end of file
require('dotenv').config()
const config = require('config')
const Seneca = require('seneca')
const msca = require('msca')
const projectstatURI = require('url').parse(config.get('dependency.projectstat_service_uri'))
const seneca = Seneca({}).client({ port: projectstatURI.port, host: projectstatURI.hostname })
const DataUsage = require('./DataUsage')
const COBJ = {
"apicall": ["deapiv.request", "devapi.response", "feed.read"],
"connection": ["mqtt.online"],
"datasource": ["ds.byteres"],
"message": ["mqtt.connect", "mqtt.deliver", "mqtt.publish", "mqtt.subscribe"],
"shadowops": ["shadow.expression", "shadow.read", "shadow.write", "shadow.publish"],
"store": ["feed.store"],
"trigger": ["devicereg.trigger", "shadow.trigger"]
}
let datausage = new DataUsage(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
const sumbillingquota = (objs, data) => {
let sumObject = {}
for (const [key, value] of Object.entries(objs)) {
let sumValue = 0
value.forEach((_value) => {
if (data.hasOwnProperty(_value) === true) {
sumValue = sumValue + data[_value]
}
else {
sumValue = sumValue + 0
}
})
sumObject[key] = sumValue
}
return sumObject
}
const getprojectusage = async ({ projectid, begintime, endtime }) => {
return new Promise(resolve => {
seneca.act({
ms: 'projectstat', version: 1, cmd: 'getProjectUsage', arg: { projectid, begintime, endtime }, auth: { jwt: msca.getServiceJWT() }
}, function (err, res) {
resolve(res)
})
})
}
let p = [
"P576424131245",
"P433349622644",
"P713213054194",
"P585620465624",
"P495313033571",
"P861223495458",
"P006527656523",
"P075662099126",
"P921651743343",
"P544277005757"
]
async function dop() {
let begintime = getDatetime(new Date(1593572400000).toISOString()), endtime = getDatetime(new Date(1596250800000).toISOString())
while (p.length) {
let projectstat = await getprojectusage({ projectid: p[0], begintime, endtime })
if (Object.keys(projectstat.result).length) {
datausage = sumObjectsByKey(datausage, projectstat.result.usage)
}
p.shift()
}
return Promise.resolve(datausage)
}
const getDatetime = (Date) => {
let date = Date.split("T")[0]
let time = Date.split("T")[1].split(".")[0]
return date + ' ' + time
}
const sumObjectsByKey = (...objs) => { // ...objs เป็นการรับค่าเข้ามาแบบเป็นหลาย objects // ใน GO ก็เขียนเหมือนกันนะ
return objs.reduce((a, b) => {
for (let k in b) {
if (b.hasOwnProperty(k))
a[k] = (a[k] || 0) + b[k]
}
return a
}, {}) // {} = initial value // เผื่อใครมาอ่านแล้วงงว่ามี {} เพื่อ
}
async function main() {
let sdu = await dop()
console.log(sdu)
let sbq = sumbillingquota(COBJ, sdu)
console.log(sbq)
}
main();
../json5/lib/cli.js
\ No newline at end of file
../ndjson/cli.js
\ No newline at end of file
../semver/bin/semver
\ No newline at end of file
Copyright (c) 2019-2020, Project 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.
<a href="https://hapi.dev"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a>
# @hapi/address
#### Validate email address and domain.
**address** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) they work even better together.
### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support
## Useful resources
- [Documentation and API](https://hapi.dev/family/address/)
- [Versions status](https://hapi.dev/resources/status/#address)
- [Changelog](https://hapi.dev/family/address/changelog/)
- [Project policies](https://hapi.dev/policies/)
- [Free and commercial support options](https://hapi.dev/support/)
'use strict';
// Adapted from:
// Copyright (c) 2017-2019 Justin Ridgewell, MIT Licensed, https://github.com/jridgewell/safe-decode-string-component
// Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>, MIT Licensed, http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
const internals = {};
exports.decode = function (string) {
let percentPos = string.indexOf('%');
if (percentPos === -1) {
return string;
}
let decoded = '';
let last = 0;
let codepoint = 0;
let startOfOctets = percentPos;
let state = internals.utf8.accept;
while (percentPos > -1 &&
percentPos < string.length) {
const high = internals.resolveHex(string[percentPos + 1], 4);
const low = internals.resolveHex(string[percentPos + 2], 0);
const byte = high | low;
const type = internals.utf8.data[byte];
state = internals.utf8.data[256 + state + type];
codepoint = (codepoint << 6) | (byte & internals.utf8.data[364 + type]);
if (state === internals.utf8.accept) {
decoded += string.slice(last, startOfOctets);
decoded += codepoint <= 0xFFFF
? String.fromCharCode(codepoint)
: String.fromCharCode(0xD7C0 + (codepoint >> 10), 0xDC00 + (codepoint & 0x3FF));
codepoint = 0;
last = percentPos + 3;
percentPos = string.indexOf('%', last);
startOfOctets = percentPos;
continue;
}
if (state === internals.utf8.reject) {
return null;
}
percentPos += 3;
if (percentPos >= string.length ||
string[percentPos] !== '%') {
return null;
}
}
return decoded + string.slice(last);
};
internals.resolveHex = function (char, shift) {
const i = internals.hex[char];
return i === undefined ? 255 : i << shift;
};
internals.hex = {
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
'5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'a': 10, 'A': 10, 'b': 11, 'B': 11, 'c': 12,
'C': 12, 'd': 13, 'D': 13, 'e': 14, 'E': 14,
'f': 15, 'F': 15
};
internals.utf8 = {
accept: 12,
reject: 0,
data: [
// Maps bytes to character to a transition
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 7,
10, 9, 9, 9, 11, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
// Maps a state to a new state when adding a transition
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12, 0, 0, 0, 0, 24, 36, 48, 60, 72, 84, 96,
0, 12, 12, 12, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0,
0, 24, 24, 24, 0, 0, 0, 0, 0, 0, 0, 0,
0, 24, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 48, 48, 48, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 48, 48, 0, 0, 0, 0, 0, 0, 0, 0,
0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// Maps the current transition to a mask that needs to apply to the byte
0x7F, 0x3F, 0x3F, 0x3F, 0x00, 0x1F, 0x0F, 0x0F, 0x0F, 0x07, 0x07, 0x07
]
};
'use strict';
const Url = require('url');
const Errors = require('./errors');
const internals = {
minDomainSegments: 2,
nonAsciiRx: /[^\x00-\x7f]/,
domainControlRx: /[\x00-\x20@\:\/]/, // Control + space + separators
tldSegmentRx: /^[a-zA-Z](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$/,
domainSegmentRx: /^[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$/,
URL: Url.URL || URL // $lab:coverage:ignore$
};
exports.analyze = function (domain, options = {}) {
if (typeof domain !== 'string') {
throw new Error('Invalid input: domain must be a string');
}
if (!domain) {
return Errors.code('DOMAIN_NON_EMPTY_STRING');
}
if (domain.length > 256) {
return Errors.code('DOMAIN_TOO_LONG');
}
const ascii = !internals.nonAsciiRx.test(domain);
if (!ascii) {
if (options.allowUnicode === false) { // Defaults to true
return Errors.code('DOMAIN_INVALID_UNICODE_CHARS');
}
domain = domain.normalize('NFC');
}
if (internals.domainControlRx.test(domain)) {
return Errors.code('DOMAIN_INVALID_CHARS');
}
domain = internals.punycode(domain);
// https://tools.ietf.org/html/rfc1035 section 2.3.1
const minDomainSegments = options.minDomainSegments || internals.minDomainSegments;
const segments = domain.split('.');
if (segments.length < minDomainSegments) {
return Errors.code('DOMAIN_SEGMENTS_COUNT');
}
if (options.maxDomainSegments) {
if (segments.length > options.maxDomainSegments) {
return Errors.code('DOMAIN_SEGMENTS_COUNT_MAX');
}
}
const tlds = options.tlds;
if (tlds) {
const tld = segments[segments.length - 1].toLowerCase();
if (tlds.deny && tlds.deny.has(tld) ||
tlds.allow && !tlds.allow.has(tld)) {
return Errors.code('DOMAIN_FORBIDDEN_TLDS');
}
}
for (let i = 0; i < segments.length; ++i) {
const segment = segments[i];
if (!segment.length) {
return Errors.code('DOMAIN_EMPTY_SEGMENT');
}
if (segment.length > 63) {
return Errors.code('DOMAIN_LONG_SEGMENT');
}
if (i < segments.length - 1) {
if (!internals.domainSegmentRx.test(segment)) {
return Errors.code('DOMAIN_INVALID_CHARS');
}
}
else {
if (!internals.tldSegmentRx.test(segment)) {
return Errors.code('DOMAIN_INVALID_TLDS_CHARS');
}
}
}
return null;
};
exports.isValid = function (domain, options) {
return !exports.analyze(domain, options);
};
internals.punycode = function (domain) {
try {
return new internals.URL(`http://${domain}`).host;
}
catch (err) {
return domain;
}
};
'use strict';
const Util = require('util');
const Domain = require('./domain');
const Errors = require('./errors');
const internals = {
nonAsciiRx: /[^\x00-\x7f]/,
encoder: new (Util.TextEncoder || TextEncoder)() // $lab:coverage:ignore$
};
exports.analyze = function (email, options) {
return internals.email(email, options);
};
exports.isValid = function (email, options) {
return !internals.email(email, options);
};
internals.email = function (email, options = {}) {
if (typeof email !== 'string') {
throw new Error('Invalid input: email must be a string');
}
if (!email) {
return Errors.code('EMPTY_STRING');
}
// Unicode
const ascii = !internals.nonAsciiRx.test(email);
if (!ascii) {
if (options.allowUnicode === false) { // Defaults to true
return Errors.code('FORBIDDEN_UNICODE');
}
email = email.normalize('NFC');
}
// Basic structure
const parts = email.split('@');
if (parts.length !== 2) {
return parts.length > 2 ? Errors.code('MULTIPLE_AT_CHAR') : Errors.code('MISSING_AT_CHAR');
}
const [local, domain] = parts;
if (!local) {
return Errors.code('EMPTY_LOCAL');
}
if (!options.ignoreLength) {
if (email.length > 254) { // http://tools.ietf.org/html/rfc5321#section-4.5.3.1.3
return Errors.code('ADDRESS_TOO_LONG');
}
if (internals.encoder.encode(local).length > 64) { // http://tools.ietf.org/html/rfc5321#section-4.5.3.1.1
return Errors.code('LOCAL_TOO_LONG');
}
}
// Validate parts
return internals.local(local, ascii) || Domain.analyze(domain, options);
};
internals.local = function (local, ascii) {
const segments = local.split('.');
for (const segment of segments) {
if (!segment.length) {
return Errors.code('EMPTY_LOCAL_SEGMENT');
}
if (ascii) {
if (!internals.atextRx.test(segment)) {
return Errors.code('INVALID_LOCAL_CHARS');
}
continue;
}
for (const char of segment) {
if (internals.atextRx.test(char)) {
continue;
}
const binary = internals.binary(char);
if (!internals.atomRx.test(binary)) {
return Errors.code('INVALID_LOCAL_CHARS');
}
}
}
};
internals.binary = function (char) {
return Array.from(internals.encoder.encode(char)).map((v) => String.fromCharCode(v)).join('');
};
/*
From RFC 5321:
Mailbox = Local-part "@" ( Domain / address-literal )
Local-part = Dot-string / Quoted-string
Dot-string = Atom *("." Atom)
Atom = 1*atext
atext = ALPHA / DIGIT / "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "/" / "=" / "?" / "^" / "_" / "`" / "{" / "|" / "}" / "~"
Domain = sub-domain *("." sub-domain)
sub-domain = Let-dig [Ldh-str]
Let-dig = ALPHA / DIGIT
Ldh-str = *( ALPHA / DIGIT / "-" ) Let-dig
ALPHA = %x41-5A / %x61-7A ; a-z, A-Z
DIGIT = %x30-39 ; 0-9
From RFC 6531:
sub-domain =/ U-label
atext =/ UTF8-non-ascii
UTF8-non-ascii = UTF8-2 / UTF8-3 / UTF8-4
UTF8-2 = %xC2-DF UTF8-tail
UTF8-3 = %xE0 %xA0-BF UTF8-tail /
%xE1-EC 2( UTF8-tail ) /
%xED %x80-9F UTF8-tail /
%xEE-EF 2( UTF8-tail )
UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) /
%xF1-F3 3( UTF8-tail ) /
%xF4 %x80-8F 2( UTF8-tail )
UTF8-tail = %x80-BF
Note: The following are not supported:
RFC 5321: address-literal, Quoted-string
RFC 5322: obs-*, CFWS
*/
internals.atextRx = /^[\w!#\$%&'\*\+\-/=\?\^`\{\|\}~]+$/; // _ included in \w
internals.atomRx = new RegExp([
// %xC2-DF UTF8-tail
'(?:[\\xc2-\\xdf][\\x80-\\xbf])',
// %xE0 %xA0-BF UTF8-tail %xE1-EC 2( UTF8-tail ) %xED %x80-9F UTF8-tail %xEE-EF 2( UTF8-tail )
'(?:\\xe0[\\xa0-\\xbf][\\x80-\\xbf])|(?:[\\xe1-\\xec][\\x80-\\xbf]{2})|(?:\\xed[\\x80-\\x9f][\\x80-\\xbf])|(?:[\\xee-\\xef][\\x80-\\xbf]{2})',
// %xF0 %x90-BF 2( UTF8-tail ) %xF1-F3 3( UTF8-tail ) %xF4 %x80-8F 2( UTF8-tail )
'(?:\\xf0[\\x90-\\xbf][\\x80-\\xbf]{2})|(?:[\\xf1-\\xf3][\\x80-\\xbf]{3})|(?:\\xf4[\\x80-\\x8f][\\x80-\\xbf]{2})'
].join('|'));
'use strict';
exports.codes = {
EMPTY_STRING: 'Address must be a non-empty string',
FORBIDDEN_UNICODE: 'Address contains forbidden Unicode characters',
MULTIPLE_AT_CHAR: 'Address cannot contain more than one @ character',
MISSING_AT_CHAR: 'Address must contain one @ character',
EMPTY_LOCAL: 'Address local part cannot be empty',
ADDRESS_TOO_LONG: 'Address too long',
LOCAL_TOO_LONG: 'Address local part too long',
EMPTY_LOCAL_SEGMENT: 'Address local part contains empty dot-separated segment',
INVALID_LOCAL_CHARS: 'Address local part contains invalid character',
DOMAIN_NON_EMPTY_STRING: 'Domain must be a non-empty string',
DOMAIN_TOO_LONG: 'Domain too long',
DOMAIN_INVALID_UNICODE_CHARS: 'Domain contains forbidden Unicode characters',
DOMAIN_INVALID_CHARS: 'Domain contains invalid character',
DOMAIN_INVALID_TLDS_CHARS: 'Domain contains invalid tld character',
DOMAIN_SEGMENTS_COUNT: 'Domain lacks the minimum required number of segments',
DOMAIN_SEGMENTS_COUNT_MAX: 'Domain contains too many segments',
DOMAIN_FORBIDDEN_TLDS: 'Domain uses forbidden TLD',
DOMAIN_EMPTY_SEGMENT: 'Domain contains empty dot-separated segment',
DOMAIN_LONG_SEGMENT: 'Domain contains dot-separated segment that is too long'
};
exports.code = function (code) {
return { code, error: exports.codes[code] };
};
/// <reference types="node" />
import * as Hoek from '@hapi/hoek';
export namespace domain {
/**
* Analyzes a string to verify it is a valid domain name.
*
* @param domain - the domain name to validate.
* @param options - optional settings.
*
* @return - undefined when valid, otherwise an object with single error key with a string message value.
*/
function analyze(domain: string, options?: Options): Analysis | null;
/**
* Analyzes a string to verify it is a valid domain name.
*
* @param domain - the domain name to validate.
* @param options - optional settings.
*
* @return - true when valid, otherwise false.
*/
function isValid(domain: string, options?: Options): boolean;
interface Options {
/**
* Determines whether Unicode characters are allowed.
*
* @default true
*/
readonly allowUnicode?: boolean;
/**
* The minimum number of domain segments (e.g. `x.y.z` has 3 segments) required.
*
* @default 2
*/
readonly minDomainSegments?: number;
/**
* Top-level-domain options
*
* @default true
*/
readonly tlds?: Tlds.Allow | Tlds.Deny | boolean;
}
namespace Tlds {
interface Allow {
readonly allow: Set<string> | true;
}
interface Deny {
readonly deny: Set<string>;
}
}
}
export namespace email {
/**
* Analyzes a string to verify it is a valid email address.
*
* @param email - the email address to validate.
* @param options - optional settings.
*
* @return - undefined when valid, otherwise an object with single error key with a string message value.
*/
function analyze(email: string, options?: Options): Analysis | null;
/**
* Analyzes a string to verify it is a valid email address.
*
* @param email - the email address to validate.
* @param options - optional settings.
*
* @return - true when valid, otherwise false.
*/
function isValid(email: string, options?: Options): boolean;
interface Options extends domain.Options {
/**
* Determines whether to ignore the standards maximum email length limit.
*
* @default false
*/
readonly ignoreLength?: boolean;
}
}
export interface Analysis {
/**
* The reason validation failed.
*/
error: string;
/**
* The error code.
*/
code: string;
}
export const errors: Record<string, string>;
export namespace ip {
/**
* Generates a regular expression used to validate IP addresses.
*
* @param options - optional settings.
*
* @returns an object with the regular expression and meta data.
*/
function regex(options?: Options): Expression;
interface Options {
/**
* The required CIDR mode.
*
* @default 'optional'
*/
readonly cidr?: Cidr;
/**
* The allowed versions.
*
* @default ['ipv4', 'ipv6', 'ipvfuture']
*/
readonly version?: Version | Version[];
}
type Cidr = 'optional' | 'required' | 'forbidden';
type Version = 'ipv4' | 'ipv6' | 'ipvfuture';
interface Expression {
/**
* The CIDR mode.
*/
cidr: Cidr;
/**
* The raw regular expression string.
*/
raw: string;
/**
* The regular expression.
*/
regex: RegExp;
/**
* The array of versions allowed.
*/
versions: Version[];
}
}
export namespace uri {
/**
* Faster version of decodeURIComponent() that does not throw.
*
* @param string - the URL string to decode.
*
* @returns the decoded string or null if invalid.
*/
function decode(string: string): string | null;
/**
* Generates a regular expression used to validate URI addresses.
*
* @param options - optional settings.
*
* @returns an object with the regular expression and meta data.
*/
function regex(options?: Options): Expression;
type Options = Hoek.ts.XOR<Options.Options, Options.Relative>;
namespace Options {
interface Query {
/**
* Allow the use of [] in query parameters.
*
* @default false
*/
readonly allowQuerySquareBrackets?: boolean;
}
interface Relative extends Query {
/**
* Requires the URI to be relative.
*
* @default false
*/
readonly relativeOnly?: boolean;
}
interface Options extends Query {
/**
* Allow relative URIs.
*
* @default false
*/
readonly allowRelative?: boolean;
/**
* Capture domain segment ($1).
*
* @default false
*/
readonly domain?: boolean;
/**
* The allowed URI schemes.
*/
readonly scheme?: Scheme | Scheme[];
}
type Scheme = string | RegExp;
}
interface Expression {
/**
* The raw regular expression string.
*/
raw: string;
/**
* The regular expression.
*/
regex: RegExp;
}
}
'use strict';
const Decode = require('./decode');
const Domain = require('./domain');
const Email = require('./email');
const Errors = require('./errors');
const Ip = require('./ip');
const Tlds = require('./tlds');
const Uri = require('./uri');
const internals = {
defaultTlds: { allow: Tlds, deny: null }
};
module.exports = {
errors: Errors.codes,
domain: {
analyze(domain, options) {
options = internals.options(options);
return Domain.analyze(domain, options);
},
isValid(domain, options) {
options = internals.options(options);
return Domain.isValid(domain, options);
}
},
email: {
analyze(email, options) {
options = internals.options(options);
return Email.analyze(email, options);
},
isValid(email, options) {
options = internals.options(options);
return Email.isValid(email, options);
}
},
ip: {
regex: Ip.regex
},
uri: {
decode: Decode.decode,
regex: Uri.regex
}
};
internals.options = function (options) {
if (!options) {
return { tlds: internals.defaultTlds };
}
if (options.tlds === false) { // Defaults to true
return options;
}
if (!options.tlds ||
options.tlds === true) {
return Object.assign({}, options, { tlds: internals.defaultTlds });
}
if (typeof options.tlds !== 'object') {
throw new Error('Invalid options: tlds must be a boolean or an object');
}
if (options.tlds.deny) {
if (options.tlds.deny instanceof Set === false) {
throw new Error('Invalid options: tlds.deny must be a Set object');
}
if (options.tlds.allow) {
throw new Error('Invalid options: cannot specify both tlds.allow and tlds.deny lists');
}
return options;
}
if (options.tlds.allow === true) {
return Object.assign({}, options, { tlds: internals.defaultTlds });
}
if (options.tlds.allow instanceof Set === false) {
throw new Error('Invalid options: tlds.allow must be a Set object or true');
}
return options;
};
'use strict';
const Assert = require('@hapi/hoek/lib/assert');
const Uri = require('./uri');
const internals = {};
exports.regex = function (options = {}) {
// CIDR
Assert(options.cidr === undefined || typeof options.cidr === 'string', 'options.cidr must be a string');
const cidr = options.cidr ? options.cidr.toLowerCase() : 'optional';
Assert(['required', 'optional', 'forbidden'].includes(cidr), 'options.cidr must be one of required, optional, forbidden');
// Versions
Assert(options.version === undefined || typeof options.version === 'string' || Array.isArray(options.version), 'options.version must be a string or an array of string');
let versions = options.version || ['ipv4', 'ipv6', 'ipvfuture'];
if (!Array.isArray(versions)) {
versions = [versions];
}
Assert(versions.length >= 1, 'options.version must have at least 1 version specified');
for (let i = 0; i < versions.length; ++i) {
Assert(typeof versions[i] === 'string', 'options.version must only contain strings');
versions[i] = versions[i].toLowerCase();
Assert(['ipv4', 'ipv6', 'ipvfuture'].includes(versions[i]), 'options.version contains unknown version ' + versions[i] + ' - must be one of ipv4, ipv6, ipvfuture');
}
versions = Array.from(new Set(versions));
// Regex
const parts = versions.map((version) => {
// Forbidden
if (cidr === 'forbidden') {
return Uri.ip[version];
}
// Required
const cidrpart = `\\/${version === 'ipv4' ? Uri.ip.v4Cidr : Uri.ip.v6Cidr}`;
if (cidr === 'required') {
return `${Uri.ip[version]}${cidrpart}`;
}
// Optional
return `${Uri.ip[version]}(?:${cidrpart})?`;
});
const raw = `(?:${parts.join('|')})`;
const regex = new RegExp(`^${raw}$`);
return { cidr, versions, regex, raw };
};
'use strict';
const Assert = require('@hapi/hoek/lib/assert');
const EscapeRegex = require('@hapi/hoek/lib/escapeRegex');
const internals = {};
internals.generate = function () {
const rfc3986 = {};
const hexDigit = '\\dA-Fa-f'; // HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
const hexDigitOnly = '[' + hexDigit + ']';
const unreserved = '\\w-\\.~'; // unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
const subDelims = '!\\$&\'\\(\\)\\*\\+,;='; // sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
const pctEncoded = '%' + hexDigit; // pct-encoded = "%" HEXDIG HEXDIG
const pchar = unreserved + pctEncoded + subDelims + ':@'; // pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
const pcharOnly = '[' + pchar + ']';
const decOctect = '(?:0{0,2}\\d|0?[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])'; // dec-octet = DIGIT / %x31-39 DIGIT / "1" 2DIGIT / "2" %x30-34 DIGIT / "25" %x30-35 ; 0-9 / 10-99 / 100-199 / 200-249 / 250-255
rfc3986.ipv4address = '(?:' + decOctect + '\\.){3}' + decOctect; // IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
/*
h16 = 1*4HEXDIG ; 16 bits of address represented in hexadecimal
ls32 = ( h16 ":" h16 ) / IPv4address ; least-significant 32 bits of address
IPv6address = 6( h16 ":" ) ls32
/ "::" 5( h16 ":" ) ls32
/ [ h16 ] "::" 4( h16 ":" ) ls32
/ [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
/ [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
/ [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
/ [ *4( h16 ":" ) h16 ] "::" ls32
/ [ *5( h16 ":" ) h16 ] "::" h16
/ [ *6( h16 ":" ) h16 ] "::"
*/
const h16 = hexDigitOnly + '{1,4}';
const ls32 = '(?:' + h16 + ':' + h16 + '|' + rfc3986.ipv4address + ')';
const IPv6SixHex = '(?:' + h16 + ':){6}' + ls32;
const IPv6FiveHex = '::(?:' + h16 + ':){5}' + ls32;
const IPv6FourHex = '(?:' + h16 + ')?::(?:' + h16 + ':){4}' + ls32;
const IPv6ThreeHex = '(?:(?:' + h16 + ':){0,1}' + h16 + ')?::(?:' + h16 + ':){3}' + ls32;
const IPv6TwoHex = '(?:(?:' + h16 + ':){0,2}' + h16 + ')?::(?:' + h16 + ':){2}' + ls32;
const IPv6OneHex = '(?:(?:' + h16 + ':){0,3}' + h16 + ')?::' + h16 + ':' + ls32;
const IPv6NoneHex = '(?:(?:' + h16 + ':){0,4}' + h16 + ')?::' + ls32;
const IPv6NoneHex2 = '(?:(?:' + h16 + ':){0,5}' + h16 + ')?::' + h16;
const IPv6NoneHex3 = '(?:(?:' + h16 + ':){0,6}' + h16 + ')?::';
rfc3986.ipv4Cidr = '(?:\\d|[1-2]\\d|3[0-2])'; // IPv4 cidr = DIGIT / %x31-32 DIGIT / "3" %x30-32 ; 0-9 / 10-29 / 30-32
rfc3986.ipv6Cidr = '(?:0{0,2}\\d|0?[1-9]\\d|1[01]\\d|12[0-8])'; // IPv6 cidr = DIGIT / %x31-39 DIGIT / "1" %x0-1 DIGIT / "12" %x0-8; 0-9 / 10-99 / 100-119 / 120-128
rfc3986.ipv6address = '(?:' + IPv6SixHex + '|' + IPv6FiveHex + '|' + IPv6FourHex + '|' + IPv6ThreeHex + '|' + IPv6TwoHex + '|' + IPv6OneHex + '|' + IPv6NoneHex + '|' + IPv6NoneHex2 + '|' + IPv6NoneHex3 + ')';
rfc3986.ipvFuture = 'v' + hexDigitOnly + '+\\.[' + unreserved + subDelims + ':]+'; // IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
rfc3986.scheme = '[a-zA-Z][a-zA-Z\\d+-\\.]*'; // scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
rfc3986.schemeRegex = new RegExp(rfc3986.scheme);
const userinfo = '[' + unreserved + pctEncoded + subDelims + ':]*'; // userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
const IPLiteral = '\\[(?:' + rfc3986.ipv6address + '|' + rfc3986.ipvFuture + ')\\]'; // IP-literal = "[" ( IPv6address / IPvFuture ) "]"
const regName = '[' + unreserved + pctEncoded + subDelims + ']{1,255}'; // reg-name = *( unreserved / pct-encoded / sub-delims )
const host = '(?:' + IPLiteral + '|' + rfc3986.ipv4address + '|' + regName + ')'; // host = IP-literal / IPv4address / reg-name
const port = '\\d*'; // port = *DIGIT
const authority = '(?:' + userinfo + '@)?' + host + '(?::' + port + ')?'; // authority = [ userinfo "@" ] host [ ":" port ]
const authorityCapture = '(?:' + userinfo + '@)?(' + host + ')(?::' + port + ')?';
/*
segment = *pchar
segment-nz = 1*pchar
path = path-abempty ; begins with "/" '|' is empty
/ path-absolute ; begins with "/" but not "//"
/ path-noscheme ; begins with a non-colon segment
/ path-rootless ; begins with a segment
/ path-empty ; zero characters
path-abempty = *( "/" segment )
path-absolute = "/" [ segment-nz *( "/" segment ) ]
path-rootless = segment-nz *( "/" segment )
*/
const segment = pcharOnly + '*';
const segmentNz = pcharOnly + '+';
const segmentNzNc = '[' + unreserved + pctEncoded + subDelims + '@' + ']+';
const pathEmpty = '';
const pathAbEmpty = '(?:\\/' + segment + ')*';
const pathAbsolute = '\\/(?:' + segmentNz + pathAbEmpty + ')?';
const pathRootless = segmentNz + pathAbEmpty;
const pathNoScheme = segmentNzNc + pathAbEmpty;
const pathAbNoAuthority = '(?:\\/\\/\\/' + segment + pathAbEmpty + ')'; // Used by file:///
// hier-part = "//" authority path
rfc3986.hierPart = '(?:' + '(?:\\/\\/' + authority + pathAbEmpty + ')' + '|' + pathAbsolute + '|' + pathRootless + '|' + pathAbNoAuthority + ')';
rfc3986.hierPartCapture = '(?:' + '(?:\\/\\/' + authorityCapture + pathAbEmpty + ')' + '|' + pathAbsolute + '|' + pathRootless + ')';
// relative-part = "//" authority path-abempty / path-absolute / path-noscheme / path-empty
rfc3986.relativeRef = '(?:' + '(?:\\/\\/' + authority + pathAbEmpty + ')' + '|' + pathAbsolute + '|' + pathNoScheme + '|' + pathEmpty + ')';
rfc3986.relativeRefCapture = '(?:' + '(?:\\/\\/' + authorityCapture + pathAbEmpty + ')' + '|' + pathAbsolute + '|' + pathNoScheme + '|' + pathEmpty + ')';
// query = *( pchar / "/" / "?" )
// query = *( pchar / "[" / "]" / "/" / "?" )
rfc3986.query = '[' + pchar + '\\/\\?]*(?=#|$)'; //Finish matching either at the fragment part '|' end of the line.
rfc3986.queryWithSquareBrackets = '[' + pchar + '\\[\\]\\/\\?]*(?=#|$)';
// fragment = *( pchar / "/" / "?" )
rfc3986.fragment = '[' + pchar + '\\/\\?]*';
return rfc3986;
};
internals.rfc3986 = internals.generate();
exports.ip = {
v4Cidr: internals.rfc3986.ipv4Cidr,
v6Cidr: internals.rfc3986.ipv6Cidr,
ipv4: internals.rfc3986.ipv4address,
ipv6: internals.rfc3986.ipv6address,
ipvfuture: internals.rfc3986.ipvFuture
};
internals.createRegex = function (options) {
const rfc = internals.rfc3986;
// Construct expression
const query = options.allowQuerySquareBrackets ? rfc.queryWithSquareBrackets : rfc.query;
const suffix = '(?:\\?' + query + ')?' + '(?:#' + rfc.fragment + ')?';
// relative-ref = relative-part [ "?" query ] [ "#" fragment ]
const relative = options.domain ? rfc.relativeRefCapture : rfc.relativeRef;
if (options.relativeOnly) {
return internals.wrap(relative + suffix);
}
// Custom schemes
let customScheme = '';
if (options.scheme) {
Assert(options.scheme instanceof RegExp || typeof options.scheme === 'string' || Array.isArray(options.scheme), 'scheme must be a RegExp, String, or Array');
const schemes = [].concat(options.scheme);
Assert(schemes.length >= 1, 'scheme must have at least 1 scheme specified');
// Flatten the array into a string to be used to match the schemes
const selections = [];
for (let i = 0; i < schemes.length; ++i) {
const scheme = schemes[i];
Assert(scheme instanceof RegExp || typeof scheme === 'string', 'scheme at position ' + i + ' must be a RegExp or String');
if (scheme instanceof RegExp) {
selections.push(scheme.source.toString());
}
else {
Assert(rfc.schemeRegex.test(scheme), 'scheme at position ' + i + ' must be a valid scheme');
selections.push(EscapeRegex(scheme));
}
}
customScheme = selections.join('|');
}
// URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
const scheme = customScheme ? '(?:' + customScheme + ')' : rfc.scheme;
const absolute = '(?:' + scheme + ':' + (options.domain ? rfc.hierPartCapture : rfc.hierPart) + ')';
const prefix = options.allowRelative ? '(?:' + absolute + '|' + relative + ')' : absolute;
return internals.wrap(prefix + suffix, customScheme);
};
internals.wrap = function (raw, scheme) {
raw = `(?=.)(?!https?\:/$)${raw}`; // Require at least one character and explicitly forbid 'http:/'
return {
raw,
regex: new RegExp(`^${raw}$`),
scheme
};
};
internals.uriRegex = internals.createRegex({});
exports.regex = function (options = {}) {
if (options.scheme ||
options.allowRelative ||
options.relativeOnly ||
options.allowQuerySquareBrackets ||
options.domain) {
return internals.createRegex(options);
}
return internals.uriRegex;
};
{
"_from": "@hapi/address@^4.0.1",
"_id": "@hapi/address@4.1.0",
"_inBundle": false,
"_integrity": "sha512-SkszZf13HVgGmChdHo/PxchnSaCJ6cetVqLzyciudzZRT0jcOouIF/Q93mgjw8cce+D+4F4C1Z/WrfFN+O3VHQ==",
"_location": "/@hapi/address",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "@hapi/address@^4.0.1",
"name": "@hapi/address",
"escapedName": "@hapi%2faddress",
"scope": "@hapi",
"rawSpec": "^4.0.1",
"saveSpec": null,
"fetchSpec": "^4.0.1"
},
"_requiredBy": [
"/@hapi/joi"
],
"_resolved": "https://registry.npmjs.org/@hapi/address/-/address-4.1.0.tgz",
"_shasum": "d60c5c0d930e77456fdcde2598e77302e2955e1d",
"_spec": "@hapi/address@^4.0.1",
"_where": "/Users/admin/Downloads/NEXPIE2/cb/node_modules/@hapi/joi",
"bugs": {
"url": "https://github.com/hapijs/address/issues"
},
"bundleDependencies": false,
"dependencies": {
"@hapi/hoek": "^9.0.0"
},
"deprecated": false,
"description": "Email address and domain validation",
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/lab": "22.x.x"
},
"files": [
"lib"
],
"homepage": "https://github.com/hapijs/address#readme",
"keywords": [
"email",
"domain",
"address",
"validation"
],
"license": "BSD-3-Clause",
"main": "lib/index.js",
"name": "@hapi/address",
"repository": {
"type": "git",
"url": "git://github.com/hapijs/address.git"
},
"scripts": {
"test": "lab -a @hapi/code -t 100 -L -Y",
"test-cov-html": "lab -a @hapi/code -t 100 -L -r html -o coverage.html"
},
"types": "lib/index.d.ts",
"version": "4.1.0"
}
Copyright (c) 2012-2020, Sideway Inc, and project contributors
Copyright (c) 2012-2014, Walmart.
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 OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<a href="https://hapi.dev"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a>
# @hapi/boom
#### HTTP-friendly error objects.
**boom** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) – they work even better together.
### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support
## Useful resources
- [Documentation and API](https://hapi.dev/family/boom/)
- [Version status](https://hapi.dev/resources/status/#boom) (builds, dependencies, node versions, licenses, eol)
- [Changelog](https://hapi.dev/family/boom/changelog/)
- [Project policies](https://hapi.dev/policies/)
- [Free and commercial support options](https://hapi.dev/support/)
{
"_from": "@hapi/boom@9.x.x",
"_id": "@hapi/boom@9.1.0",
"_inBundle": false,
"_integrity": "sha512-4nZmpp4tXbm162LaZT45P7F7sgiem8dwAh2vHWT6XX24dozNjGMg6BvKCRvtCUcmcXqeMIUqWN8Rc5X8yKuROQ==",
"_location": "/@hapi/boom",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "@hapi/boom@9.x.x",
"name": "@hapi/boom",
"escapedName": "@hapi%2fboom",
"scope": "@hapi",
"rawSpec": "9.x.x",
"saveSpec": null,
"fetchSpec": "9.x.x"
},
"_requiredBy": [
"/@hapi/wreck"
],
"_resolved": "https://registry.npmjs.org/@hapi/boom/-/boom-9.1.0.tgz",
"_shasum": "0d9517657a56ff1e0b42d0aca9da1b37706fec56",
"_spec": "@hapi/boom@9.x.x",
"_where": "/Users/admin/Downloads/NEXPIE2/cb/node_modules/@hapi/wreck",
"bugs": {
"url": "https://github.com/hapijs/boom/issues"
},
"bundleDependencies": false,
"dependencies": {
"@hapi/hoek": "9.x.x"
},
"deprecated": false,
"description": "HTTP-friendly error objects",
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/lab": "22.x.x"
},
"files": [
"lib"
],
"homepage": "https://github.com/hapijs/boom#readme",
"keywords": [
"error",
"http"
],
"license": "BSD-3-Clause",
"main": "lib/index.js",
"name": "@hapi/boom",
"repository": {
"type": "git",
"url": "git://github.com/hapijs/boom.git"
},
"scripts": {
"test": "lab -a @hapi/code -t 100 -L -Y",
"test-cov-html": "lab -a @hapi/code -t 100 -L -r html -o coverage.html"
},
"types": "lib/index.d.ts",
"version": "9.1.0"
}
Copyright (c) 2019-2020, Sideway Inc, and project 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 OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<a href="https://hapi.dev"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a>
# @hapi/bourne
#### JSON.parse() drop-in replacement with prototype poisoning protection.
**bourne** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) – they work even better together.
### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support
## Useful resources
- [Documentation and API](https://hapi.dev/family/bourne/)
- [Version status](https://hapi.dev/resources/status/#bourne) (builds, dependencies, node versions, licenses, eol)
- [Changelog](https://hapi.dev/family/bourne/changelog/)
- [Project policies](https://hapi.dev/policies/)
- [Free and commercial support options](https://hapi.dev/support/)
'use strict';
const internals = {
suspectRx: /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*\:/
};
exports.parse = function (text, ...args) {
// Normalize arguments
const firstOptions = typeof args[0] === 'object' && args[0];
const reviver = args.length > 1 || !firstOptions ? args[0] : undefined;
const options = (args.length > 1 && args[1]) || firstOptions || {};
// Parse normally, allowing exceptions
const obj = JSON.parse(text, reviver);
// options.protoAction: 'error' (default) / 'remove' / 'ignore'
if (options.protoAction === 'ignore') {
return obj;
}
// Ignore null and non-objects
if (!obj ||
typeof obj !== 'object') {
return obj;
}
// Check original string for potential exploit
if (!text.match(internals.suspectRx)) {
return obj;
}
// Scan result for proto keys
exports.scan(obj, options);
return obj;
};
exports.scan = function (obj, options = {}) {
let next = [obj];
while (next.length) {
const nodes = next;
next = [];
for (const node of nodes) {
if (Object.prototype.hasOwnProperty.call(node, '__proto__')) { // Avoid calling node.hasOwnProperty directly
if (options.protoAction !== 'remove') {
throw new SyntaxError('Object contains forbidden prototype property');
}
delete node.__proto__;
}
for (const key in node) {
const value = node[key];
if (value &&
typeof value === 'object') {
next.push(node[key]);
}
}
}
}
};
exports.safeParse = function (text, reviver) {
try {
return exports.parse(text, reviver);
}
catch (ignoreError) {
return null;
}
};
{
"_from": "@hapi/bourne@2.x.x",
"_id": "@hapi/bourne@2.0.0",
"_inBundle": false,
"_integrity": "sha512-WEezM1FWztfbzqIUbsDzFRVMxSoLy3HugVcux6KDDtTqzPsLE8NDRHfXvev66aH1i2oOKKar3/XDjbvh/OUBdg==",
"_location": "/@hapi/bourne",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "@hapi/bourne@2.x.x",
"name": "@hapi/bourne",
"escapedName": "@hapi%2fbourne",
"scope": "@hapi",
"rawSpec": "2.x.x",
"saveSpec": null,
"fetchSpec": "2.x.x"
},
"_requiredBy": [
"/@hapi/wreck"
],
"_resolved": "https://registry.npmjs.org/@hapi/bourne/-/bourne-2.0.0.tgz",
"_shasum": "5bb2193eb685c0007540ca61d166d4e1edaf918d",
"_spec": "@hapi/bourne@2.x.x",
"_where": "/Users/admin/Downloads/NEXPIE2/cb/node_modules/@hapi/wreck",
"bugs": {
"url": "https://github.com/hapijs/bourne/issues"
},
"bundleDependencies": false,
"dependencies": {},
"deprecated": false,
"description": "JSON parse with prototype poisoning protection",
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/lab": "22.x.x",
"benchmark": "2.x.x"
},
"files": [
"lib"
],
"homepage": "https://github.com/hapijs/bourne#readme",
"keywords": [
"JSON",
"parse",
"safe",
"prototype"
],
"license": "BSD-3-Clause",
"main": "lib/index.js",
"name": "@hapi/bourne",
"repository": {
"type": "git",
"url": "git://github.com/hapijs/bourne.git"
},
"scripts": {
"test": "lab -a @hapi/code -t 100 -L",
"test-cov-html": "lab -a @hapi/code -r html -o coverage.html"
},
"version": "2.0.0"
}
Breaking changes are documented using GitHub issues, see [issues labeled "release notes"](https://github.com/hapijs/formula/issues?q=is%3Aissue+label%3A%22release+notes%22).
If you want changes of a specific minor or patch release, you can browse the [GitHub milestones](https://github.com/hapijs/formula/milestones?state=closed&direction=asc&sort=due_date).
Copyright (c) 2019, Project 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.
<a href="https://hapi.dev"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a>
# @hapi/formula
#### Math and string formula parser.
**formula** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) they work even better together.
### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support
## Useful resources
- [Documentation and API](https://hapi.dev/family/formula/)
- [Version status](https://hapi.dev/resources/status/#formula) (builds, dependencies, node versions, licenses, eol)
- [Project policies](https://hapi.dev/policies/)
- [Free and commercial support options](https://hapi.dev/support/)
## Acknowledgements
Inspired by [**fparse**](https://github.com/bylexus/fparse), copyright 2012-2018 Alexander Schenkel <alex@alexi.ch>
/**
* Formula parser
*/
export class Parser<T extends (string | number)> {
/**
* Create a new formula parser.
*
* @param formula - the formula string to parse.
* @param options - optional settings.
*/
constructor(formula: string, options?: Options);
/**
* Evaluate the formula.
*
* @param context - optional object with runtime formula context used to resolve variables.
*
* @returns the string or number outcome of the resolved formula.
*/
evaluate(context?: any): T;
}
export interface Options {
/**
* A hash of key - value pairs used to convert constants to values.
*/
readonly constants?: Record<string, any>;
/**
* A regular expression used to validate token variables.
*/
readonly tokenRx?: RegExp;
/**
* A variable resolver factory function.
*/
readonly reference?: Options.Reference;
/**
* A hash of key-value pairs used to resolve formula functions.
*/
readonly functions?: Record<string, Function>;
}
export namespace Options {
type Reference = (name: string) => (context: any) => any;
}
{
"_from": "@hapi/formula@^2.0.0",
"_id": "@hapi/formula@2.0.0",
"_inBundle": false,
"_integrity": "sha512-V87P8fv7PI0LH7LiVi8Lkf3x+KCO7pQozXRssAHNXXL9L1K+uyu4XypLXwxqVDKgyQai6qj3/KteNlrqDx4W5A==",
"_location": "/@hapi/formula",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "@hapi/formula@^2.0.0",
"name": "@hapi/formula",
"escapedName": "@hapi%2fformula",
"scope": "@hapi",
"rawSpec": "^2.0.0",
"saveSpec": null,
"fetchSpec": "^2.0.0"
},
"_requiredBy": [
"/@hapi/joi"
],
"_resolved": "https://registry.npmjs.org/@hapi/formula/-/formula-2.0.0.tgz",
"_shasum": "edade0619ed58c8e4f164f233cda70211e787128",
"_spec": "@hapi/formula@^2.0.0",
"_where": "/Users/admin/Downloads/NEXPIE2/cb/node_modules/@hapi/joi",
"bugs": {
"url": "https://github.com/hapijs/formula/issues"
},
"bundleDependencies": false,
"dependencies": {},
"deprecated": false,
"description": "Math and string formula parser.",
"devDependencies": {
"@hapi/code": "6.x.x",
"@hapi/lab": "20.x.x"
},
"files": [
"lib"
],
"homepage": "https://github.com/hapijs/formula#readme",
"keywords": [
"formula",
"parser",
"math",
"string"
],
"license": "BSD-3-Clause",
"main": "lib/index.js",
"name": "@hapi/formula",
"repository": {
"type": "git",
"url": "git://github.com/hapijs/formula.git"
},
"scripts": {
"test": "lab -a @hapi/code -t 100 -L -Y",
"test-cov-html": "lab -a @hapi/code -t 100 -L -r html -o coverage.html"
},
"types": "lib/index.d.ts",
"version": "2.0.0"
}
Copyright (c) 2011-2020, Sideway Inc, and project contributors
Copyright (c) 2011-2014, Walmart
Copyright (c) 2011, Yahoo Inc.
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 OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<a href="https://hapi.dev"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a>
# @hapi/hoek
#### Utility methods for the hapi ecosystem.
**hoek** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) – they work even better together.
This module is not intended to solve every problem for everyone, but rather as a central place to store hapi-specific methods. If you're looking for a general purpose utility module, check out [lodash](https://github.com/lodash/lodash).
### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support
## Useful resources
- [Documentation and API](https://hapi.dev/family/hoek/)
- [Version status](https://hapi.dev/resources/status/#hoek) (builds, dependencies, node versions, licenses, eol)
- [Changelog](https://hapi.dev/family/hoek/changelog/)
- [Project policies](https://hapi.dev/policies/)
- [Free and commercial support options](https://hapi.dev/support/)
'use strict';
const Assert = require('./assert');
const Clone = require('./clone');
const Merge = require('./merge');
const Reach = require('./reach');
const internals = {};
module.exports = function (defaults, source, options = {}) {
Assert(defaults && typeof defaults === 'object', 'Invalid defaults value: must be an object');
Assert(!source || source === true || typeof source === 'object', 'Invalid source value: must be true, falsy or an object');
Assert(typeof options === 'object', 'Invalid options: must be an object');
if (!source) { // If no source, return null
return null;
}
if (options.shallow) {
return internals.applyToDefaultsWithShallow(defaults, source, options);
}
const copy = Clone(defaults);
if (source === true) { // If source is set to true, use defaults
return copy;
}
const nullOverride = options.nullOverride !== undefined ? options.nullOverride : false;
return Merge(copy, source, { nullOverride, mergeArrays: false });
};
internals.applyToDefaultsWithShallow = function (defaults, source, options) {
const keys = options.shallow;
Assert(Array.isArray(keys), 'Invalid keys');
const seen = new Map();
const merge = source === true ? null : new Set();
for (let key of keys) {
key = Array.isArray(key) ? key : key.split('.'); // Pre-split optimization
const ref = Reach(defaults, key);
if (ref &&
typeof ref === 'object') {
seen.set(ref, merge && Reach(source, key) || ref);
}
else if (merge) {
merge.add(key);
}
}
const copy = Clone(defaults, {}, seen);
if (!merge) {
return copy;
}
for (const key of merge) {
internals.reachCopy(copy, source, key);
}
return Merge(copy, source, { mergeArrays: false, nullOverride: false });
};
internals.reachCopy = function (dst, src, path) {
for (const segment of path) {
if (!(segment in src)) {
return;
}
src = src[segment];
}
const value = src;
let ref = dst;
for (let i = 0; i < path.length - 1; ++i) {
const segment = path[i];
if (typeof ref[segment] !== 'object') {
ref[segment] = {};
}
ref = ref[segment];
}
ref[path[path.length - 1]] = value;
};
'use strict';
const AssertError = require('./error');
const internals = {};
module.exports = function (condition, ...args) {
if (condition) {
return;
}
if (args.length === 1 &&
args[0] instanceof Error) {
throw args[0];
}
throw new AssertError(args);
};
'use strict';
const internals = {};
module.exports = internals.Bench = class {
constructor() {
this.ts = 0;
this.reset();
}
reset() {
this.ts = internals.Bench.now();
}
elapsed() {
return internals.Bench.now() - this.ts;
}
static now() {
const ts = process.hrtime();
return (ts[0] * 1e3) + (ts[1] / 1e6);
}
};
'use strict';
const Ignore = require('./ignore');
const internals = {};
module.exports = function () {
return new Promise(Ignore); // $lab:coverage:ignore$
};
'use strict';
const Reach = require('./reach');
const Types = require('./types');
const Utils = require('./utils');
const internals = {
needsProtoHack: new Set([Types.set, Types.map, Types.weakSet, Types.weakMap])
};
module.exports = internals.clone = function (obj, options = {}, _seen = null) {
if (typeof obj !== 'object' ||
obj === null) {
return obj;
}
let clone = internals.clone;
let seen = _seen;
if (options.shallow) {
if (options.shallow !== true) {
return internals.cloneWithShallow(obj, options);
}
clone = (value) => value;
}
else if (seen) {
const lookup = seen.get(obj);
if (lookup) {
return lookup;
}
}
else {
seen = new Map();
}
// Built-in object types
const baseProto = Types.getInternalProto(obj);
if (baseProto === Types.buffer) {
return Buffer && Buffer.from(obj); // $lab:coverage:ignore$
}
if (baseProto === Types.date) {
return new Date(obj.getTime());
}
if (baseProto === Types.regex) {
return new RegExp(obj);
}
// Generic objects
const newObj = internals.base(obj, baseProto, options);
if (newObj === obj) {
return obj;
}
if (seen) {
seen.set(obj, newObj); // Set seen, since obj could recurse
}
if (baseProto === Types.set) {
for (const value of obj) {
newObj.add(clone(value, options, seen));
}
}
else if (baseProto === Types.map) {
for (const [key, value] of obj) {
newObj.set(key, clone(value, options, seen));
}
}
const keys = Utils.keys(obj, options);
for (const key of keys) {
if (key === '__proto__') {
continue;
}
if (baseProto === Types.array &&
key === 'length') {
newObj.length = obj.length;
continue;
}
const descriptor = Object.getOwnPropertyDescriptor(obj, key);
if (descriptor) {
if (descriptor.get ||
descriptor.set) {
Object.defineProperty(newObj, key, descriptor);
}
else if (descriptor.enumerable) {
newObj[key] = clone(obj[key], options, seen);
}
else {
Object.defineProperty(newObj, key, { enumerable: false, writable: true, configurable: true, value: clone(obj[key], options, seen) });
}
}
else {
Object.defineProperty(newObj, key, {
enumerable: true,
writable: true,
configurable: true,
value: clone(obj[key], options, seen)
});
}
}
return newObj;
};
internals.cloneWithShallow = function (source, options) {
const keys = options.shallow;
options = Object.assign({}, options);
options.shallow = false;
const seen = new Map();
for (const key of keys) {
const ref = Reach(source, key);
if (typeof ref === 'object' ||
typeof ref === 'function') {
seen.set(ref, ref);
}
}
return internals.clone(source, options, seen);
};
internals.base = function (obj, baseProto, options) {
if (options.prototype === false) { // Defaults to true
if (internals.needsProtoHack.has(baseProto)) {
return new baseProto.constructor();
}
return baseProto === Types.array ? [] : {};
}
const proto = Object.getPrototypeOf(obj);
if (proto &&
proto.isImmutable) {
return obj;
}
if (baseProto === Types.array) {
const newObj = [];
if (proto !== baseProto) {
Object.setPrototypeOf(newObj, proto);
}
return newObj;
}
if (internals.needsProtoHack.has(baseProto)) {
const newObj = new proto.constructor();
if (proto !== baseProto) {
Object.setPrototypeOf(newObj, proto);
}
return newObj;
}
return Object.create(proto);
};
'use strict';
const Assert = require('./assert');
const DeepEqual = require('./deepEqual');
const EscapeRegex = require('./escapeRegex');
const Utils = require('./utils');
const internals = {};
module.exports = function (ref, values, options = {}) { // options: { deep, once, only, part, symbols }
/*
string -> string(s)
array -> item(s)
object -> key(s)
object -> object (key:value)
*/
if (typeof values !== 'object') {
values = [values];
}
Assert(!Array.isArray(values) || values.length, 'Values array cannot be empty');
// String
if (typeof ref === 'string') {
return internals.string(ref, values, options);
}
// Array
if (Array.isArray(ref)) {
return internals.array(ref, values, options);
}
// Object
Assert(typeof ref === 'object', 'Reference must be string or an object');
return internals.object(ref, values, options);
};
internals.array = function (ref, values, options) {
if (!Array.isArray(values)) {
values = [values];
}
if (!ref.length) {
return false;
}
if (options.only &&
options.once &&
ref.length !== values.length) {
return false;
}
let compare;
// Map values
const map = new Map();
for (const value of values) {
if (!options.deep ||
!value ||
typeof value !== 'object') {
const existing = map.get(value);
if (existing) {
++existing.allowed;
}
else {
map.set(value, { allowed: 1, hits: 0 });
}
}
else {
compare = compare || internals.compare(options);
let found = false;
for (const [key, existing] of map.entries()) {
if (compare(key, value)) {
++existing.allowed;
found = true;
break;
}
}
if (!found) {
map.set(value, { allowed: 1, hits: 0 });
}
}
}
// Lookup values
let hits = 0;
for (const item of ref) {
let match;
if (!options.deep ||
!item ||
typeof item !== 'object') {
match = map.get(item);
}
else {
compare = compare || internals.compare(options);
for (const [key, existing] of map.entries()) {
if (compare(key, item)) {
match = existing;
break;
}
}
}
if (match) {
++match.hits;
++hits;
if (options.once &&
match.hits > match.allowed) {
return false;
}
}
}
// Validate results
if (options.only &&
hits !== ref.length) {
return false;
}
for (const match of map.values()) {
if (match.hits === match.allowed) {
continue;
}
if (match.hits < match.allowed &&
!options.part) {
return false;
}
}
return !!hits;
};
internals.object = function (ref, values, options) {
Assert(options.once === undefined, 'Cannot use option once with object');
const keys = Utils.keys(ref, options);
if (!keys.length) {
return false;
}
// Keys list
if (Array.isArray(values)) {
return internals.array(keys, values, options);
}
// Key value pairs
const symbols = Object.getOwnPropertySymbols(values).filter((sym) => values.propertyIsEnumerable(sym));
const targets = [...Object.keys(values), ...symbols];
const compare = internals.compare(options);
const set = new Set(targets);
for (const key of keys) {
if (!set.has(key)) {
if (options.only) {
return false;
}
continue;
}
if (!compare(values[key], ref[key])) {
return false;
}
set.delete(key);
}
if (set.size) {
return options.part ? set.size < targets.length : false;
}
return true;
};
internals.string = function (ref, values, options) {
// Empty string
if (ref === '') {
return values.length === 1 && values[0] === '' || // '' contains ''
!options.once && !values.some((v) => v !== ''); // '' contains multiple '' if !once
}
// Map values
const map = new Map();
const patterns = [];
for (const value of values) {
Assert(typeof value === 'string', 'Cannot compare string reference to non-string value');
if (value) {
const existing = map.get(value);
if (existing) {
++existing.allowed;
}
else {
map.set(value, { allowed: 1, hits: 0 });
patterns.push(EscapeRegex(value));
}
}
else if (options.once ||
options.only) {
return false;
}
}
if (!patterns.length) { // Non-empty string contains unlimited empty string
return true;
}
// Match patterns
const regex = new RegExp(`(${patterns.join('|')})`, 'g');
const leftovers = ref.replace(regex, ($0, $1) => {
++map.get($1).hits;
return ''; // Remove from string
});
// Validate results
if (options.only &&
leftovers) {
return false;
}
let any = false;
for (const match of map.values()) {
if (match.hits) {
any = true;
}
if (match.hits === match.allowed) {
continue;
}
if (match.hits < match.allowed &&
!options.part) {
return false;
}
// match.hits > match.allowed
if (options.once) {
return false;
}
}
return !!any;
};
internals.compare = function (options) {
if (!options.deep) {
return internals.shallow;
}
const hasOnly = options.only !== undefined;
const hasPart = options.part !== undefined;
const flags = {
prototype: hasOnly ? options.only : hasPart ? !options.part : false,
part: hasOnly ? !options.only : hasPart ? options.part : false
};
return (a, b) => DeepEqual(a, b, flags);
};
internals.shallow = function (a, b) {
return a === b;
};
'use strict';
const Types = require('./types');
const internals = {
mismatched: null
};
module.exports = function (obj, ref, options) {
options = Object.assign({ prototype: true }, options);
return !!internals.isDeepEqual(obj, ref, options, []);
};
internals.isDeepEqual = function (obj, ref, options, seen) {
if (obj === ref) { // Copied from Deep-eql, copyright(c) 2013 Jake Luer, jake@alogicalparadox.com, MIT Licensed, https://github.com/chaijs/deep-eql
return obj !== 0 || 1 / obj === 1 / ref;
}
const type = typeof obj;
if (type !== typeof ref) {
return false;
}
if (obj === null ||
ref === null) {
return false;
}
if (type === 'function') {
if (!options.deepFunction ||
obj.toString() !== ref.toString()) {
return false;
}
// Continue as object
}
else if (type !== 'object') {
return obj !== obj && ref !== ref; // NaN
}
const instanceType = internals.getSharedType(obj, ref, !!options.prototype);
switch (instanceType) {
case Types.buffer:
return Buffer && Buffer.prototype.equals.call(obj, ref); // $lab:coverage:ignore$
case Types.promise:
return obj === ref;
case Types.regex:
return obj.toString() === ref.toString();
case internals.mismatched:
return false;
}
for (let i = seen.length - 1; i >= 0; --i) {
if (seen[i].isSame(obj, ref)) {
return true; // If previous comparison failed, it would have stopped execution
}
}
seen.push(new internals.SeenEntry(obj, ref));
try {
return !!internals.isDeepEqualObj(instanceType, obj, ref, options, seen);
}
finally {
seen.pop();
}
};
internals.getSharedType = function (obj, ref, checkPrototype) {
if (checkPrototype) {
if (Object.getPrototypeOf(obj) !== Object.getPrototypeOf(ref)) {
return internals.mismatched;
}
return Types.getInternalProto(obj);
}
const type = Types.getInternalProto(obj);
if (type !== Types.getInternalProto(ref)) {
return internals.mismatched;
}
return type;
};
internals.valueOf = function (obj) {
const objValueOf = obj.valueOf;
if (objValueOf === undefined) {
return obj;
}
try {
return objValueOf.call(obj);
}
catch (err) {
return err;
}
};
internals.hasOwnEnumerableProperty = function (obj, key) {
return Object.prototype.propertyIsEnumerable.call(obj, key);
};
internals.isSetSimpleEqual = function (obj, ref) {
for (const entry of obj) {
if (!ref.has(entry)) {
return false;
}
}
return true;
};
internals.isDeepEqualObj = function (instanceType, obj, ref, options, seen) {
const { isDeepEqual, valueOf, hasOwnEnumerableProperty } = internals;
const { keys, getOwnPropertySymbols } = Object;
if (instanceType === Types.array) {
if (options.part) {
// Check if any index match any other index
for (const objValue of obj) {
for (const refValue of ref) {
if (isDeepEqual(objValue, refValue, options, seen)) {
return true;
}
}
}
}
else {
if (obj.length !== ref.length) {
return false;
}
for (let i = 0; i < obj.length; ++i) {
if (!isDeepEqual(obj[i], ref[i], options, seen)) {
return false;
}
}
return true;
}
}
else if (instanceType === Types.set) {
if (obj.size !== ref.size) {
return false;
}
if (!internals.isSetSimpleEqual(obj, ref)) {
// Check for deep equality
const ref2 = new Set(ref);
for (const objEntry of obj) {
if (ref2.delete(objEntry)) {
continue;
}
let found = false;
for (const refEntry of ref2) {
if (isDeepEqual(objEntry, refEntry, options, seen)) {
ref2.delete(refEntry);
found = true;
break;
}
}
if (!found) {
return false;
}
}
}
}
else if (instanceType === Types.map) {
if (obj.size !== ref.size) {
return false;
}
for (const [key, value] of obj) {
if (value === undefined && !ref.has(key)) {
return false;
}
if (!isDeepEqual(value, ref.get(key), options, seen)) {
return false;
}
}
}
else if (instanceType === Types.error) {
// Always check name and message
if (obj.name !== ref.name ||
obj.message !== ref.message) {
return false;
}
}
// Check .valueOf()
const valueOfObj = valueOf(obj);
const valueOfRef = valueOf(ref);
if ((obj !== valueOfObj || ref !== valueOfRef) &&
!isDeepEqual(valueOfObj, valueOfRef, options, seen)) {
return false;
}
// Check properties
const objKeys = keys(obj);
if (!options.part &&
objKeys.length !== keys(ref).length &&
!options.skip) {
return false;
}
let skipped = 0;
for (const key of objKeys) {
if (options.skip &&
options.skip.includes(key)) {
if (ref[key] === undefined) {
++skipped;
}
continue;
}
if (!hasOwnEnumerableProperty(ref, key)) {
return false;
}
if (!isDeepEqual(obj[key], ref[key], options, seen)) {
return false;
}
}
if (!options.part &&
objKeys.length - skipped !== keys(ref).length) {
return false;
}
// Check symbols
if (options.symbols !== false) { // Defaults to true
const objSymbols = getOwnPropertySymbols(obj);
const refSymbols = new Set(getOwnPropertySymbols(ref));
for (const key of objSymbols) {
if (!options.skip ||
!options.skip.includes(key)) {
if (hasOwnEnumerableProperty(obj, key)) {
if (!hasOwnEnumerableProperty(ref, key)) {
return false;
}
if (!isDeepEqual(obj[key], ref[key], options, seen)) {
return false;
}
}
else if (hasOwnEnumerableProperty(ref, key)) {
return false;
}
}
refSymbols.delete(key);
}
for (const key of refSymbols) {
if (hasOwnEnumerableProperty(ref, key)) {
return false;
}
}
}
return true;
};
internals.SeenEntry = class {
constructor(obj, ref) {
this.obj = obj;
this.ref = ref;
}
isSame(obj, ref) {
return this.obj === obj && this.ref === ref;
}
};
'use strict';
const Stringify = require('./stringify');
const internals = {};
module.exports = class extends Error {
constructor(args) {
const msgs = args
.filter((arg) => arg !== '')
.map((arg) => {
return typeof arg === 'string' ? arg : arg instanceof Error ? arg.message : Stringify(arg);
});
super(msgs.join(' ') || 'Unknown error');
if (typeof Error.captureStackTrace === 'function') { // $lab:coverage:ignore$
Error.captureStackTrace(this, exports.assert);
}
}
};
'use strict';
const Assert = require('./assert');
const internals = {};
module.exports = function (attribute) {
// Allowed value characters: !#$%&'()*+,-./:;<=>?@[]^_`{|}~ and space, a-z, A-Z, 0-9, \, "
Assert(/^[ \w\!#\$%&'\(\)\*\+,\-\.\/\:;<\=>\?@\[\]\^`\{\|\}~\"\\]*$/.test(attribute), 'Bad attribute value (' + attribute + ')');
return attribute.replace(/\\/g, '\\\\').replace(/\"/g, '\\"'); // Escape quotes and slash
};
'use strict';
const internals = {};
module.exports = function (input) {
if (!input) {
return '';
}
let escaped = '';
for (let i = 0; i < input.length; ++i) {
const charCode = input.charCodeAt(i);
if (internals.isSafe(charCode)) {
escaped += input[i];
}
else {
escaped += internals.escapeHtmlChar(charCode);
}
}
return escaped;
};
internals.escapeHtmlChar = function (charCode) {
const namedEscape = internals.namedHtml[charCode];
if (typeof namedEscape !== 'undefined') {
return namedEscape;
}
if (charCode >= 256) {
return '&#' + charCode + ';';
}
const hexValue = charCode.toString(16).padStart(2, '0');
return `&#x${hexValue};`;
};
internals.isSafe = function (charCode) {
return (typeof internals.safeCharCodes[charCode] !== 'undefined');
};
internals.namedHtml = {
'38': '&amp;',
'60': '&lt;',
'62': '&gt;',
'34': '&quot;',
'160': '&nbsp;',
'162': '&cent;',
'163': '&pound;',
'164': '&curren;',
'169': '&copy;',
'174': '&reg;'
};
internals.safeCharCodes = (function () {
const safe = {};
for (let i = 32; i < 123; ++i) {
if ((i >= 97) || // a-z
(i >= 65 && i <= 90) || // A-Z
(i >= 48 && i <= 57) || // 0-9
i === 32 || // space
i === 46 || // .
i === 44 || // ,
i === 45 || // -
i === 58 || // :
i === 95) { // _
safe[i] = null;
}
}
return safe;
}());
'use strict';
const internals = {};
module.exports = function (input) {
if (!input) {
return '';
}
const lessThan = 0x3C;
const greaterThan = 0x3E;
const andSymbol = 0x26;
const lineSeperator = 0x2028;
// replace method
let charCode;
return input.replace(/[<>&\u2028\u2029]/g, (match) => {
charCode = match.charCodeAt(0);
if (charCode === lessThan) {
return '\\u003c';
}
if (charCode === greaterThan) {
return '\\u003e';
}
if (charCode === andSymbol) {
return '\\u0026';
}
if (charCode === lineSeperator) {
return '\\u2028';
}
return '\\u2029';
});
};
'use strict';
const internals = {};
module.exports = function (string) {
// Escape ^$.*+-?=!:|\/()[]{},
return string.replace(/[\^\$\.\*\+\-\?\=\!\:\|\\\/\(\)\[\]\{\}\,]/g, '\\$&');
};
'use strict';
const internals = {};
module.exports = internals.flatten = function (array, target) {
const result = target || [];
for (let i = 0; i < array.length; ++i) {
if (Array.isArray(array[i])) {
internals.flatten(array[i], result);
}
else {
result.push(array[i]);
}
}
return result;
};
'use strict';
const internals = {};
module.exports = function () { };
'use strict';
const internals = {};
module.exports = {
applyToDefaults: require('./applyToDefaults'),
assert: require('./assert'),
Bench: require('./bench'),
block: require('./block'),
clone: require('./clone'),
contain: require('./contain'),
deepEqual: require('./deepEqual'),
Error: require('./error'),
escapeHeaderAttribute: require('./escapeHeaderAttribute'),
escapeHtml: require('./escapeHtml'),
escapeJson: require('./escapeJson'),
escapeRegex: require('./escapeRegex'),
flatten: require('./flatten'),
ignore: require('./ignore'),
intersect: require('./intersect'),
isPromise: require('./isPromise'),
merge: require('./merge'),
once: require('./once'),
reach: require('./reach'),
reachTemplate: require('./reachTemplate'),
stringify: require('./stringify'),
wait: require('./wait')
};
'use strict';
const internals = {};
module.exports = function (array1, array2, options = {}) {
if (!array1 ||
!array2) {
return (options.first ? null : []);
}
const common = [];
const hash = (Array.isArray(array1) ? new Set(array1) : array1);
const found = new Set();
for (const value of array2) {
if (internals.has(hash, value) &&
!found.has(value)) {
if (options.first) {
return value;
}
common.push(value);
found.add(value);
}
}
return (options.first ? null : common);
};
internals.has = function (ref, key) {
if (typeof ref.has === 'function') {
return ref.has(key);
}
return ref[key] !== undefined;
};
'use strict';
const internals = {};
module.exports = function (promise) {
return !!promise && typeof promise.then === 'function';
};
'use strict';
const Assert = require('./assert');
const Clone = require('./clone');
const Utils = require('./utils');
const internals = {};
module.exports = internals.merge = function (target, source, options) {
Assert(target && typeof target === 'object', 'Invalid target value: must be an object');
Assert(source === null || source === undefined || typeof source === 'object', 'Invalid source value: must be null, undefined, or an object');
if (!source) {
return target;
}
options = Object.assign({ nullOverride: true, mergeArrays: true }, options);
if (Array.isArray(source)) {
Assert(Array.isArray(target), 'Cannot merge array onto an object');
if (!options.mergeArrays) {
target.length = 0; // Must not change target assignment
}
for (let i = 0; i < source.length; ++i) {
target.push(Clone(source[i], { symbols: options.symbols }));
}
return target;
}
const keys = Utils.keys(source, options);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
if (key === '__proto__' ||
!Object.prototype.propertyIsEnumerable.call(source, key)) {
continue;
}
const value = source[key];
if (value &&
typeof value === 'object') {
if (target[key] === value) {
continue; // Can occur for shallow merges
}
if (!target[key] ||
typeof target[key] !== 'object' ||
(Array.isArray(target[key]) !== Array.isArray(value)) ||
value instanceof Date ||
(Buffer && Buffer.isBuffer(value)) || // $lab:coverage:ignore$
value instanceof RegExp) {
target[key] = Clone(value, { symbols: options.symbols });
}
else {
internals.merge(target[key], value, options);
}
}
else {
if (value !== null &&
value !== undefined) { // Explicit to preserve empty strings
target[key] = value;
}
else if (options.nullOverride) {
target[key] = value;
}
}
}
return target;
};
'use strict';
const internals = {};
module.exports = function (method) {
if (method._hoekOnce) {
return method;
}
let once = false;
const wrapped = function (...args) {
if (!once) {
once = true;
method(...args);
}
};
wrapped._hoekOnce = true;
return wrapped;
};
'use strict';
const Assert = require('./assert');
const internals = {};
module.exports = function (obj, chain, options) {
if (chain === false ||
chain === null ||
chain === undefined) {
return obj;
}
options = options || {};
if (typeof options === 'string') {
options = { separator: options };
}
const isChainArray = Array.isArray(chain);
Assert(!isChainArray || !options.separator, 'Separator option no valid for array-based chain');
const path = isChainArray ? chain : chain.split(options.separator || '.');
let ref = obj;
for (let i = 0; i < path.length; ++i) {
let key = path[i];
const type = options.iterables && internals.iterables(ref);
if (Array.isArray(ref) ||
type === 'set') {
const number = Number(key);
if (Number.isInteger(number)) {
key = number < 0 ? ref.length + number : number;
}
}
if (!ref ||
typeof ref === 'function' && options.functions === false || // Defaults to true
!type && ref[key] === undefined) {
Assert(!options.strict || i + 1 === path.length, 'Missing segment', key, 'in reach path ', chain);
Assert(typeof ref === 'object' || options.functions === true || typeof ref !== 'function', 'Invalid segment', key, 'in reach path ', chain);
ref = options.default;
break;
}
if (!type) {
ref = ref[key];
}
else if (type === 'set') {
ref = [...ref][key];
}
else { // type === 'map'
ref = ref.get(key);
}
}
return ref;
};
internals.iterables = function (ref) {
if (ref instanceof Set) {
return 'set';
}
if (ref instanceof Map) {
return 'map';
}
};
'use strict';
const Reach = require('./reach');
const internals = {};
module.exports = function (obj, template, options) {
return template.replace(/{([^}]+)}/g, ($0, chain) => {
const value = Reach(obj, chain, options);
return (value === undefined || value === null ? '' : value);
});
};
'use strict';
const internals = {};
module.exports = function (...args) {
try {
return JSON.stringify.apply(null, args);
}
catch (err) {
return '[Cannot display object: ' + err.message + ']';
}
};
'use strict';
const internals = {};
exports = module.exports = {
array: Array.prototype,
buffer: Buffer && Buffer.prototype, // $lab:coverage:ignore$
date: Date.prototype,
error: Error.prototype,
generic: Object.prototype,
map: Map.prototype,
promise: Promise.prototype,
regex: RegExp.prototype,
set: Set.prototype,
weakMap: WeakMap.prototype,
weakSet: WeakSet.prototype
};
internals.typeMap = new Map([
['[object Error]', exports.error],
['[object Map]', exports.map],
['[object Promise]', exports.promise],
['[object Set]', exports.set],
['[object WeakMap]', exports.weakMap],
['[object WeakSet]', exports.weakSet]
]);
exports.getInternalProto = function (obj) {
if (Array.isArray(obj)) {
return exports.array;
}
if (Buffer && obj instanceof Buffer) { // $lab:coverage:ignore$
return exports.buffer;
}
if (obj instanceof Date) {
return exports.date;
}
if (obj instanceof RegExp) {
return exports.regex;
}
if (obj instanceof Error) {
return exports.error;
}
const objName = Object.prototype.toString.call(obj);
return internals.typeMap.get(objName) || exports.generic;
};
'use strict';
const internals = {};
exports.keys = function (obj, options = {}) {
return options.symbols !== false ? Reflect.ownKeys(obj) : Object.getOwnPropertyNames(obj); // Defaults to true
};
'use strict';
const internals = {};
module.exports = function (timeout) {
return new Promise((resolve) => setTimeout(resolve, timeout));
};
{
"_from": "@hapi/hoek@^9.0.0",
"_id": "@hapi/hoek@9.0.4",
"_inBundle": false,
"_integrity": "sha512-EwaJS7RjoXUZ2cXXKZZxZqieGtc7RbvQhUy8FwDoMQtxWVi14tFjeFCYPZAM1mBCpOpiBpyaZbb9NeHc7eGKgw==",
"_location": "/@hapi/hoek",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "@hapi/hoek@^9.0.0",
"name": "@hapi/hoek",
"escapedName": "@hapi%2fhoek",
"scope": "@hapi",
"rawSpec": "^9.0.0",
"saveSpec": null,
"fetchSpec": "^9.0.0"
},
"_requiredBy": [
"/@hapi/address",
"/@hapi/boom",
"/@hapi/joi",
"/@hapi/topo",
"/@hapi/wreck",
"/optioner",
"/ordu"
],
"_resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.0.4.tgz",
"_shasum": "e80ad4e8e8d2adc6c77d985f698447e8628b6010",
"_spec": "@hapi/hoek@^9.0.0",
"_where": "/Users/admin/Downloads/NEXPIE2/cb/node_modules/@hapi/joi",
"bugs": {
"url": "https://github.com/hapijs/hoek/issues"
},
"bundleDependencies": false,
"dependencies": {},
"deprecated": false,
"description": "General purpose node utilities",
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/lab": "22.x.x"
},
"files": [
"lib"
],
"homepage": "https://github.com/hapijs/hoek#readme",
"keywords": [
"utilities"
],
"license": "BSD-3-Clause",
"main": "lib/index.js",
"name": "@hapi/hoek",
"repository": {
"type": "git",
"url": "git://github.com/hapijs/hoek.git"
},
"scripts": {
"test": "lab -a @hapi/code -t 100 -L -Y",
"test-cov-html": "lab -a @hapi/code -t 100 -L -r html -o coverage.html"
},
"types": "lib/index.d.ts",
"version": "9.0.4"
}
Copyright (c) 2012-2020, Sideway Inc, and project contributors
Copyright (c) 2012-2014, Walmart.
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.
<a href="https://hapi.dev"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a>
# @hapi/joi
#### The most powerful schema description language and data validator for JavaScript.
**joi** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) – they work even better together.
### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support
## Useful resources
- [Documentation and API](https://hapi.dev/family/joi/)
- [Versions status](https://hapi.dev/resources/status/#joi)
- [Changelog](https://hapi.dev/family/joi/changelog/)
- [Project policies](https://hapi.dev/policies/)
- [Free and commercial support options](https://hapi.dev/support/)
This source diff could not be displayed because it is too large. You can view the blob instead.
'use strict';
const Clone = require('@hapi/hoek/lib/clone');
const Common = require('./common');
const internals = {
annotations: Symbol('annotations')
};
exports.error = function (stripColorCodes) {
if (!this._original ||
typeof this._original !== 'object') {
return this.details[0].message;
}
const redFgEscape = stripColorCodes ? '' : '\u001b[31m';
const redBgEscape = stripColorCodes ? '' : '\u001b[41m';
const endColor = stripColorCodes ? '' : '\u001b[0m';
const obj = Clone(this._original);
for (let i = this.details.length - 1; i >= 0; --i) { // Reverse order to process deepest child first
const pos = i + 1;
const error = this.details[i];
const path = error.path;
let node = obj;
for (let j = 0; ; ++j) {
const seg = path[j];
if (Common.isSchema(node)) {
node = node.clone(); // joi schemas are not cloned by hoek, we have to take this extra step
}
if (j + 1 < path.length &&
typeof node[seg] !== 'string') {
node = node[seg];
}
else {
const refAnnotations = node[internals.annotations] || { errors: {}, missing: {} };
node[internals.annotations] = refAnnotations;
const cacheKey = seg || error.context.key;
if (node[seg] !== undefined) {
refAnnotations.errors[cacheKey] = refAnnotations.errors[cacheKey] || [];
refAnnotations.errors[cacheKey].push(pos);
}
else {
refAnnotations.missing[cacheKey] = pos;
}
break;
}
}
}
const replacers = {
key: /_\$key\$_([, \d]+)_\$end\$_"/g,
missing: /"_\$miss\$_([^|]+)\|(\d+)_\$end\$_": "__missing__"/g,
arrayIndex: /\s*"_\$idx\$_([, \d]+)_\$end\$_",?\n(.*)/g,
specials: /"\[(NaN|Symbol.*|-?Infinity|function.*|\(.*)]"/g
};
let message = internals.safeStringify(obj, 2)
.replace(replacers.key, ($0, $1) => `" ${redFgEscape}[${$1}]${endColor}`)
.replace(replacers.missing, ($0, $1, $2) => `${redBgEscape}"${$1}"${endColor}${redFgEscape} [${$2}]: -- missing --${endColor}`)
.replace(replacers.arrayIndex, ($0, $1, $2) => `\n${$2} ${redFgEscape}[${$1}]${endColor}`)
.replace(replacers.specials, ($0, $1) => $1);
message = `${message}\n${redFgEscape}`;
for (let i = 0; i < this.details.length; ++i) {
const pos = i + 1;
message = `${message}\n[${pos}] ${this.details[i].message}`;
}
message = message + endColor;
return message;
};
// Inspired by json-stringify-safe
internals.safeStringify = function (obj, spaces) {
return JSON.stringify(obj, internals.serializer(), spaces);
};
internals.serializer = function () {
const keys = [];
const stack = [];
const cycleReplacer = (key, value) => {
if (stack[0] === value) {
return '[Circular ~]';
}
return '[Circular ~.' + keys.slice(0, stack.indexOf(value)).join('.') + ']';
};
return function (key, value) {
if (stack.length > 0) {
const thisPos = stack.indexOf(this);
if (~thisPos) {
stack.length = thisPos + 1;
keys.length = thisPos + 1;
keys[thisPos] = key;
}
else {
stack.push(this);
keys.push(key);
}
if (~stack.indexOf(value)) {
value = cycleReplacer.call(this, key, value);
}
}
else {
stack.push(value);
}
if (value) {
const annotations = value[internals.annotations];
if (annotations) {
if (Array.isArray(value)) {
const annotated = [];
for (let i = 0; i < value.length; ++i) {
if (annotations.errors[i]) {
annotated.push(`_$idx$_${annotations.errors[i].sort().join(', ')}_$end$_`);
}
annotated.push(value[i]);
}
value = annotated;
}
else {
for (const errorKey in annotations.errors) {
value[`${errorKey}_$key$_${annotations.errors[errorKey].sort().join(', ')}_$end$_`] = value[errorKey];
value[errorKey] = undefined;
}
for (const missingKey in annotations.missing) {
value[`_$miss$_${missingKey}|${annotations.missing[missingKey]}_$end$_`] = '__missing__';
}
}
return value;
}
}
if (value === Infinity ||
value === -Infinity ||
Number.isNaN(value) ||
typeof value === 'function' ||
typeof value === 'symbol') {
return '[' + value.toString() + ']';
}
return value;
};
};
'use strict';
const Assert = require('@hapi/hoek/lib/assert');
const Clone = require('@hapi/hoek/lib/clone');
const Common = require('./common');
const internals = {
max: 1000,
supported: new Set(['undefined', 'boolean', 'number', 'string'])
};
exports.provider = {
provision(options) {
return new internals.Cache(options);
}
};
// Least Recently Used (LRU) Cache
internals.Cache = class {
constructor(options = {}) {
Common.assertOptions(options, ['max']);
Assert(options.max === undefined || options.max && options.max > 0 && isFinite(options.max), 'Invalid max cache size');
this._max = options.max || internals.max;
this._map = new Map(); // Map of nodes by key
this._list = new internals.List(); // List of nodes (most recently used in head)
}
get length() {
return this._map.size;
}
set(key, value) {
if (key !== null &&
!internals.supported.has(typeof key)) {
return;
}
let node = this._map.get(key);
if (node) {
node.value = value;
this._list.first(node);
return;
}
node = this._list.unshift({ key, value });
this._map.set(key, node);
this._compact();
}
get(key) {
const node = this._map.get(key);
if (node) {
this._list.first(node);
return Clone(node.value);
}
}
_compact() {
if (this._map.size > this._max) {
const node = this._list.pop();
this._map.delete(node.key);
}
}
};
internals.List = class {
constructor() {
this.tail = null;
this.head = null;
}
unshift(node) {
node.next = null;
node.prev = this.head;
if (this.head) {
this.head.next = node;
}
this.head = node;
if (!this.tail) {
this.tail = node;
}
return node;
}
first(node) {
if (node === this.head) {
return;
}
this._remove(node);
this.unshift(node);
}
pop() {
return this._remove(this.tail);
}
_remove(node) {
const { next, prev } = node;
next.prev = prev;
if (prev) {
prev.next = next;
}
if (node === this.tail) {
this.tail = next;
}
node.prev = null;
node.next = null;
return node;
}
};
'use strict';
const Assert = require('@hapi/hoek/lib/assert');
const AssertError = require('@hapi/hoek/lib/error');
const Pkg = require('../package.json');
let Messages;
let Schemas;
const internals = {
isoDate: /^(?:[-+]\d{2})?(?:\d{4}(?!\d{2}\b))(?:(-?)(?:(?:0[1-9]|1[0-2])(?:\1(?:[12]\d|0[1-9]|3[01]))?|W(?:[0-4]\d|5[0-2])(?:-?[1-7])?|(?:00[1-9]|0[1-9]\d|[12]\d{2}|3(?:[0-5]\d|6[1-6])))(?![T]$|[T][\d]+Z$)(?:[T\s](?:(?:(?:[01]\d|2[0-3])(?:(:?)[0-5]\d)?|24\:?00)(?:[.,]\d+(?!:))?)(?:\2[0-5]\d(?:[.,]\d+)?)?(?:[Z]|(?:[+-])(?:[01]\d|2[0-3])(?::?[0-5]\d)?)?)?)?$/
};
exports.version = Pkg.version;
exports.defaults = {
abortEarly: true,
allowUnknown: false,
cache: true,
context: null,
convert: true,
dateFormat: 'iso',
errors: {
escapeHtml: false,
label: 'path',
language: null,
render: true,
stack: false,
wrap: {
label: '"',
array: '[]'
}
},
externals: true,
messages: {},
nonEnumerables: false,
noDefaults: false,
presence: 'optional',
skipFunctions: false,
stripUnknown: false,
warnings: false
};
exports.symbols = {
any: Symbol.for('@hapi/joi/schema'), // Used to internally identify any-based types (shared with other joi versions)
arraySingle: Symbol('arraySingle'),
deepDefault: Symbol('deepDefault'),
literal: Symbol('literal'),
override: Symbol('override'),
prefs: Symbol('prefs'),
ref: Symbol('ref'),
values: Symbol('values'),
template: Symbol('template')
};
exports.assertOptions = function (options, keys, name = 'Options') {
Assert(options && typeof options === 'object' && !Array.isArray(options), 'Options must be of type object');
const unknownKeys = Object.keys(options).filter((k) => !keys.includes(k));
Assert(unknownKeys.length === 0, `${name} contain unknown keys: ${unknownKeys}`);
};
exports.checkPreferences = function (prefs) {
Schemas = Schemas || require('./schemas');
const result = Schemas.preferences.validate(prefs);
if (result.error) {
throw new AssertError([result.error.details[0].message]);
}
};
exports.compare = function (a, b, operator) {
switch (operator) {
case '=': return a === b;
case '>': return a > b;
case '<': return a < b;
case '>=': return a >= b;
case '<=': return a <= b;
}
};
exports.default = function (value, defaultValue) {
return value === undefined ? defaultValue : value;
};
exports.isIsoDate = function (date) {
return internals.isoDate.test(date);
};
exports.isNumber = function (value) {
return typeof value === 'number' && !isNaN(value);
};
exports.isResolvable = function (obj) {
if (!obj) {
return false;
}
return obj[exports.symbols.ref] || obj[exports.symbols.template];
};
exports.isSchema = function (schema, options = {}) {
const any = schema && schema[exports.symbols.any];
if (!any) {
return false;
}
Assert(options.legacy || any.version === exports.version, 'Cannot mix different versions of joi schemas');
return true;
};
exports.isValues = function (obj) {
return obj[exports.symbols.values];
};
exports.limit = function (value) {
return Number.isSafeInteger(value) && value >= 0;
};
exports.preferences = function (target, source) {
Messages = Messages || require('./messages');
target = target || {};
source = source || {};
const merged = Object.assign({}, target, source);
if (source.errors &&
target.errors) {
merged.errors = Object.assign({}, target.errors, source.errors);
merged.errors.wrap = Object.assign({}, target.errors.wrap, source.errors.wrap);
}
if (source.messages) {
merged.messages = Messages.compile(source.messages, target.messages);
}
delete merged[exports.symbols.prefs];
return merged;
};
exports.tryWithPath = function (fn, key, options = {}) {
try {
return fn();
}
catch (err) {
if (err.path !== undefined) {
err.path = key + '.' + err.path;
}
else {
err.path = key;
}
if (options.append) {
err.message = `${err.message} (${err.path})`;
}
throw err;
}
};
exports.validateArg = function (value, label, { assert, message }) {
if (exports.isSchema(assert)) {
const result = assert.validate(value);
if (!result.error) {
return;
}
return result.error.message;
}
else if (!assert(value)) {
return label ? `${label} ${message}` : message;
}
};
exports.verifyFlat = function (args, method) {
for (const arg of args) {
Assert(!Array.isArray(arg), 'Method no longer accepts array arguments:', method);
}
};
'use strict';
const Assert = require('@hapi/hoek/lib/assert');
const Common = require('./common');
const Ref = require('./ref');
const internals = {};
exports.schema = function (Joi, config, options = {}) {
Common.assertOptions(options, ['appendPath', 'override']);
try {
return internals.schema(Joi, config, options);
}
catch (err) {
if (options.appendPath &&
err.path !== undefined) {
err.message = `${err.message} (${err.path})`;
}
throw err;
}
};
internals.schema = function (Joi, config, options) {
Assert(config !== undefined, 'Invalid undefined schema');
if (Array.isArray(config)) {
Assert(config.length, 'Invalid empty array schema');
if (config.length === 1) {
config = config[0];
}
}
const valid = (base, ...values) => {
if (options.override !== false) {
return base.valid(Joi.override, ...values);
}
return base.valid(...values);
};
if (internals.simple(config)) {
return valid(Joi, config);
}
if (typeof config === 'function') {
return Joi.custom(config);
}
Assert(typeof config === 'object', 'Invalid schema content:', typeof config);
if (Common.isResolvable(config)) {
return valid(Joi, config);
}
if (Common.isSchema(config)) {
return config;
}
if (Array.isArray(config)) {
for (const item of config) {
if (!internals.simple(item)) {
return Joi.alternatives().try(...config);
}
}
return valid(Joi, ...config);
}
if (config instanceof RegExp) {
return Joi.string().regex(config);
}
if (config instanceof Date) {
return valid(Joi.date(), config);
}
Assert(Object.getPrototypeOf(config) === Object.getPrototypeOf({}), 'Schema can only contain plain objects');
return Joi.object().keys(config);
};
exports.ref = function (id, options) {
return Ref.isRef(id) ? id : Ref.create(id, options);
};
exports.compile = function (root, schema, options = {}) {
Common.assertOptions(options, ['legacy']);
// Compiled by any supported version
const any = schema && schema[Common.symbols.any];
if (any) {
Assert(options.legacy || any.version === Common.version, 'Cannot mix different versions of joi schemas:', any.version, Common.version);
return schema;
}
// Uncompiled root
if (typeof schema !== 'object' ||
!options.legacy) {
return exports.schema(root, schema, { appendPath: true }); // Will error if schema contains other versions
}
// Scan schema for compiled parts
const compiler = internals.walk(schema);
if (!compiler) {
return exports.schema(root, schema, { appendPath: true });
}
return compiler.compile(compiler.root, schema);
};
internals.walk = function (schema) {
if (typeof schema !== 'object') {
return null;
}
if (Array.isArray(schema)) {
for (const item of schema) {
const compiler = internals.walk(item);
if (compiler) {
return compiler;
}
}
return null;
}
const any = schema[Common.symbols.any];
if (any) {
return { root: schema[any.root], compile: any.compile };
}
Assert(Object.getPrototypeOf(schema) === Object.getPrototypeOf({}), 'Schema can only contain plain objects');
for (const key in schema) {
const compiler = internals.walk(schema[key]);
if (compiler) {
return compiler;
}
}
return null;
};
internals.simple = function (value) {
return value === null || ['boolean', 'string', 'number'].includes(typeof value);
};
exports.when = function (schema, condition, options) {
if (options === undefined) {
Assert(condition && typeof condition === 'object', 'Missing options');
options = condition;
condition = Ref.create('.');
}
if (Array.isArray(options)) {
options = { switch: options };
}
Common.assertOptions(options, ['is', 'not', 'then', 'otherwise', 'switch', 'break']);
// Schema condition
if (Common.isSchema(condition)) {
Assert(options.is === undefined, '"is" can not be used with a schema condition');
Assert(options.not === undefined, '"not" can not be used with a schema condition');
Assert(options.switch === undefined, '"switch" can not be used with a schema condition');
return internals.condition(schema, { is: condition, then: options.then, otherwise: options.otherwise, break: options.break });
}
// Single condition
Assert(Ref.isRef(condition) || typeof condition === 'string', 'Invalid condition:', condition);
Assert(options.not === undefined || options.is === undefined, 'Cannot combine "is" with "not"');
if (options.switch === undefined) {
let rule = options;
if (options.not !== undefined) {
rule = { is: options.not, then: options.otherwise, otherwise: options.then, break: options.break };
}
let is = rule.is !== undefined ? schema.$_compile(rule.is) : schema.$_root.invalid(null, false, 0, '').required();
Assert(rule.then !== undefined || rule.otherwise !== undefined, 'options must have at least one of "then", "otherwise", or "switch"');
Assert(rule.break === undefined || rule.then === undefined || rule.otherwise === undefined, 'Cannot specify then, otherwise, and break all together');
if (options.is !== undefined &&
!Ref.isRef(options.is) &&
!Common.isSchema(options.is)) {
is = is.required(); // Only apply required if this wasn't already a schema or a ref
}
return internals.condition(schema, { ref: exports.ref(condition), is, then: rule.then, otherwise: rule.otherwise, break: rule.break });
}
// Switch statement
Assert(Array.isArray(options.switch), '"switch" must be an array');
Assert(options.is === undefined, 'Cannot combine "switch" with "is"');
Assert(options.not === undefined, 'Cannot combine "switch" with "not"');
Assert(options.then === undefined, 'Cannot combine "switch" with "then"');
const rule = {
ref: exports.ref(condition),
switch: [],
break: options.break
};
for (let i = 0; i < options.switch.length; ++i) {
const test = options.switch[i];
const last = i === options.switch.length - 1;
Common.assertOptions(test, last ? ['is', 'then', 'otherwise'] : ['is', 'then']);
Assert(test.is !== undefined, 'Switch statement missing "is"');
Assert(test.then !== undefined, 'Switch statement missing "then"');
const item = {
is: schema.$_compile(test.is),
then: schema.$_compile(test.then)
};
if (!Ref.isRef(test.is) &&
!Common.isSchema(test.is)) {
item.is = item.is.required(); // Only apply required if this wasn't already a schema or a ref
}
if (last) {
Assert(options.otherwise === undefined || test.otherwise === undefined, 'Cannot specify "otherwise" inside and outside a "switch"');
const otherwise = options.otherwise !== undefined ? options.otherwise : test.otherwise;
if (otherwise !== undefined) {
Assert(rule.break === undefined, 'Cannot specify both otherwise and break');
item.otherwise = schema.$_compile(otherwise);
}
}
rule.switch.push(item);
}
return rule;
};
internals.condition = function (schema, condition) {
for (const key of ['then', 'otherwise']) {
if (condition[key] === undefined) {
delete condition[key];
}
else {
condition[key] = schema.$_compile(condition[key]);
}
}
return condition;
};
'use strict';
const Annotate = require('./annotate');
const Common = require('./common');
const Template = require('./template');
const internals = {};
exports.Report = class {
constructor(code, value, local, flags, messages, state, prefs) {
this.code = code;
this.flags = flags;
this.messages = messages;
this.path = state.path;
this.prefs = prefs;
this.state = state;
this.value = value;
this.message = null;
this.template = null;
this.local = local || {};
this.local.label = exports.label(this.flags, this.state, this.prefs, this.messages);
if (this.value !== undefined &&
!this.local.hasOwnProperty('value')) {
this.local.value = this.value;
}
if (this.path.length) {
const key = this.path[this.path.length - 1];
if (typeof key !== 'object') {
this.local.key = key;
}
}
}
_setTemplate(template) {
this.template = template;
if (!this.flags.label &&
this.path.length === 0) {
const localized = this._template(this.template, 'root');
if (localized) {
this.local.label = localized;
}
}
}
toString() {
if (this.message) {
return this.message;
}
const code = this.code;
if (!this.prefs.errors.render) {
return this.code;
}
const template = this._template(this.template) ||
this._template(this.prefs.messages) ||
this._template(this.messages);
if (template === undefined) {
return `Error code "${code}" is not defined, your custom type is missing the correct messages definition`;
}
// Render and cache result
this.message = template.render(this.value, this.state, this.prefs, this.local, { errors: this.prefs.errors, messages: [this.prefs.messages, this.messages] });
if (!this.prefs.errors.label) {
this.message = this.message.replace(/^"" /, '').trim();
}
return this.message;
}
_template(messages, code) {
return exports.template(this.value, messages, code || this.code, this.state, this.prefs);
}
};
exports.path = function (path) {
let label = '';
for (const segment of path) {
if (typeof segment === 'object') { // Exclude array single path segment
continue;
}
if (typeof segment === 'string') {
if (label) {
label += '.';
}
label += segment;
}
else {
label += `[${segment}]`;
}
}
return label;
};
exports.template = function (value, messages, code, state, prefs) {
if (!messages) {
return;
}
if (Template.isTemplate(messages)) {
return code !== 'root' ? messages : null;
}
let lang = prefs.errors.language;
if (Common.isResolvable(lang)) {
lang = lang.resolve(value, state, prefs);
}
if (lang &&
messages[lang] &&
messages[lang][code] !== undefined) {
return messages[lang][code];
}
return messages[code];
};
exports.label = function (flags, state, prefs, messages) {
if (flags.label) {
return flags.label;
}
if (!prefs.errors.label) {
return '';
}
let path = state.path;
if (prefs.errors.label === 'key' &&
state.path.length > 1) {
path = state.path.slice(-1);
}
const normalized = exports.path(path);
if (normalized) {
return normalized;
}
return exports.template(null, prefs.messages, 'root', state, prefs) ||
messages && exports.template(null, messages, 'root', state, prefs) ||
'value';
};
exports.process = function (errors, original, prefs) {
if (!errors) {
return null;
}
const { override, message, details } = exports.details(errors);
if (override) {
return override;
}
if (prefs.errors.stack) {
return new exports.ValidationError(message, details, original);
}
const limit = Error.stackTraceLimit;
Error.stackTraceLimit = 0;
const validationError = new exports.ValidationError(message, details, original);
Error.stackTraceLimit = limit;
return validationError;
};
exports.details = function (errors, options = {}) {
let messages = [];
const details = [];
for (const item of errors) {
// Override
if (item instanceof Error) {
if (options.override !== false) {
return { override: item };
}
const message = item.toString();
messages.push(message);
details.push({
message,
type: 'override',
context: { error: item }
});
continue;
}
// Report
const message = item.toString();
messages.push(message);
details.push({
message,
path: item.path.filter((v) => typeof v !== 'object'),
type: item.code,
context: item.local
});
}
if (messages.length > 1) {
messages = [...new Set(messages)];
}
return { message: messages.join('. '), details };
};
exports.ValidationError = class extends Error {
constructor(message, details, original) {
super(message);
this._original = original;
this.details = details;
}
static isError(err) {
return err instanceof exports.ValidationError;
}
};
exports.ValidationError.prototype.isJoi = true;
exports.ValidationError.prototype.name = 'ValidationError';
exports.ValidationError.prototype.annotate = Annotate.error;
'use strict';
const Assert = require('@hapi/hoek/lib/assert');
const Clone = require('@hapi/hoek/lib/clone');
const Common = require('./common');
const Messages = require('./messages');
const internals = {};
exports.type = function (from, options) {
const base = Object.getPrototypeOf(from);
const prototype = Clone(base);
const schema = from._assign(Object.create(prototype));
const def = Object.assign({}, options); // Shallow cloned
delete def.base;
prototype._definition = def;
const parent = base._definition || {};
def.messages = Messages.merge(parent.messages, def.messages);
def.properties = Object.assign({}, parent.properties, def.properties);
// Type
schema.type = def.type;
// Flags
def.flags = Object.assign({}, parent.flags, def.flags);
// Terms
const terms = Object.assign({}, parent.terms);
if (def.terms) {
for (const name in def.terms) { // Only apply own terms
const term = def.terms[name];
Assert(schema.$_terms[name] === undefined, 'Invalid term override for', def.type, name);
schema.$_terms[name] = term.init;
terms[name] = term;
}
}
def.terms = terms;
// Constructor arguments
if (!def.args) {
def.args = parent.args;
}
// Prepare
def.prepare = internals.prepare(def.prepare, parent.prepare);
// Coerce
if (def.coerce) {
if (typeof def.coerce === 'function') {
def.coerce = { method: def.coerce };
}
if (def.coerce.from &&
!Array.isArray(def.coerce.from)) {
def.coerce = { method: def.coerce.method, from: [].concat(def.coerce.from) };
}
}
def.coerce = internals.coerce(def.coerce, parent.coerce);
// Validate
def.validate = internals.validate(def.validate, parent.validate);
// Rules
const rules = Object.assign({}, parent.rules);
if (def.rules) {
for (const name in def.rules) {
const rule = def.rules[name];
Assert(typeof rule === 'object', 'Invalid rule definition for', def.type, name);
let method = rule.method;
if (method === undefined) {
method = function () {
return this.$_addRule(name);
};
}
if (method) {
Assert(!prototype[name], 'Rule conflict in', def.type, name);
prototype[name] = method;
}
Assert(!rules[name], 'Rule conflict in', def.type, name);
rules[name] = rule;
if (rule.alias) {
const aliases = [].concat(rule.alias);
for (const alias of aliases) {
prototype[alias] = rule.method;
}
}
if (rule.args) {
rule.argsByName = new Map();
rule.args = rule.args.map((arg) => {
if (typeof arg === 'string') {
arg = { name: arg };
}
Assert(!rule.argsByName.has(arg.name), 'Duplicated argument name', arg.name);
if (Common.isSchema(arg.assert)) {
arg.assert = arg.assert.strict().label(arg.name);
}
rule.argsByName.set(arg.name, arg);
return arg;
});
}
}
}
def.rules = rules;
// Modifiers
const modifiers = Object.assign({}, parent.modifiers);
if (def.modifiers) {
for (const name in def.modifiers) {
Assert(!prototype[name], 'Rule conflict in', def.type, name);
const modifier = def.modifiers[name];
Assert(typeof modifier === 'function', 'Invalid modifier definition for', def.type, name);
const method = function (arg) {
return this.rule({ [name]: arg });
};
prototype[name] = method;
modifiers[name] = modifier;
}
}
def.modifiers = modifiers;
// Overrides
if (def.overrides) {
prototype._super = base;
schema.$_super = {};
for (const override in def.overrides) {
Assert(base[override], 'Cannot override missing', override);
schema.$_super[override] = base[override].bind(schema);
}
Object.assign(prototype, def.overrides);
}
// Casts
def.cast = Object.assign({}, parent.cast, def.cast);
// Manifest
const manifest = Object.assign({}, parent.manifest, def.manifest);
manifest.build = internals.build(def.manifest && def.manifest.build, parent.manifest && parent.manifest.build);
def.manifest = manifest;
// Rebuild
def.rebuild = internals.rebuild(def.rebuild, parent.rebuild);
return schema;
};
// Helpers
internals.build = function (child, parent) {
if (!child ||
!parent) {
return child || parent;
}
return function (obj, desc) {
return parent(child(obj, desc), desc);
};
};
internals.coerce = function (child, parent) {
if (!child ||
!parent) {
return child || parent;
}
return {
from: child.from && parent.from ? [...new Set([...child.from, ...parent.from])] : null,
method(value, helpers) {
let coerced;
if (!parent.from ||
parent.from.includes(typeof value)) {
coerced = parent.method(value, helpers);
if (coerced) {
if (coerced.errors ||
coerced.value === undefined) {
return coerced;
}
value = coerced.value;
}
}
if (!child.from ||
child.from.includes(typeof value)) {
const own = child.method(value, helpers);
if (own) {
return own;
}
}
return coerced;
}
};
};
internals.prepare = function (child, parent) {
if (!child ||
!parent) {
return child || parent;
}
return function (value, helpers) {
const prepared = child(value, helpers);
if (prepared) {
if (prepared.errors ||
prepared.value === undefined) {
return prepared;
}
value = prepared.value;
}
return parent(value, helpers) || prepared;
};
};
internals.rebuild = function (child, parent) {
if (!child ||
!parent) {
return child || parent;
}
return function (schema) {
parent(schema);
child(schema);
};
};
internals.validate = function (child, parent) {
if (!child ||
!parent) {
return child || parent;
}
return function (value, helpers) {
const result = parent(value, helpers);
if (result) {
if (result.errors &&
(!Array.isArray(result.errors) || result.errors.length)) {
return result;
}
value = result.value;
}
return child(value, helpers) || result;
};
};
'use strict';
const Assert = require('@hapi/hoek/lib/assert');
const Clone = require('@hapi/hoek/lib/clone');
const Cache = require('./cache');
const Common = require('./common');
const Compile = require('./compile');
const Errors = require('./errors');
const Extend = require('./extend');
const Manifest = require('./manifest');
const Ref = require('./ref');
const Template = require('./template');
const Trace = require('./trace');
let Schemas;
const internals = {
types: {
alternatives: require('./types/alternatives'),
any: require('./types/any'),
array: require('./types/array'),
boolean: require('./types/boolean'),
date: require('./types/date'),
function: require('./types/function'),
link: require('./types/link'),
number: require('./types/number'),
object: require('./types/object'),
string: require('./types/string'),
symbol: require('./types/symbol')
},
aliases: {
alt: 'alternatives',
bool: 'boolean',
func: 'function'
}
};
if (Buffer) { // $lab:coverage:ignore$
internals.types.binary = require('./types/binary');
}
internals.root = function () {
const root = {
_types: new Set(Object.keys(internals.types))
};
// Types
for (const type of root._types) {
root[type] = function (...args) {
Assert(!args.length || ['alternatives', 'link', 'object'].includes(type), 'The', type, 'type does not allow arguments');
return internals.generate(this, internals.types[type], args);
};
}
// Shortcuts
for (const method of ['allow', 'custom', 'disallow', 'equal', 'exist', 'forbidden', 'invalid', 'not', 'only', 'optional', 'options', 'prefs', 'preferences', 'required', 'strip', 'valid', 'when']) {
root[method] = function (...args) {
return this.any()[method](...args);
};
}
// Methods
Object.assign(root, internals.methods);
// Aliases
for (const alias in internals.aliases) {
const target = internals.aliases[alias];
root[alias] = root[target];
}
root.x = root.expression;
// Trace
if (Trace.setup) { // $lab:coverage:ignore$
Trace.setup(root);
}
return root;
};
internals.methods = {
ValidationError: Errors.ValidationError,
version: Common.version,
cache: Cache.provider,
assert(value, schema, ...args /* [message], [options] */) {
internals.assert(value, schema, true, args);
},
attempt(value, schema, ...args /* [message], [options] */) {
return internals.assert(value, schema, false, args);
},
build(desc) {
Assert(typeof Manifest.build === 'function', 'Manifest functionality disabled');
return Manifest.build(this, desc);
},
checkPreferences(prefs) {
Common.checkPreferences(prefs);
},
compile(schema, options) {
return Compile.compile(this, schema, options);
},
defaults(modifier) {
Assert(typeof modifier === 'function', 'modifier must be a function');
const joi = Object.assign({}, this);
for (const type of joi._types) {
const schema = modifier(joi[type]());
Assert(Common.isSchema(schema), 'modifier must return a valid schema object');
joi[type] = function (...args) {
return internals.generate(this, schema, args);
};
}
return joi;
},
expression(...args) {
return new Template(...args);
},
extend(...extensions) {
Common.verifyFlat(extensions, 'extend');
Schemas = Schemas || require('./schemas');
Assert(extensions.length, 'You need to provide at least one extension');
this.assert(extensions, Schemas.extensions);
const joi = Object.assign({}, this);
joi._types = new Set(joi._types);
for (let extension of extensions) {
if (typeof extension === 'function') {
extension = extension(joi);
}
this.assert(extension, Schemas.extension);
const expanded = internals.expandExtension(extension, joi);
for (const item of expanded) {
Assert(joi[item.type] === undefined || joi._types.has(item.type), 'Cannot override name', item.type);
const base = item.base || this.any();
const schema = Extend.type(base, item);
joi._types.add(item.type);
joi[item.type] = function (...args) {
return internals.generate(this, schema, args);
};
}
}
return joi;
},
isError: Errors.ValidationError.isError,
isExpression: Template.isTemplate,
isRef: Ref.isRef,
isSchema: Common.isSchema,
in(...args) {
return Ref.in(...args);
},
override: Common.symbols.override,
ref(...args) {
return Ref.create(...args);
},
types() {
const types = {};
for (const type of this._types) {
types[type] = this[type]();
}
for (const target in internals.aliases) {
types[target] = this[target]();
}
return types;
}
};
// Helpers
internals.assert = function (value, schema, annotate, args /* [message], [options] */) {
const message = args[0] instanceof Error || typeof args[0] === 'string' ? args[0] : null;
const options = message ? args[1] : args[0];
const result = schema.validate(value, Common.preferences({ errors: { stack: true } }, options || {}));
let error = result.error;
if (!error) {
return result.value;
}
if (message instanceof Error) {
throw message;
}
const display = annotate && typeof error.annotate === 'function' ? error.annotate() : error.message;
if (error instanceof Errors.ValidationError === false) {
error = Clone(error);
}
error.message = message ? `${message} ${display}` : display;
throw error;
};
internals.generate = function (root, schema, args) {
Assert(root, 'Must be invoked on a Joi instance.');
schema.$_root = root;
if (!schema._definition.args ||
!args.length) {
return schema;
}
return schema._definition.args(schema, ...args);
};
internals.expandExtension = function (extension, joi) {
if (typeof extension.type === 'string') {
return [extension];
}
const extended = [];
for (const type of joi._types) {
if (extension.type.test(type)) {
const item = Object.assign({}, extension);
item.type = type;
item.base = joi[type]();
extended.push(item);
}
}
return extended;
};
module.exports = internals.root();
This diff is collapsed. Click to expand it.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment