obsidian/.obsidian/plugins/obsidian-local-images/main.js

21187 lines
1.5 MiB
JavaScript
Raw Normal View History

2024-06-28 02:13:12 -05:00
/*
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
if you want to view the source visit the plugins github repository
*/
'use strict';
var obsidian = require('obsidian');
var url_1 = require('url');
var path = require('path');
var EventEmitter = require('events');
var util_1 = require('util');
var require$$0$3 = require('stream');
var fs = require('fs');
var http = require('http');
var https = require('https');
var require$$0$2 = require('dns');
var os = require('os');
var require$$0$4 = require('buffer');
var zlib = require('zlib');
var http2 = require('http2');
var tls = require('tls');
var net = require('net');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var url_1__default = /*#__PURE__*/_interopDefaultLegacy(url_1);
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
var util_1__default = /*#__PURE__*/_interopDefaultLegacy(util_1);
var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$3);
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0$2);
var os__default = /*#__PURE__*/_interopDefaultLegacy(os);
var require$$0__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$0$4);
var zlib__default = /*#__PURE__*/_interopDefaultLegacy(zlib);
var http2__default = /*#__PURE__*/_interopDefaultLegacy(http2);
var tls__default = /*#__PURE__*/_interopDefaultLegacy(tls);
var net__default = /*#__PURE__*/_interopDefaultLegacy(net);
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
// Generic options
class AnalyzerOptions {
constructor(heuristic_replimit) {
this.heuristic_replimit = heuristic_replimit;
}
}
// Abstract class
class Analyzer {
constructor(analyzerOptions) {
this.options = analyzerOptions;
}
// Subclasser must implement
// Return boolean
isVulnerable(regExp) {
return false;
}
// Subclass must implement
// Returns an AttackString or null
genAttackString(regExp) {
return null;
}
}
var analyzer$1 = function(re, replimit) {
// Build an AST
let myRegExp = null;
let ast = null;
try {
// Construct a RegExp object
if (re instanceof RegExp) {
myRegExp = re;
} else if (typeof re === "string") {
myRegExp = new RegExp(re);
} else {
myRegExp = new RegExp(String(re));
}
// Build an AST
ast = regexpTree.parse(myRegExp);
} catch (err) {
// Invalid or unparseable input
return false;
}
let currentStarHeight = 0;
let maxObservedStarHeight = 0;
let repetitionCount = 0;
regexpTree.traverse(ast, {
Repetition: {
pre({ node }) {
repetitionCount++;
currentStarHeight++;
if (maxObservedStarHeight < currentStarHeight) {
maxObservedStarHeight = currentStarHeight;
}
},
post({ node }) {
currentStarHeight--;
}
}
});
return maxObservedStarHeight <= 1 && repetitionCount <= replimit;
};
var analyzer$1 = {
"AnalyzerOptions": AnalyzerOptions,
"Analyzer": Analyzer,
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to translate `/./s` to `/[\0-\uFFFF]/`.
*/
var compatDotallSTransform = {
// Whether `u` flag present. In which case we transform to
// \u{10FFFF} instead of \uFFFF.
_hasUFlag: false,
// Only run this plugin if we have `s` flag.
shouldRun: function shouldRun(ast) {
var shouldRun = ast.flags.includes('s');
if (!shouldRun) {
return false;
}
// Strip the `s` flag.
ast.flags = ast.flags.replace('s', '');
// Whether we have also `u`.
this._hasUFlag = ast.flags.includes('u');
return true;
},
Char: function Char(path) {
var node = path.node;
if (node.kind !== 'meta' || node.value !== '.') {
return;
}
var toValue = '\\uFFFF';
var toSymbol = '\uFFFF';
if (this._hasUFlag) {
toValue = '\\u{10FFFF}';
toSymbol = '\uDBFF\uDFFF';
}
path.replace({
type: 'CharacterClass',
expressions: [{
type: 'ClassRange',
from: {
type: 'Char',
value: '\\0',
kind: 'decimal',
symbol: '\0'
},
to: {
type: 'Char',
value: toValue,
kind: 'unicode',
symbol: toSymbol
}
}]
});
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to translate `/(?<name>a)\k<name>/` to `/(a)\1/`.
*/
var compatNamedCapturingGroupsTransform = {
// To track the names of the groups, and return them
// in the transform result state.
//
// A map from name to number: {foo: 2, bar: 4}
_groupNames: {},
/**
* Initialises the trasnform.
*/
init: function init() {
this._groupNames = {};
},
/**
* Returns extra state, which eventually is returned to
*/
getExtra: function getExtra() {
return this._groupNames;
},
Group: function Group(path) {
var node = path.node;
if (!node.name) {
return;
}
// Record group name.
this._groupNames[node.name] = node.number;
delete node.name;
delete node.nameRaw;
},
Backreference: function Backreference(path) {
var node = path.node;
if (node.kind !== 'name') {
return;
}
node.kind = 'number';
node.reference = node.number;
delete node.referenceRaw;
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to remove `x` flag `/foo/x` to `/foo/`.
*
* Note: other features of `x` flags (whitespace, comments) are
* already removed at parsing stage.
*/
var compatXFlagTransform = {
RegExp: function RegExp(_ref) {
var node = _ref.node;
if (node.flags.includes('x')) {
node.flags = node.flags.replace('x', '');
}
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var transforms$1 = {
// "dotAll" `s` flag
dotAll: compatDotallSTransform,
// Named capturing groups.
namedCapturingGroups: compatNamedCapturingGroupsTransform,
// `x` flag
xFlag: compatXFlagTransform
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* Helper `gen` function calls node type handler.
*/
function gen$1(node) {
return node ? generator$1[node.type](node) : '';
}
/**
* AST handler.
*/
var generator$1 = {
RegExp: function RegExp(node) {
return '/' + gen$1(node.body) + '/' + node.flags;
},
Alternative: function Alternative(node) {
return (node.expressions || []).map(gen$1).join('');
},
Disjunction: function Disjunction(node) {
return gen$1(node.left) + '|' + gen$1(node.right);
},
Group: function Group(node) {
var expression = gen$1(node.expression);
if (node.capturing) {
// A named group.
if (node.name) {
return '(?<' + (node.nameRaw || node.name) + '>' + expression + ')';
}
return '(' + expression + ')';
}
return '(?:' + expression + ')';
},
Backreference: function Backreference(node) {
switch (node.kind) {
case 'number':
return '\\' + node.reference;
case 'name':
return '\\k<' + (node.referenceRaw || node.reference) + '>';
default:
throw new TypeError('Unknown Backreference kind: ' + node.kind);
}
},
Assertion: function Assertion(node) {
switch (node.kind) {
case '^':
case '$':
case '\\b':
case '\\B':
return node.kind;
case 'Lookahead':
{
var assertion = gen$1(node.assertion);
if (node.negative) {
return '(?!' + assertion + ')';
}
return '(?=' + assertion + ')';
}
case 'Lookbehind':
{
var _assertion = gen$1(node.assertion);
if (node.negative) {
return '(?<!' + _assertion + ')';
}
return '(?<=' + _assertion + ')';
}
default:
throw new TypeError('Unknown Assertion kind: ' + node.kind);
}
},
CharacterClass: function CharacterClass(node) {
var expressions = node.expressions.map(gen$1).join('');
if (node.negative) {
return '[^' + expressions + ']';
}
return '[' + expressions + ']';
},
ClassRange: function ClassRange(node) {
return gen$1(node.from) + '-' + gen$1(node.to);
},
Repetition: function Repetition(node) {
return '' + gen$1(node.expression) + gen$1(node.quantifier);
},
Quantifier: function Quantifier(node) {
var quantifier = void 0;
var greedy = node.greedy ? '' : '?';
switch (node.kind) {
case '+':
case '?':
case '*':
quantifier = node.kind;
break;
case 'Range':
// Exact: {1}
if (node.from === node.to) {
quantifier = '{' + node.from + '}';
}
// Open: {1,}
else if (!node.to) {
quantifier = '{' + node.from + ',}';
}
// Closed: {1,3}
else {
quantifier = '{' + node.from + ',' + node.to + '}';
}
break;
default:
throw new TypeError('Unknown Quantifier kind: ' + node.kind);
}
return '' + quantifier + greedy;
},
Char: function Char(node) {
var value = node.value;
switch (node.kind) {
case 'simple':
{
if (node.escaped) {
return '\\' + value;
}
return value;
}
case 'hex':
case 'unicode':
case 'oct':
case 'decimal':
case 'control':
case 'meta':
return value;
default:
throw new TypeError('Unknown Char kind: ' + node.kind);
}
},
UnicodeProperty: function UnicodeProperty(node) {
var escapeChar = node.negative ? 'P' : 'p';
var namePart = void 0;
if (!node.shorthand && !node.binary) {
namePart = node.name + '=';
} else {
namePart = '';
}
return '\\' + escapeChar + '{' + namePart + node.value + '}';
}
};
var generator_1 = {
/**
* Generates a regexp string from an AST.
*
* @param Object ast - an AST node
*/
generate: gen$1
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var NON_BINARY_PROP_NAMES_TO_ALIASES = {
General_Category: 'gc',
Script: 'sc',
Script_Extensions: 'scx'
};
var NON_BINARY_ALIASES_TO_PROP_NAMES = inverseMap(NON_BINARY_PROP_NAMES_TO_ALIASES);
var BINARY_PROP_NAMES_TO_ALIASES = {
ASCII: 'ASCII',
ASCII_Hex_Digit: 'AHex',
Alphabetic: 'Alpha',
Any: 'Any',
Assigned: 'Assigned',
Bidi_Control: 'Bidi_C',
Bidi_Mirrored: 'Bidi_M',
Case_Ignorable: 'CI',
Cased: 'Cased',
Changes_When_Casefolded: 'CWCF',
Changes_When_Casemapped: 'CWCM',
Changes_When_Lowercased: 'CWL',
Changes_When_NFKC_Casefolded: 'CWKCF',
Changes_When_Titlecased: 'CWT',
Changes_When_Uppercased: 'CWU',
Dash: 'Dash',
Default_Ignorable_Code_Point: 'DI',
Deprecated: 'Dep',
Diacritic: 'Dia',
Emoji: 'Emoji',
Emoji_Component: 'Emoji_Component',
Emoji_Modifier: 'Emoji_Modifier',
Emoji_Modifier_Base: 'Emoji_Modifier_Base',
Emoji_Presentation: 'Emoji_Presentation',
Extended_Pictographic: 'Extended_Pictographic',
Extender: 'Ext',
Grapheme_Base: 'Gr_Base',
Grapheme_Extend: 'Gr_Ext',
Hex_Digit: 'Hex',
IDS_Binary_Operator: 'IDSB',
IDS_Trinary_Operator: 'IDST',
ID_Continue: 'IDC',
ID_Start: 'IDS',
Ideographic: 'Ideo',
Join_Control: 'Join_C',
Logical_Order_Exception: 'LOE',
Lowercase: 'Lower',
Math: 'Math',
Noncharacter_Code_Point: 'NChar',
Pattern_Syntax: 'Pat_Syn',
Pattern_White_Space: 'Pat_WS',
Quotation_Mark: 'QMark',
Radical: 'Radical',
Regional_Indicator: 'RI',
Sentence_Terminal: 'STerm',
Soft_Dotted: 'SD',
Terminal_Punctuation: 'Term',
Unified_Ideograph: 'UIdeo',
Uppercase: 'Upper',
Variation_Selector: 'VS',
White_Space: 'space',
XID_Continue: 'XIDC',
XID_Start: 'XIDS'
};
var BINARY_ALIASES_TO_PROP_NAMES = inverseMap(BINARY_PROP_NAMES_TO_ALIASES);
var GENERAL_CATEGORY_VALUE_TO_ALIASES = {
Cased_Letter: 'LC',
Close_Punctuation: 'Pe',
Connector_Punctuation: 'Pc',
Control: ['Cc', 'cntrl'],
Currency_Symbol: 'Sc',
Dash_Punctuation: 'Pd',
Decimal_Number: ['Nd', 'digit'],
Enclosing_Mark: 'Me',
Final_Punctuation: 'Pf',
Format: 'Cf',
Initial_Punctuation: 'Pi',
Letter: 'L',
Letter_Number: 'Nl',
Line_Separator: 'Zl',
Lowercase_Letter: 'Ll',
Mark: ['M', 'Combining_Mark'],
Math_Symbol: 'Sm',
Modifier_Letter: 'Lm',
Modifier_Symbol: 'Sk',
Nonspacing_Mark: 'Mn',
Number: 'N',
Open_Punctuation: 'Ps',
Other: 'C',
Other_Letter: 'Lo',
Other_Number: 'No',
Other_Punctuation: 'Po',
Other_Symbol: 'So',
Paragraph_Separator: 'Zp',
Private_Use: 'Co',
Punctuation: ['P', 'punct'],
Separator: 'Z',
Space_Separator: 'Zs',
Spacing_Mark: 'Mc',
Surrogate: 'Cs',
Symbol: 'S',
Titlecase_Letter: 'Lt',
Unassigned: 'Cn',
Uppercase_Letter: 'Lu'
};
var GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES = inverseMap(GENERAL_CATEGORY_VALUE_TO_ALIASES);
var SCRIPT_VALUE_TO_ALIASES = {
Adlam: 'Adlm',
Ahom: 'Ahom',
Anatolian_Hieroglyphs: 'Hluw',
Arabic: 'Arab',
Armenian: 'Armn',
Avestan: 'Avst',
Balinese: 'Bali',
Bamum: 'Bamu',
Bassa_Vah: 'Bass',
Batak: 'Batk',
Bengali: 'Beng',
Bhaiksuki: 'Bhks',
Bopomofo: 'Bopo',
Brahmi: 'Brah',
Braille: 'Brai',
Buginese: 'Bugi',
Buhid: 'Buhd',
Canadian_Aboriginal: 'Cans',
Carian: 'Cari',
Caucasian_Albanian: 'Aghb',
Chakma: 'Cakm',
Cham: 'Cham',
Cherokee: 'Cher',
Common: 'Zyyy',
Coptic: ['Copt', 'Qaac'],
Cuneiform: 'Xsux',
Cypriot: 'Cprt',
Cyrillic: 'Cyrl',
Deseret: 'Dsrt',
Devanagari: 'Deva',
Dogra: 'Dogr',
Duployan: 'Dupl',
Egyptian_Hieroglyphs: 'Egyp',
Elbasan: 'Elba',
Ethiopic: 'Ethi',
Georgian: 'Geor',
Glagolitic: 'Glag',
Gothic: 'Goth',
Grantha: 'Gran',
Greek: 'Grek',
Gujarati: 'Gujr',
Gunjala_Gondi: 'Gong',
Gurmukhi: 'Guru',
Han: 'Hani',
Hangul: 'Hang',
Hanifi_Rohingya: 'Rohg',
Hanunoo: 'Hano',
Hatran: 'Hatr',
Hebrew: 'Hebr',
Hiragana: 'Hira',
Imperial_Aramaic: 'Armi',
Inherited: ['Zinh', 'Qaai'],
Inscriptional_Pahlavi: 'Phli',
Inscriptional_Parthian: 'Prti',
Javanese: 'Java',
Kaithi: 'Kthi',
Kannada: 'Knda',
Katakana: 'Kana',
Kayah_Li: 'Kali',
Kharoshthi: 'Khar',
Khmer: 'Khmr',
Khojki: 'Khoj',
Khudawadi: 'Sind',
Lao: 'Laoo',
Latin: 'Latn',
Lepcha: 'Lepc',
Limbu: 'Limb',
Linear_A: 'Lina',
Linear_B: 'Linb',
Lisu: 'Lisu',
Lycian: 'Lyci',
Lydian: 'Lydi',
Mahajani: 'Mahj',
Makasar: 'Maka',
Malayalam: 'Mlym',
Mandaic: 'Mand',
Manichaean: 'Mani',
Marchen: 'Marc',
Medefaidrin: 'Medf',
Masaram_Gondi: 'Gonm',
Meetei_Mayek: 'Mtei',
Mende_Kikakui: 'Mend',
Meroitic_Cursive: 'Merc',
Meroitic_Hieroglyphs: 'Mero',
Miao: 'Plrd',
Modi: 'Modi',
Mongolian: 'Mong',
Mro: 'Mroo',
Multani: 'Mult',
Myanmar: 'Mymr',
Nabataean: 'Nbat',
New_Tai_Lue: 'Talu',
Newa: 'Newa',
Nko: 'Nkoo',
Nushu: 'Nshu',
Ogham: 'Ogam',
Ol_Chiki: 'Olck',
Old_Hungarian: 'Hung',
Old_Italic: 'Ital',
Old_North_Arabian: 'Narb',
Old_Permic: 'Perm',
Old_Persian: 'Xpeo',
Old_Sogdian: 'Sogo',
Old_South_Arabian: 'Sarb',
Old_Turkic: 'Orkh',
Oriya: 'Orya',
Osage: 'Osge',
Osmanya: 'Osma',
Pahawh_Hmong: 'Hmng',
Palmyrene: 'Palm',
Pau_Cin_Hau: 'Pauc',
Phags_Pa: 'Phag',
Phoenician: 'Phnx',
Psalter_Pahlavi: 'Phlp',
Rejang: 'Rjng',
Runic: 'Runr',
Samaritan: 'Samr',
Saurashtra: 'Saur',
Sharada: 'Shrd',
Shavian: 'Shaw',
Siddham: 'Sidd',
SignWriting: 'Sgnw',
Sinhala: 'Sinh',
Sogdian: 'Sogd',
Sora_Sompeng: 'Sora',
Soyombo: 'Soyo',
Sundanese: 'Sund',
Syloti_Nagri: 'Sylo',
Syriac: 'Syrc',
Tagalog: 'Tglg',
Tagbanwa: 'Tagb',
Tai_Le: 'Tale',
Tai_Tham: 'Lana',
Tai_Viet: 'Tavt',
Takri: 'Takr',
Tamil: 'Taml',
Tangut: 'Tang',
Telugu: 'Telu',
Thaana: 'Thaa',
Thai: 'Thai',
Tibetan: 'Tibt',
Tifinagh: 'Tfng',
Tirhuta: 'Tirh',
Ugaritic: 'Ugar',
Vai: 'Vaii',
Warang_Citi: 'Wara',
Yi: 'Yiii',
Zanabazar_Square: 'Zanb'
};
var SCRIPT_VALUE_ALIASES_TO_VALUE = inverseMap(SCRIPT_VALUE_TO_ALIASES);
function inverseMap(data) {
var inverse = {};
for (var name in data) {
if (!data.hasOwnProperty(name)) {
continue;
}
var value = data[name];
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
inverse[value[i]] = name;
}
} else {
inverse[value] = name;
}
}
return inverse;
}
function isValidName(name) {
return NON_BINARY_PROP_NAMES_TO_ALIASES.hasOwnProperty(name) || NON_BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name) || BINARY_PROP_NAMES_TO_ALIASES.hasOwnProperty(name) || BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name);
}
function isValidValue(name, value) {
if (isGeneralCategoryName(name)) {
return isGeneralCategoryValue(value);
}
if (isScriptCategoryName(name)) {
return isScriptCategoryValue(value);
}
return false;
}
function isAlias(name) {
return NON_BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name) || BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name);
}
function isGeneralCategoryName(name) {
return name === 'General_Category' || name == 'gc';
}
function isScriptCategoryName(name) {
return name === 'Script' || name === 'Script_Extensions' || name === 'sc' || name === 'scx';
}
function isGeneralCategoryValue(value) {
return GENERAL_CATEGORY_VALUE_TO_ALIASES.hasOwnProperty(value) || GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES.hasOwnProperty(value);
}
function isScriptCategoryValue(value) {
return SCRIPT_VALUE_TO_ALIASES.hasOwnProperty(value) || SCRIPT_VALUE_ALIASES_TO_VALUE.hasOwnProperty(value);
}
function isBinaryPropertyName(name) {
return BINARY_PROP_NAMES_TO_ALIASES.hasOwnProperty(name) || BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name);
}
function getCanonicalName(name) {
if (NON_BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name)) {
return NON_BINARY_ALIASES_TO_PROP_NAMES[name];
}
if (BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(name)) {
return BINARY_ALIASES_TO_PROP_NAMES[name];
}
return null;
}
function getCanonicalValue(value) {
if (GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES.hasOwnProperty(value)) {
return GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES[value];
}
if (SCRIPT_VALUE_ALIASES_TO_VALUE.hasOwnProperty(value)) {
return SCRIPT_VALUE_ALIASES_TO_VALUE[value];
}
if (BINARY_ALIASES_TO_PROP_NAMES.hasOwnProperty(value)) {
return BINARY_ALIASES_TO_PROP_NAMES[value];
}
return null;
}
var parserUnicodeProperties = {
isAlias: isAlias,
isValidName: isValidName,
isValidValue: isValidValue,
isGeneralCategoryValue: isGeneralCategoryValue,
isScriptCategoryValue: isScriptCategoryValue,
isBinaryPropertyName: isBinaryPropertyName,
getCanonicalName: getCanonicalName,
getCanonicalValue: getCanonicalValue,
NON_BINARY_PROP_NAMES_TO_ALIASES: NON_BINARY_PROP_NAMES_TO_ALIASES,
NON_BINARY_ALIASES_TO_PROP_NAMES: NON_BINARY_ALIASES_TO_PROP_NAMES,
BINARY_PROP_NAMES_TO_ALIASES: BINARY_PROP_NAMES_TO_ALIASES,
BINARY_ALIASES_TO_PROP_NAMES: BINARY_ALIASES_TO_PROP_NAMES,
GENERAL_CATEGORY_VALUE_TO_ALIASES: GENERAL_CATEGORY_VALUE_TO_ALIASES,
GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES: GENERAL_CATEGORY_VALUE_ALIASES_TO_VALUES,
SCRIPT_VALUE_TO_ALIASES: SCRIPT_VALUE_TO_ALIASES,
SCRIPT_VALUE_ALIASES_TO_VALUE: SCRIPT_VALUE_ALIASES_TO_VALUE
};
/**
* LR parser generated by the Syntax tool.
*
* https://www.npmjs.com/package/syntax-cli
*
* npm install -g syntax-cli
*
* syntax-cli --help
*
* To regenerate run:
*
* syntax-cli \
* --grammar ~/path-to-grammar-file \
* --mode <parsing-mode> \
* --output ~/path-to-output-parser-file.js
*/
/**
* Matched token text.
*/
var _slicedToArray$2 = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
function _toConsumableArray$8(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var yytext = void 0;
/**
* Storage object.
*/
var yy = {};
/**
* Result of semantic action.
*/
var __ = void 0;
/**
* Result location object.
*/
var __loc = void 0;
function yyloc(start, end) {
if (!yy.options.captureLocations) {
return null;
}
// Epsilon doesn't produce location.
if (!start || !end) {
return start || end;
}
return {
startOffset: start.startOffset,
endOffset: end.endOffset,
startLine: start.startLine,
endLine: end.endLine,
startColumn: start.startColumn,
endColumn: end.endColumn
};
}
var EOF = '$';
/**
* List of productions (generated by Syntax tool).
*/
var productions = [[-1, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [0, 4, function (_1, _2, _3, _4, _1loc, _2loc, _3loc, _4loc) {
__loc = yyloc(_1loc, _4loc);
__ = Node({
type: 'RegExp',
body: _2,
flags: checkFlags(_4)
}, loc(_1loc, _4loc || _3loc));
}], [1, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [1, 0, function () {
__loc = null;__ = '';
}], [2, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [2, 2, function (_1, _2, _1loc, _2loc) {
__loc = yyloc(_1loc, _2loc);__ = _1 + _2;
}], [3, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [4, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [4, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
// Location for empty disjunction: /|/
var _loc = null;
if (_2loc) {
_loc = loc(_1loc || _2loc, _3loc || _2loc);
}
__ = Node({
type: 'Disjunction',
left: _1,
right: _3
}, _loc);
}], [5, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);
if (_1.length === 0) {
__ = null;
return;
}
if (_1.length === 1) {
__ = Node(_1[0], __loc);
} else {
__ = Node({
type: 'Alternative',
expressions: _1
}, __loc);
}
}], [6, 0, function () {
__loc = null;__ = [];
}], [6, 2, function (_1, _2, _1loc, _2loc) {
__loc = yyloc(_1loc, _2loc);__ = _1.concat(_2);
}], [7, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Node(Object.assign({ type: 'Assertion' }, _1), __loc);
}], [7, 2, function (_1, _2, _1loc, _2loc) {
__loc = yyloc(_1loc, _2loc);
__ = _1;
if (_2) {
__ = Node({
type: 'Repetition',
expression: _1,
quantifier: _2
}, __loc);
}
}], [8, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = { kind: '^' };
}], [8, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = { kind: '$' };
}], [8, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = { kind: '\\b' };
}], [8, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = { kind: '\\B' };
}], [8, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = {
kind: 'Lookahead',
assertion: _2
};
}], [8, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = {
kind: 'Lookahead',
negative: true,
assertion: _2
};
}], [8, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = {
kind: 'Lookbehind',
assertion: _2
};
}], [8, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = {
kind: 'Lookbehind',
negative: true,
assertion: _2
};
}], [9, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [9, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [9, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Char(_1, 'simple', __loc);
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Char(_1.slice(1), 'simple', __loc);__.escaped = true;
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Char(_1, 'unicode', __loc);__.isSurrogatePair = true;
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Char(_1, 'unicode', __loc);
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = UnicodeProperty(_1, __loc);
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Char(_1, 'control', __loc);
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Char(_1, 'hex', __loc);
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Char(_1, 'oct', __loc);
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = GroupRefOrDecChar(_1, __loc);
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Char(_1, 'meta', __loc);
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Char(_1, 'meta', __loc);
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = NamedGroupRefOrChars(_1, _1loc);
}], [11, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [11, 0], [12, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [12, 2, function (_1, _2, _1loc, _2loc) {
__loc = yyloc(_1loc, _2loc);
_1.greedy = false;
__ = _1;
}], [13, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);
__ = Node({
type: 'Quantifier',
kind: _1,
greedy: true
}, __loc);
}], [13, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);
__ = Node({
type: 'Quantifier',
kind: _1,
greedy: true
}, __loc);
}], [13, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);
__ = Node({
type: 'Quantifier',
kind: _1,
greedy: true
}, __loc);
}], [13, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);
var range = getRange(_1);
__ = Node({
type: 'Quantifier',
kind: 'Range',
from: range[0],
to: range[0],
greedy: true
}, __loc);
}], [13, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);
__ = Node({
type: 'Quantifier',
kind: 'Range',
from: getRange(_1)[0],
greedy: true
}, __loc);
}], [13, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);
var range = getRange(_1);
__ = Node({
type: 'Quantifier',
kind: 'Range',
from: range[0],
to: range[1],
greedy: true
}, __loc);
}], [14, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [14, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [15, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
var nameRaw = String(_1);
var name = decodeUnicodeGroupName(nameRaw);
if (!yy.options.allowGroupNameDuplicates && namedGroups.hasOwnProperty(name)) {
throw new SyntaxError('Duplicate of the named group "' + name + '".');
}
namedGroups[name] = _1.groupNumber;
__ = Node({
type: 'Group',
capturing: true,
name: name,
nameRaw: nameRaw,
number: _1.groupNumber,
expression: _2
}, __loc);
}], [15, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = Node({
type: 'Group',
capturing: true,
number: _1.groupNumber,
expression: _2
}, __loc);
}], [16, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = Node({
type: 'Group',
capturing: false,
expression: _2
}, __loc);
}], [17, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = Node({
type: 'CharacterClass',
negative: true,
expressions: _2
}, __loc);
}], [17, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = Node({
type: 'CharacterClass',
expressions: _2
}, __loc);
}], [18, 0, function () {
__loc = null;__ = [];
}], [18, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [19, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = [_1];
}], [19, 2, function (_1, _2, _1loc, _2loc) {
__loc = yyloc(_1loc, _2loc);__ = [_1].concat(_2);
}], [19, 4, function (_1, _2, _3, _4, _1loc, _2loc, _3loc, _4loc) {
__loc = yyloc(_1loc, _4loc);
checkClassRange(_1, _3);
__ = [Node({
type: 'ClassRange',
from: _1,
to: _3
}, loc(_1loc, _3loc))];
if (_4) {
__ = __.concat(_4);
}
}], [20, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [20, 2, function (_1, _2, _1loc, _2loc) {
__loc = yyloc(_1loc, _2loc);__ = [_1].concat(_2);
}], [20, 4, function (_1, _2, _3, _4, _1loc, _2loc, _3loc, _4loc) {
__loc = yyloc(_1loc, _4loc);
checkClassRange(_1, _3);
__ = [Node({
type: 'ClassRange',
from: _1,
to: _3
}, loc(_1loc, _3loc))];
if (_4) {
__ = __.concat(_4);
}
}], [21, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Char(_1, 'simple', __loc);
}], [21, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [22, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [22, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = Char(_1, 'meta', __loc);
}]];
/**
* Encoded tokens map.
*/
var tokens = { "SLASH": "23", "CHAR": "24", "BAR": "25", "BOS": "26", "EOS": "27", "ESC_b": "28", "ESC_B": "29", "POS_LA_ASSERT": "30", "R_PAREN": "31", "NEG_LA_ASSERT": "32", "POS_LB_ASSERT": "33", "NEG_LB_ASSERT": "34", "ESC_CHAR": "35", "U_CODE_SURROGATE": "36", "U_CODE": "37", "U_PROP_VALUE_EXP": "38", "CTRL_CH": "39", "HEX_CODE": "40", "OCT_CODE": "41", "DEC_CODE": "42", "META_CHAR": "43", "ANY": "44", "NAMED_GROUP_REF": "45", "Q_MARK": "46", "STAR": "47", "PLUS": "48", "RANGE_EXACT": "49", "RANGE_OPEN": "50", "RANGE_CLOSED": "51", "NAMED_CAPTURE_GROUP": "52", "L_PAREN": "53", "NON_CAPTURE_GROUP": "54", "NEG_CLASS": "55", "R_BRACKET": "56", "L_BRACKET": "57", "DASH": "58", "$": "59" };
/**
* Parsing table (generated by Syntax tool).
*/
var table = [{ "0": 1, "23": "s2" }, { "59": "acc" }, { "3": 3, "4": 4, "5": 5, "6": 6, "23": "r10", "24": "r10", "25": "r10", "26": "r10", "27": "r10", "28": "r10", "29": "r10", "30": "r10", "32": "r10", "33": "r10", "34": "r10", "35": "r10", "36": "r10", "37": "r10", "38": "r10", "39": "r10", "40": "r10", "41": "r10", "42": "r10", "43": "r10", "44": "r10", "45": "r10", "52": "r10", "53": "r10", "54": "r10", "55": "r10", "57": "r10" }, { "23": "s7" }, { "23": "r6", "25": "s12" }, { "23": "r7", "25": "r7", "31": "r7" }, { "7": 14, "8": 15, "9": 16, "10": 25, "14": 27, "15": 42, "16": 43, "17": 26, "23": "r9", "24": "s28", "25": "r9", "26": "s17", "27": "s18", "28": "s19", "29": "s20", "30": "s21", "31": "r9", "32": "s22", "33": "s23", "34": "s24", "35": "s29", "36": "s30", "37": "s31", "38": "s32", "39": "s33", "40": "s34", "41": "s35", "42": "s36", "43": "s37", "44": "s38", "45": "s39", "52": "s44", "53": "s45", "54": "s46", "55": "s40", "57": "s41" }, { "1": 8, "2": 9, "24": "s10", "59": "r3" }, { "59": "r1" }, { "24": "s11", "59": "r2" }, { "24": "r4", "59": "r4" }, { "24": "r5", "59": "r5" }, { "5": 13, "6": 6, "23": "r10", "24": "r10", "25": "r10", "26": "r10", "27": "r10", "28": "r10", "29": "r10", "30": "r10", "31": "r10", "32": "r10", "33": "r10", "34": "r10", "35": "r10", "36": "r10", "37": "r10", "38": "r10", "39": "r10", "40": "r10", "41": "r10", "42": "r10", "43": "r10", "44": "r10", "45": "r10", "52": "r10", "53": "r10", "54": "r10", "55": "r10", "57": "r10" }, { "23": "r8", "25": "r8", "31": "r8" }, { "23": "r11", "24": "r11", "25": "r11", "26": "r11", "27": "r11", "28": "r11", "29": "r11", "30": "r11", "31": "r11", "32": "r11", "33": "r11", "34": "r11", "35": "r11", "36": "r11", "37": "r11", "38": "r11", "39": "r11", "40": "r11", "41": "r11", "42": "r11", "43": "r11", "44": "r11", "45": "r11", "52": "r11", "53": "r11", "54": "r11", "55": "r11", "57": "r11" }, { "23": "r12", "24": "r12", "25": "r12", "26": "r12", "27": "r12", "28": "r12", "29": "r12", "30": "r12", "31": "r12", "32": "r12", "33": "r12", "34": "r12", "35": "r12", "36": "r12", "37": "r12", "38": "r12", "39": "r12", "40": "r12", "41": "r12", "42": "r12", "43": "r12", "44": "r12", "45": "r12", "52": "r12", "53": "r12", "54": "r12", "55": "r12", "57": "r12" }, { "11": 47, "12": 48, "13": 49, "23": "r38", "24": "r38", "25": "r38", "26": "r38", "27": "r38", "28": "r38", "29": "r38", "30": "r38", "31": "r38", "32": "r38", "33": "r38", "34": "r38", "35": "r38", "36": "r38", "37": "r38", "38": "r38", "39": "r38", "40": "r38", "41": "r38", "42": "r38", "43": "r38", "44": "r38", "45": "r38", "46": "s52", "47": "s50", "48": "s51", "49": "s53", "50": "s54", "51": "s55", "52": "r38", "53": "r38", "54": "r38", "55": "r38", "57": "r38" }, { "23": "r14", "24": "r14", "25": "r14", "26": "r14", "27": "r14", "28": "r14", "29": "r14", "30": "r14", "31": "r14", "32": "r14", "33": "r14", "34": "r14", "35": "r14", "36": "r14", "37": "r14", "38": "r14", "39": "r14", "40": "r14", "41": "r14", "42": "r14", "43": "r14", "44": "r14", "45": "r14", "52": "r14", "53": "r14", "54": "r14", "55": "r14", "57": "r14" }, { "23": "r15", "24": "r15", "25": "r15", "26": "r15", "27": "r15", "28": "r15", "29": "r15", "30": "r15", "31": "r15", "32": "r15", "33": "r15", "34": "r15", "35": "r15", "36": "r15", "37": "r15", "38": "r15", "39": "r15", "40": "r15", "41": "r15", "42": "r15", "43": "r15", "44": "r15", "45": "r15", "52": "r15", "53": "r15", "54": "r15", "55": "r15", "57": "r15" }, { "23": "r16", "24": "r16", "25": "r16", "26": "r16", "27": "r16", "28": "r16", "29": "r16", "30": "r16", "31": "r16", "32": "r16", "33": "r16", "34": "r16", "35": "r16", "36": "r16", "37": "r16", "38": "r16", "39": "r16", "40": "r16", "41": "r16", "42": "r16", "43": "r16", "44": "r16", "45": "r16", "52": "r16", "53": "r16", "54": "r16", "55": "r16", "57": "r16" }, { "23": "r17", "24": "r17", "25": "r17", "26": "r17", "27": "r17", "28": "r17", "29": "r17", "30": "r17", "31": "r17", "32": "r17", "33": "r17", "34": "r17", "35": "r17", "36": "r17", "37": "r17", "38": "r17", "39": "r17", "40": "r17", "41"
/**
* Parsing stack.
*/
var stack = [];
/**
* Tokenizer instance.
*/
var tokenizer = void 0;
/**
* Generic tokenizer used by the parser in the Syntax tool.
*
* https://www.npmjs.com/package/syntax-cli
*
* See `--custom-tokinzer` to skip this generation, and use a custom one.
*/
var lexRules = [[/^#[^\n]+/, function () {/* skip comments */}], [/^\s+/, function () {/* skip whitespace */}], [/^-/, function () {
return 'DASH';
}], [/^\//, function () {
return 'CHAR';
}], [/^#/, function () {
return 'CHAR';
}], [/^\|/, function () {
return 'CHAR';
}], [/^\./, function () {
return 'CHAR';
}], [/^\{/, function () {
return 'CHAR';
}], [/^\{\d+\}/, function () {
return 'RANGE_EXACT';
}], [/^\{\d+,\}/, function () {
return 'RANGE_OPEN';
}], [/^\{\d+,\d+\}/, function () {
return 'RANGE_CLOSED';
}], [/^\\k<(([\u0041-\u005a\u0061-\u007a\u00aa\u00b5\u00ba\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376-\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e-\u066f\u0671-\u06d3\u06d5\u06e5-\u06e6\u06ee-\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4-\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u08a0-\u08b4\u08b6-\u08bd\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f-\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc-\u09dd\u09df-\u09e1\u09f0-\u09f1\u09fc\u0a05-\u0a0a\u0a0f-\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32-\u0a33\u0a35-\u0a36\u0a38-\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2-\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0-\u0ae1\u0af9\u0b05-\u0b0c\u0b0f-\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32-\u0b33\u0b35-\u0b39\u0b3d\u0b5c-\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99-\u0b9a\u0b9c\u0b9e-\u0b9f\u0ba3-\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c60-\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0-\u0ce1\u0cf1-\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32-\u0e33\u0e40-\u0e46\u0e81-\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2-\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065-\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae-\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5-\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2-\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fef\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a-\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7c6\ua7f7-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd-\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5-\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab67\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb0
var groupName = yytext.slice(3, -1);
validateUnicodeGroupName(groupName, this.getCurrentState());
return 'NAMED_GROUP_REF';
}], [/^\\b/, function () {
return 'ESC_b';
}], [/^\\B/, function () {
return 'ESC_B';
}], [/^\\c[a-zA-Z]/, function () {
return 'CTRL_CH';
}], [/^\\0\d{1,2}/, function () {
return 'OCT_CODE';
}], [/^\\0/, function () {
return 'DEC_CODE';
}], [/^\\\d{1,3}/, function () {
return 'DEC_CODE';
}], [/^\\u[dD][89abAB][0-9a-fA-F]{2}\\u[dD][c-fC-F][0-9a-fA-F]{2}/, function () {
return 'U_CODE_SURROGATE';
}], [/^\\u\{[0-9a-fA-F]{1,}\}/, function () {
return 'U_CODE';
}], [/^\\u[0-9a-fA-F]{4}/, function () {
return 'U_CODE';
}], [/^\\[pP]\{\w+(?:=\w+)?\}/, function () {
return 'U_PROP_VALUE_EXP';
}], [/^\\x[0-9a-fA-F]{2}/, function () {
return 'HEX_CODE';
}], [/^\\[tnrdDsSwWvf]/, function () {
return 'META_CHAR';
}], [/^\\\//, function () {
return 'ESC_CHAR';
}], [/^\\[ #]/, function () {
return 'ESC_CHAR';
}], [/^\\[\^\$\.\*\+\?\(\)\\\[\]\{\}\|\/]/, function () {
return 'ESC_CHAR';
}], [/^\\[^*?+\[()\\|]/, function () {
var s = this.getCurrentState();
if (s === 'u_class' && yytext === "\\-") {
return 'ESC_CHAR';
} else if (s === 'u' || s === 'xu' || s === 'u_class') {
throw new SyntaxError('invalid Unicode escape ' + yytext);
}
return 'ESC_CHAR';
}], [/^\(/, function () {
return 'CHAR';
}], [/^\)/, function () {
return 'CHAR';
}], [/^\(\?=/, function () {
return 'POS_LA_ASSERT';
}], [/^\(\?!/, function () {
return 'NEG_LA_ASSERT';
}], [/^\(\?<=/, function () {
return 'POS_LB_ASSERT';
}], [/^\(\?<!/, function () {
return 'NEG_LB_ASSERT';
}], [/^\(\?:/, function () {
return 'NON_CAPTURE_GROUP';
}], [/^\(\?<(([\u0041-\u005a\u0061-\u007a\u00aa\u00b5\u00ba\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376-\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e-\u066f\u0671-\u06d3\u06d5\u06e5-\u06e6\u06ee-\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4-\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u08a0-\u08b4\u08b6-\u08bd\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f-\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc-\u09dd\u09df-\u09e1\u09f0-\u09f1\u09fc\u0a05-\u0a0a\u0a0f-\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32-\u0a33\u0a35-\u0a36\u0a38-\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2-\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0-\u0ae1\u0af9\u0b05-\u0b0c\u0b0f-\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32-\u0b33\u0b35-\u0b39\u0b3d\u0b5c-\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99-\u0b9a\u0b9c\u0b9e-\u0b9f\u0ba3-\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c60-\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0-\u0ce1\u0cf1-\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32-\u0e33\u0e40-\u0e46\u0e81-\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2-\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065-\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae-\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5-\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2-\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fef\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a-\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7c6\ua7f7-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd-\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5-\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab67\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb
yytext = yytext.slice(3, -1);
validateUnicodeGroupName(yytext, this.getCurrentState());
return 'NAMED_CAPTURE_GROUP';
}], [/^\(/, function () {
return 'L_PAREN';
}], [/^\)/, function () {
return 'R_PAREN';
}], [/^[*?+[^$]/, function () {
return 'CHAR';
}], [/^\\\]/, function () {
return 'ESC_CHAR';
}], [/^\]/, function () {
this.popState();return 'R_BRACKET';
}], [/^\^/, function () {
return 'BOS';
}], [/^\$/, function () {
return 'EOS';
}], [/^\*/, function () {
return 'STAR';
}], [/^\?/, function () {
return 'Q_MARK';
}], [/^\+/, function () {
return 'PLUS';
}], [/^\|/, function () {
return 'BAR';
}], [/^\./, function () {
return 'ANY';
}], [/^\//, function () {
return 'SLASH';
}], [/^[^*?+\[()\\|]/, function () {
return 'CHAR';
}], [/^\[\^/, function () {
var s = this.getCurrentState();this.pushState(s === 'u' || s === 'xu' ? 'u_class' : 'class');return 'NEG_CLASS';
}], [/^\[/, function () {
var s = this.getCurrentState();this.pushState(s === 'u' || s === 'xu' ? 'u_class' : 'class');return 'L_BRACKET';
}]];
var lexRulesByConditions = { "INITIAL": [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 22, 23, 24, 26, 27, 30, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51], "u": [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 30, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51], "xu": [0, 1, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 30, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51], "x": [0, 1, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 22, 23, 24, 26, 27, 30, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51], "u_class": [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51], "class": [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51] };
var EOF_TOKEN = {
type: EOF,
value: ''
};
tokenizer = {
initString: function initString(string) {
this._string = string;
this._cursor = 0;
this._states = ['INITIAL'];
this._tokensQueue = [];
this._currentLine = 1;
this._currentColumn = 0;
this._currentLineBeginOffset = 0;
/**
* Matched token location data.
*/
this._tokenStartOffset = 0;
this._tokenEndOffset = 0;
this._tokenStartLine = 1;
this._tokenEndLine = 1;
this._tokenStartColumn = 0;
this._tokenEndColumn = 0;
return this;
},
/**
* Returns tokenizer states.
*/
getStates: function getStates() {
return this._states;
},
getCurrentState: function getCurrentState() {
return this._states[this._states.length - 1];
},
pushState: function pushState(state) {
this._states.push(state);
},
begin: function begin(state) {
this.pushState(state);
},
popState: function popState() {
if (this._states.length > 1) {
return this._states.pop();
}
return this._states[0];
},
getNextToken: function getNextToken() {
// Something was queued, return it.
if (this._tokensQueue.length > 0) {
return this.onToken(this._toToken(this._tokensQueue.shift()));
}
if (!this.hasMoreTokens()) {
return this.onToken(EOF_TOKEN);
}
var string = this._string.slice(this._cursor);
var lexRulesForState = lexRulesByConditions[this.getCurrentState()];
for (var i = 0; i < lexRulesForState.length; i++) {
var lexRuleIndex = lexRulesForState[i];
var lexRule = lexRules[lexRuleIndex];
var matched = this._match(string, lexRule[0]);
// Manual handling of EOF token (the end of string). Return it
// as `EOF` symbol.
if (string === '' && matched === '') {
this._cursor++;
}
if (matched !== null) {
yytext = matched;
var token = lexRule[1].call(this);
if (!token) {
return this.getNextToken();
}
// If multiple tokens are returned, save them to return
// on next `getNextToken` call.
if (Array.isArray(token)) {
var tokensToQueue = token.slice(1);
token = token[0];
if (tokensToQueue.length > 0) {
var _tokensQueue;
(_tokensQueue = this._tokensQueue).unshift.apply(_tokensQueue, _toConsumableArray$8(tokensToQueue));
}
}
return this.onToken(this._toToken(token, yytext));
}
}
if (this.isEOF()) {
this._cursor++;
return EOF_TOKEN;
}
this.throwUnexpectedToken(string[0], this._currentLine, this._currentColumn);
},
/**
* Throws default "Unexpected token" exception, showing the actual
* line from the source, pointing with the ^ marker to the bad token.
* In addition, shows `line:column` location.
*/
throwUnexpectedToken: function throwUnexpectedToken(symbol, line, column) {
var lineSource = this._string.split('\n')[line - 1];
var lineData = '';
if (lineSource) {
var pad = ' '.repeat(column);
lineData = '\n\n' + lineSource + '\n' + pad + '^\n';
}
throw new SyntaxError(lineData + 'Unexpected token: "' + symbol + '" ' + ('at ' + line + ':' + column + '.'));
},
getCursor: function getCursor() {
return this._cursor;
},
getCurrentLine: function getCurrentLine() {
return this._currentLine;
},
getCurrentColumn: function getCurrentColumn() {
return this._currentColumn;
},
_captureLocation: function _captureLocation(matched) {
var nlRe = /\n/g;
// Absolute offsets.
this._tokenStartOffset = this._cursor;
// Line-based locations, start.
this._tokenStartLine = this._currentLine;
this._tokenStartColumn = this._tokenStartOffset - this._currentLineBeginOffset;
// Extract `\n` in the matched token.
var nlMatch = void 0;
while ((nlMatch = nlRe.exec(matched)) !== null) {
this._currentLine++;
this._currentLineBeginOffset = this._tokenStartOffset + nlMatch.index + 1;
}
this._tokenEndOffset = this._cursor + matched.length;
// Line-based locations, end.
this._tokenEndLine = this._currentLine;
this._tokenEndColumn = this._currentColumn = this._tokenEndOffset - this._currentLineBeginOffset;
},
_toToken: function _toToken(tokenType) {
var yytext = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
return {
// Basic data.
type: tokenType,
value: yytext,
// Location data.
startOffset: this._tokenStartOffset,
endOffset: this._tokenEndOffset,
startLine: this._tokenStartLine,
endLine: this._tokenEndLine,
startColumn: this._tokenStartColumn,
endColumn: this._tokenEndColumn
};
},
isEOF: function isEOF() {
return this._cursor === this._string.length;
},
hasMoreTokens: function hasMoreTokens() {
return this._cursor <= this._string.length;
},
_match: function _match(string, regexp) {
var matched = string.match(regexp);
if (matched) {
// Handle `\n` in the matched token to track line numbers.
this._captureLocation(matched[0]);
this._cursor += matched[0].length;
return matched[0];
}
return null;
},
/**
* Allows analyzing, and transforming token. Default implementation
* just passes the token through.
*/
onToken: function onToken(token) {
return token;
}
};
/**
* Expose tokenizer so it can be accessed in semantic actions.
*/
yy.lexer = tokenizer;
yy.tokenizer = tokenizer;
/**
* Global parsing options. Some options can be shadowed per
* each `parse` call, if the optations are passed.
*
* Initalized to the `captureLocations` which is passed
* from the generator. Other options can be added at runtime.
*/
yy.options = {
captureLocations: true
};
/**
* Parsing module.
*/
var yyparse = {
/**
* Sets global parsing options.
*/
setOptions: function setOptions(options) {
yy.options = options;
return this;
},
/**
* Returns parsing options.
*/
getOptions: function getOptions() {
return yy.options;
},
/**
* Parses a string.
*/
parse: function parse(string, parseOptions) {
if (!tokenizer) {
throw new Error('Tokenizer instance wasn\'t specified.');
}
tokenizer.initString(string);
/**
* If parse options are passed, override global parse options for
* this call, and later restore global options.
*/
var globalOptions = yy.options;
if (parseOptions) {
yy.options = Object.assign({}, yy.options, parseOptions);
}
/**
* Allow callers to do setup work based on the
* parsing string, and passed options.
*/
yyparse.onParseBegin(string, tokenizer, yy.options);
stack.length = 0;
stack.push(0);
var token = tokenizer.getNextToken();
var shiftedToken = null;
do {
if (!token) {
// Restore options.
yy.options = globalOptions;
unexpectedEndOfInput();
}
var state = stack[stack.length - 1];
var column = tokens[token.type];
if (!table[state].hasOwnProperty(column)) {
yy.options = globalOptions;
unexpectedToken(token);
}
var entry = table[state][column];
// Shift action.
if (entry[0] === 's') {
var _loc2 = null;
if (yy.options.captureLocations) {
_loc2 = {
startOffset: token.startOffset,
endOffset: token.endOffset,
startLine: token.startLine,
endLine: token.endLine,
startColumn: token.startColumn,
endColumn: token.endColumn
};
}
shiftedToken = this.onShift(token);
stack.push({ symbol: tokens[shiftedToken.type], semanticValue: shiftedToken.value, loc: _loc2 }, Number(entry.slice(1)));
token = tokenizer.getNextToken();
}
// Reduce action.
else if (entry[0] === 'r') {
var productionNumber = entry.slice(1);
var production = productions[productionNumber];
var hasSemanticAction = typeof production[2] === 'function';
var semanticValueArgs = hasSemanticAction ? [] : null;
var locationArgs = hasSemanticAction && yy.options.captureLocations ? [] : null;
if (production[1] !== 0) {
var rhsLength = production[1];
while (rhsLength-- > 0) {
stack.pop();
var stackEntry = stack.pop();
if (hasSemanticAction) {
semanticValueArgs.unshift(stackEntry.semanticValue);
if (locationArgs) {
locationArgs.unshift(stackEntry.loc);
}
}
}
}
var reduceStackEntry = { symbol: production[0] };
if (hasSemanticAction) {
yytext = shiftedToken ? shiftedToken.value : null;
shiftedToken ? shiftedToken.value.length : null;
var semanticActionArgs = locationArgs !== null ? semanticValueArgs.concat(locationArgs) : semanticValueArgs;
production[2].apply(production, _toConsumableArray$8(semanticActionArgs));
reduceStackEntry.semanticValue = __;
if (locationArgs) {
reduceStackEntry.loc = __loc;
}
}
var nextState = stack[stack.length - 1];
var symbolToReduceWith = production[0];
stack.push(reduceStackEntry, table[nextState][symbolToReduceWith]);
}
// Accept.
else if (entry === 'acc') {
stack.pop();
var parsed = stack.pop();
if (stack.length !== 1 || stack[0] !== 0 || tokenizer.hasMoreTokens()) {
// Restore options.
yy.options = globalOptions;
unexpectedToken(token);
}
if (parsed.hasOwnProperty('semanticValue')) {
yy.options = globalOptions;
yyparse.onParseEnd(parsed.semanticValue);
return parsed.semanticValue;
}
yyparse.onParseEnd();
// Restore options.
yy.options = globalOptions;
return true;
}
} while (tokenizer.hasMoreTokens() || stack.length > 1);
},
setTokenizer: function setTokenizer(customTokenizer) {
tokenizer = customTokenizer;
return yyparse;
},
getTokenizer: function getTokenizer() {
return tokenizer;
},
onParseBegin: function onParseBegin(string, tokenizer, options) {},
onParseEnd: function onParseEnd(parsed) {},
/**
* Allows analyzing, and transforming shifted token. Default implementation
* just passes the token through.
*/
onShift: function onShift(token) {
return token;
}
};
/**
* Tracks capturing groups.
*/
var capturingGroupsCount = 0;
/**
* Tracks named groups.
*/
var namedGroups = {};
/**
* Parsing string.
*/
var parsingString = '';
yyparse.onParseBegin = function (string, lexer) {
parsingString = string;
capturingGroupsCount = 0;
namedGroups = {};
var lastSlash = string.lastIndexOf('/');
var flags = string.slice(lastSlash);
if (flags.includes('x') && flags.includes('u')) {
lexer.pushState('xu');
} else {
if (flags.includes('x')) {
lexer.pushState('x');
}
if (flags.includes('u')) {
lexer.pushState('u');
}
}
};
/**
* On shifting `(` remember its number to used on reduce.
*/
yyparse.onShift = function (token) {
if (token.type === 'L_PAREN' || token.type === 'NAMED_CAPTURE_GROUP') {
token.value = new String(token.value);
token.value.groupNumber = ++capturingGroupsCount;
}
return token;
};
/**
* Extracts ranges from the range string.
*/
function getRange(text) {
var range = text.match(/\d+/g).map(Number);
if (Number.isFinite(range[1]) && range[1] < range[0]) {
throw new SyntaxError('Numbers out of order in ' + text + ' quantifier');
}
return range;
}
/**
* Checks class range
*/
function checkClassRange(from, to) {
if (from.kind === 'control' || to.kind === 'control' || !isNaN(from.codePoint) && !isNaN(to.codePoint) && from.codePoint > to.codePoint) {
throw new SyntaxError('Range ' + from.value + '-' + to.value + ' out of order in character class');
}
}
// ---------------------- Unicode property -------------------------------------------
/**
* Unicode property.
*/
function UnicodeProperty(matched, loc) {
var negative = matched[1] === 'P';
var separatorIdx = matched.indexOf('=');
var name = matched.slice(3, separatorIdx !== -1 ? separatorIdx : -1);
var value = void 0;
// General_Category allows using only value as a shorthand.
var isShorthand = separatorIdx === -1 && parserUnicodeProperties.isGeneralCategoryValue(name);
// Binary propery name.
var isBinaryProperty = separatorIdx === -1 && parserUnicodeProperties.isBinaryPropertyName(name);
if (isShorthand) {
value = name;
name = 'General_Category';
} else if (isBinaryProperty) {
value = name;
} else {
if (!parserUnicodeProperties.isValidName(name)) {
throw new SyntaxError('Invalid unicode property name: ' + name + '.');
}
value = matched.slice(separatorIdx + 1, -1);
if (!parserUnicodeProperties.isValidValue(name, value)) {
throw new SyntaxError('Invalid ' + name + ' unicode property value: ' + value + '.');
}
}
return Node({
type: 'UnicodeProperty',
name: name,
value: value,
negative: negative,
shorthand: isShorthand,
binary: isBinaryProperty,
canonicalName: parserUnicodeProperties.getCanonicalName(name) || name,
canonicalValue: parserUnicodeProperties.getCanonicalValue(value) || value
}, loc);
}
// ----------------------------------------------------------------------------------
/**
* Creates a character node.
*/
function Char(value, kind, loc) {
var symbol = void 0;
var codePoint = void 0;
switch (kind) {
case 'decimal':
{
codePoint = Number(value.slice(1));
symbol = String.fromCodePoint(codePoint);
break;
}
case 'oct':
{
codePoint = parseInt(value.slice(1), 8);
symbol = String.fromCodePoint(codePoint);
break;
}
case 'hex':
case 'unicode':
{
if (value.lastIndexOf('\\u') > 0) {
var _value$split$slice = value.split('\\u').slice(1),
_value$split$slice2 = _slicedToArray$2(_value$split$slice, 2),
lead = _value$split$slice2[0],
trail = _value$split$slice2[1];
lead = parseInt(lead, 16);
trail = parseInt(trail, 16);
codePoint = (lead - 0xd800) * 0x400 + (trail - 0xdc00) + 0x10000;
symbol = String.fromCodePoint(codePoint);
} else {
var hex = value.slice(2).replace('{', '');
codePoint = parseInt(hex, 16);
if (codePoint > 0x10ffff) {
throw new SyntaxError('Bad character escape sequence: ' + value);
}
symbol = String.fromCodePoint(codePoint);
}
break;
}
case 'meta':
{
switch (value) {
case '\\t':
symbol = '\t';
codePoint = symbol.codePointAt(0);
break;
case '\\n':
symbol = '\n';
codePoint = symbol.codePointAt(0);
break;
case '\\r':
symbol = '\r';
codePoint = symbol.codePointAt(0);
break;
case '\\v':
symbol = '\v';
codePoint = symbol.codePointAt(0);
break;
case '\\f':
symbol = '\f';
codePoint = symbol.codePointAt(0);
break;
case '\\b':
symbol = '\b';
codePoint = symbol.codePointAt(0);
case '\\0':
symbol = '\0';
codePoint = 0;
case '.':
symbol = '.';
codePoint = NaN;
break;
default:
codePoint = NaN;
}
break;
}
case 'simple':
{
symbol = value;
codePoint = symbol.codePointAt(0);
break;
}
}
return Node({
type: 'Char',
value: value,
kind: kind,
symbol: symbol,
codePoint: codePoint
}, loc);
}
/**
* Valid flags per current ECMAScript spec and
* stage 3+ proposals.
*/
var validFlags = 'gimsuxy';
/**
* Checks the flags are valid, and that
* we don't duplicate flags.
*/
function checkFlags(flags) {
var seen = new Set();
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = flags[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var flag = _step.value;
if (seen.has(flag) || !validFlags.includes(flag)) {
throw new SyntaxError('Invalid flags: ' + flags);
}
seen.add(flag);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return flags.split('').sort().join('');
}
/**
* Parses patterns like \1, \2, etc. either as a backreference
* to a group, or a deciaml char code.
*/
function GroupRefOrDecChar(text, textLoc) {
var reference = Number(text.slice(1));
if (reference > 0 && reference <= capturingGroupsCount) {
return Node({
type: 'Backreference',
kind: 'number',
number: reference,
reference: reference
}, textLoc);
}
return Char(text, 'decimal', textLoc);
}
/**
* Unicode names.
*/
var uReStart = /^\\u[0-9a-fA-F]{4}/; // only matches start of string
var ucpReStart = /^\\u\{[0-9a-fA-F]{1,}\}/; // only matches start of string
var ucpReAnywhere = /\\u\{[0-9a-fA-F]{1,}\}/; // matches anywhere in string
/**
* Validates Unicode group name.
*/
function validateUnicodeGroupName(name, state) {
var isUnicodeName = ucpReAnywhere.test(name);
var isUnicodeState = state === 'u' || state === 'xu' || state === 'u_class';
if (isUnicodeName && !isUnicodeState) {
throw new SyntaxError('invalid group Unicode name "' + name + '", use `u` flag.');
}
return name;
}
// Matches the following production: https://tc39.es/ecma262/#prod-RegExpUnicodeEscapeSequence
//
// RegExpUnicodeEscapeSequence ::
// `u` LeadSurrogate `\u` TrailSurrogate # as 'leadSurrogate', 'trailSurrogate'
// `u` LeadSurrogate # as 'leadSurrogateOnly'
// `u` TrailSurrogate # as 'trailSurrogateOnly'
// `u` NonSurrogate # as 'nonSurrogate'
// `u` `{` CodePoint `}` # as 'codePoint'
//
// LeadSurrogate ::
// Hex4Digits but only if the SV of Hex4Digits is in the inclusive range 0xD800 to 0xDBFF # [dD][89aAbB][0-9a-fA-F]{2}
//
// TrailSurrogate ::
// Hex4Digits but only if the SV of Hex4Digits is in the inclusive range 0xDC00 to 0xDFFF # [dD][c-fC-F][0-9a-fA-F]{2}
//
// NonSurrogate ::
// Hex4Digits but only if the SV of Hex4Digits is not in the inclusive range 0xD800 to 0xDFFF # [0-9a-ce-fA-CE-F][0-9a-fA-F]{3}|[dD][0-7][0-9a-fA-F]{2}
//
// CodePoint ::
// HexDigits but only if MV of HexDigits ≤ 0x10FFFF # 0*(?:[0-9a-fA-F]{1,5}|10[0-9a-fA-F]{4})
//
var uidRe = /\\u(?:([dD][89aAbB][0-9a-fA-F]{2})\\u([dD][c-fC-F][0-9a-fA-F]{2})|([dD][89aAbB][0-9a-fA-F]{2})|([dD][c-fC-F][0-9a-fA-F]{2})|([0-9a-ce-fA-CE-F][0-9a-fA-F]{3}|[dD][0-7][0-9a-fA-F]{2})|\{(0*(?:[0-9a-fA-F]{1,5}|10[0-9a-fA-F]{4}))\})/;
function decodeUnicodeGroupName(name) {
return name.replace(new RegExp(uidRe, 'g'), function (_, leadSurrogate, trailSurrogate, leadSurrogateOnly, trailSurrogateOnly, nonSurrogate, codePoint) {
if (leadSurrogate) {
return String.fromCodePoint(parseInt(leadSurrogate, 16), parseInt(trailSurrogate, 16));
}
if (leadSurrogateOnly) {
return String.fromCodePoint(parseInt(leadSurrogateOnly, 16));
}
if (trailSurrogateOnly) {
// TODO: Per the spec: https://tc39.es/ecma262/#prod-RegExpUnicodeEscapeSequence
// > Each `\u` TrailSurrogate for which the choice of associated `u` LeadSurrogate is ambiguous shall be associated with the nearest possible `u` LeadSurrogate that would otherwise have no corresponding `\u` TrailSurrogate.
return String.fromCodePoint(parseInt(trailSurrogateOnly, 16));
}
if (nonSurrogate) {
return String.fromCodePoint(parseInt(nonSurrogate, 16));
}
if (codePoint) {
return String.fromCodePoint(parseInt(codePoint, 16));
}
return _;
});
}
/**
* Extracts from `\k<foo>` pattern either a backreference
* to a named capturing group (if it presents), or parses it
* as a list of char: `\k`, `<`, `f`, etc.
*/
function NamedGroupRefOrChars(text, textLoc) {
var referenceRaw = text.slice(3, -1);
var reference = decodeUnicodeGroupName(referenceRaw);
if (namedGroups.hasOwnProperty(reference)) {
return Node({
type: 'Backreference',
kind: 'name',
number: namedGroups[reference],
reference: reference,
referenceRaw: referenceRaw
}, textLoc);
}
// Else `\k<foo>` should be parsed as a list of `Char`s.
// This is really a 0.01% edge case, but we should handle it.
var startOffset = null;
var startLine = null;
var endLine = null;
var startColumn = null;
if (textLoc) {
startOffset = textLoc.startOffset;
startLine = textLoc.startLine;
endLine = textLoc.endLine;
startColumn = textLoc.startColumn;
}
var charRe = /^[\w$<>]/;
var loc = void 0;
var chars = [
// Init to first \k, taking 2 symbols.
Char(text.slice(1, 2), 'simple', startOffset ? {
startLine: startLine,
endLine: endLine,
startColumn: startColumn,
startOffset: startOffset,
endOffset: startOffset += 2,
endColumn: startColumn += 2
} : null)];
// For \k
chars[0].escaped = true;
// Other symbols.
text = text.slice(2);
while (text.length > 0) {
var matched = null;
// Unicode, \u003B or \u{003B}
if ((matched = text.match(uReStart)) || (matched = text.match(ucpReStart))) {
if (startOffset) {
loc = {
startLine: startLine,
endLine: endLine,
startColumn: startColumn,
startOffset: startOffset,
endOffset: startOffset += matched[0].length,
endColumn: startColumn += matched[0].length
};
}
chars.push(Char(matched[0], 'unicode', loc));
text = text.slice(matched[0].length);
}
// Simple char.
else if (matched = text.match(charRe)) {
if (startOffset) {
loc = {
startLine: startLine,
endLine: endLine,
startColumn: startColumn,
startOffset: startOffset,
endOffset: ++startOffset,
endColumn: ++startColumn
};
}
chars.push(Char(matched[0], 'simple', loc));
text = text.slice(1);
}
}
return chars;
}
/**
* Creates an AST node with a location.
*/
function Node(node, loc) {
if (yy.options.captureLocations) {
node.loc = {
source: parsingString.slice(loc.startOffset, loc.endOffset),
start: {
line: loc.startLine,
column: loc.startColumn,
offset: loc.startOffset
},
end: {
line: loc.endLine,
column: loc.endColumn,
offset: loc.endOffset
}
};
}
return node;
}
/**
* Creates location node.
*/
function loc(start, end) {
if (!yy.options.captureLocations) {
return null;
}
return {
startOffset: start.startOffset,
endOffset: end.endOffset,
startLine: start.startLine,
endLine: end.endLine,
startColumn: start.startColumn,
endColumn: end.endColumn
};
}
function unexpectedToken(token) {
if (token.type === EOF) {
unexpectedEndOfInput();
}
tokenizer.throwUnexpectedToken(token.value, token.startLine, token.startColumn);
}
function unexpectedEndOfInput() {
parseError('Unexpected end of input.');
}
function parseError(message) {
throw new SyntaxError(message);
}
var regexpTree$3 = yyparse;
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* Original parse function.
*/
var generatedParseFn = regexpTree$3.parse.bind(regexpTree$3);
/**
* Parses a regular expression.
*
* Override original `regexpTreeParser.parse` to convert a value to a string,
* since in regexp-tree we may pass strings, and RegExp instance.
*/
regexpTree$3.parse = function (regexp, options) {
return generatedParseFn('' + regexp, options);
};
// By default do not capture locations; callers may override.
regexpTree$3.setOptions({ captureLocations: false });
var parser$1 = regexpTree$3;
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var _createClass$6 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck$6(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var DEFAULT_COLLECTION_PROP = 'expressions';
var DEFAULT_SINGLE_PROP = 'expression';
/**
* NodePath class encapsulates a traversing node,
* its parent node, property name in the parent node, and
* an index (in case if a node is part of a collection).
* It also provides set of methods for AST manipulation.
*/
var NodePath = function () {
/**
* NodePath constructor.
*
* @param Object node - an AST node
* @param NodePath parentPath - a nullable parent path
* @param string property - property name of the node in the parent
* @param number index - index of the node in a collection.
*/
function NodePath(node) {
var parentPath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var property = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var index = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
_classCallCheck$6(this, NodePath);
this.node = node;
this.parentPath = parentPath;
this.parent = parentPath ? parentPath.node : null;
this.property = property;
this.index = index;
}
_createClass$6(NodePath, [{
key: '_enforceProp',
value: function _enforceProp(property) {
if (!this.node.hasOwnProperty(property)) {
throw new Error('Node of type ' + this.node.type + ' doesn\'t have "' + property + '" collection.');
}
}
/**
* Sets a node into a children collection or the single child.
* By default child nodes are supposed to be under `expressions` property.
* An explicit property can be passed.
*
* @param Object node - a node to set into a collection or as single child
* @param number index - index at which to set
* @param string property - name of the collection or single property
*/
}, {
key: 'setChild',
value: function setChild(node) {
var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var property = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var childPath = void 0;
if (index != null) {
if (!property) {
property = DEFAULT_COLLECTION_PROP;
}
this._enforceProp(property);
this.node[property][index] = node;
childPath = NodePath.getForNode(node, this, property, index);
} else {
if (!property) {
property = DEFAULT_SINGLE_PROP;
}
this._enforceProp(property);
this.node[property] = node;
childPath = NodePath.getForNode(node, this, property, null);
}
return childPath;
}
/**
* Appends a node to a children collection.
* By default child nodes are supposed to be under `expressions` property.
* An explicit property can be passed.
*
* @param Object node - a node to set into a collection or as single child
* @param string property - name of the collection or single property
*/
}, {
key: 'appendChild',
value: function appendChild(node) {
var property = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (!property) {
property = DEFAULT_COLLECTION_PROP;
}
this._enforceProp(property);
var end = this.node[property].length;
return this.setChild(node, end, property);
}
/**
* Inserts a node into a collection.
* By default child nodes are supposed to be under `expressions` property.
* An explicit property can be passed.
*
* @param Object node - a node to insert into a collection
* @param number index - index at which to insert
* @param string property - name of the collection property
*/
}, {
key: 'insertChildAt',
value: function insertChildAt(node, index) {
var property = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_COLLECTION_PROP;
this._enforceProp(property);
this.node[property].splice(index, 0, node);
// If we inserted a node before the traversing index,
// we should increase the later.
if (index <= NodePath.getTraversingIndex()) {
NodePath.updateTraversingIndex(+1);
}
this._rebuildIndex(this.node, property);
}
/**
* Removes a node.
*/
}, {
key: 'remove',
value: function remove() {
if (this.isRemoved()) {
return;
}
NodePath.registry.delete(this.node);
this.node = null;
if (!this.parent) {
return;
}
// A node is in a collection.
if (this.index !== null) {
this.parent[this.property].splice(this.index, 1);
// If we remove a node before the traversing index,
// we should increase the later.
if (this.index <= NodePath.getTraversingIndex()) {
NodePath.updateTraversingIndex(-1);
}
// Rebuild index.
this._rebuildIndex(this.parent, this.property);
this.index = null;
this.property = null;
return;
}
// A simple node.
delete this.parent[this.property];
this.property = null;
}
/**
* Rebuilds child nodes index (used on remove/insert).
*/
}, {
key: '_rebuildIndex',
value: function _rebuildIndex(parent, property) {
var parentPath = NodePath.getForNode(parent);
for (var i = 0; i < parent[property].length; i++) {
var path = NodePath.getForNode(parent[property][i], parentPath, property, i);
path.index = i;
}
}
/**
* Whether the path was removed.
*/
}, {
key: 'isRemoved',
value: function isRemoved() {
return this.node === null;
}
/**
* Replaces a node with the passed one.
*/
}, {
key: 'replace',
value: function replace(newNode) {
NodePath.registry.delete(this.node);
this.node = newNode;
if (!this.parent) {
return null;
}
// A node is in a collection.
if (this.index !== null) {
this.parent[this.property][this.index] = newNode;
}
// A simple node.
else {
this.parent[this.property] = newNode;
}
// Rebuild the node path for the new node.
return NodePath.getForNode(newNode, this.parentPath, this.property, this.index);
}
/**
* Updates a node inline.
*/
}, {
key: 'update',
value: function update(nodeProps) {
Object.assign(this.node, nodeProps);
}
/**
* Returns parent.
*/
}, {
key: 'getParent',
value: function getParent() {
return this.parentPath;
}
/**
* Returns nth child.
*/
}, {
key: 'getChild',
value: function getChild() {
var n = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
if (this.node.expressions) {
return NodePath.getForNode(this.node.expressions[n], this, DEFAULT_COLLECTION_PROP, n);
} else if (this.node.expression && n == 0) {
return NodePath.getForNode(this.node.expression, this, DEFAULT_SINGLE_PROP);
}
return null;
}
/**
* Whether a path node is syntactically equal to the passed one.
*
* NOTE: we don't rely on `source` property from the `loc` data
* (which would be the fastest comparison), since it might be unsync
* after several modifications. We use here simple `JSON.stringify`
* excluding the `loc` data.
*
* @param NodePath other - path to compare to.
* @return boolean
*/
}, {
key: 'hasEqualSource',
value: function hasEqualSource(path) {
return JSON.stringify(this.node, jsonSkipLoc) === JSON.stringify(path.node, jsonSkipLoc);
}
/**
* JSON-encodes a node skipping location.
*/
}, {
key: 'jsonEncode',
value: function jsonEncode() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
format = _ref.format,
useLoc = _ref.useLoc;
return JSON.stringify(this.node, useLoc ? null : jsonSkipLoc, format);
}
/**
* Returns previous sibling.
*/
}, {
key: 'getPreviousSibling',
value: function getPreviousSibling() {
if (!this.parent || this.index == null) {
return null;
}
return NodePath.getForNode(this.parent[this.property][this.index - 1], NodePath.getForNode(this.parent), this.property, this.index - 1);
}
/**
* Returns next sibling.
*/
}, {
key: 'getNextSibling',
value: function getNextSibling() {
if (!this.parent || this.index == null) {
return null;
}
return NodePath.getForNode(this.parent[this.property][this.index + 1], NodePath.getForNode(this.parent), this.property, this.index + 1);
}
/**
* Returns a NodePath instance for a node.
*
* The same NodePath can be reused in several places, e.g.
* a parent node passed for all its children.
*/
}], [{
key: 'getForNode',
value: function getForNode(node) {
var parentPath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var prop = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var index = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1;
if (!node) {
return null;
}
if (!NodePath.registry.has(node)) {
NodePath.registry.set(node, new NodePath(node, parentPath, prop, index == -1 ? null : index));
}
var path = NodePath.registry.get(node);
if (parentPath !== null) {
path.parentPath = parentPath;
path.parent = path.parentPath.node;
}
if (prop !== null) {
path.property = prop;
}
if (index >= 0) {
path.index = index;
}
return path;
}
/**
* Initializes the NodePath registry. The registry is a map from
* a node to its NodePath instance.
*/
}, {
key: 'initRegistry',
value: function initRegistry() {
if (!NodePath.registry) {
NodePath.registry = new Map();
}
NodePath.registry.clear();
}
/**
* Updates index of a currently traversing collection.
*/
}, {
key: 'updateTraversingIndex',
value: function updateTraversingIndex(dx) {
return NodePath.traversingIndexStack[NodePath.traversingIndexStack.length - 1] += dx;
}
/**
* Returns current traversing index.
*/
}, {
key: 'getTraversingIndex',
value: function getTraversingIndex() {
return NodePath.traversingIndexStack[NodePath.traversingIndexStack.length - 1];
}
}]);
return NodePath;
}();
NodePath.initRegistry();
/**
* Index of a currently traversing collection is stored on top of the
* `NodePath.traversingIndexStack`. Remove/insert methods can adjust
* this index.
*/
NodePath.traversingIndexStack = [];
// Helper function used to skip `loc` in JSON operations.
function jsonSkipLoc(prop, value) {
if (prop === 'loc') {
return undefined;
}
return value;
}
var nodePath = NodePath;
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* Does an actual AST traversal, using visitor pattern,
* and calling set of callbacks.
*
* Based on https://github.com/olov/ast-traverse
*
* Expects AST in Mozilla Parser API: nodes which are supposed to be
* handled should have `type` property.
*
* @param Object root - a root node to start traversal from.
*
* @param Object options - an object with set of callbacks:
*
* - `pre(node, parent, prop, index)` - a hook called on node enter
* - `post`(node, parent, prop, index) - a hook called on node exit
* - `skipProperty(prop)` - a predicated whether a property should be skipped
*/
function astTraverse(root) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var pre = options.pre;
var post = options.post;
var skipProperty = options.skipProperty;
function visit(node, parent, prop, idx) {
if (!node || typeof node.type !== 'string') {
return;
}
var res = undefined;
if (pre) {
res = pre(node, parent, prop, idx);
}
if (res !== false) {
// A node can be replaced during traversal, so we have to
// recalculate it from the parent, to avoid traversing "dead" nodes.
if (parent && parent[prop]) {
if (!isNaN(idx)) {
node = parent[prop][idx];
} else {
node = parent[prop];
}
}
for (var _prop in node) {
if (node.hasOwnProperty(_prop)) {
if (skipProperty ? skipProperty(_prop, node) : _prop[0] === '$') {
continue;
}
var child = node[_prop];
// Collection node.
//
// NOTE: a node (or several nodes) can be removed or inserted
// during traversal.
//
// Current traversing index is stored on top of the
// `NodePath.traversingIndexStack`. The stack is used to support
// recursive nature of the traversal.
//
// In this case `NodePath.traversingIndex` (which we use here) is
// updated in the NodePath remove/insert methods.
//
if (Array.isArray(child)) {
var index = 0;
nodePath.traversingIndexStack.push(index);
while (index < child.length) {
visit(child[index], node, _prop, index);
index = nodePath.updateTraversingIndex(+1);
}
nodePath.traversingIndexStack.pop();
}
// Simple node.
else {
visit(child, node, _prop);
}
}
}
}
if (post) {
post(node, parent, prop, idx);
}
}
visit(root, null);
}
var traverse = {
/**
* Traverses an AST.
*
* @param Object ast - an AST node
*
* @param Object | Array<Object> handlers:
*
* an object (or an array of objects)
*
* Each such object contains a handler function per node.
* In case of an array of handlers, they are applied in order.
* A handler may return a transformed node (or a different type).
*
* The per-node function may instead be an object with functions pre and post.
* pre is called before visiting the node, post after.
* If a handler is a function, it is treated as the pre function, with an empty post.
*
* @param Object options:
*
* a config object, specifying traversal options:
*
* `asNodes`: boolean - whether handlers should receives raw AST nodes
* (false by default), instead of a `NodePath` wrapper. Note, by default
* `NodePath` wrapper provides a set of convenient method to manipulate
* a traversing AST, and also has access to all parents list. A raw
* nodes traversal should be used in rare cases, when no `NodePath`
* features are needed.
*
* Special hooks:
*
* - `shouldRun(ast)` - a predicate determining whether the handler
* should be applied.
*
* NOTE: Multiple handlers are used as an optimization of applying all of
* them in one AST traversal pass.
*/
traverse: function traverse(ast, handlers) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { asNodes: false };
if (!Array.isArray(handlers)) {
handlers = [handlers];
}
// Filter out handlers by result of `shouldRun`, if the method is present.
handlers = handlers.filter(function (handler) {
if (typeof handler.shouldRun !== 'function') {
return true;
}
return handler.shouldRun(ast);
});
nodePath.initRegistry();
// Allow handlers to initializer themselves.
handlers.forEach(function (handler) {
if (typeof handler.init === 'function') {
handler.init(ast);
}
});
function getPathFor(node, parent, prop, index) {
var parentPath = nodePath.getForNode(parent);
var nodePath$1 = nodePath.getForNode(node, parentPath, prop, index);
return nodePath$1;
}
// Handle actual nodes.
astTraverse(ast, {
/**
* Handler on node enter.
*/
pre: function pre(node, parent, prop, index) {
var nodePath = void 0;
if (!options.asNodes) {
nodePath = getPathFor(node, parent, prop, index);
}
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = handlers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var handler = _step.value;
// "Catch-all" `*` handler.
if (typeof handler['*'] === 'function') {
if (nodePath) {
// A path/node can be removed by some previous handler.
if (!nodePath.isRemoved()) {
var handlerResult = handler['*'](nodePath);
// Explicitly stop traversal.
if (handlerResult === false) {
return false;
}
}
} else {
handler['*'](node, parent, prop, index);
}
}
// Per-node handler.
var handlerFuncPre = void 0;
if (typeof handler[node.type] === 'function') {
handlerFuncPre = handler[node.type];
} else if (typeof handler[node.type] === 'object' && typeof handler[node.type].pre === 'function') {
handlerFuncPre = handler[node.type].pre;
}
if (handlerFuncPre) {
if (nodePath) {
// A path/node can be removed by some previous handler.
if (!nodePath.isRemoved()) {
var _handlerResult = handlerFuncPre.call(handler, nodePath);
// Explicitly stop traversal.
if (_handlerResult === false) {
return false;
}
}
} else {
handlerFuncPre.call(handler, node, parent, prop, index);
}
}
} // Loop over handlers
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
},
// pre func
/**
* Handler on node exit.
*/
post: function post(node, parent, prop, index) {
if (!node) {
return;
}
var nodePath = void 0;
if (!options.asNodes) {
nodePath = getPathFor(node, parent, prop, index);
}
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = handlers[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var handler = _step2.value;
// Per-node handler.
var handlerFuncPost = void 0;
if (typeof handler[node.type] === 'object' && typeof handler[node.type].post === 'function') {
handlerFuncPost = handler[node.type].post;
}
if (handlerFuncPost) {
if (nodePath) {
// A path/node can be removed by some previous handler.
if (!nodePath.isRemoved()) {
var handlerResult = handlerFuncPost.call(handler, nodePath);
// Explicitly stop traversal.
if (handlerResult === false) {
return false;
}
}
} else {
handlerFuncPost.call(handler, node, parent, prop, index);
}
}
} // Loop over handlers
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
},
// post func
/**
* Skip locations by default.
*/
skipProperty: function skipProperty(prop) {
return prop === 'loc';
}
});
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var _createClass$5 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck$5(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Transform result.
*/
var TransformResult = function () {
/**
* Initializes a transform result for an AST.
*
* @param Object ast - an AST node
* @param mixed extra - any extra data a transform may return
*/
function TransformResult(ast) {
var extra = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
_classCallCheck$5(this, TransformResult);
this._ast = ast;
this._source = null;
this._string = null;
this._regexp = null;
this._extra = extra;
}
_createClass$5(TransformResult, [{
key: 'getAST',
value: function getAST() {
return this._ast;
}
}, {
key: 'setExtra',
value: function setExtra(extra) {
this._extra = extra;
}
}, {
key: 'getExtra',
value: function getExtra() {
return this._extra;
}
}, {
key: 'toRegExp',
value: function toRegExp() {
if (!this._regexp) {
this._regexp = new RegExp(this.getSource(), this._ast.flags);
}
return this._regexp;
}
}, {
key: 'getSource',
value: function getSource() {
if (!this._source) {
this._source = generator_1.generate(this._ast.body);
}
return this._source;
}
}, {
key: 'getFlags',
value: function getFlags() {
return this._ast.flags;
}
}, {
key: 'toString',
value: function toString() {
if (!this._string) {
this._string = generator_1.generate(this._ast);
}
return this._string;
}
}]);
return TransformResult;
}();
var transform = {
/**
* Expose `TransformResult`.
*/
TransformResult: TransformResult,
/**
* Transforms a regular expression applying a set of
* transformation handlers.
*
* @param string | AST | RegExp:
*
* a regular expression in different representations: a string,
* a RegExp object, or an AST.
*
* @param Object | Array<Object>:
*
* a handler (or a list of handlers) from `traverse` API.
*
* @return TransformResult instance.
*
* Example:
*
* transform(/[a-z]/i, {
* onChar(path) {
* const {node} = path;
*
* if (...) {
* path.remove();
* }
* }
* });
*/
transform: function transform(regexp, handlers) {
var ast = regexp;
if (regexp instanceof RegExp) {
regexp = '' + regexp;
}
if (typeof regexp === 'string') {
ast = parser$1.parse(regexp, {
captureLocations: true
});
}
traverse.traverse(ast, handlers);
return new TransformResult(ast);
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var compatTranspiler = {
/**
* Translates a regexp in new syntax to equivalent regexp in old syntax.
*
* @param string|RegExp|AST - regexp
* @param Array transformsWhitelist - names of the transforms to apply
*/
transform: function transform$1(regexp) {
var transformsWhitelist = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var transformToApply = transformsWhitelist.length > 0 ? transformsWhitelist : Object.keys(transforms$1);
var result = void 0;
// Collect extra data per transform.
var extra = {};
transformToApply.forEach(function (transformName) {
if (!transforms$1.hasOwnProperty(transformName)) {
throw new Error('Unknown compat-transform: ' + transformName + '. ' + 'Available transforms are: ' + Object.keys(transforms$1).join(', '));
}
var handler = transforms$1[transformName];
result = transform.transform(regexp, handler);
regexp = result.getAST();
// Collect `extra` transform result.
if (typeof handler.getExtra === 'function') {
extra[transformName] = handler.getExtra();
}
});
// Set the final extras for all transforms.
result.setExtra(extra);
return result;
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* Performs a deep copy of an simple object.
* Only handles scalar values, arrays and objects.
*
* @param obj Object
*/
var clone = function clone(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
var res = void 0;
if (Array.isArray(obj)) {
res = [];
} else {
res = {};
}
for (var i in obj) {
res[i] = clone(obj[i]);
}
return res;
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to transform surrogate pairs into single unicode code point
*
* \ud83d\ude80 -> \u{1f680}
*/
var charSurrogatePairToSingleUnicodeTransform = {
shouldRun: function shouldRun(ast) {
return ast.flags.includes('u');
},
Char: function Char(path) {
var node = path.node;
if (node.kind !== 'unicode' || !node.isSurrogatePair || isNaN(node.codePoint)) {
return;
}
node.value = '\\u{' + node.codePoint.toString(16) + '}';
delete node.isSurrogatePair;
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var UPPER_A_CP$1 = 'A'.codePointAt(0);
var UPPER_Z_CP$1 = 'Z'.codePointAt(0);
var LOWER_A_CP = 'a'.codePointAt(0);
var LOWER_Z_CP = 'z'.codePointAt(0);
var DIGIT_0_CP = '0'.codePointAt(0);
var DIGIT_9_CP = '9'.codePointAt(0);
/**
* A regexp-tree plugin to transform coded chars into simple chars.
*
* \u0061 -> a
*/
var charCodeToSimpleCharTransform = {
Char: function Char(path) {
var node = path.node,
parent = path.parent;
if (isNaN(node.codePoint) || node.kind === 'simple') {
return;
}
if (parent.type === 'ClassRange') {
if (!isSimpleRange(parent)) {
return;
}
}
if (!isPrintableASCIIChar(node.codePoint)) {
return;
}
var symbol = String.fromCodePoint(node.codePoint);
var newChar = {
type: 'Char',
kind: 'simple',
value: symbol,
symbol: symbol,
codePoint: node.codePoint
};
if (needsEscape(symbol, parent.type)) {
newChar.escaped = true;
}
path.replace(newChar);
}
};
/**
* Checks if a range is included either in 0-9, a-z or A-Z
* @param classRange
* @returns {boolean}
*/
function isSimpleRange(classRange) {
var from = classRange.from,
to = classRange.to;
return from.codePoint >= DIGIT_0_CP && from.codePoint <= DIGIT_9_CP && to.codePoint >= DIGIT_0_CP && to.codePoint <= DIGIT_9_CP || from.codePoint >= UPPER_A_CP$1 && from.codePoint <= UPPER_Z_CP$1 && to.codePoint >= UPPER_A_CP$1 && to.codePoint <= UPPER_Z_CP$1 || from.codePoint >= LOWER_A_CP && from.codePoint <= LOWER_Z_CP && to.codePoint >= LOWER_A_CP && to.codePoint <= LOWER_Z_CP;
}
/**
* Checks if a code point in the range of printable ASCII chars
* (DEL char excluded)
* @param codePoint
* @returns {boolean}
*/
function isPrintableASCIIChar(codePoint) {
return codePoint >= 0x20 && codePoint <= 0x7e;
}
function needsEscape(symbol, parentType) {
if (parentType === 'ClassRange' || parentType === 'CharacterClass') {
return (/[\]\\^-]/.test(symbol)
);
}
return (/[*[()+?^$./\\|{}]/.test(symbol)
);
}
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var UPPER_A_CP = 'A'.codePointAt(0);
var UPPER_Z_CP = 'Z'.codePointAt(0);
/**
* Transforms case-insensitive regexp to lowercase
*
* /AaBbÏ/i -> /aabbï/i
*/
var charCaseInsensitiveLowercaseTransform = {
_AZClassRanges: null,
_hasUFlag: false,
init: function init(ast) {
this._AZClassRanges = new Set();
this._hasUFlag = ast.flags.includes('u');
},
shouldRun: function shouldRun(ast) {
return ast.flags.includes('i');
},
Char: function Char(path) {
var node = path.node,
parent = path.parent;
if (isNaN(node.codePoint)) {
return;
}
// Engine support for case-insensitive matching without the u flag
// for characters above \u1000 does not seem reliable.
if (!this._hasUFlag && node.codePoint >= 0x1000) {
return;
}
if (parent.type === 'ClassRange') {
// The only class ranges we handle must be inside A-Z.
// After the `from` char is processed, the isAZClassRange test
// will be false, so we use a Set to keep track of parents and
// process the `to` char.
if (!this._AZClassRanges.has(parent) && !isAZClassRange(parent)) {
return;
}
this._AZClassRanges.add(parent);
}
var lower = node.symbol.toLowerCase();
if (lower !== node.symbol) {
node.value = displaySymbolAsValue(lower, node);
node.symbol = lower;
node.codePoint = lower.codePointAt(0);
}
}
};
function isAZClassRange(classRange) {
var from = classRange.from,
to = classRange.to;
// A-Z
return from.codePoint >= UPPER_A_CP && from.codePoint <= UPPER_Z_CP && to.codePoint >= UPPER_A_CP && to.codePoint <= UPPER_Z_CP;
}
function displaySymbolAsValue(symbol, node) {
var codePoint = symbol.codePointAt(0);
if (node.kind === 'decimal') {
return '\\' + codePoint;
}
if (node.kind === 'oct') {
return '\\0' + codePoint.toString(8);
}
if (node.kind === 'hex') {
return '\\x' + codePoint.toString(16);
}
if (node.kind === 'unicode') {
if (node.isSurrogatePair) {
var _getSurrogatePairFrom = getSurrogatePairFromCodePoint(codePoint),
lead = _getSurrogatePairFrom.lead,
trail = _getSurrogatePairFrom.trail;
return '\\u' + '0'.repeat(4 - lead.length) + lead + '\\u' + '0'.repeat(4 - trail.length) + trail;
} else if (node.value.includes('{')) {
return '\\u{' + codePoint.toString(16) + '}';
} else {
var code = codePoint.toString(16);
return '\\u' + '0'.repeat(4 - code.length) + code;
}
}
// simple
return symbol;
}
/**
* Converts a code point to a surrogate pair.
* Conversion algorithm is taken from The Unicode Standard 3.0 Section 3.7
* (https://www.unicode.org/versions/Unicode3.0.0/ch03.pdf)
* @param {number} codePoint - Between 0x10000 and 0x10ffff
* @returns {{lead: string, trail: string}}
*/
function getSurrogatePairFromCodePoint(codePoint) {
var lead = Math.floor((codePoint - 0x10000) / 0x400) + 0xd800;
var trail = (codePoint - 0x10000) % 0x400 + 0xdc00;
return {
lead: lead.toString(16),
trail: trail.toString(16)
};
}
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to remove duplicates from character classes.
*/
var charClassRemoveDuplicatesTransform = {
CharacterClass: function CharacterClass(path) {
var node = path.node;
var sources = {};
for (var i = 0; i < node.expressions.length; i++) {
var childPath = path.getChild(i);
var source = childPath.jsonEncode();
if (sources.hasOwnProperty(source)) {
childPath.remove();
// Since we remove the current node.
// TODO: make it simpler for users with a method.
i--;
}
sources[source] = true;
}
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* Flattens a nested disjunction node to a list.
*
* /a|b|c|d/
*
* {{{a, b}, c}, d} -> [a, b, c, d]
*/
function _toConsumableArray$7(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function disjunctionToList$1(node) {
if (node.type !== 'Disjunction') {
throw new TypeError('Expected "Disjunction" node, got "' + node.type + '"');
}
var list = [];
if (node.left && node.left.type === 'Disjunction') {
list.push.apply(list, _toConsumableArray$7(disjunctionToList$1(node.left)).concat([node.right]));
} else {
list.push(node.left, node.right);
}
return list;
}
/**
* Builds a nested disjunction node from a list.
*
* /a|b|c|d/
*
* [a, b, c, d] -> {{{a, b}, c}, d}
*/
function listToDisjunction$1(list) {
return list.reduce(function (left, right) {
return {
type: 'Disjunction',
left: left,
right: right
};
});
}
/**
* Increases a quantifier by one.
* Does not change greediness.
* * -> +
* + -> {2,}
* ? -> {1,2}
* {2} -> {3}
* {2,} -> {3,}
* {2,3} -> {3,4}
*/
function increaseQuantifierByOne$2(quantifier) {
if (quantifier.kind === '*') {
quantifier.kind = '+';
} else if (quantifier.kind === '+') {
quantifier.kind = 'Range';
quantifier.from = 2;
delete quantifier.to;
} else if (quantifier.kind === '?') {
quantifier.kind = 'Range';
quantifier.from = 1;
quantifier.to = 2;
} else if (quantifier.kind === 'Range') {
quantifier.from += 1;
if (quantifier.to) {
quantifier.to += 1;
}
}
}
var utils = {
disjunctionToList: disjunctionToList$1,
listToDisjunction: listToDisjunction$1,
increaseQuantifierByOne: increaseQuantifierByOne$2
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var increaseQuantifierByOne$1 = utils.increaseQuantifierByOne;
/**
* A regexp-tree plugin to merge quantifiers
*
* a+a+ -> a{2,}
* a{2}a{3} -> a{5}
* a{1,2}a{2,3} -> a{3,5}
*/
var quantifiersMergeTransform = {
Repetition: function Repetition(path) {
var node = path.node,
parent = path.parent;
if (parent.type !== 'Alternative' || !path.index) {
return;
}
var previousSibling = path.getPreviousSibling();
if (!previousSibling) {
return;
}
if (previousSibling.node.type === 'Repetition') {
if (!previousSibling.getChild().hasEqualSource(path.getChild())) {
return;
}
var _extractFromTo = extractFromTo(previousSibling.node.quantifier),
previousSiblingFrom = _extractFromTo.from,
previousSiblingTo = _extractFromTo.to;
var _extractFromTo2 = extractFromTo(node.quantifier),
nodeFrom = _extractFromTo2.from,
nodeTo = _extractFromTo2.to;
// It's does not seem reliable to merge quantifiers with different greediness
// when none of both is a greedy open range
if (previousSibling.node.quantifier.greedy !== node.quantifier.greedy && !isGreedyOpenRange(previousSibling.node.quantifier) && !isGreedyOpenRange(node.quantifier)) {
return;
}
// a*a* -> a*
// a*a+ -> a+
// a+a+ -> a{2,}
// a{2}a{4} -> a{6}
// a{1,2}a{2,3} -> a{3,5}
// a{1,}a{2,} -> a{3,}
// a+a{2,} -> a{3,}
// a??a{2,} -> a{2,}
// a*?a{2,} -> a{2,}
// a+?a{2,} -> a{3,}
node.quantifier.kind = 'Range';
node.quantifier.from = previousSiblingFrom + nodeFrom;
if (previousSiblingTo && nodeTo) {
node.quantifier.to = previousSiblingTo + nodeTo;
} else {
delete node.quantifier.to;
}
if (isGreedyOpenRange(previousSibling.node.quantifier) || isGreedyOpenRange(node.quantifier)) {
node.quantifier.greedy = true;
}
previousSibling.remove();
} else {
if (!previousSibling.hasEqualSource(path.getChild())) {
return;
}
increaseQuantifierByOne$1(node.quantifier);
previousSibling.remove();
}
}
};
function isGreedyOpenRange(quantifier) {
return quantifier.greedy && (quantifier.kind === '+' || quantifier.kind === '*' || quantifier.kind === 'Range' && !quantifier.to);
}
function extractFromTo(quantifier) {
var from = void 0,
to = void 0;
if (quantifier.kind === '*') {
from = 0;
} else if (quantifier.kind === '+') {
from = 1;
} else if (quantifier.kind === '?') {
from = 0;
to = 1;
} else {
from = quantifier.from;
if (quantifier.to) {
to = quantifier.to;
}
}
return { from: from, to: to };
}
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to replace different range-based quantifiers
* with their symbol equivalents.
*
* a{0,} -> a*
* a{1,} -> a+
* a{1} -> a
*
* NOTE: the following is automatically handled in the generator:
*
* a{3,3} -> a{3}
*/
var quantifierRangeToSymbolTransform = {
Quantifier: function Quantifier(path) {
var node = path.node;
if (node.kind !== 'Range') {
return;
}
// a{0,} -> a*
rewriteOpenZero(path);
// a{1,} -> a+
rewriteOpenOne(path);
// a{1} -> a
rewriteExactOne(path);
}
};
function rewriteOpenZero(path) {
var node = path.node;
if (node.from !== 0 || node.to) {
return;
}
node.kind = '*';
delete node.from;
}
function rewriteOpenOne(path) {
var node = path.node;
if (node.from !== 1 || node.to) {
return;
}
node.kind = '+';
delete node.from;
}
function rewriteExactOne(path) {
var node = path.node;
if (node.from !== 1 || node.to !== 1) {
return;
}
path.parentPath.replace(path.parentPath.node.expression);
}
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to simplify character classes
* spanning only one or two chars.
*
* [a-a] -> [a]
* [a-b] -> [ab]
*/
var charClassClassrangesToCharsTransform = {
ClassRange: function ClassRange(path) {
var node = path.node;
if (node.from.codePoint === node.to.codePoint) {
path.replace(node.from);
} else if (node.from.codePoint === node.to.codePoint - 1) {
path.getParent().insertChildAt(node.to, path.index + 1);
path.replace(node.from);
}
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to replace standard character classes with
* their meta symbols equivalents.
*/
function _toConsumableArray$6(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var charClassToMetaTransform = {
_hasIFlag: false,
_hasUFlag: false,
init: function init(ast) {
this._hasIFlag = ast.flags.includes('i');
this._hasUFlag = ast.flags.includes('u');
},
CharacterClass: function CharacterClass(path) {
// [0-9] -> \d
rewriteNumberRanges(path);
// [a-zA-Z_0-9] -> \w
rewriteWordRanges(path, this._hasIFlag, this._hasUFlag);
// [ \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff] -> \s
rewriteWhitespaceRanges(path);
}
};
/**
* Rewrites number ranges: [0-9] -> \d
*/
function rewriteNumberRanges(path) {
var node = path.node;
node.expressions.forEach(function (expression, i) {
if (isFullNumberRange(expression)) {
path.getChild(i).replace({
type: 'Char',
value: '\\d',
kind: 'meta'
});
}
});
}
/**
* Rewrites word ranges: [a-zA-Z_0-9] -> \w
* Thus, the ranges may go in any order, and other symbols/ranges
* are kept untouched, e.g. [a-z_\dA-Z$] -> [\w$]
*/
function rewriteWordRanges(path, hasIFlag, hasUFlag) {
var node = path.node;
var numberPath = null;
var lowerCasePath = null;
var upperCasePath = null;
var underscorePath = null;
var u017fPath = null;
var u212aPath = null;
node.expressions.forEach(function (expression, i) {
// \d
if (isMetaChar(expression, '\\d')) {
numberPath = path.getChild(i);
}
// a-z
else if (isLowerCaseRange(expression)) {
lowerCasePath = path.getChild(i);
}
// A-Z
else if (isUpperCaseRange(expression)) {
upperCasePath = path.getChild(i);
}
// _
else if (isUnderscore(expression)) {
underscorePath = path.getChild(i);
} else if (hasIFlag && hasUFlag && isCodePoint(expression, 0x017f)) {
u017fPath = path.getChild(i);
} else if (hasIFlag && hasUFlag && isCodePoint(expression, 0x212a)) {
u212aPath = path.getChild(i);
}
});
// If we found the whole pattern, replace it.
if (numberPath && (lowerCasePath && upperCasePath || hasIFlag && (lowerCasePath || upperCasePath)) && underscorePath && (!hasUFlag || !hasIFlag || u017fPath && u212aPath)) {
// Put \w in place of \d.
numberPath.replace({
type: 'Char',
value: '\\w',
kind: 'meta'
});
// Other paths are removed.
if (lowerCasePath) {
lowerCasePath.remove();
}
if (upperCasePath) {
upperCasePath.remove();
}
underscorePath.remove();
if (u017fPath) {
u017fPath.remove();
}
if (u212aPath) {
u212aPath.remove();
}
}
}
/**
* Rewrites whitespace ranges: [ \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff] -> \s.
*/
var whitespaceRangeTests = [function (node) {
return isChar(node, ' ');
}].concat(_toConsumableArray$6(['\\f', '\\n', '\\r', '\\t', '\\v'].map(function (char) {
return function (node) {
return isMetaChar(node, char);
};
})), _toConsumableArray$6([0x00a0, 0x1680, 0x2028, 0x2029, 0x202f, 0x205f, 0x3000, 0xfeff].map(function (codePoint) {
return function (node) {
return isCodePoint(node, codePoint);
};
})), [function (node) {
return node.type === 'ClassRange' && isCodePoint(node.from, 0x2000) && isCodePoint(node.to, 0x200a);
}]);
function rewriteWhitespaceRanges(path) {
var node = path.node;
if (node.expressions.length < whitespaceRangeTests.length || !whitespaceRangeTests.every(function (test) {
return node.expressions.some(function (expression) {
return test(expression);
});
})) {
return;
}
// If we found the whole pattern, replace it.
// Put \s in place of \n.
var nNode = node.expressions.find(function (expression) {
return isMetaChar(expression, '\\n');
});
nNode.value = '\\s';
nNode.symbol = undefined;
nNode.codePoint = NaN;
// Other paths are removed.
node.expressions.map(function (expression, i) {
return whitespaceRangeTests.some(function (test) {
return test(expression);
}) ? path.getChild(i) : undefined;
}).filter(Boolean).forEach(function (path) {
return path.remove();
});
}
function isFullNumberRange(node) {
return node.type === 'ClassRange' && node.from.value === '0' && node.to.value === '9';
}
function isChar(node, value) {
var kind = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'simple';
return node.type === 'Char' && node.value === value && node.kind === kind;
}
function isMetaChar(node, value) {
return isChar(node, value, 'meta');
}
function isLowerCaseRange(node) {
return node.type === 'ClassRange' && node.from.value === 'a' && node.to.value === 'z';
}
function isUpperCaseRange(node) {
return node.type === 'ClassRange' && node.from.value === 'A' && node.to.value === 'Z';
}
function isUnderscore(node) {
return node.type === 'Char' && node.value === '_' && node.kind === 'simple';
}
function isCodePoint(node, codePoint) {
return node.type === 'Char' && node.kind === 'unicode' && node.codePoint === codePoint;
}
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to replace single char character classes with
* just that character.
*
* [\d] -> \d, [^\w] -> \W
*/
var charClassToSingleCharTransform = {
CharacterClass: function CharacterClass(path) {
var node = path.node;
if (node.expressions.length !== 1 || !hasAppropriateSiblings$1(path) || !isAppropriateChar(node.expressions[0])) {
return;
}
var _node$expressions$ = node.expressions[0],
value = _node$expressions$.value,
kind = _node$expressions$.kind,
escaped = _node$expressions$.escaped;
if (node.negative) {
// For negative can extract only meta chars like [^\w] -> \W
// cannot do for [^a] -> a (wrong).
if (!isMeta$1(value)) {
return;
}
value = getInverseMeta(value);
}
path.replace({
type: 'Char',
value: value,
kind: kind,
escaped: escaped || shouldEscape(value)
});
}
};
function isAppropriateChar(node) {
return node.type === 'Char' &&
// We don't extract [\b] (backspace) since \b has different
// semantics (word boundary).
node.value !== '\\b';
}
function isMeta$1(value) {
return (/^\\[dwsDWS]$/.test(value)
);
}
function getInverseMeta(value) {
return (/[dws]/.test(value) ? value.toUpperCase() : value.toLowerCase()
);
}
function hasAppropriateSiblings$1(path) {
var parent = path.parent,
index = path.index;
if (parent.type !== 'Alternative') {
return true;
}
var previousNode = parent.expressions[index - 1];
if (previousNode == null) {
return true;
}
// Don't optimized \1[0] to \10
if (previousNode.type === 'Backreference' && previousNode.kind === 'number') {
return false;
}
// Don't optimized \2[0] to \20
if (previousNode.type === 'Char' && previousNode.kind === 'decimal') {
return false;
}
return true;
}
// Note: \{ and \} are always preserved to avoid `a[{]2[}]` turning
// into `a{2}`.
function shouldEscape(value) {
return (/[*[()+?$./{}|]/.test(value)
);
}
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to remove unnecessary escape.
*
* \e -> e
*
* [\(] -> [(]
*/
var charEscapeUnescapeTransform = {
_hasXFlag: false,
init: function init(ast) {
this._hasXFlag = ast.flags.includes('x');
},
Char: function Char(path) {
var node = path.node;
if (!node.escaped) {
return;
}
if (shouldUnescape(path, this._hasXFlag)) {
delete node.escaped;
}
}
};
function shouldUnescape(path, hasXFlag) {
var value = path.node.value,
index = path.index,
parent = path.parent;
// In char class (, etc are allowed.
if (parent.type !== 'CharacterClass' && parent.type !== 'ClassRange') {
return !preservesEscape(value, index, parent, hasXFlag);
}
return !preservesInCharClass(value, index, parent);
}
/**
* \], \\, \^, \-
*/
function preservesInCharClass(value, index, parent) {
if (value === '^') {
// Avoid [\^a] turning into [^a]
return index === 0 && !parent.negative;
}
if (value === '-') {
// Avoid [a\-z] turning into [a-z]
return true;
}
return (/[\]\\]/.test(value)
);
}
function preservesEscape(value, index, parent, hasXFlag) {
if (value === '{') {
return preservesOpeningCurlyBraceEscape(index, parent);
}
if (value === '}') {
return preservesClosingCurlyBraceEscape(index, parent);
}
if (hasXFlag && /[ #]/.test(value)) {
return true;
}
return (/[*[()+?^$./\\|]/.test(value)
);
}
function consumeNumbers(startIndex, parent, rtl) {
var i = startIndex;
var siblingNode = (rtl ? i >= 0 : i < parent.expressions.length) && parent.expressions[i];
while (siblingNode && siblingNode.type === 'Char' && siblingNode.kind === 'simple' && !siblingNode.escaped && /\d/.test(siblingNode.value)) {
rtl ? i-- : i++;
siblingNode = (rtl ? i >= 0 : i < parent.expressions.length) && parent.expressions[i];
}
return Math.abs(startIndex - i);
}
function isSimpleChar(node, value) {
return node && node.type === 'Char' && node.kind === 'simple' && !node.escaped && node.value === value;
}
function preservesOpeningCurlyBraceEscape(index, parent) {
// (?:\{) -> (?:{)
if (index == null) {
return false;
}
var nbFollowingNumbers = consumeNumbers(index + 1, parent);
var i = index + nbFollowingNumbers + 1;
var nextSiblingNode = i < parent.expressions.length && parent.expressions[i];
if (nbFollowingNumbers) {
// Avoid \{3} turning into {3}
if (isSimpleChar(nextSiblingNode, '}')) {
return true;
}
if (isSimpleChar(nextSiblingNode, ',')) {
nbFollowingNumbers = consumeNumbers(i + 1, parent);
i = i + nbFollowingNumbers + 1;
nextSiblingNode = i < parent.expressions.length && parent.expressions[i];
// Avoid \{3,} turning into {3,}
return isSimpleChar(nextSiblingNode, '}');
}
}
return false;
}
function preservesClosingCurlyBraceEscape(index, parent) {
// (?:\{) -> (?:{)
if (index == null) {
return false;
}
var nbPrecedingNumbers = consumeNumbers(index - 1, parent, true);
var i = index - nbPrecedingNumbers - 1;
var previousSiblingNode = i >= 0 && parent.expressions[i];
// Avoid {3\} turning into {3}
if (nbPrecedingNumbers && isSimpleChar(previousSiblingNode, '{')) {
return true;
}
if (isSimpleChar(previousSiblingNode, ',')) {
nbPrecedingNumbers = consumeNumbers(i - 1, parent, true);
i = i - nbPrecedingNumbers - 1;
previousSiblingNode = i < parent.expressions.length && parent.expressions[i];
// Avoid {3,\} turning into {3,}
return nbPrecedingNumbers && isSimpleChar(previousSiblingNode, '{');
}
return false;
}
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to merge class ranges.
*
* [a-ec] -> [a-e]
* [a-ec-e] -> [a-e]
* [\w\da-f] -> [\w]
* [abcdef] -> [a-f]
*/
var charClassClassrangesMergeTransform = {
_hasIUFlags: false,
init: function init(ast) {
this._hasIUFlags = ast.flags.includes('i') && ast.flags.includes('u');
},
CharacterClass: function CharacterClass(path) {
var node = path.node;
var expressions = node.expressions;
var metas = [];
// Extract metas
expressions.forEach(function (expression) {
if (isMeta(expression)) {
metas.push(expression.value);
}
});
expressions.sort(sortCharClass);
for (var i = 0; i < expressions.length; i++) {
var expression = expressions[i];
if (fitsInMetas(expression, metas, this._hasIUFlags) || combinesWithPrecedingClassRange(expression, expressions[i - 1]) || combinesWithFollowingClassRange(expression, expressions[i + 1])) {
expressions.splice(i, 1);
i--;
} else {
var nbMergedChars = charCombinesWithPrecedingChars(expression, i, expressions);
expressions.splice(i - nbMergedChars + 1, nbMergedChars);
i -= nbMergedChars;
}
}
}
};
/**
* Sorts expressions in char class in the following order:
* - meta chars, ordered alphabetically by value
* - chars (except `control` kind) and class ranges, ordered alphabetically (`from` char is used for class ranges)
* - if ambiguous, class range comes before char
* - if ambiguous between two class ranges, orders alphabetically by `to` char
* - control chars, ordered alphabetically by value
* @param {Object} a - Left Char or ClassRange node
* @param {Object} b - Right Char or ClassRange node
* @returns {number}
*/
function sortCharClass(a, b) {
var aValue = getSortValue(a);
var bValue = getSortValue(b);
if (aValue === bValue) {
// We want ClassRange before Char
// [bb-d] -> [b-db]
if (a.type === 'ClassRange' && b.type !== 'ClassRange') {
return -1;
}
if (b.type === 'ClassRange' && a.type !== 'ClassRange') {
return 1;
}
if (a.type === 'ClassRange' && b.type === 'ClassRange') {
return getSortValue(a.to) - getSortValue(b.to);
}
if (isMeta(a) && isMeta(b) || isControl(a) && isControl(b)) {
return a.value < b.value ? -1 : 1;
}
}
return aValue - bValue;
}
/**
* @param {Object} expression - Char or ClassRange node
* @returns {number}
*/
function getSortValue(expression) {
if (expression.type === 'Char') {
if (expression.value === '-') {
return Infinity;
}
if (expression.kind === 'control') {
return Infinity;
}
if (expression.kind === 'meta' && isNaN(expression.codePoint)) {
return -1;
}
return expression.codePoint;
}
// ClassRange
return expression.from.codePoint;
}
/**
* Checks if a node is a meta char from the set \d\w\s\D\W\S
* @param {Object} expression - Char or ClassRange node
* @param {?string} value
* @returns {boolean}
*/
function isMeta(expression) {
var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
return expression.type === 'Char' && expression.kind === 'meta' && (value ? expression.value === value : /^\\[dws]$/i.test(expression.value));
}
/**
* @param {Object} expression - Char or ClassRange node
* @returns {boolean}
*/
function isControl(expression) {
return expression.type === 'Char' && expression.kind === 'control';
}
/**
* @param {Object} expression - Char or ClassRange node
* @param {string[]} metas - Array of meta chars, e.g. ["\\w", "\\s"]
* @param {boolean} hasIUFlags
* @returns {boolean}
*/
function fitsInMetas(expression, metas, hasIUFlags) {
for (var i = 0; i < metas.length; i++) {
if (fitsInMeta(expression, metas[i], hasIUFlags)) {
return true;
}
}
return false;
}
/**
* @param {Object} expression - Char or ClassRange node
* @param {string} meta - e.g. "\\w"
* @param {boolean} hasIUFlags
* @returns {boolean}
*/
function fitsInMeta(expression, meta, hasIUFlags) {
if (expression.type === 'ClassRange') {
return fitsInMeta(expression.from, meta, hasIUFlags) && fitsInMeta(expression.to, meta, hasIUFlags);
}
// Special cases:
// \S contains \w and \d
if (meta === '\\S' && (isMeta(expression, '\\w') || isMeta(expression, '\\d'))) {
return true;
}
// \D contains \W and \s
if (meta === '\\D' && (isMeta(expression, '\\W') || isMeta(expression, '\\s'))) {
return true;
}
// \w contains \d
if (meta === '\\w' && isMeta(expression, '\\d')) {
return true;
}
// \W contains \s
if (meta === '\\W' && isMeta(expression, '\\s')) {
return true;
}
if (expression.type !== 'Char' || isNaN(expression.codePoint)) {
return false;
}
if (meta === '\\s') {
return fitsInMetaS(expression);
}
if (meta === '\\S') {
return !fitsInMetaS(expression);
}
if (meta === '\\d') {
return fitsInMetaD(expression);
}
if (meta === '\\D') {
return !fitsInMetaD(expression);
}
if (meta === '\\w') {
return fitsInMetaW(expression, hasIUFlags);
}
if (meta === '\\W') {
return !fitsInMetaW(expression, hasIUFlags);
}
return false;
}
/**
* @param {Object} expression - Char node with codePoint
* @returns {boolean}
*/
function fitsInMetaS(expression) {
return expression.codePoint === 0x0009 || // \t
expression.codePoint === 0x000a || // \n
expression.codePoint === 0x000b || // \v
expression.codePoint === 0x000c || // \f
expression.codePoint === 0x000d || // \r
expression.codePoint === 0x0020 || // space
expression.codePoint === 0x00a0 || // nbsp
expression.codePoint === 0x1680 || // part of Zs
expression.codePoint >= 0x2000 && expression.codePoint <= 0x200a || // part of Zs
expression.codePoint === 0x2028 || // line separator
expression.codePoint === 0x2029 || // paragraph separator
expression.codePoint === 0x202f || // part of Zs
expression.codePoint === 0x205f || // part of Zs
expression.codePoint === 0x3000 || // part of Zs
expression.codePoint === 0xfeff; // zwnbsp
}
/**
* @param {Object} expression - Char node with codePoint
* @returns {boolean}
*/
function fitsInMetaD(expression) {
return expression.codePoint >= 0x30 && expression.codePoint <= 0x39; // 0-9
}
/**
* @param {Object} expression - Char node with codePoint
* @param {boolean} hasIUFlags
* @returns {boolean}
*/
function fitsInMetaW(expression, hasIUFlags) {
return fitsInMetaD(expression) || expression.codePoint >= 0x41 && expression.codePoint <= 0x5a || // A-Z
expression.codePoint >= 0x61 && expression.codePoint <= 0x7a || // a-z
expression.value === '_' || hasIUFlags && (expression.codePoint === 0x017f || expression.codePoint === 0x212a);
}
/**
* @param {Object} expression - Char or ClassRange node
* @param {Object} classRange - Char or ClassRange node
* @returns {boolean}
*/
function combinesWithPrecedingClassRange(expression, classRange) {
if (classRange && classRange.type === 'ClassRange') {
if (fitsInClassRange(expression, classRange)) {
// [a-gc] -> [a-g]
// [a-gc-e] -> [a-g]
return true;
} else if (
// We only want \w chars or char codes to keep readability
isMetaWCharOrCode(expression) && classRange.to.codePoint === expression.codePoint - 1) {
// [a-de] -> [a-e]
classRange.to = expression;
return true;
} else if (expression.type === 'ClassRange' && expression.from.codePoint <= classRange.to.codePoint + 1 && expression.to.codePoint >= classRange.from.codePoint - 1) {
// [a-db-f] -> [a-f]
// [b-fa-d] -> [a-f]
// [a-cd-f] -> [a-f]
if (expression.from.codePoint < classRange.from.codePoint) {
classRange.from = expression.from;
}
if (expression.to.codePoint > classRange.to.codePoint) {
classRange.to = expression.to;
}
return true;
}
}
return false;
}
/**
* @param {Object} expression - Char or ClassRange node
* @param {Object} classRange - Char or ClassRange node
* @returns {boolean}
*/
function combinesWithFollowingClassRange(expression, classRange) {
if (classRange && classRange.type === 'ClassRange') {
// Considering the elements were ordered alphabetically,
// there is only one case to handle
// [ab-e] -> [a-e]
if (
// We only want \w chars or char codes to keep readability
isMetaWCharOrCode(expression) && classRange.from.codePoint === expression.codePoint + 1) {
classRange.from = expression;
return true;
}
}
return false;
}
/**
* @param {Object} expression - Char or ClassRange node
* @param {Object} classRange - ClassRange node
* @returns {boolean}
*/
function fitsInClassRange(expression, classRange) {
if (expression.type === 'Char' && isNaN(expression.codePoint)) {
return false;
}
if (expression.type === 'ClassRange') {
return fitsInClassRange(expression.from, classRange) && fitsInClassRange(expression.to, classRange);
}
return expression.codePoint >= classRange.from.codePoint && expression.codePoint <= classRange.to.codePoint;
}
/**
* @param {Object} expression - Char or ClassRange node
* @param {Number} index
* @param {Object[]} expressions - expressions in CharClass
* @returns {number} - Number of characters combined with expression
*/
function charCombinesWithPrecedingChars(expression, index, expressions) {
// We only want \w chars or char codes to keep readability
if (!isMetaWCharOrCode(expression)) {
return 0;
}
var nbMergedChars = 0;
while (index > 0) {
var currentExpression = expressions[index];
var precedingExpresion = expressions[index - 1];
if (isMetaWCharOrCode(precedingExpresion) && precedingExpresion.codePoint === currentExpression.codePoint - 1) {
nbMergedChars++;
index--;
} else {
break;
}
}
if (nbMergedChars > 1) {
expressions[index] = {
type: 'ClassRange',
from: expressions[index],
to: expression
};
return nbMergedChars;
}
return 0;
}
function isMetaWCharOrCode(expression) {
return expression && expression.type === 'Char' && !isNaN(expression.codePoint) && (fitsInMetaW(expression, false) || expression.kind === 'unicode' || expression.kind === 'hex' || expression.kind === 'oct' || expression.kind === 'decimal');
}
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var disjunctionToList = utils.disjunctionToList,
listToDisjunction = utils.listToDisjunction;
/**
* Removes duplicates from a disjunction sequence:
*
* /(ab|bc|ab)+(xy|xy)+/ -> /(ab|bc)+(xy)+/
*/
var disjunctionRemoveDuplicatesTransform = {
Disjunction: function Disjunction(path) {
var node = path.node;
// Make unique nodes.
var uniqueNodesMap = {};
var parts = disjunctionToList(node).filter(function (part) {
var encoded = part ? nodePath.getForNode(part).jsonEncode() : 'null';
// Already recorded this part, filter out.
if (uniqueNodesMap.hasOwnProperty(encoded)) {
return false;
}
uniqueNodesMap[encoded] = part;
return true;
});
// Replace with the optimized disjunction.
path.replace(listToDisjunction(parts));
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to replace single char group disjunction to char group
*
* a|b|c -> [abc]
* [12]|3|4 -> [1234]
* (a|b|c) -> ([abc])
* (?:a|b|c) -> [abc]
*/
var groupSingleCharsToCharClass = {
Disjunction: function Disjunction(path) {
var node = path.node,
parent = path.parent;
if (!handlers[parent.type]) {
return;
}
var charset = new Map();
if (!shouldProcess(node, charset) || !charset.size) {
return;
}
var characterClass = {
type: 'CharacterClass',
expressions: Array.from(charset.keys()).sort().map(function (key) {
return charset.get(key);
})
};
handlers[parent.type](path.getParent(), characterClass);
}
};
var handlers = {
RegExp: function RegExp(path, characterClass) {
var node = path.node;
node.body = characterClass;
},
Group: function Group(path, characterClass) {
var node = path.node;
if (node.capturing) {
node.expression = characterClass;
} else {
path.replace(characterClass);
}
}
};
function shouldProcess(expression, charset) {
if (!expression) {
// Abort on empty disjunction part
return false;
}
var type = expression.type;
if (type === 'Disjunction') {
var left = expression.left,
right = expression.right;
return shouldProcess(left, charset) && shouldProcess(right, charset);
} else if (type === 'Char') {
var value = expression.value;
charset.set(value, expression);
return true;
} else if (type === 'CharacterClass' && !expression.negative) {
return expression.expressions.every(function (expression) {
return shouldProcess(expression, charset);
});
}
return false;
}
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to remove non-capturing empty groups.
*
* /(?:)a/ -> /a/
* /a|(?:)/ -> /a|/
*/
var removeEmptyGroupTransform = {
Group: function Group(path) {
var node = path.node,
parent = path.parent;
var childPath = path.getChild();
if (node.capturing || childPath) {
return;
}
if (parent.type === 'Repetition') {
path.getParent().replace(node);
} else if (parent.type !== 'RegExp') {
path.remove();
}
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A regexp-tree plugin to remove unnecessary groups.
*
* /(?:a)/ -> /a/
*/
function _toConsumableArray$5(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var ungroupTransform = {
Group: function Group(path) {
var node = path.node,
parent = path.parent;
var childPath = path.getChild();
if (node.capturing || !childPath) {
return;
}
// Don't optimize \1(?:0) to \10
if (!hasAppropriateSiblings(path)) {
return;
}
// Don't optimize /a(?:b|c)/ to /ab|c/
// but /(?:b|c)/ to /b|c/ is ok
if (childPath.node.type === 'Disjunction' && parent.type !== 'RegExp') {
return;
}
// Don't optimize /(?:ab)+/ to /ab+/
// but /(?:a)+/ to /a+/ is ok
// and /(?:[a-d])+/ to /[a-d]+/ is ok too
if (parent.type === 'Repetition' && childPath.node.type !== 'Char' && childPath.node.type !== 'CharacterClass') {
return;
}
if (childPath.node.type === 'Alternative') {
var parentPath = path.getParent();
if (parentPath.node.type === 'Alternative') {
// /abc(?:def)ghi/ When (?:def) is ungrouped its content must be merged with parent alternative
parentPath.replace({
type: 'Alternative',
expressions: [].concat(_toConsumableArray$5(parent.expressions.slice(0, path.index)), _toConsumableArray$5(childPath.node.expressions), _toConsumableArray$5(parent.expressions.slice(path.index + 1)))
});
}
} else {
path.replace(childPath.node);
}
}
};
function hasAppropriateSiblings(path) {
var parent = path.parent,
index = path.index;
if (parent.type !== 'Alternative') {
return true;
}
var previousNode = parent.expressions[index - 1];
if (previousNode == null) {
return true;
}
// Don't optimized \1(?:0) to \10
if (previousNode.type === 'Backreference' && previousNode.kind === 'number') {
return false;
}
// Don't optimized \2(?:0) to \20
if (previousNode.type === 'Char' && previousNode.kind === 'decimal') {
return false;
}
return true;
}
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
function _toConsumableArray$4(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var increaseQuantifierByOne = utils.increaseQuantifierByOne;
/**
* A regexp-tree plugin to combine repeating patterns.
*
* /^abcabcabc/ -> /^abc{3}/
* /^(?:abc){2}abc/ -> /^(?:abc){3}/
* /^abc(?:abc){2}/ -> /^(?:abc){3}/
*/
var combineRepeatingPatternsTransform = {
Alternative: function Alternative(path) {
var node = path.node;
// We can skip the first child
var index = 1;
while (index < node.expressions.length) {
var child = path.getChild(index);
index = Math.max(1, combineRepeatingPatternLeft(path, child, index));
if (index >= node.expressions.length) {
break;
}
child = path.getChild(index);
index = Math.max(1, combineWithPreviousRepetition(path, child, index));
if (index >= node.expressions.length) {
break;
}
child = path.getChild(index);
index = Math.max(1, combineRepetitionWithPrevious(path, child, index));
index++;
}
}
};
// abcabc -> (?:abc){2}
function combineRepeatingPatternLeft(alternative, child, index) {
var node = alternative.node;
var nbPossibleLengths = Math.ceil(index / 2);
var i = 0;
while (i < nbPossibleLengths) {
var startIndex = index - 2 * i - 1;
var right = void 0,
left = void 0;
if (i === 0) {
right = child;
left = alternative.getChild(startIndex);
} else {
right = nodePath.getForNode({
type: 'Alternative',
expressions: [].concat(_toConsumableArray$4(node.expressions.slice(index - i, index)), [child.node])
});
left = nodePath.getForNode({
type: 'Alternative',
expressions: [].concat(_toConsumableArray$4(node.expressions.slice(startIndex, index - i)))
});
}
if (right.hasEqualSource(left)) {
for (var j = 0; j < 2 * i + 1; j++) {
alternative.getChild(startIndex).remove();
}
child.replace({
type: 'Repetition',
expression: i === 0 && right.node.type !== 'Repetition' ? right.node : {
type: 'Group',
capturing: false,
expression: right.node
},
quantifier: {
type: 'Quantifier',
kind: 'Range',
from: 2,
to: 2,
greedy: true
}
});
return startIndex;
}
i++;
}
return index;
}
// (?:abc){2}abc -> (?:abc){3}
function combineWithPreviousRepetition(alternative, child, index) {
var node = alternative.node;
var i = 0;
while (i < index) {
var previousChild = alternative.getChild(i);
if (previousChild.node.type === 'Repetition' && previousChild.node.quantifier.greedy) {
var left = previousChild.getChild();
var right = void 0;
if (left.node.type === 'Group' && !left.node.capturing) {
left = left.getChild();
}
if (i + 1 === index) {
right = child;
if (right.node.type === 'Group' && !right.node.capturing) {
right = right.getChild();
}
} else {
right = nodePath.getForNode({
type: 'Alternative',
expressions: [].concat(_toConsumableArray$4(node.expressions.slice(i + 1, index + 1)))
});
}
if (left.hasEqualSource(right)) {
for (var j = i; j < index; j++) {
alternative.getChild(i + 1).remove();
}
increaseQuantifierByOne(previousChild.node.quantifier);
return i;
}
}
i++;
}
return index;
}
// abc(?:abc){2} -> (?:abc){3}
function combineRepetitionWithPrevious(alternative, child, index) {
var node = alternative.node;
if (child.node.type === 'Repetition' && child.node.quantifier.greedy) {
var right = child.getChild();
var left = void 0;
if (right.node.type === 'Group' && !right.node.capturing) {
right = right.getChild();
}
var rightLength = void 0;
if (right.node.type === 'Alternative') {
rightLength = right.node.expressions.length;
left = nodePath.getForNode({
type: 'Alternative',
expressions: [].concat(_toConsumableArray$4(node.expressions.slice(index - rightLength, index)))
});
} else {
rightLength = 1;
left = alternative.getChild(index - 1);
if (left.node.type === 'Group' && !left.node.capturing) {
left = left.getChild();
}
}
if (left.hasEqualSource(right)) {
for (var j = index - rightLength; j < index; j++) {
alternative.getChild(index - rightLength).remove();
}
increaseQuantifierByOne(child.node.quantifier);
return index - rightLength;
}
}
return index;
}
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var transforms = new Map([
// \ud83d\ude80 -> \u{1f680}
['charSurrogatePairToSingleUnicode', charSurrogatePairToSingleUnicodeTransform],
// \u0061 -> a
['charCodeToSimpleChar', charCodeToSimpleCharTransform],
// /Aa/i -> /aa/i
['charCaseInsensitiveLowerCaseTransform', charCaseInsensitiveLowercaseTransform],
// [\d\d] -> [\d]
['charClassRemoveDuplicates', charClassRemoveDuplicatesTransform],
// a{1,2}a{2,3} -> a{3,5}
['quantifiersMerge', quantifiersMergeTransform],
// a{1,} -> a+, a{3,3} -> a{3}, a{1} -> a
['quantifierRangeToSymbol', quantifierRangeToSymbolTransform],
// [a-a] -> [a], [a-b] -> [ab]
['charClassClassrangesToChars', charClassClassrangesToCharsTransform],
// [0-9] -> [\d]
['charClassToMeta', charClassToMetaTransform],
// [\d] -> \d, [^\w] -> \W
['charClassToSingleChar', charClassToSingleCharTransform],
// \e -> e
['charEscapeUnescape', charEscapeUnescapeTransform],
// [a-de-f] -> [a-f]
['charClassClassrangesMerge', charClassClassrangesMergeTransform],
// (ab|ab) -> (ab)
['disjunctionRemoveDuplicates', disjunctionRemoveDuplicatesTransform],
// (a|b|c) -> [abc]
['groupSingleCharsToCharClass', groupSingleCharsToCharClass],
// (?:)a -> a
['removeEmptyGroup', removeEmptyGroupTransform],
// (?:a) -> a
['ungroup', ungroupTransform],
// abcabcabc -> (?:abc){3}
['combineRepeatingPatterns', combineRepeatingPatternsTransform]]);
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var optimizer = {
/**
* Optimizer transforms a regular expression into an optimized version,
* replacing some sub-expressions with their idiomatic patterns.
*
* @param string | RegExp | AST - a regexp to optimize.
*
* @return TransformResult - an optimized regexp.
*
* Example:
*
* /[a-zA-Z_0-9][a-zA-Z_0-9]*\e{1,}/
*
* Optimized to:
*
* /\w+e+/
*/
optimize: function optimize(regexp) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref$whitelist = _ref.whitelist,
whitelist = _ref$whitelist === undefined ? [] : _ref$whitelist,
_ref$blacklist = _ref.blacklist,
blacklist = _ref$blacklist === undefined ? [] : _ref$blacklist;
var transformsRaw = whitelist.length > 0 ? whitelist : Array.from(transforms.keys());
var transformToApply = transformsRaw.filter(function (transform) {
return !blacklist.includes(transform);
});
var ast = regexp;
if (regexp instanceof RegExp) {
regexp = '' + regexp;
}
if (typeof regexp === 'string') {
ast = parser$1.parse(regexp);
}
var result = new transform.TransformResult(ast);
var prevResultString = void 0;
do {
// Get a copy of the current state here so
// we can compare it with the state at the
// end of the loop.
prevResultString = result.toString();
ast = clone(result.getAST());
transformToApply.forEach(function (transformName) {
if (!transforms.has(transformName)) {
throw new Error('Unknown optimization-transform: ' + transformName + '. ' + 'Available transforms are: ' + Array.from(transforms.keys()).join(', '));
}
var transformer = transforms.get(transformName);
// Don't override result just yet since we
// might want to rollback the transform
var newResult = transform.transform(ast, transformer);
if (newResult.toString() !== result.toString()) {
if (newResult.toString().length <= result.toString().length) {
result = newResult;
} else {
// Result has changed but is not shorter:
// restore ast to its previous state.
ast = clone(result.getAST());
}
}
});
// Keep running the optimizer until it stops
// making any change to the regexp.
} while (result.toString() !== prevResultString);
return result;
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* Epsilon, the empty string.
*/
var EPSILON$3 = 'ε';
/**
* Epsilon-closure.
*/
var EPSILON_CLOSURE$2 = EPSILON$3 + '*';
var specialSymbols = {
EPSILON: EPSILON$3,
EPSILON_CLOSURE: EPSILON_CLOSURE$2
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var _slicedToArray$1 = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _createClass$4 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _toConsumableArray$3(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck$4(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var EPSILON$2 = specialSymbols.EPSILON,
EPSILON_CLOSURE$1 = specialSymbols.EPSILON_CLOSURE;
/**
* NFA fragment.
*
* NFA sub-fragments can be combined to a larger NFAs building
* the resulting machine. Combining the fragments is done by patching
* edges of the in- and out-states.
*
* 2-states implementation, `in`, and `out`. Eventually all transitions
* go to the same `out`, which can further be connected via ε-transition
* with other fragment.
*/
var NFA = function () {
function NFA(inState, outState) {
_classCallCheck$4(this, NFA);
this.in = inState;
this.out = outState;
}
/**
* Tries to recognize a string based on this NFA fragment.
*/
_createClass$4(NFA, [{
key: 'matches',
value: function matches(string) {
return this.in.matches(string);
}
/**
* Returns an alphabet for this NFA.
*/
}, {
key: 'getAlphabet',
value: function getAlphabet() {
if (!this._alphabet) {
this._alphabet = new Set();
var table = this.getTransitionTable();
for (var state in table) {
var transitions = table[state];
for (var symbol in transitions) {
if (symbol !== EPSILON_CLOSURE$1) {
this._alphabet.add(symbol);
}
}
}
}
return this._alphabet;
}
/**
* Returns set of accepting states.
*/
}, {
key: 'getAcceptingStates',
value: function getAcceptingStates() {
if (!this._acceptingStates) {
// States are determined during table construction.
this.getTransitionTable();
}
return this._acceptingStates;
}
/**
* Returns accepting state numbers.
*/
}, {
key: 'getAcceptingStateNumbers',
value: function getAcceptingStateNumbers() {
if (!this._acceptingStateNumbers) {
this._acceptingStateNumbers = new Set();
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = this.getAcceptingStates()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var acceptingState = _step.value;
this._acceptingStateNumbers.add(acceptingState.number);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
return this._acceptingStateNumbers;
}
/**
* Builds and returns transition table.
*/
}, {
key: 'getTransitionTable',
value: function getTransitionTable() {
var _this = this;
if (!this._transitionTable) {
this._transitionTable = {};
this._acceptingStates = new Set();
var visited = new Set();
var symbols = new Set();
var visitState = function visitState(state) {
if (visited.has(state)) {
return;
}
visited.add(state);
state.number = visited.size;
_this._transitionTable[state.number] = {};
if (state.accepting) {
_this._acceptingStates.add(state);
}
var transitions = state.getTransitions();
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = transitions[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _ref = _step2.value;
var _ref2 = _slicedToArray$1(_ref, 2);
var symbol = _ref2[0];
var symbolTransitions = _ref2[1];
var combinedState = [];
symbols.add(symbol);
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = symbolTransitions[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var nextState = _step3.value;
visitState(nextState);
combinedState.push(nextState.number);
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
_this._transitionTable[state.number][symbol] = combinedState;
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
};
// Traverse the graph starting from the `in`.
visitState(this.in);
// Append epsilon-closure column.
visited.forEach(function (state) {
delete _this._transitionTable[state.number][EPSILON$2];
_this._transitionTable[state.number][EPSILON_CLOSURE$1] = [].concat(_toConsumableArray$3(state.getEpsilonClosure())).map(function (s) {
return s.number;
});
});
}
return this._transitionTable;
}
}]);
return NFA;
}();
var nfa = NFA;
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
// DFA minization.
/**
* Map from state to current set it goes.
*/
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); }
function _toConsumableArray$2(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var currentTransitionMap = null;
/**
* Takes a DFA, and returns a minimized version of it
* compressing some states to groups (using standard, 0-, 1-,
* 2-, ... N-equivalence algorithm).
*/
function minimize(dfa) {
var table = dfa.getTransitionTable();
var allStates = Object.keys(table);
var alphabet = dfa.getAlphabet();
var accepting = dfa.getAcceptingStateNumbers();
currentTransitionMap = {};
var nonAccepting = new Set();
allStates.forEach(function (state) {
state = Number(state);
var isAccepting = accepting.has(state);
if (isAccepting) {
currentTransitionMap[state] = accepting;
} else {
nonAccepting.add(state);
currentTransitionMap[state] = nonAccepting;
}
});
// ---------------------------------------------------------------------------
// Step 1: build equivalent sets.
// All [1..N] equivalent sets.
var all = [
// 0-equivalent sets.
[nonAccepting, accepting].filter(function (set) {
return set.size > 0;
})];
var current = void 0;
var previous = void 0;
// Top of the stack is the current list of sets to analyze.
current = all[all.length - 1];
// Previous set (to check whether we need to stop).
previous = all[all.length - 2];
// Until we'll not have the same N and N-1 equivalent rows.
var _loop = function _loop() {
var newTransitionMap = {};
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = current[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var _set = _step3.value;
// Handled states for this set.
var handledStates = {};
var _set2 = _toArray(_set),
first = _set2[0],
rest = _set2.slice(1);
handledStates[first] = new Set([first]);
// Have to compare each from the rest states with
// the already handled states, and see if they are equivalent.
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
restSets: for (var _iterator4 = rest[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var state = _step4.value;
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator5 = Object.keys(handledStates)[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var handledState = _step5.value;
// This and some previously handled state are equivalent --
// just append this state to the same set.
if (areEquivalent(state, handledState, table, alphabet)) {
handledStates[handledState].add(state);
handledStates[state] = handledStates[handledState];
continue restSets;
}
}
// Else, this state is not equivalent to any of the
// handled states -- allocate a new set for it.
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
}
}
handledStates[state] = new Set([state]);
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
// Add these handled states to all states map.
Object.assign(newTransitionMap, handledStates);
}
// Update current transition map for the handled row.
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
currentTransitionMap = newTransitionMap;
var newSets = new Set(Object.keys(newTransitionMap).map(function (state) {
return newTransitionMap[state];
}));
all.push([].concat(_toConsumableArray$2(newSets)));
// Top of the stack is the current.
current = all[all.length - 1];
// Previous set.
previous = all[all.length - 2];
};
while (!sameRow(current, previous)) {
_loop();
}
// ---------------------------------------------------------------------------
// Step 2: build minimized table from the equivalent sets.
// Remap state numbers from sets to index-based.
var remaped = new Map();
var idx = 1;
current.forEach(function (set) {
return remaped.set(set, idx++);
});
// Build the minimized table from the calculated equivalent sets.
var minimizedTable = {};
var minimizedAcceptingStates = new Set();
var updateAcceptingStates = function updateAcceptingStates(set, idx) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = set[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var state = _step.value;
if (accepting.has(state)) {
minimizedAcceptingStates.add(idx);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
};
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = remaped.entries()[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _ref = _step2.value;
var _ref2 = _slicedToArray(_ref, 2);
var set = _ref2[0];
var _idx = _ref2[1];
minimizedTable[_idx] = {};
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
for (var _iterator6 = alphabet[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var symbol = _step6.value;
updateAcceptingStates(set, _idx);
// Determine original transition for this symbol from the set.
var originalTransition = void 0;
var _iteratorNormalCompletion7 = true;
var _didIteratorError7 = false;
var _iteratorError7 = undefined;
try {
for (var _iterator7 = set[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
var originalState = _step7.value;
originalTransition = table[originalState][symbol];
if (originalTransition) {
break;
}
}
} catch (err) {
_didIteratorError7 = true;
_iteratorError7 = err;
} finally {
try {
if (!_iteratorNormalCompletion7 && _iterator7.return) {
_iterator7.return();
}
} finally {
if (_didIteratorError7) {
throw _iteratorError7;
}
}
}
if (originalTransition) {
minimizedTable[_idx][symbol] = remaped.get(currentTransitionMap[originalTransition]);
}
}
} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} finally {
if (_didIteratorError6) {
throw _iteratorError6;
}
}
}
}
// Update the table, and accepting states on the original DFA.
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
dfa.setTransitionTable(minimizedTable);
dfa.setAcceptingStateNumbers(minimizedAcceptingStates);
return dfa;
}
function sameRow(r1, r2) {
if (!r2) {
return false;
}
if (r1.length !== r2.length) {
return false;
}
for (var i = 0; i < r1.length; i++) {
var s1 = r1[i];
var s2 = r2[i];
if (s1.size !== s2.size) {
return false;
}
if ([].concat(_toConsumableArray$2(s1)).sort().join(',') !== [].concat(_toConsumableArray$2(s2)).sort().join(',')) {
return false;
}
}
return true;
}
/**
* Checks whether two states are N-equivalent, i.e. whether they go
* to the same set on a symbol.
*/
function areEquivalent(s1, s2, table, alphabet) {
var _iteratorNormalCompletion8 = true;
var _didIteratorError8 = false;
var _iteratorError8 = undefined;
try {
for (var _iterator8 = alphabet[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
var symbol = _step8.value;
if (!goToSameSet(s1, s2, table, symbol)) {
return false;
}
}
} catch (err) {
_didIteratorError8 = true;
_iteratorError8 = err;
} finally {
try {
if (!_iteratorNormalCompletion8 && _iterator8.return) {
_iterator8.return();
}
} finally {
if (_didIteratorError8) {
throw _iteratorError8;
}
}
}
return true;
}
/**
* Checks whether states go to the same set.
*/
function goToSameSet(s1, s2, table, symbol) {
if (!currentTransitionMap[s1] || !currentTransitionMap[s2]) {
return false;
}
var originalTransitionS1 = table[s1][symbol];
var originalTransitionS2 = table[s2][symbol];
// If no actual transition on this symbol, treat it as positive.
if (!originalTransitionS1 && !originalTransitionS2) {
return true;
}
// Otherwise, check if they are in the same sets.
return currentTransitionMap[s1].has(originalTransitionS1) && currentTransitionMap[s2].has(originalTransitionS2);
}
var dfaMinimizer = {
minimize: minimize
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var _createClass$3 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _toConsumableArray$1(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck$3(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var EPSILON_CLOSURE = specialSymbols.EPSILON_CLOSURE;
/**
* DFA is build by converting from NFA (subset construction).
*/
var DFA = function () {
function DFA(nfa) {
_classCallCheck$3(this, DFA);
this._nfa = nfa;
}
/**
* Minimizes DFA.
*/
_createClass$3(DFA, [{
key: 'minimize',
value: function minimize() {
this.getTransitionTable();
this._originalAcceptingStateNumbers = this._acceptingStateNumbers;
this._originalTransitionTable = this._transitionTable;
dfaMinimizer.minimize(this);
}
/**
* Returns alphabet for this DFA.
*/
}, {
key: 'getAlphabet',
value: function getAlphabet() {
return this._nfa.getAlphabet();
}
/**
* Returns accepting states.
*/
}, {
key: 'getAcceptingStateNumbers',
value: function getAcceptingStateNumbers() {
if (!this._acceptingStateNumbers) {
// Accepting states are determined during table construction.
this.getTransitionTable();
}
return this._acceptingStateNumbers;
}
/**
* Returns original accepting states.
*/
}, {
key: 'getOriginaAcceptingStateNumbers',
value: function getOriginaAcceptingStateNumbers() {
if (!this._originalAcceptingStateNumbers) {
// Accepting states are determined during table construction.
this.getTransitionTable();
}
return this._originalAcceptingStateNumbers;
}
/**
* Sets transition table.
*/
}, {
key: 'setTransitionTable',
value: function setTransitionTable(table) {
this._transitionTable = table;
}
/**
* Sets accepting states.
*/
}, {
key: 'setAcceptingStateNumbers',
value: function setAcceptingStateNumbers(stateNumbers) {
this._acceptingStateNumbers = stateNumbers;
}
/**
* DFA transition table is built from NFA table.
*/
}, {
key: 'getTransitionTable',
value: function getTransitionTable() {
var _this = this;
if (this._transitionTable) {
return this._transitionTable;
}
// Calculate from NFA transition table.
var nfaTable = this._nfa.getTransitionTable();
var nfaStates = Object.keys(nfaTable);
this._acceptingStateNumbers = new Set();
// Start state of DFA is E(S[nfa])
var startState = nfaTable[nfaStates[0]][EPSILON_CLOSURE];
// Init the worklist (states which should be in the DFA).
var worklist = [startState];
var alphabet = this.getAlphabet();
var nfaAcceptingStates = this._nfa.getAcceptingStateNumbers();
var dfaTable = {};
// Determine whether the combined DFA state is accepting.
var updateAcceptingStates = function updateAcceptingStates(states) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = nfaAcceptingStates[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var nfaAcceptingState = _step.value;
// If any of the states from NFA is accepting, DFA's
// state is accepting as well.
if (states.indexOf(nfaAcceptingState) !== -1) {
_this._acceptingStateNumbers.add(states.join(','));
break;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
};
while (worklist.length > 0) {
var states = worklist.shift();
var dfaStateLabel = states.join(',');
dfaTable[dfaStateLabel] = {};
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = alphabet[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var symbol = _step2.value;
var onSymbol = [];
// Determine whether the combined state is accepting.
updateAcceptingStates(states);
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = states[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var state = _step3.value;
var nfaStatesOnSymbol = nfaTable[state][symbol];
if (!nfaStatesOnSymbol) {
continue;
}
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = nfaStatesOnSymbol[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var nfaStateOnSymbol = _step4.value;
if (!nfaTable[nfaStateOnSymbol]) {
continue;
}
onSymbol.push.apply(onSymbol, _toConsumableArray$1(nfaTable[nfaStateOnSymbol][EPSILON_CLOSURE]));
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
var dfaStatesOnSymbolSet = new Set(onSymbol);
var dfaStatesOnSymbol = [].concat(_toConsumableArray$1(dfaStatesOnSymbolSet));
if (dfaStatesOnSymbol.length > 0) {
var dfaOnSymbolStr = dfaStatesOnSymbol.join(',');
dfaTable[dfaStateLabel][symbol] = dfaOnSymbolStr;
if (!dfaTable.hasOwnProperty(dfaOnSymbolStr)) {
worklist.unshift(dfaStatesOnSymbol);
}
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
}
return this._transitionTable = this._remapStateNumbers(dfaTable);
}
/**
* Remaps state numbers in the resulting table:
* combined states '1,2,3' -> 1, '3,4' -> 2, etc.
*/
}, {
key: '_remapStateNumbers',
value: function _remapStateNumbers(calculatedDFATable) {
var newStatesMap = {};
this._originalTransitionTable = calculatedDFATable;
var transitionTable = {};
Object.keys(calculatedDFATable).forEach(function (originalNumber, newNumber) {
newStatesMap[originalNumber] = newNumber + 1;
});
for (var originalNumber in calculatedDFATable) {
var originalRow = calculatedDFATable[originalNumber];
var row = {};
for (var symbol in originalRow) {
row[symbol] = newStatesMap[originalRow[symbol]];
}
transitionTable[newStatesMap[originalNumber]] = row;
}
// Remap accepting states.
this._originalAcceptingStateNumbers = this._acceptingStateNumbers;
this._acceptingStateNumbers = new Set();
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator5 = this._originalAcceptingStateNumbers[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var _originalNumber = _step5.value;
this._acceptingStateNumbers.add(newStatesMap[_originalNumber]);
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
}
}
return transitionTable;
}
/**
* Returns original DFA table, where state numbers
* are combined numbers from NFA.
*/
}, {
key: 'getOriginalTransitionTable',
value: function getOriginalTransitionTable() {
if (!this._originalTransitionTable) {
// Original table is determined during table construction.
this.getTransitionTable();
}
return this._originalTransitionTable;
}
/**
* Checks whether this DFA accepts a string.
*/
}, {
key: 'matches',
value: function matches(string) {
var state = 1;
var i = 0;
var table = this.getTransitionTable();
while (string[i]) {
state = table[state][string[i++]];
if (!state) {
return false;
}
}
if (!this.getAcceptingStateNumbers().has(state)) {
return false;
}
return true;
}
}]);
return DFA;
}();
var dfa = DFA;
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* A generic FA State class (base for NFA and DFA).
*
* Maintains the transition map, and the flag whether
* the state is accepting.
*/
var _createClass$2 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck$2(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var State = function () {
function State() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$accepting = _ref.accepting,
accepting = _ref$accepting === undefined ? false : _ref$accepting;
_classCallCheck$2(this, State);
/**
* Outgoing transitions to other states.
*/
this._transitions = new Map();
/**
* Whether the state is accepting.
*/
this.accepting = accepting;
}
/**
* Returns transitions for this state.
*/
_createClass$2(State, [{
key: 'getTransitions',
value: function getTransitions() {
return this._transitions;
}
/**
* Creates a transition on symbol.
*/
}, {
key: 'addTransition',
value: function addTransition(symbol, toState) {
this.getTransitionsOnSymbol(symbol).add(toState);
return this;
}
/**
* Returns transitions set on symbol.
*/
}, {
key: 'getTransitionsOnSymbol',
value: function getTransitionsOnSymbol(symbol) {
var transitions = this._transitions.get(symbol);
if (!transitions) {
transitions = new Set();
this._transitions.set(symbol, transitions);
}
return transitions;
}
}]);
return State;
}();
var state = State;
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var _createClass$1 = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck$1(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var EPSILON$1 = specialSymbols.EPSILON;
/**
* NFA state.
*
* Allows nondeterministic transitions to several states on the
* same symbol, and also epsilon-transitions.
*/
var NFAState = function (_State) {
_inherits(NFAState, _State);
function NFAState() {
_classCallCheck$1(this, NFAState);
return _possibleConstructorReturn(this, (NFAState.__proto__ || Object.getPrototypeOf(NFAState)).apply(this, arguments));
}
_createClass$1(NFAState, [{
key: 'matches',
/**
* Whether this state matches a string.
*
* We maintain set of visited epsilon-states to avoid infinite loops
* when an epsilon-transition goes eventually to itself.
*
* NOTE: this function is rather "educational", since we use DFA for strings
* matching. DFA is built on top of NFA, and uses fast transition table.
*/
value: function matches(string) {
var visited = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Set();
// An epsilon-state has been visited, stop to avoid infinite loop.
if (visited.has(this)) {
return false;
}
visited.add(this);
// No symbols left..
if (string.length === 0) {
// .. and we're in the accepting state.
if (this.accepting) {
return true;
}
// Check if we can reach any accepting state from
// on the epsilon transitions.
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = this.getTransitionsOnSymbol(EPSILON$1)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var nextState = _step.value;
if (nextState.matches('', visited)) {
return true;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return false;
}
// Else, we get some symbols.
var symbol = string[0];
var rest = string.slice(1);
var symbolTransitions = this.getTransitionsOnSymbol(symbol);
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = symbolTransitions[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _nextState = _step2.value;
if (_nextState.matches(rest)) {
return true;
}
}
// If we couldn't match on symbol, check still epsilon-transitions
// without consuming the symbol (i.e. continue from `string`, not `rest`).
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = this.getTransitionsOnSymbol(EPSILON$1)[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var _nextState2 = _step3.value;
if (_nextState2.matches(string, visited)) {
return true;
}
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
return false;
}
/**
* Returns an ε-closure for this state:
* self + all states following ε-transitions.
*/
}, {
key: 'getEpsilonClosure',
value: function getEpsilonClosure() {
var _this2 = this;
if (!this._epsilonClosure) {
(function () {
var epsilonTransitions = _this2.getTransitionsOnSymbol(EPSILON$1);
var closure = _this2._epsilonClosure = new Set();
closure.add(_this2);
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = epsilonTransitions[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var nextState = _step4.value;
if (!closure.has(nextState)) {
closure.add(nextState);
var nextClosure = nextState.getEpsilonClosure();
nextClosure.forEach(function (state) {
return closure.add(state);
});
}
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
})();
}
return this._epsilonClosure;
}
}]);
return NFAState;
}(state);
var nfaState = NFAState;
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var EPSILON = specialSymbols.EPSILON;
// -----------------------------------------------------------------------------
// Char NFA fragment: `c`
/**
* Char factory.
*
* Creates an NFA fragment for a single char.
*
* [in] --c--> [out]
*/
function char$2(c) {
var inState = new nfaState();
var outState = new nfaState({
accepting: true
});
return new nfa(inState.addTransition(c, outState), outState);
}
// -----------------------------------------------------------------------------
// Epsilon NFA fragment
/**
* Epsilon factory.
*
* Creates an NFA fragment for ε (recognizes an empty string).
*
* [in] --ε--> [out]
*/
function e() {
return char$2(EPSILON);
}
// -----------------------------------------------------------------------------
// Alteration NFA fragment: `abc`
/**
* Creates a connection between two NFA fragments on epsilon transition.
*
* [in-a] --a--> [out-a] --ε--> [in-b] --b--> [out-b]
*/
function altPair(first, second) {
first.out.accepting = false;
second.out.accepting = true;
first.out.addTransition(EPSILON, second.in);
return new nfa(first.in, second.out);
}
/**
* Alteration factory.
*
* Creates a alteration NFA for (at least) two NFA-fragments.
*/
function alt$1(first) {
for (var _len = arguments.length, fragments = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
fragments[_key - 1] = arguments[_key];
}
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = fragments[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var fragment = _step.value;
first = altPair(first, fragment);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return first;
}
// -----------------------------------------------------------------------------
// Disjunction NFA fragment: `a|b`
/**
* Creates a disjunction choice between two fragments.
*/
function orPair(first, second) {
var inState = new nfaState();
var outState = new nfaState();
inState.addTransition(EPSILON, first.in);
inState.addTransition(EPSILON, second.in);
outState.accepting = true;
first.out.accepting = false;
second.out.accepting = false;
first.out.addTransition(EPSILON, outState);
second.out.addTransition(EPSILON, outState);
return new nfa(inState, outState);
}
/**
* Disjunction factory.
*
* Creates a disjunction NFA for (at least) two NFA-fragments.
*/
function or$1(first) {
for (var _len2 = arguments.length, fragments = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
fragments[_key2 - 1] = arguments[_key2];
}
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = fragments[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var fragment = _step2.value;
first = orPair(first, fragment);
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return first;
}
// -----------------------------------------------------------------------------
// Kleene-closure
/**
* Kleene star/closure.
*
* a*
*/
function repExplicit(fragment) {
var inState = new nfaState();
var outState = new nfaState({
accepting: true
});
// 0 or more.
inState.addTransition(EPSILON, fragment.in);
inState.addTransition(EPSILON, outState);
fragment.out.accepting = false;
fragment.out.addTransition(EPSILON, outState);
outState.addTransition(EPSILON, fragment.in);
return new nfa(inState, outState);
}
/**
* Optimized Kleene-star: just adds ε-transitions from
* input to the output, and back.
*/
function rep$1(fragment) {
fragment.in.addTransition(EPSILON, fragment.out);
fragment.out.addTransition(EPSILON, fragment.in);
return fragment;
}
/**
* Optimized Plus: just adds ε-transitions from
* the output to the input.
*/
function plusRep$1(fragment) {
fragment.out.addTransition(EPSILON, fragment.in);
return fragment;
}
/**
* Optimized ? repetition: just adds ε-transitions from
* the input to the output.
*/
function questionRep$1(fragment) {
fragment.in.addTransition(EPSILON, fragment.out);
return fragment;
}
var builders = {
alt: alt$1,
char: char$2,
e: e,
or: or$1,
rep: rep$1,
repExplicit: repExplicit,
plusRep: plusRep$1,
questionRep: questionRep$1
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var alt = builders.alt,
char$1 = builders.char,
or = builders.or,
rep = builders.rep,
plusRep = builders.plusRep,
questionRep = builders.questionRep;
/**
* Helper `gen` function calls node type handler.
*/
function gen(node) {
if (node && !generator[node.type]) {
throw new Error(node.type + ' is not supported in NFA/DFA interpreter.');
}
return node ? generator[node.type](node) : '';
}
/**
* AST handler.
*/
var generator = {
RegExp: function RegExp(node) {
if (node.flags !== '') {
throw new Error('NFA/DFA: Flags are not supported yet.');
}
return gen(node.body);
},
Alternative: function Alternative(node) {
var fragments = (node.expressions || []).map(gen);
return alt.apply(undefined, _toConsumableArray(fragments));
},
Disjunction: function Disjunction(node) {
return or(gen(node.left), gen(node.right));
},
Repetition: function Repetition(node) {
switch (node.quantifier.kind) {
case '*':
return rep(gen(node.expression));
case '+':
return plusRep(gen(node.expression));
case '?':
return questionRep(gen(node.expression));
default:
throw new Error('Unknown repeatition: ' + node.quantifier.kind + '.');
}
},
Char: function Char(node) {
if (node.kind !== 'simple') {
throw new Error('NFA/DFA: Only simple chars are supported yet.');
}
return char$1(node.value);
},
Group: function Group(node) {
return gen(node.expression);
}
};
var nfaFromRegexp = {
/**
* Builds an NFA from the passed regexp.
*/
build: function build(regexp) {
var ast = regexp;
if (regexp instanceof RegExp) {
regexp = '' + regexp;
}
if (typeof regexp === 'string') {
ast = parser$1.parse(regexp, {
captureLocations: true
});
}
return gen(ast);
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var finiteAutomaton = {
/**
* Export NFA and DFA classes.
*/
NFA: nfa,
DFA: dfa,
/**
* Expose builders.
*/
builders: builders,
/**
* Builds an NFA for the passed regexp.
*
* @param string | AST | RegExp:
*
* a regular expression in different representations: a string,
* a RegExp object, or an AST.
*/
toNFA: function toNFA(regexp) {
return nfaFromRegexp.build(regexp);
},
/**
* Builds DFA for the passed regexp.
*
* @param string | AST | RegExp:
*
* a regular expression in different representations: a string,
* a RegExp object, or an AST.
*/
toDFA: function toDFA(regexp) {
return new dfa(this.toNFA(regexp));
},
/**
* Returns true if regexp accepts the string.
*/
test: function test(regexp, string) {
return this.toDFA(regexp).matches(string);
}
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
* The `RegExpTree` class provides runtime support for `compat-transpiler`
* module from `regexp-tree`.
*
* E.g. it tracks names of the capturing groups, in order to access the
* names on the matched result.
*
* It's a thin-wrapper on top of original regexp.
*/
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var RegExpTree$1 = function () {
/**
* Initializes a `RegExpTree` instance.
*
* @param RegExp - a regular expression
*
* @param Object state:
*
* An extra state which may store any related to transformation
* data, for example, names of the groups.
*
* - flags - original flags
* - groups - names of the groups, and their indices
* - source - original source
*/
function RegExpTree(re, _ref) {
var flags = _ref.flags,
groups = _ref.groups,
source = _ref.source;
_classCallCheck(this, RegExpTree);
this._re = re;
this._groups = groups;
// Original props.
this.flags = flags;
this.source = source || re.source;
this.dotAll = flags.includes('s');
// Inherited directly from `re`.
this.global = re.global;
this.ignoreCase = re.ignoreCase;
this.multiline = re.multiline;
this.sticky = re.sticky;
this.unicode = re.unicode;
}
/**
* Facade wrapper for RegExp `test` method.
*/
_createClass(RegExpTree, [{
key: 'test',
value: function test(string) {
return this._re.test(string);
}
/**
* Facade wrapper for RegExp `compile` method.
*/
}, {
key: 'compile',
value: function compile(string) {
return this._re.compile(string);
}
/**
* Facade wrapper for RegExp `toString` method.
*/
}, {
key: 'toString',
value: function toString() {
if (!this._toStringResult) {
this._toStringResult = '/' + this.source + '/' + this.flags;
}
return this._toStringResult;
}
/**
* Facade wrapper for RegExp `exec` method.
*/
}, {
key: 'exec',
value: function exec(string) {
var result = this._re.exec(string);
if (!this._groups || !result) {
return result;
}
result.groups = {};
for (var group in this._groups) {
var groupNumber = this._groups[group];
result.groups[group] = result[groupNumber];
}
return result;
}
}]);
return RegExpTree;
}();
var runtime = {
RegExpTree: RegExpTree$1
};
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var RegExpTree = runtime.RegExpTree;
/**
* An API object for RegExp processing (parsing/transform/generation).
*/
var regexpTree$2 = {
/**
* Parser module exposed.
*/
parser: parser$1,
/**
* Expose finite-automaton module.
*/
fa: finiteAutomaton,
/**
* `TransformResult` exposed.
*/
TransformResult: transform.TransformResult,
/**
* Parses a regexp string, producing an AST.
*
* @param string regexp
*
* a regular expression in different formats: string, AST, RegExp.
*
* @param Object options
*
* parsing options for this parse call. Default are:
*
* - captureLocations: boolean
* - any other custom options
*
* @return Object AST
*/
parse: function parse(regexp, options) {
return parser$1.parse('' + regexp, options);
},
/**
* Traverses a RegExp AST.
*
* @param Object ast
* @param Object | Array<Object> handlers
*
* Each `handler` is an object containing handler function for needed
* node types. Example:
*
* regexpTree.traverse(ast, {
* onChar(node) {
* ...
* },
* });
*
* The value for a node type may also be an object with functions pre and post.
* This enables more context-aware analyses, e.g. measuring star height.
*/
traverse: function traverse$1(ast, handlers, options) {
return traverse.traverse(ast, handlers, options);
},
/**
* Transforms a regular expression.
*
* A regexp can be passed in different formats (string, regexp or AST),
* applying a set of transformations. It is a convenient wrapper
* on top of "parse-traverse-generate" tool chain.
*
* @param string | AST | RegExp regexp - a regular expression;
* @param Object | Array<Object> handlers - a list of handlers.
*
* @return TransformResult - a transformation result.
*/
transform: function transform$1(regexp, handlers) {
return transform.transform(regexp, handlers);
},
/**
* Generates a RegExp string from an AST.
*
* @param Object ast
*
* Invariant:
*
* regexpTree.generate(regexpTree.parse('/[a-z]+/i')); // '/[a-z]+/i'
*/
generate: function generate(ast) {
return generator_1.generate(ast);
},
/**
* Creates a RegExp object from a regexp string.
*
* @param string regexp
*/
toRegExp: function toRegExp(regexp) {
var compat = this.compatTranspile(regexp);
return new RegExp(compat.getSource(), compat.getFlags());
},
/**
* Optimizes a regular expression by replacing some
* sub-expressions with their idiomatic patterns.
*
* @param string regexp
*
* @return TransformResult object
*/
optimize: function optimize(regexp, whitelist) {
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
blacklist = _ref.blacklist;
return optimizer.optimize(regexp, { whitelist: whitelist, blacklist: blacklist });
},
/**
* Translates a regular expression in new syntax or in new format
* into equivalent expressions in old syntax.
*
* @param string regexp
*
* @return TransformResult object
*/
compatTranspile: function compatTranspile(regexp, whitelist) {
return compatTranspiler.transform(regexp, whitelist);
},
/**
* Executes a regular expression on a string.
*
* @param RegExp|string re - a regular expression.
* @param string string - a testing string.
*/
exec: function exec(re, string) {
if (typeof re === 'string') {
var compat = this.compatTranspile(re);
var extra = compat.getExtra();
if (extra.namedCapturingGroups) {
re = new RegExpTree(compat.toRegExp(), {
flags: compat.getFlags(),
source: compat.getSource(),
groups: extra.namedCapturingGroups
});
} else {
re = compat.toRegExp();
}
}
return re.exec(string);
}
};
var regexpTree_1 = regexpTree$2;
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
var regexpTree$1 = regexpTree_1;
var analyzer = analyzer$1;
// Exports an Analyzer subclass
class HeuristicAnalyzer extends analyzer.Analyzer {
constructor(analyzerOptions) {
super(analyzerOptions);
}
isVulnerable(regExp) {
// Heuristic #1: Star height > 1
const starHeight = this._measureStarHeight(regExp);
if (starHeight > 1) {
return true;
}
// Heuristic #2: # repetitions > limit
// TODO This is a poor heuristic
const nRepetitions = this._measureRepetitions(regExp);
if (nRepetitions > this.options.heuristic_replimit) {
return true;
}
return false;
}
genAttackString(regExp) {
return null;
}
_measureStarHeight(regExp) {
let currentStarHeight = 0;
let maxObservedStarHeight = 0;
const ast = regexpTree$1.parse(regExp);
regexpTree$1.traverse(ast, {
Repetition: {
pre({ node }) {
currentStarHeight++;
if (maxObservedStarHeight < currentStarHeight) {
maxObservedStarHeight = currentStarHeight;
}
},
post({ node }) {
currentStarHeight--;
}
}
});
return maxObservedStarHeight;
}
_measureRepetitions(regExp) {
let nRepetitions = 0;
const ast = regexpTree$1.parse(regExp);
regexpTree$1.traverse(ast, {
Repetition: {
pre({ node }) {
nRepetitions++;
}
}
});
return nRepetitions;
}
}
var heuristicAnalyzer = HeuristicAnalyzer;
// Load the analyzers
var analyzerFamily = [heuristicAnalyzer];
const DEFAULT_SAFE_REP_LIMIT = 25;
const RET_IS_SAFE = true;
const RET_IS_VULNERABLE = false;
class Args {
constructor(regExp, analyzerOptions) {
this.regExp = regExp;
this.analyzerOptions = analyzerOptions;
}
}
function safeRegex(re, opts) {
try {
const args = buildArgs(re, opts);
const analyzerResponses = askAnalyzersIfVulnerable(args);
// Did any analyzer say true?
if (analyzerResponses.find((isVulnerable) => isVulnerable)) {
return RET_IS_VULNERABLE;
} else {
return RET_IS_SAFE;
}
} catch (err) {
// Invalid or unparseable input
return false;
}
}
function buildArgs(re, opts) {
// Build AnalyzerOptions
if (!opts) opts = {};
const heuristic_replimit = opts.limit === undefined ? DEFAULT_SAFE_REP_LIMIT : opts.limit;
const analyzerOptions = new analyzer.AnalyzerOptions(heuristic_replimit);
// Build RegExp
let regExp = null;
// Construct a RegExp object
if (re instanceof RegExp) {
regExp = re;
} else if (typeof re === 'string') {
regExp = new RegExp(re);
} else {
regExp = new RegExp(String(re));
}
return new Args(regExp, analyzerOptions);
}
function askAnalyzersIfVulnerable(args) {
let analyzerSaysVulnerable = [];
// Query the Analyzers
let Analyzer;
for (Analyzer of analyzerFamily) {
try {
const analyzer = new Analyzer(args.analyzerOptions);
analyzerSaysVulnerable.push(analyzer.isVulnerable(args.regExp));
} catch (err) {
/* istanbul ignore next */ // No need to worry about code coverage here.
analyzerSaysVulnerable.push(false);
}
}
return analyzerSaysVulnerable;
}
// Export
var safeRegex_1 = safeRegex;
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
function createCommonjsModule(fn) {
var module = { exports: {} };
return fn(module, module.exports), module.exports;
}
function commonjsRequire (path) {
throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
}
var dist = createCommonjsModule(function (module, exports) {
/// <reference lib="es2018"/>
/// <reference lib="dom"/>
/// <reference types="node"/>
Object.defineProperty(exports, "__esModule", { value: true });
const typedArrayTypeNames = [
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array',
'BigInt64Array',
'BigUint64Array'
];
function isTypedArrayName(name) {
return typedArrayTypeNames.includes(name);
}
const objectTypeNames = [
'Function',
'Generator',
'AsyncGenerator',
'GeneratorFunction',
'AsyncGeneratorFunction',
'AsyncFunction',
'Observable',
'Array',
'Buffer',
'Object',
'RegExp',
'Date',
'Error',
'Map',
'Set',
'WeakMap',
'WeakSet',
'ArrayBuffer',
'SharedArrayBuffer',
'DataView',
'Promise',
'URL',
'FormData',
'URLSearchParams',
'HTMLElement',
...typedArrayTypeNames
];
function isObjectTypeName(name) {
return objectTypeNames.includes(name);
}
const primitiveTypeNames = [
'null',
'undefined',
'string',
'number',
'bigint',
'boolean',
'symbol'
];
function isPrimitiveTypeName(name) {
return primitiveTypeNames.includes(name);
}
// eslint-disable-next-line @typescript-eslint/ban-types
function isOfType(type) {
return (value) => typeof value === type;
}
const { toString } = Object.prototype;
const getObjectType = (value) => {
const objectTypeName = toString.call(value).slice(8, -1);
if (/HTML\w+Element/.test(objectTypeName) && is.domElement(value)) {
return 'HTMLElement';
}
if (isObjectTypeName(objectTypeName)) {
return objectTypeName;
}
return undefined;
};
const isObjectOfType = (type) => (value) => getObjectType(value) === type;
function is(value) {
if (value === null) {
return 'null';
}
switch (typeof value) {
case 'undefined':
return 'undefined';
case 'string':
return 'string';
case 'number':
return 'number';
case 'boolean':
return 'boolean';
case 'function':
return 'Function';
case 'bigint':
return 'bigint';
case 'symbol':
return 'symbol';
}
if (is.observable(value)) {
return 'Observable';
}
if (is.array(value)) {
return 'Array';
}
if (is.buffer(value)) {
return 'Buffer';
}
const tagType = getObjectType(value);
if (tagType) {
return tagType;
}
if (value instanceof String || value instanceof Boolean || value instanceof Number) {
throw new TypeError('Please don\'t use object wrappers for primitive types');
}
return 'Object';
}
is.undefined = isOfType('undefined');
is.string = isOfType('string');
const isNumberType = isOfType('number');
is.number = (value) => isNumberType(value) && !is.nan(value);
is.bigint = isOfType('bigint');
// eslint-disable-next-line @typescript-eslint/ban-types
is.function_ = isOfType('function');
is.null_ = (value) => value === null;
is.class_ = (value) => is.function_(value) && value.toString().startsWith('class ');
is.boolean = (value) => value === true || value === false;
is.symbol = isOfType('symbol');
is.numericString = (value) => is.string(value) && !is.emptyStringOrWhitespace(value) && !Number.isNaN(Number(value));
is.array = (value, assertion) => {
if (!Array.isArray(value)) {
return false;
}
if (!is.function_(assertion)) {
return true;
}
return value.every(assertion);
};
is.buffer = (value) => { var _a, _b, _c, _d; return (_d = (_c = (_b = (_a = value) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.isBuffer) === null || _c === void 0 ? void 0 : _c.call(_b, value)) !== null && _d !== void 0 ? _d : false; };
is.nullOrUndefined = (value) => is.null_(value) || is.undefined(value);
is.object = (value) => !is.null_(value) && (typeof value === 'object' || is.function_(value));
is.iterable = (value) => { var _a; return is.function_((_a = value) === null || _a === void 0 ? void 0 : _a[Symbol.iterator]); };
is.asyncIterable = (value) => { var _a; return is.function_((_a = value) === null || _a === void 0 ? void 0 : _a[Symbol.asyncIterator]); };
is.generator = (value) => is.iterable(value) && is.function_(value.next) && is.function_(value.throw);
is.asyncGenerator = (value) => is.asyncIterable(value) && is.function_(value.next) && is.function_(value.throw);
is.nativePromise = (value) => isObjectOfType('Promise')(value);
const hasPromiseAPI = (value) => {
var _a, _b;
return is.function_((_a = value) === null || _a === void 0 ? void 0 : _a.then) &&
is.function_((_b = value) === null || _b === void 0 ? void 0 : _b.catch);
};
is.promise = (value) => is.nativePromise(value) || hasPromiseAPI(value);
is.generatorFunction = isObjectOfType('GeneratorFunction');
is.asyncGeneratorFunction = (value) => getObjectType(value) === 'AsyncGeneratorFunction';
is.asyncFunction = (value) => getObjectType(value) === 'AsyncFunction';
// eslint-disable-next-line no-prototype-builtins, @typescript-eslint/ban-types
is.boundFunction = (value) => is.function_(value) && !value.hasOwnProperty('prototype');
is.regExp = isObjectOfType('RegExp');
is.date = isObjectOfType('Date');
is.error = isObjectOfType('Error');
is.map = (value) => isObjectOfType('Map')(value);
is.set = (value) => isObjectOfType('Set')(value);
is.weakMap = (value) => isObjectOfType('WeakMap')(value);
is.weakSet = (value) => isObjectOfType('WeakSet')(value);
is.int8Array = isObjectOfType('Int8Array');
is.uint8Array = isObjectOfType('Uint8Array');
is.uint8ClampedArray = isObjectOfType('Uint8ClampedArray');
is.int16Array = isObjectOfType('Int16Array');
is.uint16Array = isObjectOfType('Uint16Array');
is.int32Array = isObjectOfType('Int32Array');
is.uint32Array = isObjectOfType('Uint32Array');
is.float32Array = isObjectOfType('Float32Array');
is.float64Array = isObjectOfType('Float64Array');
is.bigInt64Array = isObjectOfType('BigInt64Array');
is.bigUint64Array = isObjectOfType('BigUint64Array');
is.arrayBuffer = isObjectOfType('ArrayBuffer');
is.sharedArrayBuffer = isObjectOfType('SharedArrayBuffer');
is.dataView = isObjectOfType('DataView');
is.directInstanceOf = (instance, class_) => Object.getPrototypeOf(instance) === class_.prototype;
is.urlInstance = (value) => isObjectOfType('URL')(value);
is.urlString = (value) => {
if (!is.string(value)) {
return false;
}
try {
new URL(value); // eslint-disable-line no-new
return true;
}
catch (_a) {
return false;
}
};
// TODO: Use the `not` operator with a type guard here when it's available.
// Example: `is.truthy = (value: unknown): value is (not false | not 0 | not '' | not undefined | not null) => Boolean(value);`
is.truthy = (value) => Boolean(value);
// Example: `is.falsy = (value: unknown): value is (not true | 0 | '' | undefined | null) => Boolean(value);`
is.falsy = (value) => !value;
is.nan = (value) => Number.isNaN(value);
is.primitive = (value) => is.null_(value) || isPrimitiveTypeName(typeof value);
is.integer = (value) => Number.isInteger(value);
is.safeInteger = (value) => Number.isSafeInteger(value);
is.plainObject = (value) => {
// From: https://github.com/sindresorhus/is-plain-obj/blob/main/index.js
if (toString.call(value) !== '[object Object]') {
return false;
}
const prototype = Object.getPrototypeOf(value);
return prototype === null || prototype === Object.getPrototypeOf({});
};
is.typedArray = (value) => isTypedArrayName(getObjectType(value));
const isValidLength = (value) => is.safeInteger(value) && value >= 0;
is.arrayLike = (value) => !is.nullOrUndefined(value) && !is.function_(value) && isValidLength(value.length);
is.inRange = (value, range) => {
if (is.number(range)) {
return value >= Math.min(0, range) && value <= Math.max(range, 0);
}
if (is.array(range) && range.length === 2) {
return value >= Math.min(...range) && value <= Math.max(...range);
}
throw new TypeError(`Invalid range: ${JSON.stringify(range)}`);
};
const NODE_TYPE_ELEMENT = 1;
const DOM_PROPERTIES_TO_CHECK = [
'innerHTML',
'ownerDocument',
'style',
'attributes',
'nodeValue'
];
is.domElement = (value) => {
return is.object(value) &&
value.nodeType === NODE_TYPE_ELEMENT &&
is.string(value.nodeName) &&
!is.plainObject(value) &&
DOM_PROPERTIES_TO_CHECK.every(property => property in value);
};
is.observable = (value) => {
var _a, _b, _c, _d;
if (!value) {
return false;
}
// eslint-disable-next-line no-use-extend-native/no-use-extend-native
if (value === ((_b = (_a = value)[Symbol.observable]) === null || _b === void 0 ? void 0 : _b.call(_a))) {
return true;
}
if (value === ((_d = (_c = value)['@@observable']) === null || _d === void 0 ? void 0 : _d.call(_c))) {
return true;
}
return false;
};
is.nodeStream = (value) => is.object(value) && is.function_(value.pipe) && !is.observable(value);
is.infinite = (value) => value === Infinity || value === -Infinity;
const isAbsoluteMod2 = (remainder) => (value) => is.integer(value) && Math.abs(value % 2) === remainder;
is.evenInteger = isAbsoluteMod2(0);
is.oddInteger = isAbsoluteMod2(1);
is.emptyArray = (value) => is.array(value) && value.length === 0;
is.nonEmptyArray = (value) => is.array(value) && value.length > 0;
is.emptyString = (value) => is.string(value) && value.length === 0;
// TODO: Use `not ''` when the `not` operator is available.
is.nonEmptyString = (value) => is.string(value) && value.length > 0;
const isWhiteSpaceString = (value) => is.string(value) && !/\S/.test(value);
is.emptyStringOrWhitespace = (value) => is.emptyString(value) || isWhiteSpaceString(value);
is.emptyObject = (value) => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0;
// TODO: Use `not` operator here to remove `Map` and `Set` from type guard:
// - https://github.com/Microsoft/TypeScript/pull/29317
is.nonEmptyObject = (value) => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length > 0;
is.emptySet = (value) => is.set(value) && value.size === 0;
is.nonEmptySet = (value) => is.set(value) && value.size > 0;
is.emptyMap = (value) => is.map(value) && value.size === 0;
is.nonEmptyMap = (value) => is.map(value) && value.size > 0;
// `PropertyKey` is any value that can be used as an object key (string, number, or symbol)
is.propertyKey = (value) => is.any([is.string, is.number, is.symbol], value);
is.formData = (value) => isObjectOfType('FormData')(value);
is.urlSearchParams = (value) => isObjectOfType('URLSearchParams')(value);
const predicateOnArray = (method, predicate, values) => {
if (!is.function_(predicate)) {
throw new TypeError(`Invalid predicate: ${JSON.stringify(predicate)}`);
}
if (values.length === 0) {
throw new TypeError('Invalid number of values');
}
return method.call(values, predicate);
};
is.any = (predicate, ...values) => {
const predicates = is.array(predicate) ? predicate : [predicate];
return predicates.some(singlePredicate => predicateOnArray(Array.prototype.some, singlePredicate, values));
};
is.all = (predicate, ...values) => predicateOnArray(Array.prototype.every, predicate, values);
const assertType = (condition, description, value, options = {}) => {
if (!condition) {
const { multipleValues } = options;
const valuesMessage = multipleValues ?
`received values of types ${[
...new Set(value.map(singleValue => `\`${is(singleValue)}\``))
].join(', ')}` :
`received value of type \`${is(value)}\``;
throw new TypeError(`Expected value which is \`${description}\`, ${valuesMessage}.`);
}
};
exports.assert = {
// Unknowns.
undefined: (value) => assertType(is.undefined(value), 'undefined', value),
string: (value) => assertType(is.string(value), 'string', value),
number: (value) => assertType(is.number(value), 'number', value),
bigint: (value) => assertType(is.bigint(value), 'bigint', value),
// eslint-disable-next-line @typescript-eslint/ban-types
function_: (value) => assertType(is.function_(value), 'Function', value),
null_: (value) => assertType(is.null_(value), 'null', value),
class_: (value) => assertType(is.class_(value), "Class" /* class_ */, value),
boolean: (value) => assertType(is.boolean(value), 'boolean', value),
symbol: (value) => assertType(is.symbol(value), 'symbol', value),
numericString: (value) => assertType(is.numericString(value), "string with a number" /* numericString */, value),
array: (value, assertion) => {
const assert = assertType;
assert(is.array(value), 'Array', value);
if (assertion) {
value.forEach(assertion);
}
},
buffer: (value) => assertType(is.buffer(value), 'Buffer', value),
nullOrUndefined: (value) => assertType(is.nullOrUndefined(value), "null or undefined" /* nullOrUndefined */, value),
object: (value) => assertType(is.object(value), 'Object', value),
iterable: (value) => assertType(is.iterable(value), "Iterable" /* iterable */, value),
asyncIterable: (value) => assertType(is.asyncIterable(value), "AsyncIterable" /* asyncIterable */, value),
generator: (value) => assertType(is.generator(value), 'Generator', value),
asyncGenerator: (value) => assertType(is.asyncGenerator(value), 'AsyncGenerator', value),
nativePromise: (value) => assertType(is.nativePromise(value), "native Promise" /* nativePromise */, value),
promise: (value) => assertType(is.promise(value), 'Promise', value),
generatorFunction: (value) => assertType(is.generatorFunction(value), 'GeneratorFunction', value),
asyncGeneratorFunction: (value) => assertType(is.asyncGeneratorFunction(value), 'AsyncGeneratorFunction', value),
// eslint-disable-next-line @typescript-eslint/ban-types
asyncFunction: (value) => assertType(is.asyncFunction(value), 'AsyncFunction', value),
// eslint-disable-next-line @typescript-eslint/ban-types
boundFunction: (value) => assertType(is.boundFunction(value), 'Function', value),
regExp: (value) => assertType(is.regExp(value), 'RegExp', value),
date: (value) => assertType(is.date(value), 'Date', value),
error: (value) => assertType(is.error(value), 'Error', value),
map: (value) => assertType(is.map(value), 'Map', value),
set: (value) => assertType(is.set(value), 'Set', value),
weakMap: (value) => assertType(is.weakMap(value), 'WeakMap', value),
weakSet: (value) => assertType(is.weakSet(value), 'WeakSet', value),
int8Array: (value) => assertType(is.int8Array(value), 'Int8Array', value),
uint8Array: (value) => assertType(is.uint8Array(value), 'Uint8Array', value),
uint8ClampedArray: (value) => assertType(is.uint8ClampedArray(value), 'Uint8ClampedArray', value),
int16Array: (value) => assertType(is.int16Array(value), 'Int16Array', value),
uint16Array: (value) => assertType(is.uint16Array(value), 'Uint16Array', value),
int32Array: (value) => assertType(is.int32Array(value), 'Int32Array', value),
uint32Array: (value) => assertType(is.uint32Array(value), 'Uint32Array', value),
float32Array: (value) => assertType(is.float32Array(value), 'Float32Array', value),
float64Array: (value) => assertType(is.float64Array(value), 'Float64Array', value),
bigInt64Array: (value) => assertType(is.bigInt64Array(value), 'BigInt64Array', value),
bigUint64Array: (value) => assertType(is.bigUint64Array(value), 'BigUint64Array', value),
arrayBuffer: (value) => assertType(is.arrayBuffer(value), 'ArrayBuffer', value),
sharedArrayBuffer: (value) => assertType(is.sharedArrayBuffer(value), 'SharedArrayBuffer', value),
dataView: (value) => assertType(is.dataView(value), 'DataView', value),
urlInstance: (value) => assertType(is.urlInstance(value), 'URL', value),
urlString: (value) => assertType(is.urlString(value), "string with a URL" /* urlString */, value),
truthy: (value) => assertType(is.truthy(value), "truthy" /* truthy */, value),
falsy: (value) => assertType(is.falsy(value), "falsy" /* falsy */, value),
nan: (value) => assertType(is.nan(value), "NaN" /* nan */, value),
primitive: (value) => assertType(is.primitive(value), "primitive" /* primitive */, value),
integer: (value) => assertType(is.integer(value), "integer" /* integer */, value),
safeInteger: (value) => assertType(is.safeInteger(value), "integer" /* safeInteger */, value),
plainObject: (value) => assertType(is.plainObject(value), "plain object" /* plainObject */, value),
typedArray: (value) => assertType(is.typedArray(value), "TypedArray" /* typedArray */, value),
arrayLike: (value) => assertType(is.arrayLike(value), "array-like" /* arrayLike */, value),
domElement: (value) => assertType(is.domElement(value), "HTMLElement" /* domElement */, value),
observable: (value) => assertType(is.observable(value), 'Observable', value),
nodeStream: (value) => assertType(is.nodeStream(value), "Node.js Stream" /* nodeStream */, value),
infinite: (value) => assertType(is.infinite(value), "infinite number" /* infinite */, value),
emptyArray: (value) => assertType(is.emptyArray(value), "empty array" /* emptyArray */, value),
nonEmptyArray: (value) => assertType(is.nonEmptyArray(value), "non-empty array" /* nonEmptyArray */, value),
emptyString: (value) => assertType(is.emptyString(value), "empty string" /* emptyString */, value),
nonEmptyString: (value) => assertType(is.nonEmptyString(value), "non-empty string" /* nonEmptyString */, value),
emptyStringOrWhitespace: (value) => assertType(is.emptyStringOrWhitespace(value), "empty string or whitespace" /* emptyStringOrWhitespace */, value),
emptyObject: (value) => assertType(is.emptyObject(value), "empty object" /* emptyObject */, value),
nonEmptyObject: (value) => assertType(is.nonEmptyObject(value), "non-empty object" /* nonEmptyObject */, value),
emptySet: (value) => assertType(is.emptySet(value), "empty set" /* emptySet */, value),
nonEmptySet: (value) => assertType(is.nonEmptySet(value), "non-empty set" /* nonEmptySet */, value),
emptyMap: (value) => assertType(is.emptyMap(value), "empty map" /* emptyMap */, value),
nonEmptyMap: (value) => assertType(is.nonEmptyMap(value), "non-empty map" /* nonEmptyMap */, value),
propertyKey: (value) => assertType(is.propertyKey(value), 'PropertyKey', value),
formData: (value) => assertType(is.formData(value), 'FormData', value),
urlSearchParams: (value) => assertType(is.urlSearchParams(value), 'URLSearchParams', value),
// Numbers.
evenInteger: (value) => assertType(is.evenInteger(value), "even integer" /* evenInteger */, value),
oddInteger: (value) => assertType(is.oddInteger(value), "odd integer" /* oddInteger */, value),
// Two arguments.
directInstanceOf: (instance, class_) => assertType(is.directInstanceOf(instance, class_), "T" /* directInstanceOf */, instance),
inRange: (value, range) => assertType(is.inRange(value, range), "in range" /* inRange */, value),
// Variadic functions.
any: (predicate, ...values) => {
return assertType(is.any(predicate, ...values), "predicate returns truthy for any value" /* any */, values, { multipleValues: true });
},
all: (predicate, ...values) => assertType(is.all(predicate, ...values), "predicate returns truthy for all values" /* all */, values, { multipleValues: true })
};
// Some few keywords are reserved, but we'll populate them for Node.js users
// See https://github.com/Microsoft/TypeScript/issues/2536
Object.defineProperties(is, {
class: {
value: is.class_
},
function: {
value: is.function_
},
null: {
value: is.null_
}
});
Object.defineProperties(exports.assert, {
class: {
value: exports.assert.class_
},
function: {
value: exports.assert.function_
},
null: {
value: exports.assert.null_
}
});
exports.default = is;
// For CommonJS default export support
module.exports = is;
module.exports.default = is;
module.exports.assert = exports.assert;
});
class CancelError extends Error {
constructor(reason) {
super(reason || 'Promise was canceled');
this.name = 'CancelError';
}
get isCanceled() {
return true;
}
}
class PCancelable {
static fn(userFn) {
return (...arguments_) => {
return new PCancelable((resolve, reject, onCancel) => {
arguments_.push(onCancel);
// eslint-disable-next-line promise/prefer-await-to-then
userFn(...arguments_).then(resolve, reject);
});
};
}
constructor(executor) {
this._cancelHandlers = [];
this._isPending = true;
this._isCanceled = false;
this._rejectOnCancel = true;
this._promise = new Promise((resolve, reject) => {
this._reject = reject;
const onResolve = value => {
if (!this._isCanceled || !onCancel.shouldReject) {
this._isPending = false;
resolve(value);
}
};
const onReject = error => {
this._isPending = false;
reject(error);
};
const onCancel = handler => {
if (!this._isPending) {
throw new Error('The `onCancel` handler was attached after the promise settled.');
}
this._cancelHandlers.push(handler);
};
Object.defineProperties(onCancel, {
shouldReject: {
get: () => this._rejectOnCancel,
set: boolean => {
this._rejectOnCancel = boolean;
}
}
});
return executor(onResolve, onReject, onCancel);
});
}
then(onFulfilled, onRejected) {
// eslint-disable-next-line promise/prefer-await-to-then
return this._promise.then(onFulfilled, onRejected);
}
catch(onRejected) {
return this._promise.catch(onRejected);
}
finally(onFinally) {
return this._promise.finally(onFinally);
}
cancel(reason) {
if (!this._isPending || this._isCanceled) {
return;
}
this._isCanceled = true;
if (this._cancelHandlers.length > 0) {
try {
for (const handler of this._cancelHandlers) {
handler();
}
} catch (error) {
this._reject(error);
return;
}
}
if (this._rejectOnCancel) {
this._reject(new CancelError(reason));
}
}
get isCanceled() {
return this._isCanceled;
}
}
Object.setPrototypeOf(PCancelable.prototype, Promise.prototype);
var pCancelable = PCancelable;
var CancelError_1 = CancelError;
pCancelable.CancelError = CancelError_1;
var source$4 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
function isTLSSocket(socket) {
return socket.encrypted;
}
const deferToConnect = (socket, fn) => {
let listeners;
if (typeof fn === 'function') {
const connect = fn;
listeners = { connect };
}
else {
listeners = fn;
}
const hasConnectListener = typeof listeners.connect === 'function';
const hasSecureConnectListener = typeof listeners.secureConnect === 'function';
const hasCloseListener = typeof listeners.close === 'function';
const onConnect = () => {
if (hasConnectListener) {
listeners.connect();
}
if (isTLSSocket(socket) && hasSecureConnectListener) {
if (socket.authorized) {
listeners.secureConnect();
}
else if (!socket.authorizationError) {
socket.once('secureConnect', listeners.secureConnect);
}
}
if (hasCloseListener) {
socket.once('close', listeners.close);
}
};
if (socket.writable && !socket.connecting) {
onConnect();
}
else if (socket.connecting) {
socket.once('connect', onConnect);
}
else if (socket.destroyed && hasCloseListener) {
listeners.close(socket._hadError);
}
};
exports.default = deferToConnect;
// For CommonJS default export support
module.exports = deferToConnect;
module.exports.default = deferToConnect;
});
var source$3 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
const nodejsMajorVersion = Number(process.versions.node.split('.')[0]);
const timer = (request) => {
if (request.timings) {
return request.timings;
}
const timings = {
start: Date.now(),
socket: undefined,
lookup: undefined,
connect: undefined,
secureConnect: undefined,
upload: undefined,
response: undefined,
end: undefined,
error: undefined,
abort: undefined,
phases: {
wait: undefined,
dns: undefined,
tcp: undefined,
tls: undefined,
request: undefined,
firstByte: undefined,
download: undefined,
total: undefined
}
};
request.timings = timings;
const handleError = (origin) => {
const emit = origin.emit.bind(origin);
origin.emit = (event, ...args) => {
// Catches the `error` event
if (event === 'error') {
timings.error = Date.now();
timings.phases.total = timings.error - timings.start;
origin.emit = emit;
}
// Saves the original behavior
return emit(event, ...args);
};
};
handleError(request);
const onAbort = () => {
timings.abort = Date.now();
// Let the `end` response event be responsible for setting the total phase,
// unless the Node.js major version is >= 13.
if (!timings.response || nodejsMajorVersion >= 13) {
timings.phases.total = Date.now() - timings.start;
}
};
request.prependOnceListener('abort', onAbort);
const onSocket = (socket) => {
timings.socket = Date.now();
timings.phases.wait = timings.socket - timings.start;
if (util_1__default['default'].types.isProxy(socket)) {
return;
}
const lookupListener = () => {
timings.lookup = Date.now();
timings.phases.dns = timings.lookup - timings.socket;
};
socket.prependOnceListener('lookup', lookupListener);
source$4.default(socket, {
connect: () => {
timings.connect = Date.now();
if (timings.lookup === undefined) {
socket.removeListener('lookup', lookupListener);
timings.lookup = timings.connect;
timings.phases.dns = timings.lookup - timings.socket;
}
timings.phases.tcp = timings.connect - timings.lookup;
// This callback is called before flushing any data,
// so we don't need to set `timings.phases.request` here.
},
secureConnect: () => {
timings.secureConnect = Date.now();
timings.phases.tls = timings.secureConnect - timings.connect;
}
});
};
if (request.socket) {
onSocket(request.socket);
}
else {
request.prependOnceListener('socket', onSocket);
}
const onUpload = () => {
var _a;
timings.upload = Date.now();
timings.phases.request = timings.upload - ((_a = timings.secureConnect) !== null && _a !== void 0 ? _a : timings.connect);
};
const writableFinished = () => {
if (typeof request.writableFinished === 'boolean') {
return request.writableFinished;
}
// Node.js doesn't have `request.writableFinished` property
return request.finished && request.outputSize === 0 && (!request.socket || request.socket.writableLength === 0);
};
if (writableFinished()) {
onUpload();
}
else {
request.prependOnceListener('finish', onUpload);
}
request.prependOnceListener('response', (response) => {
timings.response = Date.now();
timings.phases.firstByte = timings.response - timings.upload;
response.timings = timings;
handleError(response);
response.prependOnceListener('end', () => {
timings.end = Date.now();
timings.phases.download = timings.end - timings.response;
timings.phases.total = timings.end - timings.start;
});
response.prependOnceListener('aborted', onAbort);
});
return timings;
};
exports.default = timer;
// For CommonJS default export support
module.exports = timer;
module.exports.default = timer;
});
const {
V4MAPPED,
ADDRCONFIG,
ALL,
promises: {
Resolver: AsyncResolver
},
lookup: dnsLookup
} = require$$0__default['default'];
const {promisify} = util_1__default['default'];
const kCacheableLookupCreateConnection = Symbol('cacheableLookupCreateConnection');
const kCacheableLookupInstance = Symbol('cacheableLookupInstance');
const kExpires = Symbol('expires');
const supportsALL = typeof ALL === 'number';
const verifyAgent = agent => {
if (!(agent && typeof agent.createConnection === 'function')) {
throw new Error('Expected an Agent instance as the first argument');
}
};
const map4to6 = entries => {
for (const entry of entries) {
if (entry.family === 6) {
continue;
}
entry.address = `::ffff:${entry.address}`;
entry.family = 6;
}
};
const getIfaceInfo = () => {
let has4 = false;
let has6 = false;
for (const device of Object.values(os__default['default'].networkInterfaces())) {
for (const iface of device) {
if (iface.internal) {
continue;
}
if (iface.family === 'IPv6') {
has6 = true;
} else {
has4 = true;
}
if (has4 && has6) {
return {has4, has6};
}
}
}
return {has4, has6};
};
const isIterable = map => {
return Symbol.iterator in map;
};
const ttl = {ttl: true};
const all = {all: true};
class CacheableLookup {
constructor({
cache = new Map(),
maxTtl = Infinity,
fallbackDuration = 3600,
errorTtl = 0.15,
resolver = new AsyncResolver(),
lookup = dnsLookup
} = {}) {
this.maxTtl = maxTtl;
this.errorTtl = errorTtl;
this._cache = cache;
this._resolver = resolver;
this._dnsLookup = promisify(lookup);
if (this._resolver instanceof AsyncResolver) {
this._resolve4 = this._resolver.resolve4.bind(this._resolver);
this._resolve6 = this._resolver.resolve6.bind(this._resolver);
} else {
this._resolve4 = promisify(this._resolver.resolve4.bind(this._resolver));
this._resolve6 = promisify(this._resolver.resolve6.bind(this._resolver));
}
this._iface = getIfaceInfo();
this._pending = {};
this._nextRemovalTime = false;
this._hostnamesToFallback = new Set();
if (fallbackDuration < 1) {
this._fallback = false;
} else {
this._fallback = true;
const interval = setInterval(() => {
this._hostnamesToFallback.clear();
}, fallbackDuration * 1000);
/* istanbul ignore next: There is no `interval.unref()` when running inside an Electron renderer */
if (interval.unref) {
interval.unref();
}
}
this.lookup = this.lookup.bind(this);
this.lookupAsync = this.lookupAsync.bind(this);
}
set servers(servers) {
this.clear();
this._resolver.setServers(servers);
}
get servers() {
return this._resolver.getServers();
}
lookup(hostname, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
} else if (typeof options === 'number') {
options = {
family: options
};
}
if (!callback) {
throw new Error('Callback must be a function.');
}
// eslint-disable-next-line promise/prefer-await-to-then
this.lookupAsync(hostname, options).then(result => {
if (options.all) {
callback(null, result);
} else {
callback(null, result.address, result.family, result.expires, result.ttl);
}
}, callback);
}
async lookupAsync(hostname, options = {}) {
if (typeof options === 'number') {
options = {
family: options
};
}
let cached = await this.query(hostname);
if (options.family === 6) {
const filtered = cached.filter(entry => entry.family === 6);
if (options.hints & V4MAPPED) {
if ((supportsALL && options.hints & ALL) || filtered.length === 0) {
map4to6(cached);
} else {
cached = filtered;
}
} else {
cached = filtered;
}
} else if (options.family === 4) {
cached = cached.filter(entry => entry.family === 4);
}
if (options.hints & ADDRCONFIG) {
const {_iface} = this;
cached = cached.filter(entry => entry.family === 6 ? _iface.has6 : _iface.has4);
}
if (cached.length === 0) {
const error = new Error(`cacheableLookup ENOTFOUND ${hostname}`);
error.code = 'ENOTFOUND';
error.hostname = hostname;
throw error;
}
if (options.all) {
return cached;
}
return cached[0];
}
async query(hostname) {
let cached = await this._cache.get(hostname);
if (!cached) {
const pending = this._pending[hostname];
if (pending) {
cached = await pending;
} else {
const newPromise = this.queryAndCache(hostname);
this._pending[hostname] = newPromise;
try {
cached = await newPromise;
} finally {
delete this._pending[hostname];
}
}
}
cached = cached.map(entry => {
return {...entry};
});
return cached;
}
async _resolve(hostname) {
const wrap = async promise => {
try {
return await promise;
} catch (error) {
if (error.code === 'ENODATA' || error.code === 'ENOTFOUND') {
return [];
}
throw error;
}
};
// ANY is unsafe as it doesn't trigger new queries in the underlying server.
const [A, AAAA] = await Promise.all([
this._resolve4(hostname, ttl),
this._resolve6(hostname, ttl)
].map(promise => wrap(promise)));
let aTtl = 0;
let aaaaTtl = 0;
let cacheTtl = 0;
const now = Date.now();
for (const entry of A) {
entry.family = 4;
entry.expires = now + (entry.ttl * 1000);
aTtl = Math.max(aTtl, entry.ttl);
}
for (const entry of AAAA) {
entry.family = 6;
entry.expires = now + (entry.ttl * 1000);
aaaaTtl = Math.max(aaaaTtl, entry.ttl);
}
if (A.length > 0) {
if (AAAA.length > 0) {
cacheTtl = Math.min(aTtl, aaaaTtl);
} else {
cacheTtl = aTtl;
}
} else {
cacheTtl = aaaaTtl;
}
return {
entries: [
...A,
...AAAA
],
cacheTtl
};
}
async _lookup(hostname) {
try {
const entries = await this._dnsLookup(hostname, {
all: true
});
return {
entries,
cacheTtl: 0
};
} catch (_) {
return {
entries: [],
cacheTtl: 0
};
}
}
async _set(hostname, data, cacheTtl) {
if (this.maxTtl > 0 && cacheTtl > 0) {
cacheTtl = Math.min(cacheTtl, this.maxTtl) * 1000;
data[kExpires] = Date.now() + cacheTtl;
try {
await this._cache.set(hostname, data, cacheTtl);
} catch (error) {
this.lookupAsync = async () => {
const cacheError = new Error('Cache Error. Please recreate the CacheableLookup instance.');
cacheError.cause = error;
throw cacheError;
};
}
if (isIterable(this._cache)) {
this._tick(cacheTtl);
}
}
}
async queryAndCache(hostname) {
if (this._hostnamesToFallback.has(hostname)) {
return this._dnsLookup(hostname, all);
}
let query = await this._resolve(hostname);
if (query.entries.length === 0 && this._fallback) {
query = await this._lookup(hostname);
if (query.entries.length !== 0) {
// Use `dns.lookup(...)` for that particular hostname
this._hostnamesToFallback.add(hostname);
}
}
const cacheTtl = query.entries.length === 0 ? this.errorTtl : query.cacheTtl;
await this._set(hostname, query.entries, cacheTtl);
return query.entries;
}
_tick(ms) {
const nextRemovalTime = this._nextRemovalTime;
if (!nextRemovalTime || ms < nextRemovalTime) {
clearTimeout(this._removalTimeout);
this._nextRemovalTime = ms;
this._removalTimeout = setTimeout(() => {
this._nextRemovalTime = false;
let nextExpiry = Infinity;
const now = Date.now();
for (const [hostname, entries] of this._cache) {
const expires = entries[kExpires];
if (now >= expires) {
this._cache.delete(hostname);
} else if (expires < nextExpiry) {
nextExpiry = expires;
}
}
if (nextExpiry !== Infinity) {
this._tick(nextExpiry - now);
}
}, ms);
/* istanbul ignore next: There is no `timeout.unref()` when running inside an Electron renderer */
if (this._removalTimeout.unref) {
this._removalTimeout.unref();
}
}
}
install(agent) {
verifyAgent(agent);
if (kCacheableLookupCreateConnection in agent) {
throw new Error('CacheableLookup has been already installed');
}
agent[kCacheableLookupCreateConnection] = agent.createConnection;
agent[kCacheableLookupInstance] = this;
agent.createConnection = (options, callback) => {
if (!('lookup' in options)) {
options.lookup = this.lookup;
}
return agent[kCacheableLookupCreateConnection](options, callback);
};
}
uninstall(agent) {
verifyAgent(agent);
if (agent[kCacheableLookupCreateConnection]) {
if (agent[kCacheableLookupInstance] !== this) {
throw new Error('The agent is not owned by this CacheableLookup instance');
}
agent.createConnection = agent[kCacheableLookupCreateConnection];
delete agent[kCacheableLookupCreateConnection];
delete agent[kCacheableLookupInstance];
}
}
updateInterfaceInfo() {
const {_iface} = this;
this._iface = getIfaceInfo();
if ((_iface.has4 && !this._iface.has4) || (_iface.has6 && !this._iface.has6)) {
this._cache.clear();
}
}
clear(hostname) {
if (hostname) {
this._cache.delete(hostname);
return;
}
this._cache.clear();
}
}
var source$2 = CacheableLookup;
var _default$k = CacheableLookup;
source$2.default = _default$k;
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
const DATA_URL_DEFAULT_MIME_TYPE = 'text/plain';
const DATA_URL_DEFAULT_CHARSET = 'us-ascii';
const testParameter = (name, filters) => {
return filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);
};
const normalizeDataURL = (urlString, {stripHash}) => {
const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString);
if (!match) {
throw new Error(`Invalid URL: ${urlString}`);
}
let {type, data, hash} = match.groups;
const mediaType = type.split(';');
hash = stripHash ? '' : hash;
let isBase64 = false;
if (mediaType[mediaType.length - 1] === 'base64') {
mediaType.pop();
isBase64 = true;
}
// Lowercase MIME type
const mimeType = (mediaType.shift() || '').toLowerCase();
const attributes = mediaType
.map(attribute => {
let [key, value = ''] = attribute.split('=').map(string => string.trim());
// Lowercase `charset`
if (key === 'charset') {
value = value.toLowerCase();
if (value === DATA_URL_DEFAULT_CHARSET) {
return '';
}
}
return `${key}${value ? `=${value}` : ''}`;
})
.filter(Boolean);
const normalizedMediaType = [
...attributes
];
if (isBase64) {
normalizedMediaType.push('base64');
}
if (normalizedMediaType.length !== 0 || (mimeType && mimeType !== DATA_URL_DEFAULT_MIME_TYPE)) {
normalizedMediaType.unshift(mimeType);
}
return `data:${normalizedMediaType.join(';')},${isBase64 ? data.trim() : data}${hash ? `#${hash}` : ''}`;
};
const normalizeUrl = (urlString, options) => {
options = {
defaultProtocol: 'http:',
normalizeProtocol: true,
forceHttp: false,
forceHttps: false,
stripAuthentication: true,
stripHash: false,
stripTextFragment: true,
stripWWW: true,
removeQueryParameters: [/^utm_\w+/i],
removeTrailingSlash: true,
removeSingleSlash: true,
removeDirectoryIndex: false,
sortQueryParameters: true,
...options
};
urlString = urlString.trim();
// Data URL
if (/^data:/i.test(urlString)) {
return normalizeDataURL(urlString, options);
}
if (/^view-source:/i.test(urlString)) {
throw new Error('`view-source:` is not supported as it is a non-standard protocol');
}
const hasRelativeProtocol = urlString.startsWith('//');
const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
// Prepend protocol
if (!isRelativeUrl) {
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, options.defaultProtocol);
}
const urlObj = new URL(urlString);
if (options.forceHttp && options.forceHttps) {
throw new Error('The `forceHttp` and `forceHttps` options cannot be used together');
}
if (options.forceHttp && urlObj.protocol === 'https:') {
urlObj.protocol = 'http:';
}
if (options.forceHttps && urlObj.protocol === 'http:') {
urlObj.protocol = 'https:';
}
// Remove auth
if (options.stripAuthentication) {
urlObj.username = '';
urlObj.password = '';
}
// Remove hash
if (options.stripHash) {
urlObj.hash = '';
} else if (options.stripTextFragment) {
urlObj.hash = urlObj.hash.replace(/#?:~:text.*?$/i, '');
}
// Remove duplicate slashes if not preceded by a protocol
if (urlObj.pathname) {
urlObj.pathname = urlObj.pathname.replace(/(?<!\b(?:[a-z][a-z\d+\-.]{1,50}:))\/{2,}/g, '/');
}
// Decode URI octets
if (urlObj.pathname) {
try {
urlObj.pathname = decodeURI(urlObj.pathname);
} catch (_) {}
}
// Remove directory index
if (options.removeDirectoryIndex === true) {
options.removeDirectoryIndex = [/^index\.[a-z]+$/];
}
if (Array.isArray(options.removeDirectoryIndex) && options.removeDirectoryIndex.length > 0) {
let pathComponents = urlObj.pathname.split('/');
const lastComponent = pathComponents[pathComponents.length - 1];
if (testParameter(lastComponent, options.removeDirectoryIndex)) {
pathComponents = pathComponents.slice(0, pathComponents.length - 1);
urlObj.pathname = pathComponents.slice(1).join('/') + '/';
}
}
if (urlObj.hostname) {
// Remove trailing dot
urlObj.hostname = urlObj.hostname.replace(/\.$/, '');
// Remove `www.`
if (options.stripWWW && /^www\.(?!www\.)(?:[a-z\-\d]{1,63})\.(?:[a-z.\-\d]{2,63})$/.test(urlObj.hostname)) {
// Each label should be max 63 at length (min: 1).
// Source: https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
// Each TLD should be up to 63 characters long (min: 2).
// It is technically possible to have a single character TLD, but none currently exist.
urlObj.hostname = urlObj.hostname.replace(/^www\./, '');
}
}
// Remove query unwanted parameters
if (Array.isArray(options.removeQueryParameters)) {
for (const key of [...urlObj.searchParams.keys()]) {
if (testParameter(key, options.removeQueryParameters)) {
urlObj.searchParams.delete(key);
}
}
}
if (options.removeQueryParameters === true) {
urlObj.search = '';
}
// Sort query parameters
if (options.sortQueryParameters) {
urlObj.searchParams.sort();
}
if (options.removeTrailingSlash) {
urlObj.pathname = urlObj.pathname.replace(/\/$/, '');
}
const oldUrlString = urlString;
// Take advantage of many of the Node `url` normalizations
urlString = urlObj.toString();
if (!options.removeSingleSlash && urlObj.pathname === '/' && !oldUrlString.endsWith('/') && urlObj.hash === '') {
urlString = urlString.replace(/\/$/, '');
}
// Remove ending `/` unless removeSingleSlash is false
if ((options.removeTrailingSlash || urlObj.pathname === '/') && urlObj.hash === '' && options.removeSingleSlash) {
urlString = urlString.replace(/\/$/, '');
}
// Restore relative protocol, if applicable
if (hasRelativeProtocol && !options.normalizeProtocol) {
urlString = urlString.replace(/^http:\/\//, '//');
}
// Remove http/https
if (options.stripProtocol) {
urlString = urlString.replace(/^(?:https?:)?\/\//, '');
}
return urlString;
};
var normalizeUrl_1 = normalizeUrl;
// Returns a wrapper function that returns a wrapped callback
// The wrapper function should do some stuff, and return a
// presumably different callback function.
// This makes sure that own properties are retained, so that
// decorations and such are not lost along the way.
var wrappy_1 = wrappy;
function wrappy (fn, cb) {
if (fn && cb) return wrappy(fn)(cb)
if (typeof fn !== 'function')
throw new TypeError('need wrapper function')
Object.keys(fn).forEach(function (k) {
wrapper[k] = fn[k];
});
return wrapper
function wrapper() {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; i++) {
args[i] = arguments[i];
}
var ret = fn.apply(this, args);
var cb = args[args.length-1];
if (typeof ret === 'function' && ret !== cb) {
Object.keys(cb).forEach(function (k) {
ret[k] = cb[k];
});
}
return ret
}
}
var once_1 = wrappy_1(once);
var strict = wrappy_1(onceStrict);
once.proto = once(function () {
Object.defineProperty(Function.prototype, 'once', {
value: function () {
return once(this)
},
configurable: true
});
Object.defineProperty(Function.prototype, 'onceStrict', {
value: function () {
return onceStrict(this)
},
configurable: true
});
});
function once (fn) {
var f = function () {
if (f.called) return f.value
f.called = true;
return f.value = fn.apply(this, arguments)
};
f.called = false;
return f
}
function onceStrict (fn) {
var f = function () {
if (f.called)
throw new Error(f.onceError)
f.called = true;
return f.value = fn.apply(this, arguments)
};
var name = fn.name || 'Function wrapped with `once`';
f.onceError = name + " shouldn't be called more than once";
f.called = false;
return f
}
once_1.strict = strict;
var noop$1 = function() {};
var isRequest$1 = function(stream) {
return stream.setHeader && typeof stream.abort === 'function';
};
var isChildProcess = function(stream) {
return stream.stdio && Array.isArray(stream.stdio) && stream.stdio.length === 3
};
var eos = function(stream, opts, callback) {
if (typeof opts === 'function') return eos(stream, null, opts);
if (!opts) opts = {};
callback = once_1(callback || noop$1);
var ws = stream._writableState;
var rs = stream._readableState;
var readable = opts.readable || (opts.readable !== false && stream.readable);
var writable = opts.writable || (opts.writable !== false && stream.writable);
var cancelled = false;
var onlegacyfinish = function() {
if (!stream.writable) onfinish();
};
var onfinish = function() {
writable = false;
if (!readable) callback.call(stream);
};
var onend = function() {
readable = false;
if (!writable) callback.call(stream);
};
var onexit = function(exitCode) {
callback.call(stream, exitCode ? new Error('exited with error code: ' + exitCode) : null);
};
var onerror = function(err) {
callback.call(stream, err);
};
var onclose = function() {
process.nextTick(onclosenexttick);
};
var onclosenexttick = function() {
if (cancelled) return;
if (readable && !(rs && (rs.ended && !rs.destroyed))) return callback.call(stream, new Error('premature close'));
if (writable && !(ws && (ws.ended && !ws.destroyed))) return callback.call(stream, new Error('premature close'));
};
var onrequest = function() {
stream.req.on('finish', onfinish);
};
if (isRequest$1(stream)) {
stream.on('complete', onfinish);
stream.on('abort', onclose);
if (stream.req) onrequest();
else stream.on('request', onrequest);
} else if (writable && !ws) { // legacy streams
stream.on('end', onlegacyfinish);
stream.on('close', onlegacyfinish);
}
if (isChildProcess(stream)) stream.on('exit', onexit);
stream.on('end', onend);
stream.on('finish', onfinish);
if (opts.error !== false) stream.on('error', onerror);
stream.on('close', onclose);
return function() {
cancelled = true;
stream.removeListener('complete', onfinish);
stream.removeListener('abort', onclose);
stream.removeListener('request', onrequest);
if (stream.req) stream.req.removeListener('finish', onfinish);
stream.removeListener('end', onlegacyfinish);
stream.removeListener('close', onlegacyfinish);
stream.removeListener('finish', onfinish);
stream.removeListener('exit', onexit);
stream.removeListener('end', onend);
stream.removeListener('error', onerror);
stream.removeListener('close', onclose);
};
};
var endOfStream = eos;
// we only need fs to get the ReadStream and WriteStream prototypes
var noop = function () {};
var ancient = /^v?\.0/.test(process.version);
var isFn = function (fn) {
return typeof fn === 'function'
};
var isFS = function (stream) {
if (!ancient) return false // newer node version do not need to care about fs is a special way
if (!fs__default['default']) return false // browser
return (stream instanceof (fs__default['default'].ReadStream || noop) || stream instanceof (fs__default['default'].WriteStream || noop)) && isFn(stream.close)
};
var isRequest = function (stream) {
return stream.setHeader && isFn(stream.abort)
};
var destroyer = function (stream, reading, writing, callback) {
callback = once_1(callback);
var closed = false;
stream.on('close', function () {
closed = true;
});
endOfStream(stream, {readable: reading, writable: writing}, function (err) {
if (err) return callback(err)
closed = true;
callback();
});
var destroyed = false;
return function (err) {
if (closed) return
if (destroyed) return
destroyed = true;
if (isFS(stream)) return stream.close(noop) // use close for fs streams to avoid fd leaks
if (isRequest(stream)) return stream.abort() // request.destroy just do .end - .abort is what we want
if (isFn(stream.destroy)) return stream.destroy()
callback(err || new Error('stream was destroyed'));
}
};
var call = function (fn) {
fn();
};
var pipe = function (from, to) {
return from.pipe(to)
};
var pump = function () {
var streams = Array.prototype.slice.call(arguments);
var callback = isFn(streams[streams.length - 1] || noop) && streams.pop() || noop;
if (Array.isArray(streams[0])) streams = streams[0];
if (streams.length < 2) throw new Error('pump requires two streams per minimum')
var error;
var destroys = streams.map(function (stream, i) {
var reading = i < streams.length - 1;
var writing = i > 0;
return destroyer(stream, reading, writing, function (err) {
if (!error) error = err;
if (err) destroys.forEach(call);
if (reading) return
destroys.forEach(call);
callback(error);
})
});
return streams.reduce(pipe)
};
var pump_1 = pump;
const {PassThrough: PassThroughStream} = require$$0__default$1['default'];
var bufferStream = options => {
options = {...options};
const {array} = options;
let {encoding} = options;
const isBuffer = encoding === 'buffer';
let objectMode = false;
if (array) {
objectMode = !(encoding || isBuffer);
} else {
encoding = encoding || 'utf8';
}
if (isBuffer) {
encoding = null;
}
const stream = new PassThroughStream({objectMode});
if (encoding) {
stream.setEncoding(encoding);
}
let length = 0;
const chunks = [];
stream.on('data', chunk => {
chunks.push(chunk);
if (objectMode) {
length = chunks.length;
} else {
length += chunk.length;
}
});
stream.getBufferedValue = () => {
if (array) {
return chunks;
}
return isBuffer ? Buffer.concat(chunks, length) : chunks.join('');
};
stream.getBufferedLength = () => length;
return stream;
};
const {constants: BufferConstants} = require$$0__default$2['default'];
class MaxBufferError extends Error {
constructor() {
super('maxBuffer exceeded');
this.name = 'MaxBufferError';
}
}
async function getStream(inputStream, options) {
if (!inputStream) {
return Promise.reject(new Error('Expected a stream'));
}
options = {
maxBuffer: Infinity,
...options
};
const {maxBuffer} = options;
let stream;
await new Promise((resolve, reject) => {
const rejectPromise = error => {
// Don't retrieve an oversized buffer.
if (error && stream.getBufferedLength() <= BufferConstants.MAX_LENGTH) {
error.bufferedData = stream.getBufferedValue();
}
reject(error);
};
stream = pump_1(inputStream, bufferStream(options), error => {
if (error) {
rejectPromise(error);
return;
}
resolve();
});
stream.on('data', () => {
if (stream.getBufferedLength() > maxBuffer) {
rejectPromise(new MaxBufferError());
}
});
});
return stream.getBufferedValue();
}
var getStream_1 = getStream;
// TODO: Remove this for the next major release
var _default$j = getStream;
var buffer = (stream, options) => getStream(stream, {...options, encoding: 'buffer'});
var array = (stream, options) => getStream(stream, {...options, array: true});
var MaxBufferError_1 = MaxBufferError;
getStream_1.default = _default$j;
getStream_1.buffer = buffer;
getStream_1.array = array;
getStream_1.MaxBufferError = MaxBufferError_1;
// rfc7231 6.1
const statusCodeCacheableByDefault = new Set([
200,
203,
204,
206,
300,
301,
404,
405,
410,
414,
501,
]);
// This implementation does not understand partial responses (206)
const understoodStatuses = new Set([
200,
203,
204,
300,
301,
302,
303,
307,
308,
404,
405,
410,
414,
501,
]);
const errorStatusCodes = new Set([
500,
502,
503,
504,
]);
const hopByHopHeaders = {
date: true, // included, because we add Age update Date
connection: true,
'keep-alive': true,
'proxy-authenticate': true,
'proxy-authorization': true,
te: true,
trailer: true,
'transfer-encoding': true,
upgrade: true,
};
const excludedFromRevalidationUpdate = {
// Since the old body is reused, it doesn't make sense to change properties of the body
'content-length': true,
'content-encoding': true,
'transfer-encoding': true,
'content-range': true,
};
function toNumberOrZero(s) {
const n = parseInt(s, 10);
return isFinite(n) ? n : 0;
}
// RFC 5861
function isErrorResponse(response) {
// consider undefined response as faulty
if(!response) {
return true
}
return errorStatusCodes.has(response.status);
}
function parseCacheControl(header) {
const cc = {};
if (!header) return cc;
// TODO: When there is more than one value present for a given directive (e.g., two Expires header fields, multiple Cache-Control: max-age directives),
// the directive's value is considered invalid. Caches are encouraged to consider responses that have invalid freshness information to be stale
const parts = header.trim().split(/\s*,\s*/); // TODO: lame parsing
for (const part of parts) {
const [k, v] = part.split(/\s*=\s*/, 2);
cc[k] = v === undefined ? true : v.replace(/^"|"$/g, ''); // TODO: lame unquoting
}
return cc;
}
function formatCacheControl(cc) {
let parts = [];
for (const k in cc) {
const v = cc[k];
parts.push(v === true ? k : k + '=' + v);
}
if (!parts.length) {
return undefined;
}
return parts.join(', ');
}
var httpCacheSemantics = class CachePolicy {
constructor(
req,
res,
{
shared,
cacheHeuristic,
immutableMinTimeToLive,
ignoreCargoCult,
_fromObject,
} = {}
) {
if (_fromObject) {
this._fromObject(_fromObject);
return;
}
if (!res || !res.headers) {
throw Error('Response headers missing');
}
this._assertRequestHasHeaders(req);
this._responseTime = this.now();
this._isShared = shared !== false;
this._cacheHeuristic =
undefined !== cacheHeuristic ? cacheHeuristic : 0.1; // 10% matches IE
this._immutableMinTtl =
undefined !== immutableMinTimeToLive
? immutableMinTimeToLive
: 24 * 3600 * 1000;
this._status = 'status' in res ? res.status : 200;
this._resHeaders = res.headers;
this._rescc = parseCacheControl(res.headers['cache-control']);
this._method = 'method' in req ? req.method : 'GET';
this._url = req.url;
this._host = req.headers.host;
this._noAuthorization = !req.headers.authorization;
this._reqHeaders = res.headers.vary ? req.headers : null; // Don't keep all request headers if they won't be used
this._reqcc = parseCacheControl(req.headers['cache-control']);
// Assume that if someone uses legacy, non-standard uncecessary options they don't understand caching,
// so there's no point stricly adhering to the blindly copy&pasted directives.
if (
ignoreCargoCult &&
'pre-check' in this._rescc &&
'post-check' in this._rescc
) {
delete this._rescc['pre-check'];
delete this._rescc['post-check'];
delete this._rescc['no-cache'];
delete this._rescc['no-store'];
delete this._rescc['must-revalidate'];
this._resHeaders = Object.assign({}, this._resHeaders, {
'cache-control': formatCacheControl(this._rescc),
});
delete this._resHeaders.expires;
delete this._resHeaders.pragma;
}
// When the Cache-Control header field is not present in a request, caches MUST consider the no-cache request pragma-directive
// as having the same effect as if "Cache-Control: no-cache" were present (see Section 5.2.1).
if (
res.headers['cache-control'] == null &&
/no-cache/.test(res.headers.pragma)
) {
this._rescc['no-cache'] = true;
}
}
now() {
return Date.now();
}
storable() {
// The "no-store" request directive indicates that a cache MUST NOT store any part of either this request or any response to it.
return !!(
!this._reqcc['no-store'] &&
// A cache MUST NOT store a response to any request, unless:
// The request method is understood by the cache and defined as being cacheable, and
('GET' === this._method ||
'HEAD' === this._method ||
('POST' === this._method && this._hasExplicitExpiration())) &&
// the response status code is understood by the cache, and
understoodStatuses.has(this._status) &&
// the "no-store" cache directive does not appear in request or response header fields, and
!this._rescc['no-store'] &&
// the "private" response directive does not appear in the response, if the cache is shared, and
(!this._isShared || !this._rescc.private) &&
// the Authorization header field does not appear in the request, if the cache is shared,
(!this._isShared ||
this._noAuthorization ||
this._allowsStoringAuthenticated()) &&
// the response either:
// contains an Expires header field, or
(this._resHeaders.expires ||
// contains a max-age response directive, or
// contains a s-maxage response directive and the cache is shared, or
// contains a public response directive.
this._rescc['max-age'] ||
(this._isShared && this._rescc['s-maxage']) ||
this._rescc.public ||
// has a status code that is defined as cacheable by default
statusCodeCacheableByDefault.has(this._status))
);
}
_hasExplicitExpiration() {
// 4.2.1 Calculating Freshness Lifetime
return (
(this._isShared && this._rescc['s-maxage']) ||
this._rescc['max-age'] ||
this._resHeaders.expires
);
}
_assertRequestHasHeaders(req) {
if (!req || !req.headers) {
throw Error('Request headers missing');
}
}
satisfiesWithoutRevalidation(req) {
this._assertRequestHasHeaders(req);
// When presented with a request, a cache MUST NOT reuse a stored response, unless:
// the presented request does not contain the no-cache pragma (Section 5.4), nor the no-cache cache directive,
// unless the stored response is successfully validated (Section 4.3), and
const requestCC = parseCacheControl(req.headers['cache-control']);
if (requestCC['no-cache'] || /no-cache/.test(req.headers.pragma)) {
return false;
}
if (requestCC['max-age'] && this.age() > requestCC['max-age']) {
return false;
}
if (
requestCC['min-fresh'] &&
this.timeToLive() < 1000 * requestCC['min-fresh']
) {
return false;
}
// the stored response is either:
// fresh, or allowed to be served stale
if (this.stale()) {
const allowsStale =
requestCC['max-stale'] &&
!this._rescc['must-revalidate'] &&
(true === requestCC['max-stale'] ||
requestCC['max-stale'] > this.age() - this.maxAge());
if (!allowsStale) {
return false;
}
}
return this._requestMatches(req, false);
}
_requestMatches(req, allowHeadMethod) {
// The presented effective request URI and that of the stored response match, and
return (
(!this._url || this._url === req.url) &&
this._host === req.headers.host &&
// the request method associated with the stored response allows it to be used for the presented request, and
(!req.method ||
this._method === req.method ||
(allowHeadMethod && 'HEAD' === req.method)) &&
// selecting header fields nominated by the stored response (if any) match those presented, and
this._varyMatches(req)
);
}
_allowsStoringAuthenticated() {
// following Cache-Control response directives (Section 5.2.2) have such an effect: must-revalidate, public, and s-maxage.
return (
this._rescc['must-revalidate'] ||
this._rescc.public ||
this._rescc['s-maxage']
);
}
_varyMatches(req) {
if (!this._resHeaders.vary) {
return true;
}
// A Vary header field-value of "*" always fails to match
if (this._resHeaders.vary === '*') {
return false;
}
const fields = this._resHeaders.vary
.trim()
.toLowerCase()
.split(/\s*,\s*/);
for (const name of fields) {
if (req.headers[name] !== this._reqHeaders[name]) return false;
}
return true;
}
_copyWithoutHopByHopHeaders(inHeaders) {
const headers = {};
for (const name in inHeaders) {
if (hopByHopHeaders[name]) continue;
headers[name] = inHeaders[name];
}
// 9.1. Connection
if (inHeaders.connection) {
const tokens = inHeaders.connection.trim().split(/\s*,\s*/);
for (const name of tokens) {
delete headers[name];
}
}
if (headers.warning) {
const warnings = headers.warning.split(/,/).filter(warning => {
return !/^\s*1[0-9][0-9]/.test(warning);
});
if (!warnings.length) {
delete headers.warning;
} else {
headers.warning = warnings.join(',').trim();
}
}
return headers;
}
responseHeaders() {
const headers = this._copyWithoutHopByHopHeaders(this._resHeaders);
const age = this.age();
// A cache SHOULD generate 113 warning if it heuristically chose a freshness
// lifetime greater than 24 hours and the response's age is greater than 24 hours.
if (
age > 3600 * 24 &&
!this._hasExplicitExpiration() &&
this.maxAge() > 3600 * 24
) {
headers.warning =
(headers.warning ? `${headers.warning}, ` : '') +
'113 - "rfc7234 5.5.4"';
}
headers.age = `${Math.round(age)}`;
headers.date = new Date(this.now()).toUTCString();
return headers;
}
/**
* Value of the Date response header or current time if Date was invalid
* @return timestamp
*/
date() {
const serverDate = Date.parse(this._resHeaders.date);
if (isFinite(serverDate)) {
return serverDate;
}
return this._responseTime;
}
/**
* Value of the Age header, in seconds, updated for the current time.
* May be fractional.
*
* @return Number
*/
age() {
let age = this._ageValue();
const residentTime = (this.now() - this._responseTime) / 1000;
return age + residentTime;
}
_ageValue() {
return toNumberOrZero(this._resHeaders.age);
}
/**
* Value of applicable max-age (or heuristic equivalent) in seconds. This counts since response's `Date`.
*
* For an up-to-date value, see `timeToLive()`.
*
* @return Number
*/
maxAge() {
if (!this.storable() || this._rescc['no-cache']) {
return 0;
}
// Shared responses with cookies are cacheable according to the RFC, but IMHO it'd be unwise to do so by default
// so this implementation requires explicit opt-in via public header
if (
this._isShared &&
(this._resHeaders['set-cookie'] &&
!this._rescc.public &&
!this._rescc.immutable)
) {
return 0;
}
if (this._resHeaders.vary === '*') {
return 0;
}
if (this._isShared) {
if (this._rescc['proxy-revalidate']) {
return 0;
}
// if a response includes the s-maxage directive, a shared cache recipient MUST ignore the Expires field.
if (this._rescc['s-maxage']) {
return toNumberOrZero(this._rescc['s-maxage']);
}
}
// If a response includes a Cache-Control field with the max-age directive, a recipient MUST ignore the Expires field.
if (this._rescc['max-age']) {
return toNumberOrZero(this._rescc['max-age']);
}
const defaultMinTtl = this._rescc.immutable ? this._immutableMinTtl : 0;
const serverDate = this.date();
if (this._resHeaders.expires) {
const expires = Date.parse(this._resHeaders.expires);
// A cache recipient MUST interpret invalid date formats, especially the value "0", as representing a time in the past (i.e., "already expired").
if (Number.isNaN(expires) || expires < serverDate) {
return 0;
}
return Math.max(defaultMinTtl, (expires - serverDate) / 1000);
}
if (this._resHeaders['last-modified']) {
const lastModified = Date.parse(this._resHeaders['last-modified']);
if (isFinite(lastModified) && serverDate > lastModified) {
return Math.max(
defaultMinTtl,
((serverDate - lastModified) / 1000) * this._cacheHeuristic
);
}
}
return defaultMinTtl;
}
timeToLive() {
const age = this.maxAge() - this.age();
const staleIfErrorAge = age + toNumberOrZero(this._rescc['stale-if-error']);
const staleWhileRevalidateAge = age + toNumberOrZero(this._rescc['stale-while-revalidate']);
return Math.max(0, age, staleIfErrorAge, staleWhileRevalidateAge) * 1000;
}
stale() {
return this.maxAge() <= this.age();
}
_useStaleIfError() {
return this.maxAge() + toNumberOrZero(this._rescc['stale-if-error']) > this.age();
}
useStaleWhileRevalidate() {
return this.maxAge() + toNumberOrZero(this._rescc['stale-while-revalidate']) > this.age();
}
static fromObject(obj) {
return new this(undefined, undefined, { _fromObject: obj });
}
_fromObject(obj) {
if (this._responseTime) throw Error('Reinitialized');
if (!obj || obj.v !== 1) throw Error('Invalid serialization');
this._responseTime = obj.t;
this._isShared = obj.sh;
this._cacheHeuristic = obj.ch;
this._immutableMinTtl =
obj.imm !== undefined ? obj.imm : 24 * 3600 * 1000;
this._status = obj.st;
this._resHeaders = obj.resh;
this._rescc = obj.rescc;
this._method = obj.m;
this._url = obj.u;
this._host = obj.h;
this._noAuthorization = obj.a;
this._reqHeaders = obj.reqh;
this._reqcc = obj.reqcc;
}
toObject() {
return {
v: 1,
t: this._responseTime,
sh: this._isShared,
ch: this._cacheHeuristic,
imm: this._immutableMinTtl,
st: this._status,
resh: this._resHeaders,
rescc: this._rescc,
m: this._method,
u: this._url,
h: this._host,
a: this._noAuthorization,
reqh: this._reqHeaders,
reqcc: this._reqcc,
};
}
/**
* Headers for sending to the origin server to revalidate stale response.
* Allows server to return 304 to allow reuse of the previous response.
*
* Hop by hop headers are always stripped.
* Revalidation headers may be added or removed, depending on request.
*/
revalidationHeaders(incomingReq) {
this._assertRequestHasHeaders(incomingReq);
const headers = this._copyWithoutHopByHopHeaders(incomingReq.headers);
// This implementation does not understand range requests
delete headers['if-range'];
if (!this._requestMatches(incomingReq, true) || !this.storable()) {
// revalidation allowed via HEAD
// not for the same resource, or wasn't allowed to be cached anyway
delete headers['if-none-match'];
delete headers['if-modified-since'];
return headers;
}
/* MUST send that entity-tag in any cache validation request (using If-Match or If-None-Match) if an entity-tag has been provided by the origin server. */
if (this._resHeaders.etag) {
headers['if-none-match'] = headers['if-none-match']
? `${headers['if-none-match']}, ${this._resHeaders.etag}`
: this._resHeaders.etag;
}
// Clients MAY issue simple (non-subrange) GET requests with either weak validators or strong validators. Clients MUST NOT use weak validators in other forms of request.
const forbidsWeakValidators =
headers['accept-ranges'] ||
headers['if-match'] ||
headers['if-unmodified-since'] ||
(this._method && this._method != 'GET');
/* SHOULD send the Last-Modified value in non-subrange cache validation requests (using If-Modified-Since) if only a Last-Modified value has been provided by the origin server.
Note: This implementation does not understand partial responses (206) */
if (forbidsWeakValidators) {
delete headers['if-modified-since'];
if (headers['if-none-match']) {
const etags = headers['if-none-match']
.split(/,/)
.filter(etag => {
return !/^\s*W\//.test(etag);
});
if (!etags.length) {
delete headers['if-none-match'];
} else {
headers['if-none-match'] = etags.join(',').trim();
}
}
} else if (
this._resHeaders['last-modified'] &&
!headers['if-modified-since']
) {
headers['if-modified-since'] = this._resHeaders['last-modified'];
}
return headers;
}
/**
* Creates new CachePolicy with information combined from the previews response,
* and the new revalidation response.
*
* Returns {policy, modified} where modified is a boolean indicating
* whether the response body has been modified, and old cached body can't be used.
*
* @return {Object} {policy: CachePolicy, modified: Boolean}
*/
revalidatedPolicy(request, response) {
this._assertRequestHasHeaders(request);
if(this._useStaleIfError() && isErrorResponse(response)) { // I consider the revalidation request unsuccessful
return {
modified: false,
matches: false,
policy: this,
};
}
if (!response || !response.headers) {
throw Error('Response headers missing');
}
// These aren't going to be supported exactly, since one CachePolicy object
// doesn't know about all the other cached objects.
let matches = false;
if (response.status !== undefined && response.status != 304) {
matches = false;
} else if (
response.headers.etag &&
!/^\s*W\//.test(response.headers.etag)
) {
// "All of the stored responses with the same strong validator are selected.
// If none of the stored responses contain the same strong validator,
// then the cache MUST NOT use the new response to update any stored responses."
matches =
this._resHeaders.etag &&
this._resHeaders.etag.replace(/^\s*W\//, '') ===
response.headers.etag;
} else if (this._resHeaders.etag && response.headers.etag) {
// "If the new response contains a weak validator and that validator corresponds
// to one of the cache's stored responses,
// then the most recent of those matching stored responses is selected for update."
matches =
this._resHeaders.etag.replace(/^\s*W\//, '') ===
response.headers.etag.replace(/^\s*W\//, '');
} else if (this._resHeaders['last-modified']) {
matches =
this._resHeaders['last-modified'] ===
response.headers['last-modified'];
} else {
// If the new response does not include any form of validator (such as in the case where
// a client generates an If-Modified-Since request from a source other than the Last-Modified
// response header field), and there is only one stored response, and that stored response also
// lacks a validator, then that stored response is selected for update.
if (
!this._resHeaders.etag &&
!this._resHeaders['last-modified'] &&
!response.headers.etag &&
!response.headers['last-modified']
) {
matches = true;
}
}
if (!matches) {
return {
policy: new this.constructor(request, response),
// Client receiving 304 without body, even if it's invalid/mismatched has no option
// but to reuse a cached body. We don't have a good way to tell clients to do
// error recovery in such case.
modified: response.status != 304,
matches: false,
};
}
// use other header fields provided in the 304 (Not Modified) response to replace all instances
// of the corresponding header fields in the stored response.
const headers = {};
for (const k in this._resHeaders) {
headers[k] =
k in response.headers && !excludedFromRevalidationUpdate[k]
? response.headers[k]
: this._resHeaders[k];
}
const newResponse = Object.assign({}, response, {
status: this._status,
method: this._method,
headers,
});
return {
policy: new this.constructor(request, newResponse, {
shared: this._isShared,
cacheHeuristic: this._cacheHeuristic,
immutableMinTimeToLive: this._immutableMinTtl,
}),
modified: false,
matches: true,
};
}
};
var lowercaseKeys = object => {
const result = {};
for (const [key, value] of Object.entries(object)) {
result[key.toLowerCase()] = value;
}
return result;
};
const Readable$1 = require$$0__default$1['default'].Readable;
class Response extends Readable$1 {
constructor(statusCode, headers, body, url) {
if (typeof statusCode !== 'number') {
throw new TypeError('Argument `statusCode` should be a number');
}
if (typeof headers !== 'object') {
throw new TypeError('Argument `headers` should be an object');
}
if (!(body instanceof Buffer)) {
throw new TypeError('Argument `body` should be a buffer');
}
if (typeof url !== 'string') {
throw new TypeError('Argument `url` should be a string');
}
super();
this.statusCode = statusCode;
this.headers = lowercaseKeys(headers);
this.body = body;
this.url = url;
}
_read() {
this.push(this.body);
this.push(null);
}
}
var src$3 = Response;
// We define these manually to ensure they're always copied
// even if they would move up the prototype chain
// https://nodejs.org/api/http.html#http_class_http_incomingmessage
const knownProps = [
'destroy',
'setTimeout',
'socket',
'headers',
'trailers',
'rawHeaders',
'statusCode',
'httpVersion',
'httpVersionMinor',
'httpVersionMajor',
'rawTrailers',
'statusMessage'
];
var mimicResponse$1 = (fromStream, toStream) => {
const fromProps = new Set(Object.keys(fromStream).concat(knownProps));
for (const prop of fromProps) {
// Don't overwrite existing properties
if (prop in toStream) {
continue;
}
toStream[prop] = typeof fromStream[prop] === 'function' ? fromStream[prop].bind(fromStream) : fromStream[prop];
}
};
const PassThrough$1 = require$$0__default$1['default'].PassThrough;
const cloneResponse = response => {
if (!(response && response.pipe)) {
throw new TypeError('Parameter `response` must be a response stream.');
}
const clone = new PassThrough$1();
mimicResponse$1(response, clone);
return response.pipe(clone);
};
var src$2 = cloneResponse;
//TODO: handle reviver/dehydrate function like normal
//and handle indentation, like normal.
//if anyone needs this... please send pull request.
var stringify = function stringify (o) {
if('undefined' == typeof o) return o
if(o && Buffer.isBuffer(o))
return JSON.stringify(':base64:' + o.toString('base64'))
if(o && o.toJSON)
o = o.toJSON();
if(o && 'object' === typeof o) {
var s = '';
var array = Array.isArray(o);
s = array ? '[' : '{';
var first = true;
for(var k in o) {
var ignore = 'function' == typeof o[k] || (!array && 'undefined' === typeof o[k]);
if(Object.hasOwnProperty.call(o, k) && !ignore) {
if(!first)
s += ',';
first = false;
if (array) {
if(o[k] == undefined)
s += 'null';
else
s += stringify(o[k]);
} else if (o[k] !== void(0)) {
s += stringify(k) + ':' + stringify(o[k]);
}
}
}
s += array ? ']' : '}';
return s
} else if ('string' === typeof o) {
return JSON.stringify(/^:/.test(o) ? ':' + o : o)
} else if ('undefined' === typeof o) {
return 'null';
} else
return JSON.stringify(o)
};
var parse = function (s) {
return JSON.parse(s, function (key, value) {
if('string' === typeof value) {
if(/^:base64:/.test(value))
return Buffer.from(value.substring(8), 'base64')
else
return /^:/.test(value) ? value.substring(1) : value
}
return value
})
};
var jsonBuffer = {
stringify: stringify,
parse: parse
};
const loadStore = opts => {
const adapters = {
redis: '@keyv/redis',
mongodb: '@keyv/mongo',
mongo: '@keyv/mongo',
sqlite: '@keyv/sqlite',
postgresql: '@keyv/postgres',
postgres: '@keyv/postgres',
mysql: '@keyv/mysql'
};
if (opts.adapter || opts.uri) {
const adapter = opts.adapter || /^[^:]*/.exec(opts.uri)[0];
return new (commonjsRequire(adapters[adapter]))(opts);
}
return new Map();
};
class Keyv extends EventEmitter__default['default'] {
constructor(uri, opts) {
super();
this.opts = Object.assign(
{
namespace: 'keyv',
serialize: jsonBuffer.stringify,
deserialize: jsonBuffer.parse
},
(typeof uri === 'string') ? { uri } : uri,
opts
);
if (!this.opts.store) {
const adapterOpts = Object.assign({}, this.opts);
this.opts.store = loadStore(adapterOpts);
}
if (typeof this.opts.store.on === 'function') {
this.opts.store.on('error', err => this.emit('error', err));
}
this.opts.store.namespace = this.opts.namespace;
}
_getKeyPrefix(key) {
return `${this.opts.namespace}:${key}`;
}
get(key, opts) {
const keyPrefixed = this._getKeyPrefix(key);
const { store } = this.opts;
return Promise.resolve()
.then(() => store.get(keyPrefixed))
.then(data => {
return (typeof data === 'string') ? this.opts.deserialize(data) : data;
})
.then(data => {
if (data === undefined) {
return undefined;
}
if (typeof data.expires === 'number' && Date.now() > data.expires) {
this.delete(key);
return undefined;
}
return (opts && opts.raw) ? data : data.value;
});
}
set(key, value, ttl) {
const keyPrefixed = this._getKeyPrefix(key);
if (typeof ttl === 'undefined') {
ttl = this.opts.ttl;
}
if (ttl === 0) {
ttl = undefined;
}
const { store } = this.opts;
return Promise.resolve()
.then(() => {
const expires = (typeof ttl === 'number') ? (Date.now() + ttl) : null;
value = { value, expires };
return this.opts.serialize(value);
})
.then(value => store.set(keyPrefixed, value, ttl))
.then(() => true);
}
delete(key) {
const keyPrefixed = this._getKeyPrefix(key);
const { store } = this.opts;
return Promise.resolve()
.then(() => store.delete(keyPrefixed));
}
clear() {
const { store } = this.opts;
return Promise.resolve()
.then(() => store.clear());
}
}
var src$1 = Keyv;
class CacheableRequest {
constructor(request, cacheAdapter) {
if (typeof request !== 'function') {
throw new TypeError('Parameter `request` must be a function');
}
this.cache = new src$1({
uri: typeof cacheAdapter === 'string' && cacheAdapter,
store: typeof cacheAdapter !== 'string' && cacheAdapter,
namespace: 'cacheable-request'
});
return this.createCacheableRequest(request);
}
createCacheableRequest(request) {
return (opts, cb) => {
let url;
if (typeof opts === 'string') {
url = normalizeUrlObject(url_1__default['default'].parse(opts));
opts = {};
} else if (opts instanceof url_1__default['default'].URL) {
url = normalizeUrlObject(url_1__default['default'].parse(opts.toString()));
opts = {};
} else {
const [pathname, ...searchParts] = (opts.path || '').split('?');
const search = searchParts.length > 0 ?
`?${searchParts.join('?')}` :
'';
url = normalizeUrlObject({ ...opts, pathname, search });
}
opts = {
headers: {},
method: 'GET',
cache: true,
strictTtl: false,
automaticFailover: false,
...opts,
...urlObjectToRequestOptions(url)
};
opts.headers = lowercaseKeys(opts.headers);
const ee = new EventEmitter__default['default']();
const normalizedUrlString = normalizeUrl_1(
url_1__default['default'].format(url),
{
stripWWW: false,
removeTrailingSlash: false,
stripAuthentication: false
}
);
const key = `${opts.method}:${normalizedUrlString}`;
let revalidate = false;
let madeRequest = false;
const makeRequest = opts => {
madeRequest = true;
let requestErrored = false;
let requestErrorCallback;
const requestErrorPromise = new Promise(resolve => {
requestErrorCallback = () => {
if (!requestErrored) {
requestErrored = true;
resolve();
}
};
});
const handler = response => {
if (revalidate && !opts.forceRefresh) {
response.status = response.statusCode;
const revalidatedPolicy = httpCacheSemantics.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response);
if (!revalidatedPolicy.modified) {
const headers = revalidatedPolicy.policy.responseHeaders();
response = new src$3(revalidate.statusCode, headers, revalidate.body, revalidate.url);
response.cachePolicy = revalidatedPolicy.policy;
response.fromCache = true;
}
}
if (!response.fromCache) {
response.cachePolicy = new httpCacheSemantics(opts, response, opts);
response.fromCache = false;
}
let clonedResponse;
if (opts.cache && response.cachePolicy.storable()) {
clonedResponse = src$2(response);
(async () => {
try {
const bodyPromise = getStream_1.buffer(response);
await Promise.race([
requestErrorPromise,
new Promise(resolve => response.once('end', resolve))
]);
if (requestErrored) {
return;
}
const body = await bodyPromise;
const value = {
cachePolicy: response.cachePolicy.toObject(),
url: response.url,
statusCode: response.fromCache ? revalidate.statusCode : response.statusCode,
body
};
let ttl = opts.strictTtl ? response.cachePolicy.timeToLive() : undefined;
if (opts.maxTtl) {
ttl = ttl ? Math.min(ttl, opts.maxTtl) : opts.maxTtl;
}
await this.cache.set(key, value, ttl);
} catch (error) {
ee.emit('error', new CacheableRequest.CacheError(error));
}
})();
} else if (opts.cache && revalidate) {
(async () => {
try {
await this.cache.delete(key);
} catch (error) {
ee.emit('error', new CacheableRequest.CacheError(error));
}
})();
}
ee.emit('response', clonedResponse || response);
if (typeof cb === 'function') {
cb(clonedResponse || response);
}
};
try {
const req = request(opts, handler);
req.once('error', requestErrorCallback);
req.once('abort', requestErrorCallback);
ee.emit('request', req);
} catch (error) {
ee.emit('error', new CacheableRequest.RequestError(error));
}
};
(async () => {
const get = async opts => {
await Promise.resolve();
const cacheEntry = opts.cache ? await this.cache.get(key) : undefined;
if (typeof cacheEntry === 'undefined') {
return makeRequest(opts);
}
const policy = httpCacheSemantics.fromObject(cacheEntry.cachePolicy);
if (policy.satisfiesWithoutRevalidation(opts) && !opts.forceRefresh) {
const headers = policy.responseHeaders();
const response = new src$3(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
response.cachePolicy = policy;
response.fromCache = true;
ee.emit('response', response);
if (typeof cb === 'function') {
cb(response);
}
} else {
revalidate = cacheEntry;
opts.headers = policy.revalidationHeaders(opts);
makeRequest(opts);
}
};
const errorHandler = error => ee.emit('error', new CacheableRequest.CacheError(error));
this.cache.once('error', errorHandler);
ee.on('response', () => this.cache.removeListener('error', errorHandler));
try {
await get(opts);
} catch (error) {
if (opts.automaticFailover && !madeRequest) {
makeRequest(opts);
}
ee.emit('error', new CacheableRequest.CacheError(error));
}
})();
return ee;
};
}
}
function urlObjectToRequestOptions(url) {
const options = { ...url };
options.path = `${url.pathname || '/'}${url.search || ''}`;
delete options.pathname;
delete options.search;
return options;
}
function normalizeUrlObject(url) {
// If url was parsed by url.parse or new URL:
// - hostname will be set
// - host will be hostname[:port]
// - port will be set if it was explicit in the parsed string
// Otherwise, url was from request options:
// - hostname or host may be set
// - host shall not have port encoded
return {
protocol: url.protocol,
auth: url.auth,
hostname: url.hostname || url.host || 'localhost',
port: url.port,
pathname: url.pathname,
search: url.search
};
}
CacheableRequest.RequestError = class extends Error {
constructor(error) {
super(error.message);
this.name = 'RequestError';
Object.assign(this, error);
}
};
CacheableRequest.CacheError = class extends Error {
constructor(error) {
super(error.message);
this.name = 'CacheError';
Object.assign(this, error);
}
};
var src = CacheableRequest;
// We define these manually to ensure they're always copied
// even if they would move up the prototype chain
// https://nodejs.org/api/http.html#http_class_http_incomingmessage
const knownProperties = [
'aborted',
'complete',
'headers',
'httpVersion',
'httpVersionMinor',
'httpVersionMajor',
'method',
'rawHeaders',
'rawTrailers',
'setTimeout',
'socket',
'statusCode',
'statusMessage',
'trailers',
'url'
];
var mimicResponse = (fromStream, toStream) => {
if (toStream._readableState.autoDestroy) {
throw new Error('The second stream must have the `autoDestroy` option set to `false`');
}
const fromProperties = new Set(Object.keys(fromStream).concat(knownProperties));
const properties = {};
for (const property of fromProperties) {
// Don't overwrite existing properties.
if (property in toStream) {
continue;
}
properties[property] = {
get() {
const value = fromStream[property];
const isFunction = typeof value === 'function';
return isFunction ? value.bind(fromStream) : value;
},
set(value) {
fromStream[property] = value;
},
enumerable: true,
configurable: false
};
}
Object.defineProperties(toStream, properties);
fromStream.once('aborted', () => {
toStream.destroy();
toStream.emit('aborted');
});
fromStream.once('close', () => {
if (fromStream.complete) {
if (toStream.readable) {
toStream.once('end', () => {
toStream.emit('close');
});
} else {
toStream.emit('close');
}
} else {
toStream.emit('close');
}
});
return toStream;
};
const {Transform, PassThrough} = require$$0__default$1['default'];
var decompressResponse = response => {
const contentEncoding = (response.headers['content-encoding'] || '').toLowerCase();
if (!['gzip', 'deflate', 'br'].includes(contentEncoding)) {
return response;
}
// TODO: Remove this when targeting Node.js 12.
const isBrotli = contentEncoding === 'br';
if (isBrotli && typeof zlib__default['default'].createBrotliDecompress !== 'function') {
response.destroy(new Error('Brotli is not supported on Node.js < 12'));
return response;
}
let isEmpty = true;
const checker = new Transform({
transform(data, _encoding, callback) {
isEmpty = false;
callback(null, data);
},
flush(callback) {
callback();
}
});
const finalStream = new PassThrough({
autoDestroy: false,
destroy(error, callback) {
response.destroy();
callback(error);
}
});
const decompressStream = isBrotli ? zlib__default['default'].createBrotliDecompress() : zlib__default['default'].createUnzip();
decompressStream.once('error', error => {
if (isEmpty && !response.readable) {
finalStream.end();
return;
}
finalStream.destroy(error);
});
mimicResponse(response, finalStream);
response.pipe(checker).pipe(decompressStream).pipe(finalStream);
return finalStream;
};
class QuickLRU {
constructor(options = {}) {
if (!(options.maxSize && options.maxSize > 0)) {
throw new TypeError('`maxSize` must be a number greater than 0');
}
this.maxSize = options.maxSize;
this.onEviction = options.onEviction;
this.cache = new Map();
this.oldCache = new Map();
this._size = 0;
}
_set(key, value) {
this.cache.set(key, value);
this._size++;
if (this._size >= this.maxSize) {
this._size = 0;
if (typeof this.onEviction === 'function') {
for (const [key, value] of this.oldCache.entries()) {
this.onEviction(key, value);
}
}
this.oldCache = this.cache;
this.cache = new Map();
}
}
get(key) {
if (this.cache.has(key)) {
return this.cache.get(key);
}
if (this.oldCache.has(key)) {
const value = this.oldCache.get(key);
this.oldCache.delete(key);
this._set(key, value);
return value;
}
}
set(key, value) {
if (this.cache.has(key)) {
this.cache.set(key, value);
} else {
this._set(key, value);
}
return this;
}
has(key) {
return this.cache.has(key) || this.oldCache.has(key);
}
peek(key) {
if (this.cache.has(key)) {
return this.cache.get(key);
}
if (this.oldCache.has(key)) {
return this.oldCache.get(key);
}
}
delete(key) {
const deleted = this.cache.delete(key);
if (deleted) {
this._size--;
}
return this.oldCache.delete(key) || deleted;
}
clear() {
this.cache.clear();
this.oldCache.clear();
this._size = 0;
}
* keys() {
for (const [key] of this) {
yield key;
}
}
* values() {
for (const [, value] of this) {
yield value;
}
}
* [Symbol.iterator]() {
for (const item of this.cache) {
yield item;
}
for (const item of this.oldCache) {
const [key] = item;
if (!this.cache.has(key)) {
yield item;
}
}
}
get size() {
let oldCacheSize = 0;
for (const key of this.oldCache.keys()) {
if (!this.cache.has(key)) {
oldCacheSize++;
}
}
return Math.min(this._size + oldCacheSize, this.maxSize);
}
}
var quickLru = QuickLRU;
const kCurrentStreamsCount = Symbol('currentStreamsCount');
const kRequest = Symbol('request');
const kOriginSet = Symbol('cachedOriginSet');
const kGracefullyClosing = Symbol('gracefullyClosing');
const nameKeys = [
// `http2.connect()` options
'maxDeflateDynamicTableSize',
'maxSessionMemory',
'maxHeaderListPairs',
'maxOutstandingPings',
'maxReservedRemoteStreams',
'maxSendHeaderBlockLength',
'paddingStrategy',
// `tls.connect()` options
'localAddress',
'path',
'rejectUnauthorized',
'minDHSize',
// `tls.createSecureContext()` options
'ca',
'cert',
'clientCertEngine',
'ciphers',
'key',
'pfx',
'servername',
'minVersion',
'maxVersion',
'secureProtocol',
'crl',
'honorCipherOrder',
'ecdhCurve',
'dhparam',
'secureOptions',
'sessionIdContext'
];
const getSortedIndex = (array, value, compare) => {
let low = 0;
let high = array.length;
while (low < high) {
const mid = (low + high) >>> 1;
/* istanbul ignore next */
if (compare(array[mid], value)) {
// This never gets called because we use descending sort. Better to have this anyway.
low = mid + 1;
} else {
high = mid;
}
}
return low;
};
const compareSessions = (a, b) => {
return a.remoteSettings.maxConcurrentStreams > b.remoteSettings.maxConcurrentStreams;
};
// See https://tools.ietf.org/html/rfc8336
const closeCoveredSessions = (where, session) => {
// Clients SHOULD NOT emit new requests on any connection whose Origin
// Set is a proper subset of another connection's Origin Set, and they
// SHOULD close it once all outstanding requests are satisfied.
for (const coveredSession of where) {
if (
// The set is a proper subset when its length is less than the other set.
coveredSession[kOriginSet].length < session[kOriginSet].length &&
// And the other set includes all elements of the subset.
coveredSession[kOriginSet].every(origin => session[kOriginSet].includes(origin)) &&
// Makes sure that the session can handle all requests from the covered session.
coveredSession[kCurrentStreamsCount] + session[kCurrentStreamsCount] <= session.remoteSettings.maxConcurrentStreams
) {
// This allows pending requests to finish and prevents making new requests.
gracefullyClose(coveredSession);
}
}
};
// This is basically inverted `closeCoveredSessions(...)`.
const closeSessionIfCovered = (where, coveredSession) => {
for (const session of where) {
if (
coveredSession[kOriginSet].length < session[kOriginSet].length &&
coveredSession[kOriginSet].every(origin => session[kOriginSet].includes(origin)) &&
coveredSession[kCurrentStreamsCount] + session[kCurrentStreamsCount] <= session.remoteSettings.maxConcurrentStreams
) {
gracefullyClose(coveredSession);
}
}
};
const getSessions = ({agent, isFree}) => {
const result = {};
// eslint-disable-next-line guard-for-in
for (const normalizedOptions in agent.sessions) {
const sessions = agent.sessions[normalizedOptions];
const filtered = sessions.filter(session => {
const result = session[Agent$1.kCurrentStreamsCount] < session.remoteSettings.maxConcurrentStreams;
return isFree ? result : !result;
});
if (filtered.length !== 0) {
result[normalizedOptions] = filtered;
}
}
return result;
};
const gracefullyClose = session => {
session[kGracefullyClosing] = true;
if (session[kCurrentStreamsCount] === 0) {
session.close();
}
};
class Agent$1 extends EventEmitter__default['default'] {
constructor({timeout = 60000, maxSessions = Infinity, maxFreeSessions = 10, maxCachedTlsSessions = 100} = {}) {
super();
// A session is considered busy when its current streams count
// is equal to or greater than the `maxConcurrentStreams` value.
// A session is considered free when its current streams count
// is less than the `maxConcurrentStreams` value.
// SESSIONS[NORMALIZED_OPTIONS] = [];
this.sessions = {};
// The queue for creating new sessions. It looks like this:
// QUEUE[NORMALIZED_OPTIONS][NORMALIZED_ORIGIN] = ENTRY_FUNCTION
//
// The entry function has `listeners`, `completed` and `destroyed` properties.
// `listeners` is an array of objects containing `resolve` and `reject` functions.
// `completed` is a boolean. It's set to true after ENTRY_FUNCTION is executed.
// `destroyed` is a boolean. If it's set to true, the session will be destroyed if hasn't connected yet.
this.queue = {};
// Each session will use this timeout value.
this.timeout = timeout;
// Max sessions in total
this.maxSessions = maxSessions;
// Max free sessions in total
// TODO: decreasing `maxFreeSessions` should close some sessions
this.maxFreeSessions = maxFreeSessions;
this._freeSessionsCount = 0;
this._sessionsCount = 0;
// We don't support push streams by default.
this.settings = {
enablePush: false
};
// Reusing TLS sessions increases performance.
this.tlsSessionCache = new quickLru({maxSize: maxCachedTlsSessions});
}
static normalizeOrigin(url, servername) {
if (typeof url === 'string') {
url = new URL(url);
}
if (servername && url.hostname !== servername) {
url.hostname = servername;
}
return url.origin;
}
normalizeOptions(options) {
let normalized = '';
if (options) {
for (const key of nameKeys) {
if (options[key]) {
normalized += `:${options[key]}`;
}
}
}
return normalized;
}
_tryToCreateNewSession(normalizedOptions, normalizedOrigin) {
if (!(normalizedOptions in this.queue) || !(normalizedOrigin in this.queue[normalizedOptions])) {
return;
}
const item = this.queue[normalizedOptions][normalizedOrigin];
// The entry function can be run only once.
// BUG: The session may be never created when:
// - the first condition is false AND
// - this function is never called with the same arguments in the future.
if (this._sessionsCount < this.maxSessions && !item.completed) {
item.completed = true;
item();
}
}
getSession(origin, options, listeners) {
return new Promise((resolve, reject) => {
if (Array.isArray(listeners)) {
listeners = [...listeners];
// Resolve the current promise ASAP, we're just moving the listeners.
// They will be executed at a different time.
resolve();
} else {
listeners = [{resolve, reject}];
}
const normalizedOptions = this.normalizeOptions(options);
const normalizedOrigin = Agent$1.normalizeOrigin(origin, options && options.servername);
if (normalizedOrigin === undefined) {
for (const {reject} of listeners) {
reject(new TypeError('The `origin` argument needs to be a string or an URL object'));
}
return;
}
if (normalizedOptions in this.sessions) {
const sessions = this.sessions[normalizedOptions];
let maxConcurrentStreams = -1;
let currentStreamsCount = -1;
let optimalSession;
// We could just do this.sessions[normalizedOptions].find(...) but that isn't optimal.
// Additionally, we are looking for session which has biggest current pending streams count.
for (const session of sessions) {
const sessionMaxConcurrentStreams = session.remoteSettings.maxConcurrentStreams;
if (sessionMaxConcurrentStreams < maxConcurrentStreams) {
break;
}
if (session[kOriginSet].includes(normalizedOrigin)) {
const sessionCurrentStreamsCount = session[kCurrentStreamsCount];
if (
sessionCurrentStreamsCount >= sessionMaxConcurrentStreams ||
session[kGracefullyClosing] ||
// Unfortunately the `close` event isn't called immediately,
// so `session.destroyed` is `true`, but `session.closed` is `false`.
session.destroyed
) {
continue;
}
// We only need set this once.
if (!optimalSession) {
maxConcurrentStreams = sessionMaxConcurrentStreams;
}
// We're looking for the session which has biggest current pending stream count,
// in order to minimalize the amount of active sessions.
if (sessionCurrentStreamsCount > currentStreamsCount) {
optimalSession = session;
currentStreamsCount = sessionCurrentStreamsCount;
}
}
}
if (optimalSession) {
/* istanbul ignore next: safety check */
if (listeners.length !== 1) {
for (const {reject} of listeners) {
const error = new Error(
`Expected the length of listeners to be 1, got ${listeners.length}.\n` +
'Please report this to https://github.com/szmarczak/http2-wrapper/'
);
reject(error);
}
return;
}
listeners[0].resolve(optimalSession);
return;
}
}
if (normalizedOptions in this.queue) {
if (normalizedOrigin in this.queue[normalizedOptions]) {
// There's already an item in the queue, just attach ourselves to it.
this.queue[normalizedOptions][normalizedOrigin].listeners.push(...listeners);
// This shouldn't be executed here.
// See the comment inside _tryToCreateNewSession.
this._tryToCreateNewSession(normalizedOptions, normalizedOrigin);
return;
}
} else {
this.queue[normalizedOptions] = {};
}
// The entry must be removed from the queue IMMEDIATELY when:
// 1. the session connects successfully,
// 2. an error occurs.
const removeFromQueue = () => {
// Our entry can be replaced. We cannot remove the new one.
if (normalizedOptions in this.queue && this.queue[normalizedOptions][normalizedOrigin] === entry) {
delete this.queue[normalizedOptions][normalizedOrigin];
if (Object.keys(this.queue[normalizedOptions]).length === 0) {
delete this.queue[normalizedOptions];
}
}
};
// The main logic is here
const entry = () => {
const name = `${normalizedOrigin}:${normalizedOptions}`;
let receivedSettings = false;
try {
const session = http2__default['default'].connect(origin, {
createConnection: this.createConnection,
settings: this.settings,
session: this.tlsSessionCache.get(name),
...options
});
session[kCurrentStreamsCount] = 0;
session[kGracefullyClosing] = false;
const isFree = () => session[kCurrentStreamsCount] < session.remoteSettings.maxConcurrentStreams;
let wasFree = true;
session.socket.once('session', tlsSession => {
this.tlsSessionCache.set(name, tlsSession);
});
session.once('error', error => {
// Listeners are empty when the session successfully connected.
for (const {reject} of listeners) {
reject(error);
}
// The connection got broken, purge the cache.
this.tlsSessionCache.delete(name);
});
session.setTimeout(this.timeout, () => {
// Terminates all streams owned by this session.
// TODO: Maybe the streams should have a "Session timed out" error?
session.destroy();
});
session.once('close', () => {
if (receivedSettings) {
// 1. If it wasn't free then no need to decrease because
// it has been decreased already in session.request().
// 2. `stream.once('close')` won't increment the count
// because the session is already closed.
if (wasFree) {
this._freeSessionsCount--;
}
this._sessionsCount--;
// This cannot be moved to the stream logic,
// because there may be a session that hadn't made a single request.
const where = this.sessions[normalizedOptions];
where.splice(where.indexOf(session), 1);
if (where.length === 0) {
delete this.sessions[normalizedOptions];
}
} else {
// Broken connection
const error = new Error('Session closed without receiving a SETTINGS frame');
error.code = 'HTTP2WRAPPER_NOSETTINGS';
for (const {reject} of listeners) {
reject(error);
}
removeFromQueue();
}
// There may be another session awaiting.
this._tryToCreateNewSession(normalizedOptions, normalizedOrigin);
});
// Iterates over the queue and processes listeners.
const processListeners = () => {
if (!(normalizedOptions in this.queue) || !isFree()) {
return;
}
for (const origin of session[kOriginSet]) {
if (origin in this.queue[normalizedOptions]) {
const {listeners} = this.queue[normalizedOptions][origin];
// Prevents session overloading.
while (listeners.length !== 0 && isFree()) {
// We assume `resolve(...)` calls `request(...)` *directly*,
// otherwise the session will get overloaded.
listeners.shift().resolve(session);
}
const where = this.queue[normalizedOptions];
if (where[origin].listeners.length === 0) {
delete where[origin];
if (Object.keys(where).length === 0) {
delete this.queue[normalizedOptions];
break;
}
}
// We're no longer free, no point in continuing.
if (!isFree()) {
break;
}
}
}
};
// The Origin Set cannot shrink. No need to check if it suddenly became covered by another one.
session.on('origin', () => {
session[kOriginSet] = session.originSet;
if (!isFree()) {
// The session is full.
return;
}
processListeners();
// Close covered sessions (if possible).
closeCoveredSessions(this.sessions[normalizedOptions], session);
});
session.once('remoteSettings', () => {
// Fix Node.js bug preventing the process from exiting
session.ref();
session.unref();
this._sessionsCount++;
// The Agent could have been destroyed already.
if (entry.destroyed) {
const error = new Error('Agent has been destroyed');
for (const listener of listeners) {
listener.reject(error);
}
session.destroy();
return;
}
session[kOriginSet] = session.originSet;
{
const where = this.sessions;
if (normalizedOptions in where) {
const sessions = where[normalizedOptions];
sessions.splice(getSortedIndex(sessions, session, compareSessions), 0, session);
} else {
where[normalizedOptions] = [session];
}
}
this._freeSessionsCount += 1;
receivedSettings = true;
this.emit('session', session);
processListeners();
removeFromQueue();
// TODO: Close last recently used (or least used?) session
if (session[kCurrentStreamsCount] === 0 && this._freeSessionsCount > this.maxFreeSessions) {
session.close();
}
// Check if we haven't managed to execute all listeners.
if (listeners.length !== 0) {
// Request for a new session with predefined listeners.
this.getSession(normalizedOrigin, options, listeners);
listeners.length = 0;
}
// `session.remoteSettings.maxConcurrentStreams` might get increased
session.on('remoteSettings', () => {
processListeners();
// In case the Origin Set changes
closeCoveredSessions(this.sessions[normalizedOptions], session);
});
});
// Shim `session.request()` in order to catch all streams
session[kRequest] = session.request;
session.request = (headers, streamOptions) => {
if (session[kGracefullyClosing]) {
throw new Error('The session is gracefully closing. No new streams are allowed.');
}
const stream = session[kRequest](headers, streamOptions);
// The process won't exit until the session is closed or all requests are gone.
session.ref();
++session[kCurrentStreamsCount];
if (session[kCurrentStreamsCount] === session.remoteSettings.maxConcurrentStreams) {
this._freeSessionsCount--;
}
stream.once('close', () => {
wasFree = isFree();
--session[kCurrentStreamsCount];
if (!session.destroyed && !session.closed) {
closeSessionIfCovered(this.sessions[normalizedOptions], session);
if (isFree() && !session.closed) {
if (!wasFree) {
this._freeSessionsCount++;
wasFree = true;
}
const isEmpty = session[kCurrentStreamsCount] === 0;
if (isEmpty) {
session.unref();
}
if (
isEmpty &&
(
this._freeSessionsCount > this.maxFreeSessions ||
session[kGracefullyClosing]
)
) {
session.close();
} else {
closeCoveredSessions(this.sessions[normalizedOptions], session);
processListeners();
}
}
}
});
return stream;
};
} catch (error) {
for (const listener of listeners) {
listener.reject(error);
}
removeFromQueue();
}
};
entry.listeners = listeners;
entry.completed = false;
entry.destroyed = false;
this.queue[normalizedOptions][normalizedOrigin] = entry;
this._tryToCreateNewSession(normalizedOptions, normalizedOrigin);
});
}
request(origin, options, headers, streamOptions) {
return new Promise((resolve, reject) => {
this.getSession(origin, options, [{
reject,
resolve: session => {
try {
resolve(session.request(headers, streamOptions));
} catch (error) {
reject(error);
}
}
}]);
});
}
createConnection(origin, options) {
return Agent$1.connect(origin, options);
}
static connect(origin, options) {
options.ALPNProtocols = ['h2'];
const port = origin.port || 443;
const host = origin.hostname || origin.host;
if (typeof options.servername === 'undefined') {
options.servername = host;
}
return tls__default['default'].connect(port, host, options);
}
closeFreeSessions() {
for (const sessions of Object.values(this.sessions)) {
for (const session of sessions) {
if (session[kCurrentStreamsCount] === 0) {
session.close();
}
}
}
}
destroy(reason) {
for (const sessions of Object.values(this.sessions)) {
for (const session of sessions) {
session.destroy(reason);
}
}
for (const entriesOfAuthority of Object.values(this.queue)) {
for (const entry of Object.values(entriesOfAuthority)) {
entry.destroyed = true;
}
}
// New requests should NOT attach to destroyed sessions
this.queue = {};
}
get freeSessions() {
return getSessions({agent: this, isFree: true});
}
get busySessions() {
return getSessions({agent: this, isFree: false});
}
}
Agent$1.kCurrentStreamsCount = kCurrentStreamsCount;
Agent$1.kGracefullyClosing = kGracefullyClosing;
var agent = {
Agent: Agent$1,
globalAgent: new Agent$1()
};
const {Readable} = require$$0__default$1['default'];
class IncomingMessage extends Readable {
constructor(socket, highWaterMark) {
super({
highWaterMark,
autoDestroy: false
});
this.statusCode = null;
this.statusMessage = '';
this.httpVersion = '2.0';
this.httpVersionMajor = 2;
this.httpVersionMinor = 0;
this.headers = {};
this.trailers = {};
this.req = null;
this.aborted = false;
this.complete = false;
this.upgrade = null;
this.rawHeaders = [];
this.rawTrailers = [];
this.socket = socket;
this.connection = socket;
this._dumped = false;
}
_destroy(error) {
this.req._request.destroy(error);
}
setTimeout(ms, callback) {
this.req.setTimeout(ms, callback);
return this;
}
_dump() {
if (!this._dumped) {
this._dumped = true;
this.removeAllListeners('data');
this.resume();
}
}
_read() {
if (this.req) {
this.req._request.resume();
}
}
}
var incomingMessage = IncomingMessage;
/* istanbul ignore file: https://github.com/nodejs/node/blob/a91293d4d9ab403046ab5eb022332e4e3d249bd3/lib/internal/url.js#L1257 */
var urlToOptions$1 = url => {
const options = {
protocol: url.protocol,
hostname: typeof url.hostname === 'string' && url.hostname.startsWith('[') ? url.hostname.slice(1, -1) : url.hostname,
host: url.host,
hash: url.hash,
search: url.search,
pathname: url.pathname,
href: url.href,
path: `${url.pathname || ''}${url.search || ''}`
};
if (typeof url.port === 'string' && url.port.length !== 0) {
options.port = Number(url.port);
}
if (url.username || url.password) {
options.auth = `${url.username || ''}:${url.password || ''}`;
}
return options;
};
var proxyEvents$1 = (from, to, events) => {
for (const event of events) {
from.on(event, (...args) => to.emit(event, ...args));
}
};
var isRequestPseudoHeader = header => {
switch (header) {
case ':method':
case ':scheme':
case ':authority':
case ':path':
return true;
default:
return false;
}
};
var errors = createCommonjsModule(function (module) {
/* istanbul ignore file: https://github.com/nodejs/node/blob/master/lib/internal/errors.js */
const makeError = (Base, key, getMessage) => {
module.exports[key] = class NodeError extends Base {
constructor(...args) {
super(typeof getMessage === 'string' ? getMessage : getMessage(args));
this.name = `${super.name} [${key}]`;
this.code = key;
}
};
};
makeError(TypeError, 'ERR_INVALID_ARG_TYPE', args => {
const type = args[0].includes('.') ? 'property' : 'argument';
let valid = args[1];
const isManyTypes = Array.isArray(valid);
if (isManyTypes) {
valid = `${valid.slice(0, -1).join(', ')} or ${valid.slice(-1)}`;
}
return `The "${args[0]}" ${type} must be ${isManyTypes ? 'one of' : 'of'} type ${valid}. Received ${typeof args[2]}`;
});
makeError(TypeError, 'ERR_INVALID_PROTOCOL', args => {
return `Protocol "${args[0]}" not supported. Expected "${args[1]}"`;
});
makeError(Error, 'ERR_HTTP_HEADERS_SENT', args => {
return `Cannot ${args[0]} headers after they are sent to the client`;
});
makeError(TypeError, 'ERR_INVALID_HTTP_TOKEN', args => {
return `${args[0]} must be a valid HTTP token [${args[1]}]`;
});
makeError(TypeError, 'ERR_HTTP_INVALID_HEADER_VALUE', args => {
return `Invalid value "${args[0]} for header "${args[1]}"`;
});
makeError(TypeError, 'ERR_INVALID_CHAR', args => {
return `Invalid character in ${args[0]} [${args[1]}]`;
});
});
const {Writable} = require$$0__default$1['default'];
const {Agent, globalAgent} = agent;
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_PROTOCOL,
ERR_HTTP_HEADERS_SENT,
ERR_INVALID_HTTP_TOKEN,
ERR_HTTP_INVALID_HEADER_VALUE,
ERR_INVALID_CHAR
} = errors;
const {
HTTP2_HEADER_STATUS,
HTTP2_HEADER_METHOD,
HTTP2_HEADER_PATH,
HTTP2_METHOD_CONNECT
} = http2__default['default'].constants;
const kHeaders = Symbol('headers');
const kOrigin = Symbol('origin');
const kSession = Symbol('session');
const kOptions = Symbol('options');
const kFlushedHeaders = Symbol('flushedHeaders');
const kJobs = Symbol('jobs');
const isValidHttpToken = /^[\^`\-\w!#$%&*+.|~]+$/;
const isInvalidHeaderValue = /[^\t\u0020-\u007E\u0080-\u00FF]/;
class ClientRequest extends Writable {
constructor(input, options, callback) {
super({
autoDestroy: false
});
const hasInput = typeof input === 'string' || input instanceof URL;
if (hasInput) {
input = urlToOptions$1(input instanceof URL ? input : new URL(input));
}
if (typeof options === 'function' || options === undefined) {
// (options, callback)
callback = options;
options = hasInput ? input : {...input};
} else {
// (input, options, callback)
options = {...input, ...options};
}
if (options.h2session) {
this[kSession] = options.h2session;
} else if (options.agent === false) {
this.agent = new Agent({maxFreeSessions: 0});
} else if (typeof options.agent === 'undefined' || options.agent === null) {
if (typeof options.createConnection === 'function') {
// This is a workaround - we don't have to create the session on our own.
this.agent = new Agent({maxFreeSessions: 0});
this.agent.createConnection = options.createConnection;
} else {
this.agent = globalAgent;
}
} else if (typeof options.agent.request === 'function') {
this.agent = options.agent;
} else {
throw new ERR_INVALID_ARG_TYPE('options.agent', ['Agent-like Object', 'undefined', 'false'], options.agent);
}
if (options.protocol && options.protocol !== 'https:') {
throw new ERR_INVALID_PROTOCOL(options.protocol, 'https:');
}
const port = options.port || options.defaultPort || (this.agent && this.agent.defaultPort) || 443;
const host = options.hostname || options.host || 'localhost';
// Don't enforce the origin via options. It may be changed in an Agent.
delete options.hostname;
delete options.host;
delete options.port;
const {timeout} = options;
options.timeout = undefined;
this[kHeaders] = Object.create(null);
this[kJobs] = [];
this.socket = null;
this.connection = null;
this.method = options.method || 'GET';
this.path = options.path;
this.res = null;
this.aborted = false;
this.reusedSocket = false;
if (options.headers) {
for (const [header, value] of Object.entries(options.headers)) {
this.setHeader(header, value);
}
}
if (options.auth && !('authorization' in this[kHeaders])) {
this[kHeaders].authorization = 'Basic ' + Buffer.from(options.auth).toString('base64');
}
options.session = options.tlsSession;
options.path = options.socketPath;
this[kOptions] = options;
// Clients that generate HTTP/2 requests directly SHOULD use the :authority pseudo-header field instead of the Host header field.
if (port === 443) {
this[kOrigin] = `https://${host}`;
if (!(':authority' in this[kHeaders])) {
this[kHeaders][':authority'] = host;
}
} else {
this[kOrigin] = `https://${host}:${port}`;
if (!(':authority' in this[kHeaders])) {
this[kHeaders][':authority'] = `${host}:${port}`;
}
}
if (timeout) {
this.setTimeout(timeout);
}
if (callback) {
this.once('response', callback);
}
this[kFlushedHeaders] = false;
}
get method() {
return this[kHeaders][HTTP2_HEADER_METHOD];
}
set method(value) {
if (value) {
this[kHeaders][HTTP2_HEADER_METHOD] = value.toUpperCase();
}
}
get path() {
return this[kHeaders][HTTP2_HEADER_PATH];
}
set path(value) {
if (value) {
this[kHeaders][HTTP2_HEADER_PATH] = value;
}
}
get _mustNotHaveABody() {
return this.method === 'GET' || this.method === 'HEAD' || this.method === 'DELETE';
}
_write(chunk, encoding, callback) {
// https://github.com/nodejs/node/blob/654df09ae0c5e17d1b52a900a545f0664d8c7627/lib/internal/http2/util.js#L148-L156
if (this._mustNotHaveABody) {
callback(new Error('The GET, HEAD and DELETE methods must NOT have a body'));
/* istanbul ignore next: Node.js 12 throws directly */
return;
}
this.flushHeaders();
const callWrite = () => this._request.write(chunk, encoding, callback);
if (this._request) {
callWrite();
} else {
this[kJobs].push(callWrite);
}
}
_final(callback) {
if (this.destroyed) {
return;
}
this.flushHeaders();
const callEnd = () => {
// For GET, HEAD and DELETE
if (this._mustNotHaveABody) {
callback();
return;
}
this._request.end(callback);
};
if (this._request) {
callEnd();
} else {
this[kJobs].push(callEnd);
}
}
abort() {
if (this.res && this.res.complete) {
return;
}
if (!this.aborted) {
process.nextTick(() => this.emit('abort'));
}
this.aborted = true;
this.destroy();
}
_destroy(error, callback) {
if (this.res) {
this.res._dump();
}
if (this._request) {
this._request.destroy();
}
callback(error);
}
async flushHeaders() {
if (this[kFlushedHeaders] || this.destroyed) {
return;
}
this[kFlushedHeaders] = true;
const isConnectMethod = this.method === HTTP2_METHOD_CONNECT;
// The real magic is here
const onStream = stream => {
this._request = stream;
if (this.destroyed) {
stream.destroy();
return;
}
// Forwards `timeout`, `continue`, `close` and `error` events to this instance.
if (!isConnectMethod) {
proxyEvents$1(stream, this, ['timeout', 'continue', 'close', 'error']);
}
// Wait for the `finish` event. We don't want to emit the `response` event
// before `request.end()` is called.
const waitForEnd = fn => {
return (...args) => {
if (!this.writable && !this.destroyed) {
fn(...args);
} else {
this.once('finish', () => {
fn(...args);
});
}
};
};
// This event tells we are ready to listen for the data.
stream.once('response', waitForEnd((headers, flags, rawHeaders) => {
// If we were to emit raw request stream, it would be as fast as the native approach.
// Note that wrapping the raw stream in a Proxy instance won't improve the performance (already tested it).
const response = new incomingMessage(this.socket, stream.readableHighWaterMark);
this.res = response;
response.req = this;
response.statusCode = headers[HTTP2_HEADER_STATUS];
response.headers = headers;
response.rawHeaders = rawHeaders;
response.once('end', () => {
if (this.aborted) {
response.aborted = true;
response.emit('aborted');
} else {
response.complete = true;
// Has no effect, just be consistent with the Node.js behavior
response.socket = null;
response.connection = null;
}
});
if (isConnectMethod) {
response.upgrade = true;
// The HTTP1 API says the socket is detached here,
// but we can't do that so we pass the original HTTP2 request.
if (this.emit('connect', response, stream, Buffer.alloc(0))) {
this.emit('close');
} else {
// No listeners attached, destroy the original request.
stream.destroy();
}
} else {
// Forwards data
stream.on('data', chunk => {
if (!response._dumped && !response.push(chunk)) {
stream.pause();
}
});
stream.once('end', () => {
response.push(null);
});
if (!this.emit('response', response)) {
// No listeners attached, dump the response.
response._dump();
}
}
}));
// Emits `information` event
stream.once('headers', waitForEnd(
headers => this.emit('information', {statusCode: headers[HTTP2_HEADER_STATUS]})
));
stream.once('trailers', waitForEnd((trailers, flags, rawTrailers) => {
const {res} = this;
// Assigns trailers to the response object.
res.trailers = trailers;
res.rawTrailers = rawTrailers;
}));
const {socket} = stream.session;
this.socket = socket;
this.connection = socket;
for (const job of this[kJobs]) {
job();
}
this.emit('socket', this.socket);
};
// Makes a HTTP2 request
if (this[kSession]) {
try {
onStream(this[kSession].request(this[kHeaders]));
} catch (error) {
this.emit('error', error);
}
} else {
this.reusedSocket = true;
try {
onStream(await this.agent.request(this[kOrigin], this[kOptions], this[kHeaders]));
} catch (error) {
this.emit('error', error);
}
}
}
getHeader(name) {
if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
}
return this[kHeaders][name.toLowerCase()];
}
get headersSent() {
return this[kFlushedHeaders];
}
removeHeader(name) {
if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
}
if (this.headersSent) {
throw new ERR_HTTP_HEADERS_SENT('remove');
}
delete this[kHeaders][name.toLowerCase()];
}
setHeader(name, value) {
if (this.headersSent) {
throw new ERR_HTTP_HEADERS_SENT('set');
}
if (typeof name !== 'string' || (!isValidHttpToken.test(name) && !isRequestPseudoHeader(name))) {
throw new ERR_INVALID_HTTP_TOKEN('Header name', name);
}
if (typeof value === 'undefined') {
throw new ERR_HTTP_INVALID_HEADER_VALUE(value, name);
}
if (isInvalidHeaderValue.test(value)) {
throw new ERR_INVALID_CHAR('header content', name);
}
this[kHeaders][name.toLowerCase()] = value;
}
setNoDelay() {
// HTTP2 sockets cannot be malformed, do nothing.
}
setSocketKeepAlive() {
// HTTP2 sockets cannot be malformed, do nothing.
}
setTimeout(ms, callback) {
const applyTimeout = () => this._request.setTimeout(ms, callback);
if (this._request) {
applyTimeout();
} else {
this[kJobs].push(applyTimeout);
}
return this;
}
get maxHeadersCount() {
if (!this.destroyed && this._request) {
return this._request.session.localSettings.maxHeaderListSize;
}
return undefined;
}
set maxHeadersCount(_value) {
// Updating HTTP2 settings would affect all requests, do nothing.
}
}
var clientRequest = ClientRequest;
var resolveAlpn = (options = {}, connect = tls__default['default'].connect) => new Promise((resolve, reject) => {
let timeout = false;
let socket;
const callback = async () => {
await socketPromise;
socket.off('timeout', onTimeout);
socket.off('error', reject);
if (options.resolveSocket) {
resolve({alpnProtocol: socket.alpnProtocol, socket, timeout});
if (timeout) {
await Promise.resolve();
socket.emit('timeout');
}
} else {
socket.destroy();
resolve({alpnProtocol: socket.alpnProtocol, timeout});
}
};
const onTimeout = async () => {
timeout = true;
callback();
};
const socketPromise = (async () => {
try {
socket = await connect(options, callback);
socket.on('error', reject);
socket.once('timeout', onTimeout);
} catch (error) {
reject(error);
}
})();
});
/* istanbul ignore file: https://github.com/nodejs/node/blob/v13.0.1/lib/_http_agent.js */
var calculateServerName = options => {
let servername = options.host;
const hostHeader = options.headers && options.headers.host;
if (hostHeader) {
if (hostHeader.startsWith('[')) {
const index = hostHeader.indexOf(']');
if (index === -1) {
servername = hostHeader;
} else {
servername = hostHeader.slice(1, -1);
}
} else {
servername = hostHeader.split(':', 1)[0];
}
}
if (net__default['default'].isIP(servername)) {
return '';
}
return servername;
};
const cache = new quickLru({maxSize: 100});
const queue = new Map();
const installSocket = (agent, socket, options) => {
socket._httpMessage = {shouldKeepAlive: true};
const onFree = () => {
agent.emit('free', socket, options);
};
socket.on('free', onFree);
const onClose = () => {
agent.removeSocket(socket, options);
};
socket.on('close', onClose);
const onRemove = () => {
agent.removeSocket(socket, options);
socket.off('close', onClose);
socket.off('free', onFree);
socket.off('agentRemove', onRemove);
};
socket.on('agentRemove', onRemove);
agent.emit('free', socket, options);
};
const resolveProtocol = async options => {
const name = `${options.host}:${options.port}:${options.ALPNProtocols.sort()}`;
if (!cache.has(name)) {
if (queue.has(name)) {
const result = await queue.get(name);
return result.alpnProtocol;
}
const {path, agent} = options;
options.path = options.socketPath;
const resultPromise = resolveAlpn(options);
queue.set(name, resultPromise);
try {
const {socket, alpnProtocol} = await resultPromise;
cache.set(name, alpnProtocol);
options.path = path;
if (alpnProtocol === 'h2') {
// https://github.com/nodejs/node/issues/33343
socket.destroy();
} else {
const {globalAgent} = https__default['default'];
const defaultCreateConnection = https__default['default'].Agent.prototype.createConnection;
if (agent) {
if (agent.createConnection === defaultCreateConnection) {
installSocket(agent, socket, options);
} else {
socket.destroy();
}
} else if (globalAgent.createConnection === defaultCreateConnection) {
installSocket(globalAgent, socket, options);
} else {
socket.destroy();
}
}
queue.delete(name);
return alpnProtocol;
} catch (error) {
queue.delete(name);
throw error;
}
}
return cache.get(name);
};
var auto = async (input, options, callback) => {
if (typeof input === 'string' || input instanceof URL) {
input = urlToOptions$1(new URL(input));
}
if (typeof options === 'function') {
callback = options;
options = undefined;
}
options = {
ALPNProtocols: ['h2', 'http/1.1'],
...input,
...options,
resolveSocket: true
};
if (!Array.isArray(options.ALPNProtocols) || options.ALPNProtocols.length === 0) {
throw new Error('The `ALPNProtocols` option must be an Array with at least one entry');
}
options.protocol = options.protocol || 'https:';
const isHttps = options.protocol === 'https:';
options.host = options.hostname || options.host || 'localhost';
options.session = options.tlsSession;
options.servername = options.servername || calculateServerName(options);
options.port = options.port || (isHttps ? 443 : 80);
options._defaultAgent = isHttps ? https__default['default'].globalAgent : http__default['default'].globalAgent;
const agents = options.agent;
if (agents) {
if (agents.addRequest) {
throw new Error('The `options.agent` object can contain only `http`, `https` or `http2` properties');
}
options.agent = agents[isHttps ? 'https' : 'http'];
}
if (isHttps) {
const protocol = await resolveProtocol(options);
if (protocol === 'h2') {
if (agents) {
options.agent = agents.http2;
}
return new clientRequest(options, callback);
}
}
return http__default['default'].request(options, callback);
};
var protocolCache = cache;
auto.protocolCache = protocolCache;
const request = (url, options, callback) => {
return new clientRequest(url, options, callback);
};
const get = (url, options, callback) => {
// eslint-disable-next-line unicorn/prevent-abbreviations
const req = new clientRequest(url, options, callback);
req.end();
return req;
};
var source$1 = {
...http2__default['default'],
ClientRequest: clientRequest,
IncomingMessage: incomingMessage,
...agent,
request,
get,
auto
};
var is_1 = dist;
var _default$i = (body) => is_1.default.nodeStream(body) && is_1.default.function_(body.getBoundary);
var isFormData = /*#__PURE__*/Object.defineProperty({
default: _default$i
}, '__esModule', {value: true});
var is_form_data_1 = isFormData;
const statAsync = util_1__default['default'].promisify(fs__default['default'].stat);
var _default$h = async (body, headers) => {
if (headers && 'content-length' in headers) {
return Number(headers['content-length']);
}
if (!body) {
return 0;
}
if (is_1.default.string(body)) {
return Buffer.byteLength(body);
}
if (is_1.default.buffer(body)) {
return body.length;
}
if (is_form_data_1.default(body)) {
return util_1__default['default'].promisify(body.getLength.bind(body))();
}
if (body instanceof fs__default['default'].ReadStream) {
const { size } = await statAsync(body.path);
if (size === 0) {
return undefined;
}
return size;
}
return undefined;
};
var getBodySize = /*#__PURE__*/Object.defineProperty({
default: _default$h
}, '__esModule', {value: true});
function default_1$1(from, to, events) {
const fns = {};
for (const event of events) {
fns[event] = (...args) => {
to.emit(event, ...args);
};
from.on(event, fns[event]);
}
return () => {
for (const event of events) {
from.off(event, fns[event]);
}
};
}
var _default$g = default_1$1;
var proxyEvents = /*#__PURE__*/Object.defineProperty({
default: _default$g
}, '__esModule', {value: true});
// When attaching listeners, it's very easy to forget about them.
// Especially if you do error handling and set timeouts.
// So instead of checking if it's proper to throw an error on every timeout ever,
// use this simple tool which will remove all listeners you have attached.
var _default$f = () => {
const handlers = [];
return {
once(origin, event, fn) {
origin.once(event, fn);
handlers.push({ origin, event, fn });
},
unhandleAll() {
for (const handler of handlers) {
const { origin, event, fn } = handler;
origin.removeListener(event, fn);
}
handlers.length = 0;
}
};
};
var unhandle = /*#__PURE__*/Object.defineProperty({
default: _default$f
}, '__esModule', {value: true});
var unhandle_1 = unhandle;
var timedOut = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimeoutError = void 0;
const reentry = Symbol('reentry');
const noop = () => { };
class TimeoutError extends Error {
constructor(threshold, event) {
super(`Timeout awaiting '${event}' for ${threshold}ms`);
this.event = event;
this.name = 'TimeoutError';
this.code = 'ETIMEDOUT';
}
}
exports.TimeoutError = TimeoutError;
exports.default = (request, delays, options) => {
if (reentry in request) {
return noop;
}
request[reentry] = true;
const cancelers = [];
const { once, unhandleAll } = unhandle_1.default();
const addTimeout = (delay, callback, event) => {
var _a;
const timeout = setTimeout(callback, delay, delay, event);
(_a = timeout.unref) === null || _a === void 0 ? void 0 : _a.call(timeout);
const cancel = () => {
clearTimeout(timeout);
};
cancelers.push(cancel);
return cancel;
};
const { host, hostname } = options;
const timeoutHandler = (delay, event) => {
request.destroy(new TimeoutError(delay, event));
};
const cancelTimeouts = () => {
for (const cancel of cancelers) {
cancel();
}
unhandleAll();
};
request.once('error', error => {
cancelTimeouts();
// Save original behavior
/* istanbul ignore next */
if (request.listenerCount('error') === 0) {
throw error;
}
});
request.once('close', cancelTimeouts);
once(request, 'response', (response) => {
once(response, 'end', cancelTimeouts);
});
if (typeof delays.request !== 'undefined') {
addTimeout(delays.request, timeoutHandler, 'request');
}
if (typeof delays.socket !== 'undefined') {
const socketTimeoutHandler = () => {
timeoutHandler(delays.socket, 'socket');
};
request.setTimeout(delays.socket, socketTimeoutHandler);
// `request.setTimeout(0)` causes a memory leak.
// We can just remove the listener and forget about the timer - it's unreffed.
// See https://github.com/sindresorhus/got/issues/690
cancelers.push(() => {
request.removeListener('timeout', socketTimeoutHandler);
});
}
once(request, 'socket', (socket) => {
var _a;
const { socketPath } = request;
/* istanbul ignore next: hard to test */
if (socket.connecting) {
const hasPath = Boolean(socketPath !== null && socketPath !== void 0 ? socketPath : net__default['default'].isIP((_a = hostname !== null && hostname !== void 0 ? hostname : host) !== null && _a !== void 0 ? _a : '') !== 0);
if (typeof delays.lookup !== 'undefined' && !hasPath && typeof socket.address().address === 'undefined') {
const cancelTimeout = addTimeout(delays.lookup, timeoutHandler, 'lookup');
once(socket, 'lookup', cancelTimeout);
}
if (typeof delays.connect !== 'undefined') {
const timeConnect = () => addTimeout(delays.connect, timeoutHandler, 'connect');
if (hasPath) {
once(socket, 'connect', timeConnect());
}
else {
once(socket, 'lookup', (error) => {
if (error === null) {
once(socket, 'connect', timeConnect());
}
});
}
}
if (typeof delays.secureConnect !== 'undefined' && options.protocol === 'https:') {
once(socket, 'connect', () => {
const cancelTimeout = addTimeout(delays.secureConnect, timeoutHandler, 'secureConnect');
once(socket, 'secureConnect', cancelTimeout);
});
}
}
if (typeof delays.send !== 'undefined') {
const timeRequest = () => addTimeout(delays.send, timeoutHandler, 'send');
/* istanbul ignore next: hard to test */
if (socket.connecting) {
once(socket, 'connect', () => {
once(request, 'upload-complete', timeRequest());
});
}
else {
once(request, 'upload-complete', timeRequest());
}
}
});
if (typeof delays.response !== 'undefined') {
once(request, 'upload-complete', () => {
const cancelTimeout = addTimeout(delays.response, timeoutHandler, 'response');
once(request, 'response', cancelTimeout);
});
}
return cancelTimeouts;
};
});
var _default$e = (url) => {
// Cast to URL
url = url;
const options = {
protocol: url.protocol,
hostname: is_1.default.string(url.hostname) && url.hostname.startsWith('[') ? url.hostname.slice(1, -1) : url.hostname,
host: url.host,
hash: url.hash,
search: url.search,
pathname: url.pathname,
href: url.href,
path: `${url.pathname || ''}${url.search || ''}`
};
if (is_1.default.string(url.port) && url.port.length > 0) {
options.port = Number(url.port);
}
if (url.username || url.password) {
options.auth = `${url.username || ''}:${url.password || ''}`;
}
return options;
};
var urlToOptions = /*#__PURE__*/Object.defineProperty({
default: _default$e
}, '__esModule', {value: true});
/* istanbul ignore file: deprecated */
const keys = [
'protocol',
'host',
'hostname',
'port',
'pathname',
'search'
];
var _default$d = (origin, options) => {
var _a, _b;
if (options.path) {
if (options.pathname) {
throw new TypeError('Parameters `path` and `pathname` are mutually exclusive.');
}
if (options.search) {
throw new TypeError('Parameters `path` and `search` are mutually exclusive.');
}
if (options.searchParams) {
throw new TypeError('Parameters `path` and `searchParams` are mutually exclusive.');
}
}
if (options.search && options.searchParams) {
throw new TypeError('Parameters `search` and `searchParams` are mutually exclusive.');
}
if (!origin) {
if (!options.protocol) {
throw new TypeError('No URL protocol specified');
}
origin = `${options.protocol}//${(_b = (_a = options.hostname) !== null && _a !== void 0 ? _a : options.host) !== null && _b !== void 0 ? _b : ''}`;
}
const url = new url_1__default['default'].URL(origin);
if (options.path) {
const searchIndex = options.path.indexOf('?');
if (searchIndex === -1) {
options.pathname = options.path;
}
else {
options.pathname = options.path.slice(0, searchIndex);
options.search = options.path.slice(searchIndex + 1);
}
delete options.path;
}
for (const key of keys) {
if (options[key]) {
url[key] = options[key].toString();
}
}
return url;
};
var optionsToUrl = /*#__PURE__*/Object.defineProperty({
default: _default$d
}, '__esModule', {value: true});
class WeakableMap {
constructor() {
this.weakMap = new WeakMap();
this.map = new Map();
}
set(key, value) {
if (typeof key === 'object') {
this.weakMap.set(key, value);
}
else {
this.map.set(key, value);
}
}
get(key) {
if (typeof key === 'object') {
return this.weakMap.get(key);
}
return this.map.get(key);
}
has(key) {
if (typeof key === 'object') {
return this.weakMap.has(key);
}
return this.map.has(key);
}
}
var _default$c = WeakableMap;
var weakableMap = /*#__PURE__*/Object.defineProperty({
default: _default$c
}, '__esModule', {value: true});
// TODO: Update https://github.com/sindresorhus/get-stream
const getBuffer = async (stream) => {
const chunks = [];
let length = 0;
for await (const chunk of stream) {
chunks.push(chunk);
length += Buffer.byteLength(chunk);
}
if (Buffer.isBuffer(chunks[0])) {
return Buffer.concat(chunks, length);
}
return Buffer.from(chunks.join(''));
};
var _default$b = getBuffer;
var getBuffer_1 = /*#__PURE__*/Object.defineProperty({
default: _default$b
}, '__esModule', {value: true});
var dnsIpVersion = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.dnsLookupIpVersionToFamily = exports.isDnsLookupIpVersion = void 0;
const conversionTable = {
auto: 0,
ipv4: 4,
ipv6: 6
};
exports.isDnsLookupIpVersion = (value) => {
return value in conversionTable;
};
exports.dnsLookupIpVersionToFamily = (dnsLookupIpVersion) => {
if (exports.isDnsLookupIpVersion(dnsLookupIpVersion)) {
return conversionTable[dnsLookupIpVersion];
}
throw new Error('Invalid DNS lookup IP version');
};
});
var isResponseOk = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.isResponseOk = void 0;
exports.isResponseOk = (response) => {
const { statusCode } = response;
const limitStatusCode = response.request.options.followRedirect ? 299 : 399;
return (statusCode >= 200 && statusCode <= limitStatusCode) || statusCode === 304;
};
});
const alreadyWarned = new Set();
var _default$a = (message) => {
if (alreadyWarned.has(message)) {
return;
}
alreadyWarned.add(message);
// @ts-expect-error Missing types.
process.emitWarning(`Got: ${message}`, {
type: 'DeprecationWarning'
});
};
var deprecationWarning = /*#__PURE__*/Object.defineProperty({
default: _default$a
}, '__esModule', {value: true});
const normalizeArguments = (options, defaults) => {
if (is_1.default.null_(options.encoding)) {
throw new TypeError('To get a Buffer, set `options.responseType` to `buffer` instead');
}
is_1.assert.any([is_1.default.string, is_1.default.undefined], options.encoding);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.resolveBodyOnly);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.methodRewriting);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.isStream);
is_1.assert.any([is_1.default.string, is_1.default.undefined], options.responseType);
// `options.responseType`
if (options.responseType === undefined) {
options.responseType = 'text';
}
// `options.retry`
const { retry } = options;
if (defaults) {
options.retry = { ...defaults.retry };
}
else {
options.retry = {
calculateDelay: retryObject => retryObject.computedValue,
limit: 0,
methods: [],
statusCodes: [],
errorCodes: [],
maxRetryAfter: undefined
};
}
if (is_1.default.object(retry)) {
options.retry = {
...options.retry,
...retry
};
options.retry.methods = [...new Set(options.retry.methods.map(method => method.toUpperCase()))];
options.retry.statusCodes = [...new Set(options.retry.statusCodes)];
options.retry.errorCodes = [...new Set(options.retry.errorCodes)];
}
else if (is_1.default.number(retry)) {
options.retry.limit = retry;
}
if (is_1.default.undefined(options.retry.maxRetryAfter)) {
options.retry.maxRetryAfter = Math.min(
// TypeScript is not smart enough to handle `.filter(x => is.number(x))`.
// eslint-disable-next-line unicorn/no-fn-reference-in-iterator
...[options.timeout.request, options.timeout.connect].filter(is_1.default.number));
}
// `options.pagination`
if (is_1.default.object(options.pagination)) {
if (defaults) {
options.pagination = {
...defaults.pagination,
...options.pagination
};
}
const { pagination } = options;
if (!is_1.default.function_(pagination.transform)) {
throw new Error('`options.pagination.transform` must be implemented');
}
if (!is_1.default.function_(pagination.shouldContinue)) {
throw new Error('`options.pagination.shouldContinue` must be implemented');
}
if (!is_1.default.function_(pagination.filter)) {
throw new TypeError('`options.pagination.filter` must be implemented');
}
if (!is_1.default.function_(pagination.paginate)) {
throw new Error('`options.pagination.paginate` must be implemented');
}
}
// JSON mode
if (options.responseType === 'json' && options.headers.accept === undefined) {
options.headers.accept = 'application/json';
}
return options;
};
var _default$9 = normalizeArguments;
var normalizeArguments_1 = /*#__PURE__*/Object.defineProperty({
default: _default$9
}, '__esModule', {value: true});
var calculateRetryDelay_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.retryAfterStatusCodes = void 0;
exports.retryAfterStatusCodes = new Set([413, 429, 503]);
const calculateRetryDelay = ({ attemptCount, retryOptions, error, retryAfter }) => {
if (attemptCount > retryOptions.limit) {
return 0;
}
const hasMethod = retryOptions.methods.includes(error.options.method);
const hasErrorCode = retryOptions.errorCodes.includes(error.code);
const hasStatusCode = error.response && retryOptions.statusCodes.includes(error.response.statusCode);
if (!hasMethod || (!hasErrorCode && !hasStatusCode)) {
return 0;
}
if (error.response) {
if (retryAfter) {
if (retryOptions.maxRetryAfter === undefined || retryAfter > retryOptions.maxRetryAfter) {
return 0;
}
return retryAfter;
}
if (error.response.statusCode === 413) {
return 0;
}
}
const noise = Math.random() * 100;
return ((2 ** (attemptCount - 1)) * 1000) + noise;
};
exports.default = calculateRetryDelay;
});
var get_body_size_1 = getBodySize;
var proxy_events_1 = proxyEvents;
var timed_out_1 = timedOut;
var url_to_options_1 = urlToOptions;
var options_to_url_1 = optionsToUrl;
var weakable_map_1 = weakableMap;
var get_buffer_1 = getBuffer_1;
var dns_ip_version_1 = dnsIpVersion;
var is_response_ok_1 = isResponseOk;
var deprecation_warning_1 = deprecationWarning;
var normalize_arguments_1 = normalizeArguments_1;
var calculate_retry_delay_1 = calculateRetryDelay_1;
var core$2 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnsupportedProtocolError = exports.ReadError = exports.TimeoutError = exports.UploadError = exports.CacheError = exports.HTTPError = exports.MaxRedirectsError = exports.RequestError = exports.setNonEnumerableProperties = exports.knownHookEvents = exports.withoutBody = exports.kIsNormalizedAlready = void 0;
const http_1 = http__default['default'];
// @ts-expect-error Missing types
let globalDnsCache;
const kRequest = Symbol('request');
const kResponse = Symbol('response');
const kResponseSize = Symbol('responseSize');
const kDownloadedSize = Symbol('downloadedSize');
const kBodySize = Symbol('bodySize');
const kUploadedSize = Symbol('uploadedSize');
const kServerResponsesPiped = Symbol('serverResponsesPiped');
const kUnproxyEvents = Symbol('unproxyEvents');
const kIsFromCache = Symbol('isFromCache');
const kCancelTimeouts = Symbol('cancelTimeouts');
const kStartedReading = Symbol('startedReading');
const kStopReading = Symbol('stopReading');
const kTriggerRead = Symbol('triggerRead');
const kBody = Symbol('body');
const kJobs = Symbol('jobs');
const kOriginalResponse = Symbol('originalResponse');
const kRetryTimeout = Symbol('retryTimeout');
exports.kIsNormalizedAlready = Symbol('isNormalizedAlready');
const supportsBrotli = is_1.default.string(process.versions.brotli);
exports.withoutBody = new Set(['GET', 'HEAD']);
exports.knownHookEvents = [
'init',
'beforeRequest',
'beforeRedirect',
'beforeError',
'beforeRetry',
// Promise-Only
'afterResponse'
];
function validateSearchParameters(searchParameters) {
// eslint-disable-next-line guard-for-in
for (const key in searchParameters) {
const value = searchParameters[key];
if (!is_1.default.string(value) && !is_1.default.number(value) && !is_1.default.boolean(value) && !is_1.default.null_(value) && !is_1.default.undefined(value)) {
throw new TypeError(`The \`searchParams\` value '${String(value)}' must be a string, number, boolean or null`);
}
}
}
function isClientRequest(clientRequest) {
return is_1.default.object(clientRequest) && !('statusCode' in clientRequest);
}
const cacheableStore = new weakable_map_1.default();
const waitForOpenFile = async (file) => new Promise((resolve, reject) => {
const onError = (error) => {
reject(error);
};
// Node.js 12 has incomplete types
if (!file.pending) {
resolve();
}
file.once('error', onError);
file.once('ready', () => {
file.off('error', onError);
resolve();
});
});
const redirectCodes = new Set([300, 301, 302, 303, 304, 307, 308]);
const nonEnumerableProperties = [
'context',
'body',
'json',
'form'
];
exports.setNonEnumerableProperties = (sources, to) => {
// Non enumerable properties shall not be merged
const properties = {};
for (const source of sources) {
if (!source) {
continue;
}
for (const name of nonEnumerableProperties) {
if (!(name in source)) {
continue;
}
properties[name] = {
writable: true,
configurable: true,
enumerable: false,
// @ts-expect-error TS doesn't see the check above
value: source[name]
};
}
}
Object.defineProperties(to, properties);
};
/**
An error to be thrown when a request fails.
Contains a `code` property with error class code, like `ECONNREFUSED`.
*/
class RequestError extends Error {
constructor(message, error, self) {
var _a;
super(message);
Error.captureStackTrace(this, this.constructor);
this.name = 'RequestError';
this.code = error.code;
if (self instanceof Request) {
Object.defineProperty(this, 'request', {
enumerable: false,
value: self
});
Object.defineProperty(this, 'response', {
enumerable: false,
value: self[kResponse]
});
Object.defineProperty(this, 'options', {
// This fails because of TS 3.7.2 useDefineForClassFields
// Ref: https://github.com/microsoft/TypeScript/issues/34972
enumerable: false,
value: self.options
});
}
else {
Object.defineProperty(this, 'options', {
// This fails because of TS 3.7.2 useDefineForClassFields
// Ref: https://github.com/microsoft/TypeScript/issues/34972
enumerable: false,
value: self
});
}
this.timings = (_a = this.request) === null || _a === void 0 ? void 0 : _a.timings;
// Recover the original stacktrace
if (is_1.default.string(error.stack) && is_1.default.string(this.stack)) {
const indexOfMessage = this.stack.indexOf(this.message) + this.message.length;
const thisStackTrace = this.stack.slice(indexOfMessage).split('\n').reverse();
const errorStackTrace = error.stack.slice(error.stack.indexOf(error.message) + error.message.length).split('\n').reverse();
// Remove duplicated traces
while (errorStackTrace.length !== 0 && errorStackTrace[0] === thisStackTrace[0]) {
thisStackTrace.shift();
}
this.stack = `${this.stack.slice(0, indexOfMessage)}${thisStackTrace.reverse().join('\n')}${errorStackTrace.reverse().join('\n')}`;
}
}
}
exports.RequestError = RequestError;
/**
An error to be thrown when the server redirects you more than ten times.
Includes a `response` property.
*/
class MaxRedirectsError extends RequestError {
constructor(request) {
super(`Redirected ${request.options.maxRedirects} times. Aborting.`, {}, request);
this.name = 'MaxRedirectsError';
}
}
exports.MaxRedirectsError = MaxRedirectsError;
/**
An error to be thrown when the server response code is not 2xx nor 3xx if `options.followRedirect` is `true`, but always except for 304.
Includes a `response` property.
*/
class HTTPError extends RequestError {
constructor(response) {
super(`Response code ${response.statusCode} (${response.statusMessage})`, {}, response.request);
this.name = 'HTTPError';
}
}
exports.HTTPError = HTTPError;
/**
An error to be thrown when a cache method fails.
For example, if the database goes down or there's a filesystem error.
*/
class CacheError extends RequestError {
constructor(error, request) {
super(error.message, error, request);
this.name = 'CacheError';
}
}
exports.CacheError = CacheError;
/**
An error to be thrown when the request body is a stream and an error occurs while reading from that stream.
*/
class UploadError extends RequestError {
constructor(error, request) {
super(error.message, error, request);
this.name = 'UploadError';
}
}
exports.UploadError = UploadError;
/**
An error to be thrown when the request is aborted due to a timeout.
Includes an `event` and `timings` property.
*/
class TimeoutError extends RequestError {
constructor(error, timings, request) {
super(error.message, error, request);
this.name = 'TimeoutError';
this.event = error.event;
this.timings = timings;
}
}
exports.TimeoutError = TimeoutError;
/**
An error to be thrown when reading from response stream fails.
*/
class ReadError extends RequestError {
constructor(error, request) {
super(error.message, error, request);
this.name = 'ReadError';
}
}
exports.ReadError = ReadError;
/**
An error to be thrown when given an unsupported protocol.
*/
class UnsupportedProtocolError extends RequestError {
constructor(options) {
super(`Unsupported protocol "${options.url.protocol}"`, {}, options);
this.name = 'UnsupportedProtocolError';
}
}
exports.UnsupportedProtocolError = UnsupportedProtocolError;
const proxiedRequestEvents = [
'socket',
'connect',
'continue',
'information',
'upgrade',
'timeout'
];
class Request extends require$$0__default$1['default'].Duplex {
constructor(url, options = {}, defaults) {
super({
// This must be false, to enable throwing after destroy
// It is used for retry logic in Promise API
autoDestroy: false,
// It needs to be zero because we're just proxying the data to another stream
highWaterMark: 0
});
this[kDownloadedSize] = 0;
this[kUploadedSize] = 0;
this.requestInitialized = false;
this[kServerResponsesPiped] = new Set();
this.redirects = [];
this[kStopReading] = false;
this[kTriggerRead] = false;
this[kJobs] = [];
this.retryCount = 0;
// TODO: Remove this when targeting Node.js >= 12
this._progressCallbacks = [];
const unlockWrite = () => this._unlockWrite();
const lockWrite = () => this._lockWrite();
this.on('pipe', (source) => {
source.prependListener('data', unlockWrite);
source.on('data', lockWrite);
source.prependListener('end', unlockWrite);
source.on('end', lockWrite);
});
this.on('unpipe', (source) => {
source.off('data', unlockWrite);
source.off('data', lockWrite);
source.off('end', unlockWrite);
source.off('end', lockWrite);
});
this.on('pipe', source => {
if (source instanceof http_1.IncomingMessage) {
this.options.headers = {
...source.headers,
...this.options.headers
};
}
});
const { json, body, form } = options;
if (json || body || form) {
this._lockWrite();
}
if (exports.kIsNormalizedAlready in options) {
this.options = options;
}
else {
try {
// @ts-expect-error Common TypeScript bug saying that `this.constructor` is not accessible
this.options = this.constructor.normalizeArguments(url, options, defaults);
}
catch (error) {
// TODO: Move this to `_destroy()`
if (is_1.default.nodeStream(options.body)) {
options.body.destroy();
}
this.destroy(error);
return;
}
}
(async () => {
var _a;
try {
if (this.options.body instanceof fs__default['default'].ReadStream) {
await waitForOpenFile(this.options.body);
}
const { url: normalizedURL } = this.options;
if (!normalizedURL) {
throw new TypeError('Missing `url` property');
}
this.requestUrl = normalizedURL.toString();
decodeURI(this.requestUrl);
await this._finalizeBody();
await this._makeRequest();
if (this.destroyed) {
(_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.destroy();
return;
}
// Queued writes etc.
for (const job of this[kJobs]) {
job();
}
// Prevent memory leak
this[kJobs].length = 0;
this.requestInitialized = true;
}
catch (error) {
if (error instanceof RequestError) {
this._beforeError(error);
return;
}
// This is a workaround for https://github.com/nodejs/node/issues/33335
if (!this.destroyed) {
this.destroy(error);
}
}
})();
}
static normalizeArguments(url, options, defaults) {
var _a, _b, _c, _d, _e;
const rawOptions = options;
if (is_1.default.object(url) && !is_1.default.urlInstance(url)) {
options = { ...defaults, ...url, ...options };
}
else {
if (url && options && options.url !== undefined) {
throw new TypeError('The `url` option is mutually exclusive with the `input` argument');
}
options = { ...defaults, ...options };
if (url !== undefined) {
options.url = url;
}
if (is_1.default.urlInstance(options.url)) {
options.url = new url_1__default['default'].URL(options.url.toString());
}
}
// TODO: Deprecate URL options in Got 12.
// Support extend-specific options
if (options.cache === false) {
options.cache = undefined;
}
if (options.dnsCache === false) {
options.dnsCache = undefined;
}
// Nice type assertions
is_1.assert.any([is_1.default.string, is_1.default.undefined], options.method);
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.headers);
is_1.assert.any([is_1.default.string, is_1.default.urlInstance, is_1.default.undefined], options.prefixUrl);
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.cookieJar);
is_1.assert.any([is_1.default.object, is_1.default.string, is_1.default.undefined], options.searchParams);
is_1.assert.any([is_1.default.object, is_1.default.string, is_1.default.undefined], options.cache);
is_1.assert.any([is_1.default.object, is_1.default.number, is_1.default.undefined], options.timeout);
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.context);
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.hooks);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.decompress);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.ignoreInvalidCookies);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.followRedirect);
is_1.assert.any([is_1.default.number, is_1.default.undefined], options.maxRedirects);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.throwHttpErrors);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.http2);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.allowGetBody);
is_1.assert.any([is_1.default.string, is_1.default.undefined], options.localAddress);
is_1.assert.any([dns_ip_version_1.isDnsLookupIpVersion, is_1.default.undefined], options.dnsLookupIpVersion);
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.https);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.rejectUnauthorized);
if (options.https) {
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.https.rejectUnauthorized);
is_1.assert.any([is_1.default.function_, is_1.default.undefined], options.https.checkServerIdentity);
is_1.assert.any([is_1.default.string, is_1.default.object, is_1.default.array, is_1.default.undefined], options.https.certificateAuthority);
is_1.assert.any([is_1.default.string, is_1.default.object, is_1.default.array, is_1.default.undefined], options.https.key);
is_1.assert.any([is_1.default.string, is_1.default.object, is_1.default.array, is_1.default.undefined], options.https.certificate);
is_1.assert.any([is_1.default.string, is_1.default.undefined], options.https.passphrase);
is_1.assert.any([is_1.default.string, is_1.default.buffer, is_1.default.array, is_1.default.undefined], options.https.pfx);
}
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.cacheOptions);
// `options.method`
if (is_1.default.string(options.method)) {
options.method = options.method.toUpperCase();
}
else {
options.method = 'GET';
}
// `options.headers`
if (options.headers === (defaults === null || defaults === void 0 ? void 0 : defaults.headers)) {
options.headers = { ...options.headers };
}
else {
options.headers = lowercaseKeys({ ...(defaults === null || defaults === void 0 ? void 0 : defaults.headers), ...options.headers });
}
// Disallow legacy `url.Url`
if ('slashes' in options) {
throw new TypeError('The legacy `url.Url` has been deprecated. Use `URL` instead.');
}
// `options.auth`
if ('auth' in options) {
throw new TypeError('Parameter `auth` is deprecated. Use `username` / `password` instead.');
}
// `options.searchParams`
if ('searchParams' in options) {
if (options.searchParams && options.searchParams !== (defaults === null || defaults === void 0 ? void 0 : defaults.searchParams)) {
let searchParameters;
if (is_1.default.string(options.searchParams) || (options.searchParams instanceof url_1__default['default'].URLSearchParams)) {
searchParameters = new url_1__default['default'].URLSearchParams(options.searchParams);
}
else {
validateSearchParameters(options.searchParams);
searchParameters = new url_1__default['default'].URLSearchParams();
// eslint-disable-next-line guard-for-in
for (const key in options.searchParams) {
const value = options.searchParams[key];
if (value === null) {
searchParameters.append(key, '');
}
else if (value !== undefined) {
searchParameters.append(key, value);
}
}
}
// `normalizeArguments()` is also used to merge options
(_a = defaults === null || defaults === void 0 ? void 0 : defaults.searchParams) === null || _a === void 0 ? void 0 : _a.forEach((value, key) => {
// Only use default if one isn't already defined
if (!searchParameters.has(key)) {
searchParameters.append(key, value);
}
});
options.searchParams = searchParameters;
}
}
// `options.username` & `options.password`
options.username = (_b = options.username) !== null && _b !== void 0 ? _b : '';
options.password = (_c = options.password) !== null && _c !== void 0 ? _c : '';
// `options.prefixUrl` & `options.url`
if (is_1.default.undefined(options.prefixUrl)) {
options.prefixUrl = (_d = defaults === null || defaults === void 0 ? void 0 : defaults.prefixUrl) !== null && _d !== void 0 ? _d : '';
}
else {
options.prefixUrl = options.prefixUrl.toString();
if (options.prefixUrl !== '' && !options.prefixUrl.endsWith('/')) {
options.prefixUrl += '/';
}
}
if (is_1.default.string(options.url)) {
if (options.url.startsWith('/')) {
throw new Error('`input` must not start with a slash when using `prefixUrl`');
}
options.url = options_to_url_1.default(options.prefixUrl + options.url, options);
}
else if ((is_1.default.undefined(options.url) && options.prefixUrl !== '') || options.protocol) {
options.url = options_to_url_1.default(options.prefixUrl, options);
}
if (options.url) {
if ('port' in options) {
delete options.port;
}
// Make it possible to change `options.prefixUrl`
let { prefixUrl } = options;
Object.defineProperty(options, 'prefixUrl', {
set: (value) => {
const url = options.url;
if (!url.href.startsWith(value)) {
throw new Error(`Cannot change \`prefixUrl\` from ${prefixUrl} to ${value}: ${url.href}`);
}
options.url = new url_1__default['default'].URL(value + url.href.slice(prefixUrl.length));
prefixUrl = value;
},
get: () => prefixUrl
});
// Support UNIX sockets
let { protocol } = options.url;
if (protocol === 'unix:') {
protocol = 'http:';
options.url = new url_1__default['default'].URL(`http://unix${options.url.pathname}${options.url.search}`);
}
// Set search params
if (options.searchParams) {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
options.url.search = options.searchParams.toString();
}
// Protocol check
if (protocol !== 'http:' && protocol !== 'https:') {
throw new UnsupportedProtocolError(options);
}
// Update `username`
if (options.username === '') {
options.username = options.url.username;
}
else {
options.url.username = options.username;
}
// Update `password`
if (options.password === '') {
options.password = options.url.password;
}
else {
options.url.password = options.password;
}
}
// `options.cookieJar`
const { cookieJar } = options;
if (cookieJar) {
let { setCookie, getCookieString } = cookieJar;
is_1.assert.function_(setCookie);
is_1.assert.function_(getCookieString);
/* istanbul ignore next: Horrible `tough-cookie` v3 check */
if (setCookie.length === 4 && getCookieString.length === 0) {
setCookie = util_1__default['default'].promisify(setCookie.bind(options.cookieJar));
getCookieString = util_1__default['default'].promisify(getCookieString.bind(options.cookieJar));
options.cookieJar = {
setCookie,
getCookieString: getCookieString
};
}
}
// `options.cache`
const { cache } = options;
if (cache) {
if (!cacheableStore.has(cache)) {
cacheableStore.set(cache, new src(((requestOptions, handler) => {
const result = requestOptions[kRequest](requestOptions, handler);
// TODO: remove this when `cacheable-request` supports async request functions.
if (is_1.default.promise(result)) {
// @ts-expect-error
// We only need to implement the error handler in order to support HTTP2 caching.
// The result will be a promise anyway.
result.once = (event, handler) => {
if (event === 'error') {
result.catch(handler);
}
else if (event === 'abort') {
// The empty catch is needed here in case when
// it rejects before it's `await`ed in `_makeRequest`.
(async () => {
try {
const request = (await result);
request.once('abort', handler);
}
catch (_a) { }
})();
}
else {
/* istanbul ignore next: safety check */
throw new Error(`Unknown HTTP2 promise event: ${event}`);
}
return result;
};
}
return result;
}), cache));
}
}
// `options.cacheOptions`
options.cacheOptions = { ...options.cacheOptions };
// `options.dnsCache`
if (options.dnsCache === true) {
if (!globalDnsCache) {
globalDnsCache = new source$2.default();
}
options.dnsCache = globalDnsCache;
}
else if (!is_1.default.undefined(options.dnsCache) && !options.dnsCache.lookup) {
throw new TypeError(`Parameter \`dnsCache\` must be a CacheableLookup instance or a boolean, got ${is_1.default(options.dnsCache)}`);
}
// `options.timeout`
if (is_1.default.number(options.timeout)) {
options.timeout = { request: options.timeout };
}
else if (defaults && options.timeout !== defaults.timeout) {
options.timeout = {
...defaults.timeout,
...options.timeout
};
}
else {
options.timeout = { ...options.timeout };
}
// `options.context`
if (!options.context) {
options.context = {};
}
// `options.hooks`
const areHooksDefault = options.hooks === (defaults === null || defaults === void 0 ? void 0 : defaults.hooks);
options.hooks = { ...options.hooks };
for (const event of exports.knownHookEvents) {
if (event in options.hooks) {
if (is_1.default.array(options.hooks[event])) {
// See https://github.com/microsoft/TypeScript/issues/31445#issuecomment-576929044
options.hooks[event] = [...options.hooks[event]];
}
else {
throw new TypeError(`Parameter \`${event}\` must be an Array, got ${is_1.default(options.hooks[event])}`);
}
}
else {
options.hooks[event] = [];
}
}
if (defaults && !areHooksDefault) {
for (const event of exports.knownHookEvents) {
const defaultHooks = defaults.hooks[event];
if (defaultHooks.length > 0) {
// See https://github.com/microsoft/TypeScript/issues/31445#issuecomment-576929044
options.hooks[event] = [
...defaults.hooks[event],
...options.hooks[event]
];
}
}
}
// DNS options
if ('family' in options) {
deprecation_warning_1.default('"options.family" was never documented, please use "options.dnsLookupIpVersion"');
}
// HTTPS options
if (defaults === null || defaults === void 0 ? void 0 : defaults.https) {
options.https = { ...defaults.https, ...options.https };
}
if ('rejectUnauthorized' in options) {
deprecation_warning_1.default('"options.rejectUnauthorized" is now deprecated, please use "options.https.rejectUnauthorized"');
}
if ('checkServerIdentity' in options) {
deprecation_warning_1.default('"options.checkServerIdentity" was never documented, please use "options.https.checkServerIdentity"');
}
if ('ca' in options) {
deprecation_warning_1.default('"options.ca" was never documented, please use "options.https.certificateAuthority"');
}
if ('key' in options) {
deprecation_warning_1.default('"options.key" was never documented, please use "options.https.key"');
}
if ('cert' in options) {
deprecation_warning_1.default('"options.cert" was never documented, please use "options.https.certificate"');
}
if ('passphrase' in options) {
deprecation_warning_1.default('"options.passphrase" was never documented, please use "options.https.passphrase"');
}
if ('pfx' in options) {
deprecation_warning_1.default('"options.pfx" was never documented, please use "options.https.pfx"');
}
// Other options
if ('followRedirects' in options) {
throw new TypeError('The `followRedirects` option does not exist. Use `followRedirect` instead.');
}
if (options.agent) {
for (const key in options.agent) {
if (key !== 'http' && key !== 'https' && key !== 'http2') {
throw new TypeError(`Expected the \`options.agent\` properties to be \`http\`, \`https\` or \`http2\`, got \`${key}\``);
}
}
}
options.maxRedirects = (_e = options.maxRedirects) !== null && _e !== void 0 ? _e : 0;
// Set non-enumerable properties
exports.setNonEnumerableProperties([defaults, rawOptions], options);
return normalize_arguments_1.default(options, defaults);
}
_lockWrite() {
const onLockedWrite = () => {
throw new TypeError('The payload has been already provided');
};
this.write = onLockedWrite;
this.end = onLockedWrite;
}
_unlockWrite() {
this.write = super.write;
this.end = super.end;
}
async _finalizeBody() {
const { options } = this;
const { headers } = options;
const isForm = !is_1.default.undefined(options.form);
const isJSON = !is_1.default.undefined(options.json);
const isBody = !is_1.default.undefined(options.body);
const hasPayload = isForm || isJSON || isBody;
const cannotHaveBody = exports.withoutBody.has(options.method) && !(options.method === 'GET' && options.allowGetBody);
this._cannotHaveBody = cannotHaveBody;
if (hasPayload) {
if (cannotHaveBody) {
throw new TypeError(`The \`${options.method}\` method cannot be used with a body`);
}
if ([isBody, isForm, isJSON].filter(isTrue => isTrue).length > 1) {
throw new TypeError('The `body`, `json` and `form` options are mutually exclusive');
}
if (isBody &&
!(options.body instanceof require$$0__default$1['default'].Readable) &&
!is_1.default.string(options.body) &&
!is_1.default.buffer(options.body) &&
!is_form_data_1.default(options.body)) {
throw new TypeError('The `body` option must be a stream.Readable, string or Buffer');
}
if (isForm && !is_1.default.object(options.form)) {
throw new TypeError('The `form` option must be an Object');
}
{
// Serialize body
const noContentType = !is_1.default.string(headers['content-type']);
if (isBody) {
// Special case for https://github.com/form-data/form-data
if (is_form_data_1.default(options.body) && noContentType) {
headers['content-type'] = `multipart/form-data; boundary=${options.body.getBoundary()}`;
}
this[kBody] = options.body;
}
else if (isForm) {
if (noContentType) {
headers['content-type'] = 'application/x-www-form-urlencoded';
}
this[kBody] = (new url_1__default['default'].URLSearchParams(options.form)).toString();
}
else {
if (noContentType) {
headers['content-type'] = 'application/json';
}
this[kBody] = options.stringifyJson(options.json);
}
const uploadBodySize = await get_body_size_1.default(this[kBody], options.headers);
// See https://tools.ietf.org/html/rfc7230#section-3.3.2
// A user agent SHOULD send a Content-Length in a request message when
// no Transfer-Encoding is sent and the request method defines a meaning
// for an enclosed payload body. For example, a Content-Length header
// field is normally sent in a POST request even when the value is 0
// (indicating an empty payload body). A user agent SHOULD NOT send a
// Content-Length header field when the request message does not contain
// a payload body and the method semantics do not anticipate such a
// body.
if (is_1.default.undefined(headers['content-length']) && is_1.default.undefined(headers['transfer-encoding'])) {
if (!cannotHaveBody && !is_1.default.undefined(uploadBodySize)) {
headers['content-length'] = String(uploadBodySize);
}
}
}
}
else if (cannotHaveBody) {
this._lockWrite();
}
else {
this._unlockWrite();
}
this[kBodySize] = Number(headers['content-length']) || undefined;
}
async _onResponseBase(response) {
const { options } = this;
const { url } = options;
this[kOriginalResponse] = response;
if (options.decompress) {
response = decompressResponse(response);
}
const statusCode = response.statusCode;
const typedResponse = response;
typedResponse.statusMessage = typedResponse.statusMessage ? typedResponse.statusMessage : http__default['default'].STATUS_CODES[statusCode];
typedResponse.url = options.url.toString();
typedResponse.requestUrl = this.requestUrl;
typedResponse.redirectUrls = this.redirects;
typedResponse.request = this;
typedResponse.isFromCache = response.fromCache || false;
typedResponse.ip = this.ip;
typedResponse.retryCount = this.retryCount;
this[kIsFromCache] = typedResponse.isFromCache;
this[kResponseSize] = Number(response.headers['content-length']) || undefined;
this[kResponse] = response;
response.once('end', () => {
this[kResponseSize] = this[kDownloadedSize];
this.emit('downloadProgress', this.downloadProgress);
});
response.once('error', (error) => {
// Force clean-up, because some packages don't do this.
// TODO: Fix decompress-response
response.destroy();
this._beforeError(new ReadError(error, this));
});
response.once('aborted', () => {
this._beforeError(new ReadError({
name: 'Error',
message: 'The server aborted pending request',
code: 'ECONNRESET'
}, this));
});
this.emit('downloadProgress', this.downloadProgress);
const rawCookies = response.headers['set-cookie'];
if (is_1.default.object(options.cookieJar) && rawCookies) {
let promises = rawCookies.map(async (rawCookie) => options.cookieJar.setCookie(rawCookie, url.toString()));
if (options.ignoreInvalidCookies) {
promises = promises.map(async (p) => p.catch(() => { }));
}
try {
await Promise.all(promises);
}
catch (error) {
this._beforeError(error);
return;
}
}
if (options.followRedirect && response.headers.location && redirectCodes.has(statusCode)) {
// We're being redirected, we don't care about the response.
// It'd be best to abort the request, but we can't because
// we would have to sacrifice the TCP connection. We don't want that.
response.resume();
if (this[kRequest]) {
this[kCancelTimeouts]();
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this[kRequest];
this[kUnproxyEvents]();
}
const shouldBeGet = statusCode === 303 && options.method !== 'GET' && options.method !== 'HEAD';
if (shouldBeGet || !options.methodRewriting) {
// Server responded with "see other", indicating that the resource exists at another location,
// and the client should request it from that location via GET or HEAD.
options.method = 'GET';
if ('body' in options) {
delete options.body;
}
if ('json' in options) {
delete options.json;
}
if ('form' in options) {
delete options.form;
}
this[kBody] = undefined;
delete options.headers['content-length'];
}
if (this.redirects.length >= options.maxRedirects) {
this._beforeError(new MaxRedirectsError(this));
return;
}
try {
// Do not remove. See https://github.com/sindresorhus/got/pull/214
const redirectBuffer = Buffer.from(response.headers.location, 'binary').toString();
// Handles invalid URLs. See https://github.com/sindresorhus/got/issues/604
const redirectUrl = new url_1__default['default'].URL(redirectBuffer, url);
const redirectString = redirectUrl.toString();
decodeURI(redirectString);
// Redirecting to a different site, clear sensitive data.
if (redirectUrl.hostname !== url.hostname || redirectUrl.port !== url.port) {
if ('host' in options.headers) {
delete options.headers.host;
}
if ('cookie' in options.headers) {
delete options.headers.cookie;
}
if ('authorization' in options.headers) {
delete options.headers.authorization;
}
if (options.username || options.password) {
options.username = '';
options.password = '';
}
}
else {
redirectUrl.username = options.username;
redirectUrl.password = options.password;
}
this.redirects.push(redirectString);
options.url = redirectUrl;
for (const hook of options.hooks.beforeRedirect) {
// eslint-disable-next-line no-await-in-loop
await hook(options, typedResponse);
}
this.emit('redirect', typedResponse, options);
await this._makeRequest();
}
catch (error) {
this._beforeError(error);
return;
}
return;
}
if (options.isStream && options.throwHttpErrors && !is_response_ok_1.isResponseOk(typedResponse)) {
this._beforeError(new HTTPError(typedResponse));
return;
}
response.on('readable', () => {
if (this[kTriggerRead]) {
this._read();
}
});
this.on('resume', () => {
response.resume();
});
this.on('pause', () => {
response.pause();
});
response.once('end', () => {
this.push(null);
});
this.emit('response', response);
for (const destination of this[kServerResponsesPiped]) {
if (destination.headersSent) {
continue;
}
// eslint-disable-next-line guard-for-in
for (const key in response.headers) {
const isAllowed = options.decompress ? key !== 'content-encoding' : true;
const value = response.headers[key];
if (isAllowed) {
destination.setHeader(key, value);
}
}
destination.statusCode = statusCode;
}
}
async _onResponse(response) {
try {
await this._onResponseBase(response);
}
catch (error) {
/* istanbul ignore next: better safe than sorry */
this._beforeError(error);
}
}
_onRequest(request) {
const { options } = this;
const { timeout, url } = options;
source$3.default(request);
this[kCancelTimeouts] = timed_out_1.default(request, timeout, url);
const responseEventName = options.cache ? 'cacheableResponse' : 'response';
request.once(responseEventName, (response) => {
void this._onResponse(response);
});
request.once('error', (error) => {
var _a;
// Force clean-up, because some packages (e.g. nock) don't do this.
request.destroy();
// Node.js <= 12.18.2 mistakenly emits the response `end` first.
(_a = request.res) === null || _a === void 0 ? void 0 : _a.removeAllListeners('end');
error = error instanceof timed_out_1.TimeoutError ? new TimeoutError(error, this.timings, this) : new RequestError(error.message, error, this);
this._beforeError(error);
});
this[kUnproxyEvents] = proxy_events_1.default(request, this, proxiedRequestEvents);
this[kRequest] = request;
this.emit('uploadProgress', this.uploadProgress);
// Send body
const body = this[kBody];
const currentRequest = this.redirects.length === 0 ? this : request;
if (is_1.default.nodeStream(body)) {
body.pipe(currentRequest);
body.once('error', (error) => {
this._beforeError(new UploadError(error, this));
});
}
else {
this._unlockWrite();
if (!is_1.default.undefined(body)) {
this._writeRequest(body, undefined, () => { });
currentRequest.end();
this._lockWrite();
}
else if (this._cannotHaveBody || this._noPipe) {
currentRequest.end();
this._lockWrite();
}
}
this.emit('request', request);
}
async _createCacheableRequest(url, options) {
return new Promise((resolve, reject) => {
// TODO: Remove `utils/url-to-options.ts` when `cacheable-request` is fixed
Object.assign(options, url_to_options_1.default(url));
// `http-cache-semantics` checks this
// TODO: Fix this ignore.
// @ts-expect-error
delete options.url;
let request;
// This is ugly
const cacheRequest = cacheableStore.get(options.cache)(options, async (response) => {
// TODO: Fix `cacheable-response`
response._readableState.autoDestroy = false;
if (request) {
(await request).emit('cacheableResponse', response);
}
resolve(response);
});
// Restore options
options.url = url;
cacheRequest.once('error', reject);
cacheRequest.once('request', async (requestOrPromise) => {
request = requestOrPromise;
resolve(request);
});
});
}
async _makeRequest() {
var _a, _b, _c, _d, _e;
const { options } = this;
const { headers } = options;
for (const key in headers) {
if (is_1.default.undefined(headers[key])) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete headers[key];
}
else if (is_1.default.null_(headers[key])) {
throw new TypeError(`Use \`undefined\` instead of \`null\` to delete the \`${key}\` header`);
}
}
if (options.decompress && is_1.default.undefined(headers['accept-encoding'])) {
headers['accept-encoding'] = supportsBrotli ? 'gzip, deflate, br' : 'gzip, deflate';
}
// Set cookies
if (options.cookieJar) {
const cookieString = await options.cookieJar.getCookieString(options.url.toString());
if (is_1.default.nonEmptyString(cookieString)) {
options.headers.cookie = cookieString;
}
}
for (const hook of options.hooks.beforeRequest) {
// eslint-disable-next-line no-await-in-loop
const result = await hook(options);
if (!is_1.default.undefined(result)) {
// @ts-expect-error Skip the type mismatch to support abstract responses
options.request = () => result;
break;
}
}
if (options.body && this[kBody] !== options.body) {
this[kBody] = options.body;
}
const { agent, request, timeout, url } = options;
if (options.dnsCache && !('lookup' in options)) {
options.lookup = options.dnsCache.lookup;
}
// UNIX sockets
if (url.hostname === 'unix') {
const matches = /(?<socketPath>.+?):(?<path>.+)/.exec(`${url.pathname}${url.search}`);
if (matches === null || matches === void 0 ? void 0 : matches.groups) {
const { socketPath, path } = matches.groups;
Object.assign(options, {
socketPath,
path,
host: ''
});
}
}
const isHttps = url.protocol === 'https:';
// Fallback function
let fallbackFn;
if (options.http2) {
fallbackFn = source$1.auto;
}
else {
fallbackFn = isHttps ? https__default['default'].request : http__default['default'].request;
}
const realFn = (_a = options.request) !== null && _a !== void 0 ? _a : fallbackFn;
// Cache support
const fn = options.cache ? this._createCacheableRequest : realFn;
// Pass an agent directly when HTTP2 is disabled
if (agent && !options.http2) {
options.agent = agent[isHttps ? 'https' : 'http'];
}
// Prepare plain HTTP request options
options[kRequest] = realFn;
delete options.request;
// TODO: Fix this ignore.
// @ts-expect-error
delete options.timeout;
const requestOptions = options;
requestOptions.shared = (_b = options.cacheOptions) === null || _b === void 0 ? void 0 : _b.shared;
requestOptions.cacheHeuristic = (_c = options.cacheOptions) === null || _c === void 0 ? void 0 : _c.cacheHeuristic;
requestOptions.immutableMinTimeToLive = (_d = options.cacheOptions) === null || _d === void 0 ? void 0 : _d.immutableMinTimeToLive;
requestOptions.ignoreCargoCult = (_e = options.cacheOptions) === null || _e === void 0 ? void 0 : _e.ignoreCargoCult;
// If `dnsLookupIpVersion` is not present do not override `family`
if (options.dnsLookupIpVersion !== undefined) {
try {
requestOptions.family = dns_ip_version_1.dnsLookupIpVersionToFamily(options.dnsLookupIpVersion);
}
catch (_f) {
throw new Error('Invalid `dnsLookupIpVersion` option value');
}
}
// HTTPS options remapping
if (options.https) {
if ('rejectUnauthorized' in options.https) {
requestOptions.rejectUnauthorized = options.https.rejectUnauthorized;
}
if (options.https.checkServerIdentity) {
requestOptions.checkServerIdentity = options.https.checkServerIdentity;
}
if (options.https.certificateAuthority) {
requestOptions.ca = options.https.certificateAuthority;
}
if (options.https.certificate) {
requestOptions.cert = options.https.certificate;
}
if (options.https.key) {
requestOptions.key = options.https.key;
}
if (options.https.passphrase) {
requestOptions.passphrase = options.https.passphrase;
}
if (options.https.pfx) {
requestOptions.pfx = options.https.pfx;
}
}
try {
let requestOrResponse = await fn(url, requestOptions);
if (is_1.default.undefined(requestOrResponse)) {
requestOrResponse = fallbackFn(url, requestOptions);
}
// Restore options
options.request = request;
options.timeout = timeout;
options.agent = agent;
// HTTPS options restore
if (options.https) {
if ('rejectUnauthorized' in options.https) {
delete requestOptions.rejectUnauthorized;
}
if (options.https.checkServerIdentity) {
// @ts-expect-error - This one will be removed when we remove the alias.
delete requestOptions.checkServerIdentity;
}
if (options.https.certificateAuthority) {
delete requestOptions.ca;
}
if (options.https.certificate) {
delete requestOptions.cert;
}
if (options.https.key) {
delete requestOptions.key;
}
if (options.https.passphrase) {
delete requestOptions.passphrase;
}
if (options.https.pfx) {
delete requestOptions.pfx;
}
}
if (isClientRequest(requestOrResponse)) {
this._onRequest(requestOrResponse);
// Emit the response after the stream has been ended
}
else if (this.writable) {
this.once('finish', () => {
void this._onResponse(requestOrResponse);
});
this._unlockWrite();
this.end();
this._lockWrite();
}
else {
void this._onResponse(requestOrResponse);
}
}
catch (error) {
if (error instanceof src.CacheError) {
throw new CacheError(error, this);
}
throw new RequestError(error.message, error, this);
}
}
async _error(error) {
try {
for (const hook of this.options.hooks.beforeError) {
// eslint-disable-next-line no-await-in-loop
error = await hook(error);
}
}
catch (error_) {
error = new RequestError(error_.message, error_, this);
}
this.destroy(error);
}
_beforeError(error) {
if (this[kStopReading]) {
return;
}
const { options } = this;
const retryCount = this.retryCount + 1;
this[kStopReading] = true;
if (!(error instanceof RequestError)) {
error = new RequestError(error.message, error, this);
}
const typedError = error;
const { response } = typedError;
void (async () => {
if (response && !response.body) {
response.setEncoding(this._readableState.encoding);
try {
response.rawBody = await get_buffer_1.default(response);
response.body = response.rawBody.toString();
}
catch (_a) { }
}
if (this.listenerCount('retry') !== 0) {
let backoff;
try {
let retryAfter;
if (response && 'retry-after' in response.headers) {
retryAfter = Number(response.headers['retry-after']);
if (Number.isNaN(retryAfter)) {
retryAfter = Date.parse(response.headers['retry-after']) - Date.now();
if (retryAfter <= 0) {
retryAfter = 1;
}
}
else {
retryAfter *= 1000;
}
}
backoff = await options.retry.calculateDelay({
attemptCount: retryCount,
retryOptions: options.retry,
error: typedError,
retryAfter,
computedValue: calculate_retry_delay_1.default({
attemptCount: retryCount,
retryOptions: options.retry,
error: typedError,
retryAfter,
computedValue: 0
})
});
}
catch (error_) {
void this._error(new RequestError(error_.message, error_, this));
return;
}
if (backoff) {
const retry = async () => {
try {
for (const hook of this.options.hooks.beforeRetry) {
// eslint-disable-next-line no-await-in-loop
await hook(this.options, typedError, retryCount);
}
}
catch (error_) {
void this._error(new RequestError(error_.message, error, this));
return;
}
// Something forced us to abort the retry
if (this.destroyed) {
return;
}
this.destroy();
this.emit('retry', retryCount, error);
};
this[kRetryTimeout] = setTimeout(retry, backoff);
return;
}
}
void this._error(typedError);
})();
}
_read() {
this[kTriggerRead] = true;
const response = this[kResponse];
if (response && !this[kStopReading]) {
// We cannot put this in the `if` above
// because `.read()` also triggers the `end` event
if (response.readableLength) {
this[kTriggerRead] = false;
}
let data;
while ((data = response.read()) !== null) {
this[kDownloadedSize] += data.length;
this[kStartedReading] = true;
const progress = this.downloadProgress;
if (progress.percent < 1) {
this.emit('downloadProgress', progress);
}
this.push(data);
}
}
}
// Node.js 12 has incorrect types, so the encoding must be a string
_write(chunk, encoding, callback) {
const write = () => {
this._writeRequest(chunk, encoding, callback);
};
if (this.requestInitialized) {
write();
}
else {
this[kJobs].push(write);
}
}
_writeRequest(chunk, encoding, callback) {
if (this[kRequest].destroyed) {
// Probably the `ClientRequest` instance will throw
return;
}
this._progressCallbacks.push(() => {
this[kUploadedSize] += Buffer.byteLength(chunk, encoding);
const progress = this.uploadProgress;
if (progress.percent < 1) {
this.emit('uploadProgress', progress);
}
});
// TODO: What happens if it's from cache? Then this[kRequest] won't be defined.
this[kRequest].write(chunk, encoding, (error) => {
if (!error && this._progressCallbacks.length > 0) {
this._progressCallbacks.shift()();
}
callback(error);
});
}
_final(callback) {
const endRequest = () => {
// FIX: Node.js 10 calls the write callback AFTER the end callback!
while (this._progressCallbacks.length !== 0) {
this._progressCallbacks.shift()();
}
// We need to check if `this[kRequest]` is present,
// because it isn't when we use cache.
if (!(kRequest in this)) {
callback();
return;
}
if (this[kRequest].destroyed) {
callback();
return;
}
this[kRequest].end((error) => {
if (!error) {
this[kBodySize] = this[kUploadedSize];
this.emit('uploadProgress', this.uploadProgress);
this[kRequest].emit('upload-complete');
}
callback(error);
});
};
if (this.requestInitialized) {
endRequest();
}
else {
this[kJobs].push(endRequest);
}
}
_destroy(error, callback) {
var _a;
this[kStopReading] = true;
// Prevent further retries
clearTimeout(this[kRetryTimeout]);
if (kRequest in this) {
this[kCancelTimeouts]();
// TODO: Remove the next `if` when these get fixed:
// - https://github.com/nodejs/node/issues/32851
if (!((_a = this[kResponse]) === null || _a === void 0 ? void 0 : _a.complete)) {
this[kRequest].destroy();
}
}
if (error !== null && !is_1.default.undefined(error) && !(error instanceof RequestError)) {
error = new RequestError(error.message, error, this);
}
callback(error);
}
get _isAboutToError() {
return this[kStopReading];
}
/**
The remote IP address.
*/
get ip() {
var _a;
return (_a = this.socket) === null || _a === void 0 ? void 0 : _a.remoteAddress;
}
/**
Indicates whether the request has been aborted or not.
*/
get aborted() {
var _a, _b, _c;
return ((_b = (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.destroyed) !== null && _b !== void 0 ? _b : this.destroyed) && !((_c = this[kOriginalResponse]) === null || _c === void 0 ? void 0 : _c.complete);
}
get socket() {
var _a, _b;
return (_b = (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.socket) !== null && _b !== void 0 ? _b : undefined;
}
/**
Progress event for downloading (receiving a response).
*/
get downloadProgress() {
let percent;
if (this[kResponseSize]) {
percent = this[kDownloadedSize] / this[kResponseSize];
}
else if (this[kResponseSize] === this[kDownloadedSize]) {
percent = 1;
}
else {
percent = 0;
}
return {
percent,
transferred: this[kDownloadedSize],
total: this[kResponseSize]
};
}
/**
Progress event for uploading (sending a request).
*/
get uploadProgress() {
let percent;
if (this[kBodySize]) {
percent = this[kUploadedSize] / this[kBodySize];
}
else if (this[kBodySize] === this[kUploadedSize]) {
percent = 1;
}
else {
percent = 0;
}
return {
percent,
transferred: this[kUploadedSize],
total: this[kBodySize]
};
}
/**
The object contains the following properties:
- `start` - Time when the request started.
- `socket` - Time when a socket was assigned to the request.
- `lookup` - Time when the DNS lookup finished.
- `connect` - Time when the socket successfully connected.
- `secureConnect` - Time when the socket securely connected.
- `upload` - Time when the request finished uploading.
- `response` - Time when the request fired `response` event.
- `end` - Time when the response fired `end` event.
- `error` - Time when the request fired `error` event.
- `abort` - Time when the request fired `abort` event.
- `phases`
- `wait` - `timings.socket - timings.start`
- `dns` - `timings.lookup - timings.socket`
- `tcp` - `timings.connect - timings.lookup`
- `tls` - `timings.secureConnect - timings.connect`
- `request` - `timings.upload - (timings.secureConnect || timings.connect)`
- `firstByte` - `timings.response - timings.upload`
- `download` - `timings.end - timings.response`
- `total` - `(timings.end || timings.error || timings.abort) - timings.start`
If something has not been measured yet, it will be `undefined`.
__Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
*/
get timings() {
var _a;
return (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.timings;
}
/**
Whether the response was retrieved from the cache.
*/
get isFromCache() {
return this[kIsFromCache];
}
pipe(destination, options) {
if (this[kStartedReading]) {
throw new Error('Failed to pipe. The response has been emitted already.');
}
if (destination instanceof http_1.ServerResponse) {
this[kServerResponsesPiped].add(destination);
}
return super.pipe(destination, options);
}
unpipe(destination) {
if (destination instanceof http_1.ServerResponse) {
this[kServerResponsesPiped].delete(destination);
}
super.unpipe(destination);
return this;
}
}
exports.default = Request;
});
var core_1 = core$2;
var types$1 = createCommonjsModule(function (module, exports) {
var __createBinding = (commonjsGlobal && commonjsGlobal.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (commonjsGlobal && commonjsGlobal.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CancelError = exports.ParseError = void 0;
/**
An error to be thrown when server response code is 2xx, and parsing body fails.
Includes a `response` property.
*/
class ParseError extends core_1.RequestError {
constructor(error, response) {
const { options } = response.request;
super(`${error.message} in "${options.url.toString()}"`, error, response.request);
this.name = 'ParseError';
}
}
exports.ParseError = ParseError;
/**
An error to be thrown when the request is aborted with `.cancel()`.
*/
class CancelError extends core_1.RequestError {
constructor(request) {
super('Promise was canceled', {}, request);
this.name = 'CancelError';
}
get isCanceled() {
return true;
}
}
exports.CancelError = CancelError;
__exportStar(core_1, exports);
});
var types_1 = types$1;
const parseBody = (response, responseType, parseJson, encoding) => {
const { rawBody } = response;
try {
if (responseType === 'text') {
return rawBody.toString(encoding);
}
if (responseType === 'json') {
return rawBody.length === 0 ? '' : parseJson(rawBody.toString());
}
if (responseType === 'buffer') {
return rawBody;
}
throw new types_1.ParseError({
message: `Unknown body type '${responseType}'`,
name: 'Error'
}, response);
}
catch (error) {
throw new types_1.ParseError(error, response);
}
};
var _default$8 = parseBody;
var parseBody_1 = /*#__PURE__*/Object.defineProperty({
default: _default$8
}, '__esModule', {value: true});
var parse_body_1 = parseBody_1;
var asPromise_1 = createCommonjsModule(function (module, exports) {
var __createBinding = (commonjsGlobal && commonjsGlobal.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (commonjsGlobal && commonjsGlobal.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
const proxiedRequestEvents = [
'request',
'response',
'redirect',
'uploadProgress',
'downloadProgress'
];
function asPromise(normalizedOptions) {
let globalRequest;
let globalResponse;
const emitter = new EventEmitter__default['default'].EventEmitter();
const promise = new pCancelable((resolve, reject, onCancel) => {
const makeRequest = (retryCount) => {
const request = new core_1.default(undefined, normalizedOptions);
request.retryCount = retryCount;
request._noPipe = true;
onCancel(() => request.destroy());
onCancel.shouldReject = false;
onCancel(() => reject(new types_1.CancelError(request)));
globalRequest = request;
request.once('response', async (response) => {
var _a;
response.retryCount = retryCount;
if (response.request.aborted) {
// Canceled while downloading - will throw a `CancelError` or `TimeoutError` error
return;
}
// Download body
let rawBody;
try {
rawBody = await get_buffer_1.default(request);
response.rawBody = rawBody;
}
catch (_b) {
// The same error is caught below.
// See request.once('error')
return;
}
if (request._isAboutToError) {
return;
}
// Parse body
const contentEncoding = ((_a = response.headers['content-encoding']) !== null && _a !== void 0 ? _a : '').toLowerCase();
const isCompressed = ['gzip', 'deflate', 'br'].includes(contentEncoding);
const { options } = request;
if (isCompressed && !options.decompress) {
response.body = rawBody;
}
else {
try {
response.body = parse_body_1.default(response, options.responseType, options.parseJson, options.encoding);
}
catch (error) {
// Fallback to `utf8`
response.body = rawBody.toString();
if (is_response_ok_1.isResponseOk(response)) {
request._beforeError(error);
return;
}
}
}
try {
for (const [index, hook] of options.hooks.afterResponse.entries()) {
// @ts-expect-error TS doesn't notice that CancelableRequest is a Promise
// eslint-disable-next-line no-await-in-loop
response = await hook(response, async (updatedOptions) => {
const typedOptions = core_1.default.normalizeArguments(undefined, {
...updatedOptions,
retry: {
calculateDelay: () => 0
},
throwHttpErrors: false,
resolveBodyOnly: false
}, options);
// Remove any further hooks for that request, because we'll call them anyway.
// The loop continues. We don't want duplicates (asPromise recursion).
typedOptions.hooks.afterResponse = typedOptions.hooks.afterResponse.slice(0, index);
for (const hook of typedOptions.hooks.beforeRetry) {
// eslint-disable-next-line no-await-in-loop
await hook(typedOptions);
}
const promise = asPromise(typedOptions);
onCancel(() => {
promise.catch(() => { });
promise.cancel();
});
return promise;
});
}
}
catch (error) {
request._beforeError(new types_1.RequestError(error.message, error, request));
return;
}
if (!is_response_ok_1.isResponseOk(response)) {
request._beforeError(new types_1.HTTPError(response));
return;
}
globalResponse = response;
resolve(request.options.resolveBodyOnly ? response.body : response);
});
const onError = (error) => {
if (promise.isCanceled) {
return;
}
const { options } = request;
if (error instanceof types_1.HTTPError && !options.throwHttpErrors) {
const { response } = error;
resolve(request.options.resolveBodyOnly ? response.body : response);
return;
}
reject(error);
};
request.once('error', onError);
const previousBody = request.options.body;
request.once('retry', (newRetryCount, error) => {
var _a, _b;
if (previousBody === ((_a = error.request) === null || _a === void 0 ? void 0 : _a.options.body) && is_1.default.nodeStream((_b = error.request) === null || _b === void 0 ? void 0 : _b.options.body)) {
onError(error);
return;
}
makeRequest(newRetryCount);
});
proxy_events_1.default(request, emitter, proxiedRequestEvents);
};
makeRequest(0);
});
promise.on = (event, fn) => {
emitter.on(event, fn);
return promise;
};
const shortcut = (responseType) => {
const newPromise = (async () => {
// Wait until downloading has ended
await promise;
const { options } = globalResponse.request;
return parse_body_1.default(globalResponse, responseType, options.parseJson, options.encoding);
})();
Object.defineProperties(newPromise, Object.getOwnPropertyDescriptors(promise));
return newPromise;
};
promise.json = () => {
const { headers } = globalRequest.options;
if (!globalRequest.writableFinished && headers.accept === undefined) {
headers.accept = 'application/json';
}
return shortcut('json');
};
promise.buffer = () => shortcut('buffer');
promise.text = () => shortcut('text');
return promise;
}
exports.default = asPromise;
__exportStar(types_1, exports);
});
function createRejection(error, ...beforeErrorGroups) {
const promise = (async () => {
if (error instanceof types_1.RequestError) {
try {
for (const hooks of beforeErrorGroups) {
if (hooks) {
for (const hook of hooks) {
// eslint-disable-next-line no-await-in-loop
error = await hook(error);
}
}
}
}
catch (error_) {
error = error_;
}
}
throw error;
})();
const returnPromise = () => promise;
promise.json = returnPromise;
promise.text = returnPromise;
promise.buffer = returnPromise;
promise.on = returnPromise;
return promise;
}
var _default$7 = createRejection;
var createRejection_1 = /*#__PURE__*/Object.defineProperty({
default: _default$7
}, '__esModule', {value: true});
function deepFreeze(object) {
for (const value of Object.values(object)) {
if (is_1.default.plainObject(value) || is_1.default.array(value)) {
deepFreeze(value);
}
}
return Object.freeze(object);
}
var _default$6 = deepFreeze;
var deepFreeze_1 = /*#__PURE__*/Object.defineProperty({
default: _default$6
}, '__esModule', {value: true});
var types = /*#__PURE__*/Object.defineProperty({
}, '__esModule', {value: true});
var require$$0$1 = asPromise_1;
var create_rejection_1 = createRejection_1;
var deep_freeze_1 = deepFreeze_1;
var require$$0 = types;
var create_1$1 = createCommonjsModule(function (module, exports) {
var __createBinding = (commonjsGlobal && commonjsGlobal.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (commonjsGlobal && commonjsGlobal.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultHandler = void 0;
const errors = {
RequestError: require$$0$1.RequestError,
CacheError: require$$0$1.CacheError,
ReadError: require$$0$1.ReadError,
HTTPError: require$$0$1.HTTPError,
MaxRedirectsError: require$$0$1.MaxRedirectsError,
TimeoutError: require$$0$1.TimeoutError,
ParseError: require$$0$1.ParseError,
CancelError: require$$0$1.CancelError,
UnsupportedProtocolError: require$$0$1.UnsupportedProtocolError,
UploadError: require$$0$1.UploadError
};
// The `delay` package weighs 10KB (!)
const delay = async (ms) => new Promise(resolve => {
setTimeout(resolve, ms);
});
const { normalizeArguments } = core_1.default;
const mergeOptions = (...sources) => {
let mergedOptions;
for (const source of sources) {
mergedOptions = normalizeArguments(undefined, source, mergedOptions);
}
return mergedOptions;
};
const getPromiseOrStream = (options) => options.isStream ? new core_1.default(undefined, options) : require$$0$1.default(options);
const isGotInstance = (value) => ('defaults' in value && 'options' in value.defaults);
const aliases = [
'get',
'post',
'put',
'patch',
'head',
'delete'
];
exports.defaultHandler = (options, next) => next(options);
const callInitHooks = (hooks, options) => {
if (hooks) {
for (const hook of hooks) {
hook(options);
}
}
};
const create = (defaults) => {
// Proxy properties from next handlers
defaults._rawHandlers = defaults.handlers;
defaults.handlers = defaults.handlers.map(fn => ((options, next) => {
// This will be assigned by assigning result
let root;
const result = fn(options, newOptions => {
root = next(newOptions);
return root;
});
if (result !== root && !options.isStream && root) {
const typedResult = result;
const { then: promiseThen, catch: promiseCatch, finally: promiseFianlly } = typedResult;
Object.setPrototypeOf(typedResult, Object.getPrototypeOf(root));
Object.defineProperties(typedResult, Object.getOwnPropertyDescriptors(root));
// These should point to the new promise
// eslint-disable-next-line promise/prefer-await-to-then
typedResult.then = promiseThen;
typedResult.catch = promiseCatch;
typedResult.finally = promiseFianlly;
}
return result;
}));
// Got interface
const got = ((url, options = {}, _defaults) => {
var _a, _b;
let iteration = 0;
const iterateHandlers = (newOptions) => {
return defaults.handlers[iteration++](newOptions, iteration === defaults.handlers.length ? getPromiseOrStream : iterateHandlers);
};
// TODO: Remove this in Got 12.
if (is_1.default.plainObject(url)) {
const mergedOptions = {
...url,
...options
};
core_1.setNonEnumerableProperties([url, options], mergedOptions);
options = mergedOptions;
url = undefined;
}
try {
// Call `init` hooks
let initHookError;
try {
callInitHooks(defaults.options.hooks.init, options);
callInitHooks((_a = options.hooks) === null || _a === void 0 ? void 0 : _a.init, options);
}
catch (error) {
initHookError = error;
}
// Normalize options & call handlers
const normalizedOptions = normalizeArguments(url, options, _defaults !== null && _defaults !== void 0 ? _defaults : defaults.options);
normalizedOptions[core_1.kIsNormalizedAlready] = true;
if (initHookError) {
throw new require$$0$1.RequestError(initHookError.message, initHookError, normalizedOptions);
}
return iterateHandlers(normalizedOptions);
}
catch (error) {
if (options.isStream) {
throw error;
}
else {
return create_rejection_1.default(error, defaults.options.hooks.beforeError, (_b = options.hooks) === null || _b === void 0 ? void 0 : _b.beforeError);
}
}
});
got.extend = (...instancesOrOptions) => {
const optionsArray = [defaults.options];
let handlers = [...defaults._rawHandlers];
let isMutableDefaults;
for (const value of instancesOrOptions) {
if (isGotInstance(value)) {
optionsArray.push(value.defaults.options);
handlers.push(...value.defaults._rawHandlers);
isMutableDefaults = value.defaults.mutableDefaults;
}
else {
optionsArray.push(value);
if ('handlers' in value) {
handlers.push(...value.handlers);
}
isMutableDefaults = value.mutableDefaults;
}
}
handlers = handlers.filter(handler => handler !== exports.defaultHandler);
if (handlers.length === 0) {
handlers.push(exports.defaultHandler);
}
return create({
options: mergeOptions(...optionsArray),
handlers,
mutableDefaults: Boolean(isMutableDefaults)
});
};
// Pagination
const paginateEach = (async function* (url, options) {
// TODO: Remove this `@ts-expect-error` when upgrading to TypeScript 4.
// Error: Argument of type 'Merge<Options, PaginationOptions<T, R>> | undefined' is not assignable to parameter of type 'Options | undefined'.
// @ts-expect-error
let normalizedOptions = normalizeArguments(url, options, defaults.options);
normalizedOptions.resolveBodyOnly = false;
const pagination = normalizedOptions.pagination;
if (!is_1.default.object(pagination)) {
throw new TypeError('`options.pagination` must be implemented');
}
const all = [];
let { countLimit } = pagination;
let numberOfRequests = 0;
while (numberOfRequests < pagination.requestLimit) {
if (numberOfRequests !== 0) {
// eslint-disable-next-line no-await-in-loop
await delay(pagination.backoff);
}
// @ts-expect-error FIXME!
// TODO: Throw when result is not an instance of Response
// eslint-disable-next-line no-await-in-loop
const result = (await got(undefined, undefined, normalizedOptions));
// eslint-disable-next-line no-await-in-loop
const parsed = await pagination.transform(result);
const current = [];
for (const item of parsed) {
if (pagination.filter(item, all, current)) {
if (!pagination.shouldContinue(item, all, current)) {
return;
}
yield item;
if (pagination.stackAllItems) {
all.push(item);
}
current.push(item);
if (--countLimit <= 0) {
return;
}
}
}
const optionsToMerge = pagination.paginate(result, all, current);
if (optionsToMerge === false) {
return;
}
if (optionsToMerge === result.request.options) {
normalizedOptions = result.request.options;
}
else if (optionsToMerge !== undefined) {
normalizedOptions = normalizeArguments(undefined, optionsToMerge, normalizedOptions);
}
numberOfRequests++;
}
});
got.paginate = paginateEach;
got.paginate.all = (async (url, options) => {
const results = [];
for await (const item of paginateEach(url, options)) {
results.push(item);
}
return results;
});
// For those who like very descriptive names
got.paginate.each = paginateEach;
// Stream API
got.stream = ((url, options) => got(url, { ...options, isStream: true }));
// Shortcuts
for (const method of aliases) {
got[method] = ((url, options) => got(url, { ...options, method }));
got.stream[method] = ((url, options) => {
return got(url, { ...options, method, isStream: true });
});
}
Object.assign(got, errors);
Object.defineProperty(got, 'defaults', {
value: defaults.mutableDefaults ? defaults : deep_freeze_1.default(defaults),
writable: defaults.mutableDefaults,
configurable: defaults.mutableDefaults,
enumerable: true
});
got.mergeOptions = mergeOptions;
return got;
};
exports.default = create;
__exportStar(require$$0, exports);
});
var create_1 = create_1$1;
var source = createCommonjsModule(function (module, exports) {
var __createBinding = (commonjsGlobal && commonjsGlobal.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (commonjsGlobal && commonjsGlobal.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
const defaults = {
options: {
method: 'GET',
retry: {
limit: 2,
methods: [
'GET',
'PUT',
'HEAD',
'DELETE',
'OPTIONS',
'TRACE'
],
statusCodes: [
408,
413,
429,
500,
502,
503,
504,
521,
522,
524
],
errorCodes: [
'ETIMEDOUT',
'ECONNRESET',
'EADDRINUSE',
'ECONNREFUSED',
'EPIPE',
'ENOTFOUND',
'ENETUNREACH',
'EAI_AGAIN'
],
maxRetryAfter: undefined,
calculateDelay: ({ computedValue }) => computedValue
},
timeout: {},
headers: {
'user-agent': 'got (https://github.com/sindresorhus/got)'
},
hooks: {
init: [],
beforeRequest: [],
beforeRedirect: [],
beforeRetry: [],
beforeError: [],
afterResponse: []
},
cache: undefined,
dnsCache: undefined,
decompress: true,
throwHttpErrors: true,
followRedirect: true,
isStream: false,
responseType: 'text',
resolveBodyOnly: false,
maxRedirects: 10,
prefixUrl: '',
methodRewriting: true,
ignoreInvalidCookies: false,
context: {},
// TODO: Set this to `true` when Got 12 gets released
http2: false,
allowGetBody: false,
https: undefined,
pagination: {
transform: (response) => {
if (response.request.options.responseType === 'json') {
return response.body;
}
return JSON.parse(response.body);
},
paginate: response => {
if (!Reflect.has(response.headers, 'link')) {
return false;
}
const items = response.headers.link.split(',');
let next;
for (const item of items) {
const parsed = item.split(';');
if (parsed[1].includes('next')) {
next = parsed[0].trimStart().trim();
next = next.slice(1, -1);
break;
}
}
if (next) {
const options = {
url: new url_1__default['default'].URL(next)
};
return options;
}
return false;
},
filter: () => true,
shouldContinue: () => true,
countLimit: Infinity,
backoff: 0,
requestLimit: 10000,
stackAllItems: true
},
parseJson: (text) => JSON.parse(text),
stringifyJson: (object) => JSON.stringify(object),
cacheOptions: {}
},
handlers: [create_1.defaultHandler],
mutableDefaults: false
};
const got = create_1.default(defaults);
exports.default = got;
// For CommonJS default export support
module.exports = got;
module.exports.default = got;
module.exports.__esModule = true; // Workaround for TS issue: https://github.com/sindresorhus/got/pull/1267
__exportStar(create_1, exports);
__exportStar(require$$0$1, exports);
});
var got = /*@__PURE__*/getDefaultExportFromCjs(source);
var FsPromise = createCommonjsModule(function (module, exports) {
/**
* Module convert fs functions to promise based functions
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.readFile = exports.writeFileSync = exports.writeFile = exports.read = exports.open = exports.close = exports.stat = exports.createReadStream = exports.pathExists = void 0;
exports.pathExists = fs__default['default'].existsSync;
exports.createReadStream = fs__default['default'].createReadStream;
async function stat(path) {
return new Promise((resolve, reject) => {
fs__default['default'].stat(path, (err, stats) => {
if (err)
reject(err);
else
resolve(stats);
});
});
}
exports.stat = stat;
async function close(fd) {
return new Promise((resolve, reject) => {
fs__default['default'].close(fd, err => {
if (err)
reject(err);
else
resolve();
});
});
}
exports.close = close;
async function open(path, mode) {
return new Promise((resolve, reject) => {
fs__default['default'].open(path, mode, (err, fd) => {
if (err)
reject(err);
else
resolve(fd);
});
});
}
exports.open = open;
async function read(fd, buffer, offset, length, position) {
return new Promise((resolve, reject) => {
fs__default['default'].read(fd, buffer, offset, length, position, (err, bytesRead, _buffer) => {
if (err)
reject(err);
else
resolve({ bytesRead, buffer: _buffer });
});
});
}
exports.read = read;
async function writeFile(path, data) {
return new Promise((resolve, reject) => {
fs__default['default'].writeFile(path, data, err => {
if (err)
reject(err);
else
resolve();
});
});
}
exports.writeFile = writeFile;
function writeFileSync(path, data) {
fs__default['default'].writeFileSync(path, data);
}
exports.writeFileSync = writeFileSync;
async function readFile(path) {
return new Promise((resolve, reject) => {
fs__default['default'].readFile(path, (err, buffer) => {
if (err)
reject(err);
else
resolve(buffer);
});
});
}
exports.readFile = readFile;
});
var EndOfFileStream = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.EndOfStreamError = exports.defaultMessages = void 0;
exports.defaultMessages = 'End-Of-Stream';
/**
* Thrown on read operation of the end of file or stream has been reached
*/
class EndOfStreamError extends Error {
constructor() {
super(exports.defaultMessages);
}
}
exports.EndOfStreamError = EndOfStreamError;
});
var lib$2 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.StreamReader = exports.EndOfStreamError = void 0;
var EndOfFileStream_2 = EndOfFileStream;
Object.defineProperty(exports, "EndOfStreamError", { enumerable: true, get: function () { return EndOfFileStream_2.EndOfStreamError; } });
class Deferred {
constructor() {
this.resolve = () => null;
this.reject = () => null;
this.promise = new Promise((resolve, reject) => {
this.reject = reject;
this.resolve = resolve;
});
}
}
const maxStreamReadSize = 1 * 1024 * 1024; // Maximum request length on read-stream operation
class StreamReader {
constructor(s) {
this.s = s;
/**
* Deferred read request
*/
this.request = null;
this.endOfStream = false;
/**
* Store peeked data
* @type {Array}
*/
this.peekQueue = [];
if (!s.read || !s.once) {
throw new Error('Expected an instance of stream.Readable');
}
this.s.once('end', () => this.reject(new EndOfFileStream.EndOfStreamError()));
this.s.once('error', err => this.reject(err));
this.s.once('close', () => this.reject(new Error('Stream closed')));
}
/**
* Read ahead (peek) from stream. Subsequent read or peeks will return the same data
* @param uint8Array - Uint8Array (or Buffer) to store data read from stream in
* @param offset - Offset target
* @param length - Number of bytes to read
* @returns Number of bytes peeked
*/
async peek(uint8Array, offset, length) {
const bytesRead = await this.read(uint8Array, offset, length);
this.peekQueue.push(uint8Array.subarray(offset, offset + bytesRead)); // Put read data back to peek buffer
return bytesRead;
}
/**
* Read chunk from stream
* @param buffer - Target Uint8Array (or Buffer) to store data read from stream in
* @param offset - Offset target
* @param length - Number of bytes to read
* @returns Number of bytes read
*/
async read(buffer, offset, length) {
if (length === 0) {
return 0;
}
if (this.peekQueue.length === 0 && this.endOfStream) {
throw new EndOfFileStream.EndOfStreamError();
}
let remaining = length;
let bytesRead = 0;
// consume peeked data first
while (this.peekQueue.length > 0 && remaining > 0) {
const peekData = this.peekQueue.pop(); // Front of queue
if (!peekData)
throw new Error('peekData should be defined');
const lenCopy = Math.min(peekData.length, remaining);
buffer.set(peekData.subarray(0, lenCopy), offset + bytesRead);
bytesRead += lenCopy;
remaining -= lenCopy;
if (lenCopy < peekData.length) {
// remainder back to queue
this.peekQueue.push(peekData.subarray(lenCopy));
}
}
// continue reading from stream if required
while (remaining > 0 && !this.endOfStream) {
const reqLen = Math.min(remaining, maxStreamReadSize);
const chunkLen = await this._read(buffer, offset + bytesRead, reqLen);
bytesRead += chunkLen;
if (chunkLen < reqLen)
break;
remaining -= chunkLen;
}
return bytesRead;
}
/**
* Read chunk from stream
* @param buffer Target Uint8Array (or Buffer) to store data read from stream in
* @param offset Offset target
* @param length Number of bytes to read
* @returns Number of bytes read
*/
async _read(buffer, offset, length) {
if (this.request)
throw new Error('Concurrent read operation?');
const readBuffer = this.s.read(length);
if (readBuffer) {
buffer.set(readBuffer, offset);
return readBuffer.length;
}
else {
this.request = {
buffer,
offset,
length,
deferred: new Deferred()
};
this.s.once('readable', () => {
this.tryRead();
});
return this.request.deferred.promise;
}
}
tryRead() {
if (!this.request)
throw new Error('this.request should be defined');
const readBuffer = this.s.read(this.request.length);
if (readBuffer) {
this.request.buffer.set(readBuffer, this.request.offset);
this.request.deferred.resolve(readBuffer.length);
this.request = null;
}
else {
this.s.once('readable', () => {
this.tryRead();
});
}
}
reject(err) {
this.endOfStream = true;
if (this.request) {
this.request.deferred.reject(err);
this.request = null;
}
}
}
exports.StreamReader = StreamReader;
});
var AbstractTokenizer_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractTokenizer = void 0;
/**
* Core tokenizer
*/
class AbstractTokenizer {
constructor(fileInfo) {
/**
* Tokenizer-stream position
*/
this.position = 0;
this.numBuffer = new Uint8Array(8);
this.fileInfo = fileInfo ? fileInfo : {};
}
/**
* Read a token from the tokenizer-stream
* @param token - The token to read
* @param position - If provided, the desired position in the tokenizer-stream
* @returns Promise with token data
*/
async readToken(token, position = this.position) {
const uint8Array = Buffer.alloc(token.len);
const len = await this.readBuffer(uint8Array, { position });
if (len < token.len)
throw new lib$2.EndOfStreamError();
return token.get(uint8Array, 0);
}
/**
* Peek a token from the tokenizer-stream.
* @param token - Token to peek from the tokenizer-stream.
* @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
* @returns Promise with token data
*/
async peekToken(token, position = this.position) {
const uint8Array = Buffer.alloc(token.len);
const len = await this.peekBuffer(uint8Array, { position });
if (len < token.len)
throw new lib$2.EndOfStreamError();
return token.get(uint8Array, 0);
}
/**
* Read a numeric token from the stream
* @param token - Numeric token
* @returns Promise with number
*/
async readNumber(token) {
const len = await this.readBuffer(this.numBuffer, { length: token.len });
if (len < token.len)
throw new lib$2.EndOfStreamError();
return token.get(this.numBuffer, 0);
}
/**
* Read a numeric token from the stream
* @param token - Numeric token
* @returns Promise with number
*/
async peekNumber(token) {
const len = await this.peekBuffer(this.numBuffer, { length: token.len });
if (len < token.len)
throw new lib$2.EndOfStreamError();
return token.get(this.numBuffer, 0);
}
/**
* Ignore number of bytes, advances the pointer in under tokenizer-stream.
* @param length - Number of bytes to ignore
* @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
*/
async ignore(length) {
if (this.fileInfo.size !== undefined) {
const bytesLeft = this.fileInfo.size - this.position;
if (length > bytesLeft) {
this.position += bytesLeft;
return bytesLeft;
}
}
this.position += length;
return length;
}
async close() {
// empty
}
normalizeOptions(uint8Array, options) {
if (options && options.position !== undefined && options.position < this.position) {
throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
}
if (options) {
return {
mayBeLess: options.mayBeLess === true,
offset: options.offset ? options.offset : 0,
length: options.length ? options.length : (uint8Array.length - (options.offset ? options.offset : 0)),
position: options.position ? options.position : this.position
};
}
return {
mayBeLess: false,
offset: 0,
length: uint8Array.length,
position: this.position
};
}
}
exports.AbstractTokenizer = AbstractTokenizer;
});
var ReadStreamTokenizer_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReadStreamTokenizer = void 0;
const maxBufferSize = 256000;
class ReadStreamTokenizer extends AbstractTokenizer_1.AbstractTokenizer {
constructor(stream, fileInfo) {
super(fileInfo);
this.streamReader = new lib$2.StreamReader(stream);
}
/**
* Get file information, an HTTP-client may implement this doing a HEAD request
* @return Promise with file information
*/
async getFileInfo() {
return this.fileInfo;
}
/**
* Read buffer from tokenizer
* @param uint8Array - Target Uint8Array to fill with data read from the tokenizer-stream
* @param options - Read behaviour options
* @returns Promise with number of bytes read
*/
async readBuffer(uint8Array, options) {
const normOptions = this.normalizeOptions(uint8Array, options);
const skipBytes = normOptions.position - this.position;
if (skipBytes > 0) {
await this.ignore(skipBytes);
return this.readBuffer(uint8Array, options);
}
else if (skipBytes < 0) {
throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
}
if (normOptions.length === 0) {
return 0;
}
const bytesRead = await this.streamReader.read(uint8Array, normOptions.offset, normOptions.length);
this.position += bytesRead;
if ((!options || !options.mayBeLess) && bytesRead < normOptions.length) {
throw new lib$2.EndOfStreamError();
}
return bytesRead;
}
/**
* Peek (read ahead) buffer from tokenizer
* @param uint8Array - Uint8Array (or Buffer) to write data to
* @param options - Read behaviour options
* @returns Promise with number of bytes peeked
*/
async peekBuffer(uint8Array, options) {
const normOptions = this.normalizeOptions(uint8Array, options);
let bytesRead = 0;
if (normOptions.position) {
const skipBytes = normOptions.position - this.position;
if (skipBytes > 0) {
const skipBuffer = new Uint8Array(normOptions.length + skipBytes);
bytesRead = await this.peekBuffer(skipBuffer, { mayBeLess: normOptions.mayBeLess });
uint8Array.set(skipBuffer.subarray(skipBytes), normOptions.offset);
return bytesRead - skipBytes;
}
else if (skipBytes < 0) {
throw new Error('Cannot peek from a negative offset in a stream');
}
}
if (normOptions.length > 0) {
try {
bytesRead = await this.streamReader.peek(uint8Array, normOptions.offset, normOptions.length);
}
catch (err) {
if (options && options.mayBeLess && err instanceof lib$2.EndOfStreamError) {
return 0;
}
throw err;
}
if ((!normOptions.mayBeLess) && bytesRead < normOptions.length) {
throw new lib$2.EndOfStreamError();
}
}
return bytesRead;
}
async ignore(length) {
// debug(`ignore ${this.position}...${this.position + length - 1}`);
const bufSize = Math.min(maxBufferSize, length);
const buf = new Uint8Array(bufSize);
let totBytesRead = 0;
while (totBytesRead < length) {
const remaining = length - totBytesRead;
const bytesRead = await this.readBuffer(buf, { length: Math.min(bufSize, remaining) });
if (bytesRead < 0) {
return bytesRead;
}
totBytesRead += bytesRead;
}
return totBytesRead;
}
}
exports.ReadStreamTokenizer = ReadStreamTokenizer;
});
var BufferTokenizer_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.BufferTokenizer = void 0;
class BufferTokenizer extends AbstractTokenizer_1.AbstractTokenizer {
/**
* Construct BufferTokenizer
* @param uint8Array - Uint8Array to tokenize
* @param fileInfo - Pass additional file information to the tokenizer
*/
constructor(uint8Array, fileInfo) {
super(fileInfo);
this.uint8Array = uint8Array;
this.fileInfo.size = this.fileInfo.size ? this.fileInfo.size : uint8Array.length;
}
/**
* Read buffer from tokenizer
* @param uint8Array - Uint8Array to tokenize
* @param options - Read behaviour options
* @returns {Promise<number>}
*/
async readBuffer(uint8Array, options) {
if (options && options.position) {
if (options.position < this.position) {
throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
}
this.position = options.position;
}
const bytesRead = await this.peekBuffer(uint8Array, options);
this.position += bytesRead;
return bytesRead;
}
/**
* Peek (read ahead) buffer from tokenizer
* @param uint8Array
* @param options - Read behaviour options
* @returns {Promise<number>}
*/
async peekBuffer(uint8Array, options) {
const normOptions = this.normalizeOptions(uint8Array, options);
const bytes2read = Math.min(this.uint8Array.length - normOptions.position, normOptions.length);
if ((!normOptions.mayBeLess) && bytes2read < normOptions.length) {
throw new lib$2.EndOfStreamError();
}
else {
uint8Array.set(this.uint8Array.subarray(normOptions.position, normOptions.position + bytes2read), normOptions.offset);
return bytes2read;
}
}
async close() {
// empty
}
}
exports.BufferTokenizer = BufferTokenizer;
});
var core$1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromBuffer = exports.fromStream = exports.EndOfStreamError = void 0;
Object.defineProperty(exports, "EndOfStreamError", { enumerable: true, get: function () { return lib$2.EndOfStreamError; } });
/**
* Construct ReadStreamTokenizer from given Stream.
* Will set fileSize, if provided given Stream has set the .path property/
* @param stream - Read from Node.js Stream.Readable
* @param fileInfo - Pass the file information, like size and MIME-type of the correspnding stream.
* @returns ReadStreamTokenizer
*/
function fromStream(stream, fileInfo) {
fileInfo = fileInfo ? fileInfo : {};
return new ReadStreamTokenizer_1.ReadStreamTokenizer(stream, fileInfo);
}
exports.fromStream = fromStream;
/**
* Construct ReadStreamTokenizer from given Buffer.
* @param uint8Array - Uint8Array to tokenize
* @param fileInfo - Pass additional file information to the tokenizer
* @returns BufferTokenizer
*/
function fromBuffer(uint8Array, fileInfo) {
return new BufferTokenizer_1.BufferTokenizer(uint8Array, fileInfo);
}
exports.fromBuffer = fromBuffer;
});
var FileTokenizer_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromFile = exports.FileTokenizer = void 0;
class FileTokenizer extends AbstractTokenizer_1.AbstractTokenizer {
constructor(fd, fileInfo) {
super(fileInfo);
this.fd = fd;
}
/**
* Read buffer from file
* @param uint8Array - Uint8Array to write result to
* @param options - Read behaviour options
* @returns Promise number of bytes read
*/
async readBuffer(uint8Array, options) {
const normOptions = this.normalizeOptions(uint8Array, options);
this.position = normOptions.position;
const res = await FsPromise.read(this.fd, uint8Array, normOptions.offset, normOptions.length, normOptions.position);
this.position += res.bytesRead;
if (res.bytesRead < normOptions.length && (!options || !options.mayBeLess)) {
throw new lib$2.EndOfStreamError();
}
return res.bytesRead;
}
/**
* Peek buffer from file
* @param uint8Array - Uint8Array (or Buffer) to write data to
* @param options - Read behaviour options
* @returns Promise number of bytes read
*/
async peekBuffer(uint8Array, options) {
const normOptions = this.normalizeOptions(uint8Array, options);
const res = await FsPromise.read(this.fd, uint8Array, normOptions.offset, normOptions.length, normOptions.position);
if ((!normOptions.mayBeLess) && res.bytesRead < normOptions.length) {
throw new lib$2.EndOfStreamError();
}
return res.bytesRead;
}
async close() {
return FsPromise.close(this.fd);
}
}
exports.FileTokenizer = FileTokenizer;
async function fromFile(sourceFilePath) {
const stat = await FsPromise.stat(sourceFilePath);
if (!stat.isFile) {
throw new Error(`File not a file: ${sourceFilePath}`);
}
const fd = await FsPromise.open(sourceFilePath, 'r');
return new FileTokenizer(fd, { path: sourceFilePath, size: stat.size });
}
exports.fromFile = fromFile;
});
var lib$1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromStream = exports.fromBuffer = exports.EndOfStreamError = exports.fromFile = void 0;
Object.defineProperty(exports, "fromFile", { enumerable: true, get: function () { return FileTokenizer_1.fromFile; } });
var core_1 = core$1;
Object.defineProperty(exports, "EndOfStreamError", { enumerable: true, get: function () { return core_1.EndOfStreamError; } });
Object.defineProperty(exports, "fromBuffer", { enumerable: true, get: function () { return core_1.fromBuffer; } });
/**
* Construct ReadStreamTokenizer from given Stream.
* Will set fileSize, if provided given Stream has set the .path property.
* @param stream - Node.js Stream.Readable
* @param fileInfo - Pass additional file information to the tokenizer
* @returns Tokenizer
*/
async function fromStream(stream, fileInfo) {
fileInfo = fileInfo ? fileInfo : {};
if (stream.path) {
const stat = await FsPromise.stat(stream.path);
fileInfo.path = stream.path;
fileInfo.size = stat.size;
}
return core$1.fromStream(stream, fileInfo);
}
exports.fromStream = fromStream;
});
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
var read = function (buffer, offset, isLE, mLen, nBytes) {
var e, m;
var eLen = (nBytes * 8) - mLen - 1;
var eMax = (1 << eLen) - 1;
var eBias = eMax >> 1;
var nBits = -7;
var i = isLE ? (nBytes - 1) : 0;
var d = isLE ? -1 : 1;
var s = buffer[offset + i];
i += d;
e = s & ((1 << (-nBits)) - 1);
s >>= (-nBits);
nBits += eLen;
for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {}
m = e & ((1 << (-nBits)) - 1);
e >>= (-nBits);
nBits += mLen;
for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {}
if (e === 0) {
e = 1 - eBias;
} else if (e === eMax) {
return m ? NaN : ((s ? -1 : 1) * Infinity)
} else {
m = m + Math.pow(2, mLen);
e = e - eBias;
}
return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
};
var write = function (buffer, value, offset, isLE, mLen, nBytes) {
var e, m, c;
var eLen = (nBytes * 8) - mLen - 1;
var eMax = (1 << eLen) - 1;
var eBias = eMax >> 1;
var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0);
var i = isLE ? 0 : (nBytes - 1);
var d = isLE ? 1 : -1;
var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
value = Math.abs(value);
if (isNaN(value) || value === Infinity) {
m = isNaN(value) ? 1 : 0;
e = eMax;
} else {
e = Math.floor(Math.log(value) / Math.LN2);
if (value * (c = Math.pow(2, -e)) < 1) {
e--;
c *= 2;
}
if (e + eBias >= 1) {
value += rt / c;
} else {
value += rt * Math.pow(2, 1 - eBias);
}
if (value * c >= 2) {
e++;
c /= 2;
}
if (e + eBias >= eMax) {
m = 0;
e = eMax;
} else if (e + eBias >= 1) {
m = ((value * c) - 1) * Math.pow(2, mLen);
e = e + eBias;
} else {
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
e = 0;
}
}
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
e = (e << mLen) | m;
eLen += mLen;
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
buffer[offset + i - d] |= s * 128;
};
var ieee754 = {
read: read,
write: write
};
var lib = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnsiStringType = exports.StringType = exports.BufferType = exports.Uint8ArrayType = exports.IgnoreType = exports.Float80_LE = exports.Float80_BE = exports.Float64_LE = exports.Float64_BE = exports.Float32_LE = exports.Float32_BE = exports.Float16_LE = exports.Float16_BE = exports.INT64_BE = exports.UINT64_BE = exports.INT64_LE = exports.UINT64_LE = exports.INT32_LE = exports.INT32_BE = exports.INT24_BE = exports.INT24_LE = exports.INT16_LE = exports.INT16_BE = exports.INT8 = exports.UINT32_BE = exports.UINT32_LE = exports.UINT24_BE = exports.UINT24_LE = exports.UINT16_BE = exports.UINT16_LE = exports.UINT8 = void 0;
// Primitive types
function dv(array) {
return new DataView(array.buffer, array.byteOffset);
}
/**
* 8-bit unsigned integer
*/
exports.UINT8 = {
len: 1,
get(array, offset) {
return dv(array).getUint8(offset);
},
put(array, offset, value) {
dv(array).setUint8(offset, value);
return offset + 1;
}
};
/**
* 16-bit unsigned integer, Little Endian byte order
*/
exports.UINT16_LE = {
len: 2,
get(array, offset) {
return dv(array).getUint16(offset, true);
},
put(array, offset, value) {
dv(array).setUint16(offset, value, true);
return offset + 2;
}
};
/**
* 16-bit unsigned integer, Big Endian byte order
*/
exports.UINT16_BE = {
len: 2,
get(array, offset) {
return dv(array).getUint16(offset);
},
put(array, offset, value) {
dv(array).setUint16(offset, value);
return offset + 2;
}
};
/**
* 24-bit unsigned integer, Little Endian byte order
*/
exports.UINT24_LE = {
len: 3,
get(array, offset) {
const dataView = dv(array);
return dataView.getUint8(offset) + (dataView.getUint16(offset + 1, true) << 8);
},
put(array, offset, value) {
const dataView = dv(array);
dataView.setUint8(offset, value & 0xff);
dataView.setUint16(offset + 1, value >> 8, true);
return offset + 3;
}
};
/**
* 24-bit unsigned integer, Big Endian byte order
*/
exports.UINT24_BE = {
len: 3,
get(array, offset) {
const dataView = dv(array);
return (dataView.getUint16(offset) << 8) + dataView.getUint8(offset + 2);
},
put(array, offset, value) {
const dataView = dv(array);
dataView.setUint16(offset, value >> 8);
dataView.setUint8(offset + 2, value & 0xff);
return offset + 3;
}
};
/**
* 32-bit unsigned integer, Little Endian byte order
*/
exports.UINT32_LE = {
len: 4,
get(array, offset) {
return dv(array).getUint32(offset, true);
},
put(array, offset, value) {
dv(array).setUint32(offset, value, true);
return offset + 4;
}
};
/**
* 32-bit unsigned integer, Big Endian byte order
*/
exports.UINT32_BE = {
len: 4,
get(array, offset) {
return dv(array).getUint32(offset);
},
put(array, offset, value) {
dv(array).setUint32(offset, value);
return offset + 4;
}
};
/**
* 8-bit signed integer
*/
exports.INT8 = {
len: 1,
get(array, offset) {
return dv(array).getInt8(offset);
},
put(array, offset, value) {
dv(array).setInt8(offset, value);
return offset + 2;
}
};
/**
* 16-bit signed integer, Big Endian byte order
*/
exports.INT16_BE = {
len: 2,
get(array, offset) {
return dv(array).getInt16(offset);
},
put(array, offset, value) {
dv(array).setInt16(offset, value);
return offset + 2;
}
};
/**
* 16-bit signed integer, Little Endian byte order
*/
exports.INT16_LE = {
len: 2,
get(array, offset) {
return dv(array).getInt16(offset, true);
},
put(array, offset, value) {
dv(array).setInt16(offset, value, true);
return offset + 2;
}
};
/**
* 24-bit signed integer, Little Endian byte order
*/
exports.INT24_LE = {
len: 3,
get(array, offset) {
const unsigned = exports.UINT24_LE.get(array, offset);
return unsigned > 0x7fffff ? unsigned - 0x1000000 : unsigned;
},
put(array, offset, value) {
const dataView = dv(array);
dataView.setUint8(offset, value & 0xff);
dataView.setUint16(offset + 1, value >> 8, true);
return offset + 3;
}
};
/**
* 24-bit signed integer, Big Endian byte order
*/
exports.INT24_BE = {
len: 3,
get(array, offset) {
const unsigned = exports.UINT24_BE.get(array, offset);
return unsigned > 0x7fffff ? unsigned - 0x1000000 : unsigned;
},
put(array, offset, value) {
const dataView = dv(array);
dataView.setUint16(offset, value >> 8);
dataView.setUint8(offset + 2, value & 0xff);
return offset + 3;
}
};
/**
* 32-bit signed integer, Big Endian byte order
*/
exports.INT32_BE = {
len: 4,
get(array, offset) {
return dv(array).getInt32(offset);
},
put(array, offset, value) {
dv(array).setInt32(offset, value);
return offset + 4;
}
};
/**
* 32-bit signed integer, Big Endian byte order
*/
exports.INT32_LE = {
len: 4,
get(array, offset) {
return dv(array).getInt32(offset, true);
},
put(array, offset, value) {
dv(array).setInt32(offset, value, true);
return offset + 4;
}
};
/**
* 64-bit unsigned integer, Little Endian byte order
*/
exports.UINT64_LE = {
len: 8,
get(array, offset) {
return dv(array).getBigUint64(offset, true);
},
put(array, offset, value) {
dv(array).setBigUint64(offset, value, true);
return offset + 8;
}
};
/**
* 64-bit signed integer, Little Endian byte order
*/
exports.INT64_LE = {
len: 8,
get(array, offset) {
return dv(array).getBigInt64(offset, true);
},
put(array, offset, value) {
dv(array).setBigInt64(offset, value, true);
return offset + 8;
}
};
/**
* 64-bit unsigned integer, Big Endian byte order
*/
exports.UINT64_BE = {
len: 8,
get(array, offset) {
return dv(array).getBigUint64(offset);
},
put(array, offset, value) {
dv(array).setBigUint64(offset, value);
return offset + 8;
}
};
/**
* 64-bit signed integer, Big Endian byte order
*/
exports.INT64_BE = {
len: 8,
get(array, offset) {
return dv(array).getBigInt64(offset);
},
put(array, offset, value) {
dv(array).setBigInt64(offset, value);
return offset + 8;
}
};
/**
* IEEE 754 16-bit (half precision) float, big endian
*/
exports.Float16_BE = {
len: 2,
get(dataView, offset) {
return ieee754.read(dataView, offset, false, 10, this.len);
},
put(dataView, offset, value) {
ieee754.write(dataView, value, offset, false, 10, this.len);
return offset + this.len;
}
};
/**
* IEEE 754 16-bit (half precision) float, little endian
*/
exports.Float16_LE = {
len: 2,
get(array, offset) {
return ieee754.read(array, offset, true, 10, this.len);
},
put(array, offset, value) {
ieee754.write(array, value, offset, true, 10, this.len);
return offset + this.len;
}
};
/**
* IEEE 754 32-bit (single precision) float, big endian
*/
exports.Float32_BE = {
len: 4,
get(array, offset) {
return dv(array).getFloat32(offset);
},
put(array, offset, value) {
dv(array).setFloat32(offset, value);
return offset + 4;
}
};
/**
* IEEE 754 32-bit (single precision) float, little endian
*/
exports.Float32_LE = {
len: 4,
get(array, offset) {
return dv(array).getFloat32(offset, true);
},
put(array, offset, value) {
dv(array).setFloat32(offset, value, true);
return offset + 4;
}
};
/**
* IEEE 754 64-bit (double precision) float, big endian
*/
exports.Float64_BE = {
len: 8,
get(array, offset) {
return dv(array).getFloat64(offset);
},
put(array, offset, value) {
dv(array).setFloat64(offset, value);
return offset + 8;
}
};
/**
* IEEE 754 64-bit (double precision) float, little endian
*/
exports.Float64_LE = {
len: 8,
get(array, offset) {
return dv(array).getFloat64(offset, true);
},
put(array, offset, value) {
dv(array).setFloat64(offset, value, true);
return offset + 8;
}
};
/**
* IEEE 754 80-bit (extended precision) float, big endian
*/
exports.Float80_BE = {
len: 10,
get(array, offset) {
return ieee754.read(array, offset, false, 63, this.len);
},
put(array, offset, value) {
ieee754.write(array, value, offset, false, 63, this.len);
return offset + this.len;
}
};
/**
* IEEE 754 80-bit (extended precision) float, little endian
*/
exports.Float80_LE = {
len: 10,
get(array, offset) {
return ieee754.read(array, offset, true, 63, this.len);
},
put(array, offset, value) {
ieee754.write(array, value, offset, true, 63, this.len);
return offset + this.len;
}
};
/**
* Ignore a given number of bytes
*/
class IgnoreType {
/**
* @param len number of bytes to ignore
*/
constructor(len) {
this.len = len;
}
// ToDo: don't read, but skip data
get(array, off) {
}
}
exports.IgnoreType = IgnoreType;
class Uint8ArrayType {
constructor(len) {
this.len = len;
}
get(array, offset) {
return array.subarray(offset, offset + this.len);
}
}
exports.Uint8ArrayType = Uint8ArrayType;
class BufferType {
constructor(len) {
this.len = len;
}
get(buffer, off) {
return buffer.slice(off, off + this.len);
}
}
exports.BufferType = BufferType;
/**
* Consume a fixed number of bytes from the stream and return a string with a specified encoding.
*/
class StringType {
constructor(len, encoding) {
this.len = len;
this.encoding = encoding;
}
get(uint8Array, offset) {
return Buffer.from(uint8Array).toString(this.encoding, offset, offset + this.len);
}
}
exports.StringType = StringType;
/**
* ANSI Latin 1 String
* Using windows-1252 / ISO 8859-1 decoding
*/
class AnsiStringType {
constructor(len) {
this.len = len;
}
static decode(buffer, offset, until) {
let str = '';
for (let i = offset; i < until; ++i) {
str += AnsiStringType.codePointToString(AnsiStringType.singleByteDecoder(buffer[i]));
}
return str;
}
static inRange(a, min, max) {
return min <= a && a <= max;
}
static codePointToString(cp) {
if (cp <= 0xFFFF) {
return String.fromCharCode(cp);
}
else {
cp -= 0x10000;
return String.fromCharCode((cp >> 10) + 0xD800, (cp & 0x3FF) + 0xDC00);
}
}
static singleByteDecoder(bite) {
if (AnsiStringType.inRange(bite, 0x00, 0x7F)) {
return bite;
}
const codePoint = AnsiStringType.windows1252[bite - 0x80];
if (codePoint === null) {
throw Error('invaliding encoding');
}
return codePoint;
}
get(buffer, offset = 0) {
return AnsiStringType.decode(buffer, offset, offset + this.len);
}
}
exports.AnsiStringType = AnsiStringType;
AnsiStringType.windows1252 = [8364, 129, 8218, 402, 8222, 8230, 8224, 8225, 710, 8240, 352,
8249, 338, 141, 381, 143, 144, 8216, 8217, 8220, 8221, 8226, 8211, 8212, 732,
8482, 353, 8250, 339, 157, 382, 376, 160, 161, 162, 163, 164, 165, 166, 167, 168,
169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200,
201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216,
217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
248, 249, 250, 251, 252, 253, 254, 255];
});
var stringToBytes$1 = string => [...string].map(character => character.charCodeAt(0));
/**
Checks whether the TAR checksum is valid.
@param {Buffer} buffer - The TAR header `[offset ... offset + 512]`.
@param {number} offset - TAR header offset.
@returns {boolean} `true` if the TAR checksum is valid, otherwise `false`.
*/
var tarHeaderChecksumMatches$1 = (buffer, offset = 0) => {
const readSum = parseInt(buffer.toString('utf8', 148, 154).replace(/\0.*$/, '').trim(), 8); // Read sum in header
if (isNaN(readSum)) {
return false;
}
let sum = 8 * 0x20; // Initialize signed bit sum
for (let i = offset; i < offset + 148; i++) {
sum += buffer[i];
}
for (let i = offset + 156; i < offset + 512; i++) {
sum += buffer[i];
}
return readSum === sum;
};
/**
ID3 UINT32 sync-safe tokenizer token.
28 bits (representing up to 256MB) integer, the msb is 0 to avoid "false syncsignals".
*/
var uint32SyncSafeToken$1 = {
get: (buffer, offset) => {
return (buffer[offset + 3] & 0x7F) | ((buffer[offset + 2]) << 7) | ((buffer[offset + 1]) << 14) | ((buffer[offset]) << 21);
},
len: 4
};
var util$1 = {
stringToBytes: stringToBytes$1,
tarHeaderChecksumMatches: tarHeaderChecksumMatches$1,
uint32SyncSafeToken: uint32SyncSafeToken$1
};
var supported = {
extensions: [
'jpg',
'png',
'apng',
'gif',
'webp',
'flif',
'xcf',
'cr2',
'cr3',
'orf',
'arw',
'dng',
'nef',
'rw2',
'raf',
'tif',
'bmp',
'icns',
'jxr',
'psd',
'indd',
'zip',
'tar',
'rar',
'gz',
'bz2',
'7z',
'dmg',
'mp4',
'mid',
'mkv',
'webm',
'mov',
'avi',
'mpg',
'mp2',
'mp3',
'm4a',
'oga',
'ogg',
'ogv',
'opus',
'flac',
'wav',
'spx',
'amr',
'pdf',
'epub',
'exe',
'swf',
'rtf',
'wasm',
'woff',
'woff2',
'eot',
'ttf',
'otf',
'ico',
'flv',
'ps',
'xz',
'sqlite',
'nes',
'crx',
'xpi',
'cab',
'deb',
'ar',
'rpm',
'Z',
'lz',
'cfb',
'mxf',
'mts',
'blend',
'bpg',
'docx',
'pptx',
'xlsx',
'3gp',
'3g2',
'jp2',
'jpm',
'jpx',
'mj2',
'aif',
'qcp',
'odt',
'ods',
'odp',
'xml',
'mobi',
'heic',
'cur',
'ktx',
'ape',
'wv',
'dcm',
'ics',
'glb',
'pcap',
'dsf',
'lnk',
'alias',
'voc',
'ac3',
'm4v',
'm4p',
'm4b',
'f4v',
'f4p',
'f4b',
'f4a',
'mie',
'asf',
'ogm',
'ogx',
'mpc',
'arrow',
'shp',
'aac',
'mp1',
'it',
's3m',
'xm',
'ai',
'skp',
'avif',
'eps',
'lzh',
'pgp',
'asar',
'stl',
'chm',
'3mf',
'zst',
'jxl',
'vcf'
],
mimeTypes: [
'image/jpeg',
'image/png',
'image/gif',
'image/webp',
'image/flif',
'image/x-xcf',
'image/x-canon-cr2',
'image/x-canon-cr3',
'image/tiff',
'image/bmp',
'image/vnd.ms-photo',
'image/vnd.adobe.photoshop',
'application/x-indesign',
'application/epub+zip',
'application/x-xpinstall',
'application/vnd.oasis.opendocument.text',
'application/vnd.oasis.opendocument.spreadsheet',
'application/vnd.oasis.opendocument.presentation',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/zip',
'application/x-tar',
'application/x-rar-compressed',
'application/gzip',
'application/x-bzip2',
'application/x-7z-compressed',
'application/x-apple-diskimage',
'application/x-apache-arrow',
'video/mp4',
'audio/midi',
'video/x-matroska',
'video/webm',
'video/quicktime',
'video/vnd.avi',
'audio/vnd.wave',
'audio/qcelp',
'audio/x-ms-asf',
'video/x-ms-asf',
'application/vnd.ms-asf',
'video/mpeg',
'video/3gpp',
'audio/mpeg',
'audio/mp4', // RFC 4337
'audio/opus',
'video/ogg',
'audio/ogg',
'application/ogg',
'audio/x-flac',
'audio/ape',
'audio/wavpack',
'audio/amr',
'application/pdf',
'application/x-msdownload',
'application/x-shockwave-flash',
'application/rtf',
'application/wasm',
'font/woff',
'font/woff2',
'application/vnd.ms-fontobject',
'font/ttf',
'font/otf',
'image/x-icon',
'video/x-flv',
'application/postscript',
'application/eps',
'application/x-xz',
'application/x-sqlite3',
'application/x-nintendo-nes-rom',
'application/x-google-chrome-extension',
'application/vnd.ms-cab-compressed',
'application/x-deb',
'application/x-unix-archive',
'application/x-rpm',
'application/x-compress',
'application/x-lzip',
'application/x-cfb',
'application/x-mie',
'application/mxf',
'video/mp2t',
'application/x-blender',
'image/bpg',
'image/jp2',
'image/jpx',
'image/jpm',
'image/mj2',
'audio/aiff',
'application/xml',
'application/x-mobipocket-ebook',
'image/heif',
'image/heif-sequence',
'image/heic',
'image/heic-sequence',
'image/icns',
'image/ktx',
'application/dicom',
'audio/x-musepack',
'text/calendar',
'text/vcard',
'model/gltf-binary',
'application/vnd.tcpdump.pcap',
'audio/x-dsf', // Non-standard
'application/x.ms.shortcut', // Invented by us
'application/x.apple.alias', // Invented by us
'audio/x-voc',
'audio/vnd.dolby.dd-raw',
'audio/x-m4a',
'image/apng',
'image/x-olympus-orf',
'image/x-sony-arw',
'image/x-adobe-dng',
'image/x-nikon-nef',
'image/x-panasonic-rw2',
'image/x-fujifilm-raf',
'video/x-m4v',
'video/3gpp2',
'application/x-esri-shape',
'audio/aac',
'audio/x-it',
'audio/x-s3m',
'audio/x-xm',
'video/MP1S',
'video/MP2P',
'application/vnd.sketchup.skp',
'image/avif',
'application/x-lzh-compressed',
'application/pgp-encrypted',
'application/x-asar',
'model/stl',
'application/vnd.ms-htmlhelp',
'model/3mf',
'image/jxl',
'application/zstd'
]
};
const {
stringToBytes,
tarHeaderChecksumMatches,
uint32SyncSafeToken
} = util$1;
const minimumBytes = 4100; // A fair amount of file-types are detectable within this range
async function fromStream(stream) {
const tokenizer = await core$1.fromStream(stream);
try {
return await fromTokenizer(tokenizer);
} finally {
await tokenizer.close();
}
}
async function fromBuffer(input) {
if (!(input instanceof Uint8Array || input instanceof ArrayBuffer || Buffer.isBuffer(input))) {
throw new TypeError(`Expected the \`input\` argument to be of type \`Uint8Array\` or \`Buffer\` or \`ArrayBuffer\`, got \`${typeof input}\``);
}
const buffer = input instanceof Buffer ? input : Buffer.from(input);
if (!(buffer && buffer.length > 1)) {
return;
}
const tokenizer = core$1.fromBuffer(buffer);
return fromTokenizer(tokenizer);
}
function _check(buffer, headers, options) {
options = {
offset: 0,
...options
};
for (const [index, header] of headers.entries()) {
// If a bitmask is set
if (options.mask) {
// If header doesn't equal `buf` with bits masked off
if (header !== (options.mask[index] & buffer[index + options.offset])) {
return false;
}
} else if (header !== buffer[index + options.offset]) {
return false;
}
}
return true;
}
async function fromTokenizer(tokenizer) {
try {
return _fromTokenizer(tokenizer);
} catch (error) {
if (!(error instanceof core$1.EndOfStreamError)) {
throw error;
}
}
}
async function _fromTokenizer(tokenizer) {
let buffer = Buffer.alloc(minimumBytes);
const bytesRead = 12;
const check = (header, options) => _check(buffer, header, options);
const checkString = (header, options) => check(stringToBytes(header), options);
// Keep reading until EOF if the file size is unknown.
if (!tokenizer.fileInfo.size) {
tokenizer.fileInfo.size = Number.MAX_SAFE_INTEGER;
}
await tokenizer.peekBuffer(buffer, {length: bytesRead, mayBeLess: true});
// -- 2-byte signatures --
if (check([0x42, 0x4D])) {
return {
ext: 'bmp',
mime: 'image/bmp'
};
}
if (check([0x0B, 0x77])) {
return {
ext: 'ac3',
mime: 'audio/vnd.dolby.dd-raw'
};
}
if (check([0x78, 0x01])) {
return {
ext: 'dmg',
mime: 'application/x-apple-diskimage'
};
}
if (check([0x4D, 0x5A])) {
return {
ext: 'exe',
mime: 'application/x-msdownload'
};
}
if (check([0x25, 0x21])) {
await tokenizer.peekBuffer(buffer, {length: 24, mayBeLess: true});
if (checkString('PS-Adobe-', {offset: 2}) &&
checkString(' EPSF-', {offset: 14})) {
return {
ext: 'eps',
mime: 'application/eps'
};
}
return {
ext: 'ps',
mime: 'application/postscript'
};
}
if (
check([0x1F, 0xA0]) ||
check([0x1F, 0x9D])
) {
return {
ext: 'Z',
mime: 'application/x-compress'
};
}
// -- 3-byte signatures --
if (check([0xFF, 0xD8, 0xFF])) {
return {
ext: 'jpg',
mime: 'image/jpeg'
};
}
if (check([0x49, 0x49, 0xBC])) {
return {
ext: 'jxr',
mime: 'image/vnd.ms-photo'
};
}
if (check([0x1F, 0x8B, 0x8])) {
return {
ext: 'gz',
mime: 'application/gzip'
};
}
if (check([0x42, 0x5A, 0x68])) {
return {
ext: 'bz2',
mime: 'application/x-bzip2'
};
}
if (checkString('ID3')) {
await tokenizer.ignore(6); // Skip ID3 header until the header size
const id3HeaderLen = await tokenizer.readToken(uint32SyncSafeToken);
if (tokenizer.position + id3HeaderLen > tokenizer.fileInfo.size) {
// Guess file type based on ID3 header for backward compatibility
return {
ext: 'mp3',
mime: 'audio/mpeg'
};
}
await tokenizer.ignore(id3HeaderLen);
return fromTokenizer(tokenizer); // Skip ID3 header, recursion
}
// Musepack, SV7
if (checkString('MP+')) {
return {
ext: 'mpc',
mime: 'audio/x-musepack'
};
}
if (
(buffer[0] === 0x43 || buffer[0] === 0x46) &&
check([0x57, 0x53], {offset: 1})
) {
return {
ext: 'swf',
mime: 'application/x-shockwave-flash'
};
}
// -- 4-byte signatures --
if (check([0x47, 0x49, 0x46])) {
return {
ext: 'gif',
mime: 'image/gif'
};
}
if (checkString('FLIF')) {
return {
ext: 'flif',
mime: 'image/flif'
};
}
if (checkString('8BPS')) {
return {
ext: 'psd',
mime: 'image/vnd.adobe.photoshop'
};
}
if (checkString('WEBP', {offset: 8})) {
return {
ext: 'webp',
mime: 'image/webp'
};
}
// Musepack, SV8
if (checkString('MPCK')) {
return {
ext: 'mpc',
mime: 'audio/x-musepack'
};
}
if (checkString('FORM')) {
return {
ext: 'aif',
mime: 'audio/aiff'
};
}
if (checkString('icns', {offset: 0})) {
return {
ext: 'icns',
mime: 'image/icns'
};
}
// Zip-based file formats
// Need to be before the `zip` check
if (check([0x50, 0x4B, 0x3, 0x4])) { // Local file header signature
try {
while (tokenizer.position + 30 < tokenizer.fileInfo.size) {
await tokenizer.readBuffer(buffer, {length: 30});
// https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers
const zipHeader = {
compressedSize: buffer.readUInt32LE(18),
uncompressedSize: buffer.readUInt32LE(22),
filenameLength: buffer.readUInt16LE(26),
extraFieldLength: buffer.readUInt16LE(28)
};
zipHeader.filename = await tokenizer.readToken(new lib.StringType(zipHeader.filenameLength, 'utf-8'));
await tokenizer.ignore(zipHeader.extraFieldLength);
// Assumes signed `.xpi` from addons.mozilla.org
if (zipHeader.filename === 'META-INF/mozilla.rsa') {
return {
ext: 'xpi',
mime: 'application/x-xpinstall'
};
}
if (zipHeader.filename.endsWith('.rels') || zipHeader.filename.endsWith('.xml')) {
const type = zipHeader.filename.split('/')[0];
switch (type) {
case '_rels':
break;
case 'word':
return {
ext: 'docx',
mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
};
case 'ppt':
return {
ext: 'pptx',
mime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
};
case 'xl':
return {
ext: 'xlsx',
mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
};
default:
break;
}
}
if (zipHeader.filename.startsWith('xl/')) {
return {
ext: 'xlsx',
mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
};
}
if (zipHeader.filename.startsWith('3D/') && zipHeader.filename.endsWith('.model')) {
return {
ext: '3mf',
mime: 'model/3mf'
};
}
// The docx, xlsx and pptx file types extend the Office Open XML file format:
// https://en.wikipedia.org/wiki/Office_Open_XML_file_formats
// We look for:
// - one entry named '[Content_Types].xml' or '_rels/.rels',
// - one entry indicating specific type of file.
// MS Office, OpenOffice and LibreOffice may put the parts in different order, so the check should not rely on it.
if (zipHeader.filename === 'mimetype' && zipHeader.compressedSize === zipHeader.uncompressedSize) {
const mimeType = await tokenizer.readToken(new lib.StringType(zipHeader.compressedSize, 'utf-8'));
switch (mimeType) {
case 'application/epub+zip':
return {
ext: 'epub',
mime: 'application/epub+zip'
};
case 'application/vnd.oasis.opendocument.text':
return {
ext: 'odt',
mime: 'application/vnd.oasis.opendocument.text'
};
case 'application/vnd.oasis.opendocument.spreadsheet':
return {
ext: 'ods',
mime: 'application/vnd.oasis.opendocument.spreadsheet'
};
case 'application/vnd.oasis.opendocument.presentation':
return {
ext: 'odp',
mime: 'application/vnd.oasis.opendocument.presentation'
};
default:
}
}
// Try to find next header manually when current one is corrupted
if (zipHeader.compressedSize === 0) {
let nextHeaderIndex = -1;
while (nextHeaderIndex < 0 && (tokenizer.position < tokenizer.fileInfo.size)) {
await tokenizer.peekBuffer(buffer, {mayBeLess: true});
nextHeaderIndex = buffer.indexOf('504B0304', 0, 'hex');
// Move position to the next header if found, skip the whole buffer otherwise
await tokenizer.ignore(nextHeaderIndex >= 0 ? nextHeaderIndex : buffer.length);
}
} else {
await tokenizer.ignore(zipHeader.compressedSize);
}
}
} catch (error) {
if (!(error instanceof core$1.EndOfStreamError)) {
throw error;
}
}
return {
ext: 'zip',
mime: 'application/zip'
};
}
if (checkString('OggS')) {
// This is an OGG container
await tokenizer.ignore(28);
const type = Buffer.alloc(8);
await tokenizer.readBuffer(type);
// Needs to be before `ogg` check
if (_check(type, [0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64])) {
return {
ext: 'opus',
mime: 'audio/opus'
};
}
// If ' theora' in header.
if (_check(type, [0x80, 0x74, 0x68, 0x65, 0x6F, 0x72, 0x61])) {
return {
ext: 'ogv',
mime: 'video/ogg'
};
}
// If '\x01video' in header.
if (_check(type, [0x01, 0x76, 0x69, 0x64, 0x65, 0x6F, 0x00])) {
return {
ext: 'ogm',
mime: 'video/ogg'
};
}
// If ' FLAC' in header https://xiph.org/flac/faq.html
if (_check(type, [0x7F, 0x46, 0x4C, 0x41, 0x43])) {
return {
ext: 'oga',
mime: 'audio/ogg'
};
}
// 'Speex ' in header https://en.wikipedia.org/wiki/Speex
if (_check(type, [0x53, 0x70, 0x65, 0x65, 0x78, 0x20, 0x20])) {
return {
ext: 'spx',
mime: 'audio/ogg'
};
}
// If '\x01vorbis' in header
if (_check(type, [0x01, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73])) {
return {
ext: 'ogg',
mime: 'audio/ogg'
};
}
// Default OGG container https://www.iana.org/assignments/media-types/application/ogg
return {
ext: 'ogx',
mime: 'application/ogg'
};
}
if (
check([0x50, 0x4B]) &&
(buffer[2] === 0x3 || buffer[2] === 0x5 || buffer[2] === 0x7) &&
(buffer[3] === 0x4 || buffer[3] === 0x6 || buffer[3] === 0x8)
) {
return {
ext: 'zip',
mime: 'application/zip'
};
}
//
// File Type Box (https://en.wikipedia.org/wiki/ISO_base_media_file_format)
// It's not required to be first, but it's recommended to be. Almost all ISO base media files start with `ftyp` box.
// `ftyp` box must contain a brand major identifier, which must consist of ISO 8859-1 printable characters.
// Here we check for 8859-1 printable characters (for simplicity, it's a mask which also catches one non-printable character).
if (
checkString('ftyp', {offset: 4}) &&
(buffer[8] & 0x60) !== 0x00 // Brand major, first character ASCII?
) {
// They all can have MIME `video/mp4` except `application/mp4` special-case which is hard to detect.
// For some cases, we're specific, everything else falls to `video/mp4` with `mp4` extension.
const brandMajor = buffer.toString('binary', 8, 12).replace('\0', ' ').trim();
switch (brandMajor) {
case 'avif':
return {ext: 'avif', mime: 'image/avif'};
case 'mif1':
return {ext: 'heic', mime: 'image/heif'};
case 'msf1':
return {ext: 'heic', mime: 'image/heif-sequence'};
case 'heic':
case 'heix':
return {ext: 'heic', mime: 'image/heic'};
case 'hevc':
case 'hevx':
return {ext: 'heic', mime: 'image/heic-sequence'};
case 'qt':
return {ext: 'mov', mime: 'video/quicktime'};
case 'M4V':
case 'M4VH':
case 'M4VP':
return {ext: 'm4v', mime: 'video/x-m4v'};
case 'M4P':
return {ext: 'm4p', mime: 'video/mp4'};
case 'M4B':
return {ext: 'm4b', mime: 'audio/mp4'};
case 'M4A':
return {ext: 'm4a', mime: 'audio/x-m4a'};
case 'F4V':
return {ext: 'f4v', mime: 'video/mp4'};
case 'F4P':
return {ext: 'f4p', mime: 'video/mp4'};
case 'F4A':
return {ext: 'f4a', mime: 'audio/mp4'};
case 'F4B':
return {ext: 'f4b', mime: 'audio/mp4'};
case 'crx':
return {ext: 'cr3', mime: 'image/x-canon-cr3'};
default:
if (brandMajor.startsWith('3g')) {
if (brandMajor.startsWith('3g2')) {
return {ext: '3g2', mime: 'video/3gpp2'};
}
return {ext: '3gp', mime: 'video/3gpp'};
}
return {ext: 'mp4', mime: 'video/mp4'};
}
}
if (checkString('MThd')) {
return {
ext: 'mid',
mime: 'audio/midi'
};
}
if (
checkString('wOFF') &&
(
check([0x00, 0x01, 0x00, 0x00], {offset: 4}) ||
checkString('OTTO', {offset: 4})
)
) {
return {
ext: 'woff',
mime: 'font/woff'
};
}
if (
checkString('wOF2') &&
(
check([0x00, 0x01, 0x00, 0x00], {offset: 4}) ||
checkString('OTTO', {offset: 4})
)
) {
return {
ext: 'woff2',
mime: 'font/woff2'
};
}
if (check([0xD4, 0xC3, 0xB2, 0xA1]) || check([0xA1, 0xB2, 0xC3, 0xD4])) {
return {
ext: 'pcap',
mime: 'application/vnd.tcpdump.pcap'
};
}
// Sony DSD Stream File (DSF)
if (checkString('DSD ')) {
return {
ext: 'dsf',
mime: 'audio/x-dsf' // Non-standard
};
}
if (checkString('LZIP')) {
return {
ext: 'lz',
mime: 'application/x-lzip'
};
}
if (checkString('fLaC')) {
return {
ext: 'flac',
mime: 'audio/x-flac'
};
}
if (check([0x42, 0x50, 0x47, 0xFB])) {
return {
ext: 'bpg',
mime: 'image/bpg'
};
}
if (checkString('wvpk')) {
return {
ext: 'wv',
mime: 'audio/wavpack'
};
}
if (checkString('%PDF')) {
await tokenizer.ignore(1350);
const maxBufferSize = 10 * 1024 * 1024;
const buffer = Buffer.alloc(Math.min(maxBufferSize, tokenizer.fileInfo.size));
await tokenizer.readBuffer(buffer, {mayBeLess: true});
// Check if this is an Adobe Illustrator file
if (buffer.includes(Buffer.from('AIPrivateData'))) {
return {
ext: 'ai',
mime: 'application/postscript'
};
}
// Assume this is just a normal PDF
return {
ext: 'pdf',
mime: 'application/pdf'
};
}
if (check([0x00, 0x61, 0x73, 0x6D])) {
return {
ext: 'wasm',
mime: 'application/wasm'
};
}
// TIFF, little-endian type
if (check([0x49, 0x49, 0x2A, 0x0])) {
if (checkString('CR', {offset: 8})) {
return {
ext: 'cr2',
mime: 'image/x-canon-cr2'
};
}
if (check([0x1C, 0x00, 0xFE, 0x00], {offset: 8}) || check([0x1F, 0x00, 0x0B, 0x00], {offset: 8})) {
return {
ext: 'nef',
mime: 'image/x-nikon-nef'
};
}
if (
check([0x08, 0x00, 0x00, 0x00], {offset: 4}) &&
(check([0x2D, 0x00, 0xFE, 0x00], {offset: 8}) ||
check([0x27, 0x00, 0xFE, 0x00], {offset: 8}))
) {
return {
ext: 'dng',
mime: 'image/x-adobe-dng'
};
}
buffer = Buffer.alloc(24);
await tokenizer.peekBuffer(buffer);
if (
(check([0x10, 0xFB, 0x86, 0x01], {offset: 4}) || check([0x08, 0x00, 0x00, 0x00], {offset: 4})) &&
// This pattern differentiates ARW from other TIFF-ish file types:
check([0x00, 0xFE, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x01], {offset: 9})
) {
return {
ext: 'arw',
mime: 'image/x-sony-arw'
};
}
return {
ext: 'tif',
mime: 'image/tiff'
};
}
// TIFF, big-endian type
if (check([0x4D, 0x4D, 0x0, 0x2A])) {
return {
ext: 'tif',
mime: 'image/tiff'
};
}
if (checkString('MAC ')) {
return {
ext: 'ape',
mime: 'audio/ape'
};
}
// https://github.com/threatstack/libmagic/blob/master/magic/Magdir/matroska
if (check([0x1A, 0x45, 0xDF, 0xA3])) { // Root element: EBML
async function readField() {
const msb = await tokenizer.peekNumber(lib.UINT8);
let mask = 0x80;
let ic = 0; // 0 = A, 1 = B, 2 = C, 3 = D
while ((msb & mask) === 0) {
++ic;
mask >>= 1;
}
const id = Buffer.alloc(ic + 1);
await tokenizer.readBuffer(id);
return id;
}
async function readElement() {
const id = await readField();
const lenField = await readField();
lenField[0] ^= 0x80 >> (lenField.length - 1);
const nrLen = Math.min(6, lenField.length); // JavaScript can max read 6 bytes integer
return {
id: id.readUIntBE(0, id.length),
len: lenField.readUIntBE(lenField.length - nrLen, nrLen)
};
}
async function readChildren(level, children) {
while (children > 0) {
const e = await readElement();
if (e.id === 0x4282) {
return tokenizer.readToken(new lib.StringType(e.len, 'utf-8')); // Return DocType
}
await tokenizer.ignore(e.len); // ignore payload
--children;
}
}
const re = await readElement();
const docType = await readChildren(1, re.len);
switch (docType) {
case 'webm':
return {
ext: 'webm',
mime: 'video/webm'
};
case 'matroska':
return {
ext: 'mkv',
mime: 'video/x-matroska'
};
default:
return;
}
}
// RIFF file format which might be AVI, WAV, QCP, etc
if (check([0x52, 0x49, 0x46, 0x46])) {
if (check([0x41, 0x56, 0x49], {offset: 8})) {
return {
ext: 'avi',
mime: 'video/vnd.avi'
};
}
if (check([0x57, 0x41, 0x56, 0x45], {offset: 8})) {
return {
ext: 'wav',
mime: 'audio/vnd.wave'
};
}
// QLCM, QCP file
if (check([0x51, 0x4C, 0x43, 0x4D], {offset: 8})) {
return {
ext: 'qcp',
mime: 'audio/qcelp'
};
}
}
if (checkString('SQLi')) {
return {
ext: 'sqlite',
mime: 'application/x-sqlite3'
};
}
if (check([0x4E, 0x45, 0x53, 0x1A])) {
return {
ext: 'nes',
mime: 'application/x-nintendo-nes-rom'
};
}
if (checkString('Cr24')) {
return {
ext: 'crx',
mime: 'application/x-google-chrome-extension'
};
}
if (
checkString('MSCF') ||
checkString('ISc(')
) {
return {
ext: 'cab',
mime: 'application/vnd.ms-cab-compressed'
};
}
if (check([0xED, 0xAB, 0xEE, 0xDB])) {
return {
ext: 'rpm',
mime: 'application/x-rpm'
};
}
if (check([0xC5, 0xD0, 0xD3, 0xC6])) {
return {
ext: 'eps',
mime: 'application/eps'
};
}
if (check([0x28, 0xB5, 0x2F, 0xFD])) {
return {
ext: 'zst',
mime: 'application/zstd'
};
}
// -- 5-byte signatures --
if (check([0x4F, 0x54, 0x54, 0x4F, 0x00])) {
return {
ext: 'otf',
mime: 'font/otf'
};
}
if (checkString('#!AMR')) {
return {
ext: 'amr',
mime: 'audio/amr'
};
}
if (checkString('{\\rtf')) {
return {
ext: 'rtf',
mime: 'application/rtf'
};
}
if (check([0x46, 0x4C, 0x56, 0x01])) {
return {
ext: 'flv',
mime: 'video/x-flv'
};
}
if (checkString('IMPM')) {
return {
ext: 'it',
mime: 'audio/x-it'
};
}
if (
checkString('-lh0-', {offset: 2}) ||
checkString('-lh1-', {offset: 2}) ||
checkString('-lh2-', {offset: 2}) ||
checkString('-lh3-', {offset: 2}) ||
checkString('-lh4-', {offset: 2}) ||
checkString('-lh5-', {offset: 2}) ||
checkString('-lh6-', {offset: 2}) ||
checkString('-lh7-', {offset: 2}) ||
checkString('-lzs-', {offset: 2}) ||
checkString('-lz4-', {offset: 2}) ||
checkString('-lz5-', {offset: 2}) ||
checkString('-lhd-', {offset: 2})
) {
return {
ext: 'lzh',
mime: 'application/x-lzh-compressed'
};
}
// MPEG program stream (PS or MPEG-PS)
if (check([0x00, 0x00, 0x01, 0xBA])) {
// MPEG-PS, MPEG-1 Part 1
if (check([0x21], {offset: 4, mask: [0xF1]})) {
return {
ext: 'mpg', // May also be .ps, .mpeg
mime: 'video/MP1S'
};
}
// MPEG-PS, MPEG-2 Part 1
if (check([0x44], {offset: 4, mask: [0xC4]})) {
return {
ext: 'mpg', // May also be .mpg, .m2p, .vob or .sub
mime: 'video/MP2P'
};
}
}
if (checkString('ITSF')) {
return {
ext: 'chm',
mime: 'application/vnd.ms-htmlhelp'
};
}
// -- 6-byte signatures --
if (check([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])) {
return {
ext: 'xz',
mime: 'application/x-xz'
};
}
if (checkString('<?xml ')) {
return {
ext: 'xml',
mime: 'application/xml'
};
}
if (check([0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C])) {
return {
ext: '7z',
mime: 'application/x-7z-compressed'
};
}
if (
check([0x52, 0x61, 0x72, 0x21, 0x1A, 0x7]) &&
(buffer[6] === 0x0 || buffer[6] === 0x1)
) {
return {
ext: 'rar',
mime: 'application/x-rar-compressed'
};
}
if (checkString('solid ')) {
return {
ext: 'stl',
mime: 'model/stl'
};
}
// -- 7-byte signatures --
if (checkString('BLENDER')) {
return {
ext: 'blend',
mime: 'application/x-blender'
};
}
if (checkString('!<arch>')) {
await tokenizer.ignore(8);
const str = await tokenizer.readToken(new lib.StringType(13, 'ascii'));
if (str === 'debian-binary') {
return {
ext: 'deb',
mime: 'application/x-deb'
};
}
return {
ext: 'ar',
mime: 'application/x-unix-archive'
};
}
// -- 8-byte signatures --
if (check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) {
// APNG format (https://wiki.mozilla.org/APNG_Specification)
// 1. Find the first IDAT (image data) chunk (49 44 41 54)
// 2. Check if there is an "acTL" chunk before the IDAT one (61 63 54 4C)
// Offset calculated as follows:
// - 8 bytes: PNG signature
// - 4 (length) + 4 (chunk type) + 13 (chunk data) + 4 (CRC): IHDR chunk
await tokenizer.ignore(8); // ignore PNG signature
async function readChunkHeader() {
return {
length: await tokenizer.readToken(lib.INT32_BE),
type: await tokenizer.readToken(new lib.StringType(4, 'binary'))
};
}
do {
const chunk = await readChunkHeader();
if (chunk.length < 0) {
return; // Invalid chunk length
}
switch (chunk.type) {
case 'IDAT':
return {
ext: 'png',
mime: 'image/png'
};
case 'acTL':
return {
ext: 'apng',
mime: 'image/apng'
};
default:
await tokenizer.ignore(chunk.length + 4); // Ignore chunk-data + CRC
}
} while (tokenizer.position + 8 < tokenizer.fileInfo.size);
return {
ext: 'png',
mime: 'image/png'
};
}
if (check([0x41, 0x52, 0x52, 0x4F, 0x57, 0x31, 0x00, 0x00])) {
return {
ext: 'arrow',
mime: 'application/x-apache-arrow'
};
}
if (check([0x67, 0x6C, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00])) {
return {
ext: 'glb',
mime: 'model/gltf-binary'
};
}
// `mov` format variants
if (
check([0x66, 0x72, 0x65, 0x65], {offset: 4}) || // `free`
check([0x6D, 0x64, 0x61, 0x74], {offset: 4}) || // `mdat` MJPEG
check([0x6D, 0x6F, 0x6F, 0x76], {offset: 4}) || // `moov`
check([0x77, 0x69, 0x64, 0x65], {offset: 4}) // `wide`
) {
return {
ext: 'mov',
mime: 'video/quicktime'
};
}
// -- 9-byte signatures --
if (check([0x49, 0x49, 0x52, 0x4F, 0x08, 0x00, 0x00, 0x00, 0x18])) {
return {
ext: 'orf',
mime: 'image/x-olympus-orf'
};
}
if (checkString('gimp xcf ')) {
return {
ext: 'xcf',
mime: 'image/x-xcf'
};
}
// -- 12-byte signatures --
if (check([0x49, 0x49, 0x55, 0x00, 0x18, 0x00, 0x00, 0x00, 0x88, 0xE7, 0x74, 0xD8])) {
return {
ext: 'rw2',
mime: 'image/x-panasonic-rw2'
};
}
// ASF_Header_Object first 80 bytes
if (check([0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9])) {
async function readHeader() {
const guid = Buffer.alloc(16);
await tokenizer.readBuffer(guid);
return {
id: guid,
size: Number(await tokenizer.readToken(lib.UINT64_LE))
};
}
await tokenizer.ignore(30);
// Search for header should be in first 1KB of file.
while (tokenizer.position + 24 < tokenizer.fileInfo.size) {
const header = await readHeader();
let payload = header.size - 24;
if (_check(header.id, [0x91, 0x07, 0xDC, 0xB7, 0xB7, 0xA9, 0xCF, 0x11, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65])) {
// Sync on Stream-Properties-Object (B7DC0791-A9B7-11CF-8EE6-00C00C205365)
const typeId = Buffer.alloc(16);
payload -= await tokenizer.readBuffer(typeId);
if (_check(typeId, [0x40, 0x9E, 0x69, 0xF8, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B])) {
// Found audio:
return {
ext: 'asf',
mime: 'audio/x-ms-asf'
};
}
if (_check(typeId, [0xC0, 0xEF, 0x19, 0xBC, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B])) {
// Found video:
return {
ext: 'asf',
mime: 'video/x-ms-asf'
};
}
break;
}
await tokenizer.ignore(payload);
}
// Default to ASF generic extension
return {
ext: 'asf',
mime: 'application/vnd.ms-asf'
};
}
if (check([0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A])) {
return {
ext: 'ktx',
mime: 'image/ktx'
};
}
if ((check([0x7E, 0x10, 0x04]) || check([0x7E, 0x18, 0x04])) && check([0x30, 0x4D, 0x49, 0x45], {offset: 4})) {
return {
ext: 'mie',
mime: 'application/x-mie'
};
}
if (check([0x27, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], {offset: 2})) {
return {
ext: 'shp',
mime: 'application/x-esri-shape'
};
}
if (check([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A])) {
// JPEG-2000 family
await tokenizer.ignore(20);
const type = await tokenizer.readToken(new lib.StringType(4, 'ascii'));
switch (type) {
case 'jp2 ':
return {
ext: 'jp2',
mime: 'image/jp2'
};
case 'jpx ':
return {
ext: 'jpx',
mime: 'image/jpx'
};
case 'jpm ':
return {
ext: 'jpm',
mime: 'image/jpm'
};
case 'mjp2':
return {
ext: 'mj2',
mime: 'image/mj2'
};
default:
return;
}
}
if (
check([0xFF, 0x0A]) ||
check([0x00, 0x00, 0x00, 0x0C, 0x4A, 0x58, 0x4C, 0x20, 0x0D, 0x0A, 0x87, 0x0A])
) {
return {
ext: 'jxl',
mime: 'image/jxl'
};
}
// -- Unsafe signatures --
if (
check([0x0, 0x0, 0x1, 0xBA]) ||
check([0x0, 0x0, 0x1, 0xB3])
) {
return {
ext: 'mpg',
mime: 'video/mpeg'
};
}
if (check([0x00, 0x01, 0x00, 0x00, 0x00])) {
return {
ext: 'ttf',
mime: 'font/ttf'
};
}
if (check([0x00, 0x00, 0x01, 0x00])) {
return {
ext: 'ico',
mime: 'image/x-icon'
};
}
if (check([0x00, 0x00, 0x02, 0x00])) {
return {
ext: 'cur',
mime: 'image/x-icon'
};
}
if (check([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1])) {
// Detected Microsoft Compound File Binary File (MS-CFB) Format.
return {
ext: 'cfb',
mime: 'application/x-cfb'
};
}
// Increase sample size from 12 to 256.
await tokenizer.peekBuffer(buffer, {length: Math.min(256, tokenizer.fileInfo.size), mayBeLess: true});
// -- 15-byte signatures --
if (checkString('BEGIN:')) {
if (checkString('VCARD', {offset: 6})) {
return {
ext: 'vcf',
mime: 'text/vcard'
};
}
if (checkString('VCALENDAR', {offset: 6})) {
return {
ext: 'ics',
mime: 'text/calendar'
};
}
}
// `raf` is here just to keep all the raw image detectors together.
if (checkString('FUJIFILMCCD-RAW')) {
return {
ext: 'raf',
mime: 'image/x-fujifilm-raf'
};
}
if (checkString('Extended Module:')) {
return {
ext: 'xm',
mime: 'audio/x-xm'
};
}
if (checkString('Creative Voice File')) {
return {
ext: 'voc',
mime: 'audio/x-voc'
};
}
if (check([0x04, 0x00, 0x00, 0x00]) && buffer.length >= 16) { // Rough & quick check Pickle/ASAR
const jsonSize = buffer.readUInt32LE(12);
if (jsonSize > 12 && buffer.length >= jsonSize + 16) {
try {
const header = buffer.slice(16, jsonSize + 16).toString();
const json = JSON.parse(header);
// Check if Pickle is ASAR
if (json.files) { // Final check, assuring Pickle/ASAR format
return {
ext: 'asar',
mime: 'application/x-asar'
};
}
} catch (_) {
}
}
}
if (check([0x06, 0x0E, 0x2B, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0D, 0x01, 0x02, 0x01, 0x01, 0x02])) {
return {
ext: 'mxf',
mime: 'application/mxf'
};
}
if (checkString('SCRM', {offset: 44})) {
return {
ext: 's3m',
mime: 'audio/x-s3m'
};
}
if (check([0x47], {offset: 4}) && (check([0x47], {offset: 192}) || check([0x47], {offset: 196}))) {
return {
ext: 'mts',
mime: 'video/mp2t'
};
}
if (check([0x42, 0x4F, 0x4F, 0x4B, 0x4D, 0x4F, 0x42, 0x49], {offset: 60})) {
return {
ext: 'mobi',
mime: 'application/x-mobipocket-ebook'
};
}
if (check([0x44, 0x49, 0x43, 0x4D], {offset: 128})) {
return {
ext: 'dcm',
mime: 'application/dicom'
};
}
if (check([0x4C, 0x00, 0x00, 0x00, 0x01, 0x14, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46])) {
return {
ext: 'lnk',
mime: 'application/x.ms.shortcut' // Invented by us
};
}
if (check([0x62, 0x6F, 0x6F, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x61, 0x72, 0x6B, 0x00, 0x00, 0x00, 0x00])) {
return {
ext: 'alias',
mime: 'application/x.apple.alias' // Invented by us
};
}
if (
check([0x4C, 0x50], {offset: 34}) &&
(
check([0x00, 0x00, 0x01], {offset: 8}) ||
check([0x01, 0x00, 0x02], {offset: 8}) ||
check([0x02, 0x00, 0x02], {offset: 8})
)
) {
return {
ext: 'eot',
mime: 'application/vnd.ms-fontobject'
};
}
if (check([0x06, 0x06, 0xED, 0xF5, 0xD8, 0x1D, 0x46, 0xE5, 0xBD, 0x31, 0xEF, 0xE7, 0xFE, 0x74, 0xB7, 0x1D])) {
return {
ext: 'indd',
mime: 'application/x-indesign'
};
}
// Increase sample size from 256 to 512
await tokenizer.peekBuffer(buffer, {length: Math.min(512, tokenizer.fileInfo.size), mayBeLess: true});
// Requires a buffer size of 512 bytes
if (tarHeaderChecksumMatches(buffer)) {
return {
ext: 'tar',
mime: 'application/x-tar'
};
}
if (check([0xFF, 0xFE, 0xFF, 0x0E, 0x53, 0x00, 0x6B, 0x00, 0x65, 0x00, 0x74, 0x00, 0x63, 0x00, 0x68, 0x00, 0x55, 0x00, 0x70, 0x00, 0x20, 0x00, 0x4D, 0x00, 0x6F, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6C, 0x00])) {
return {
ext: 'skp',
mime: 'application/vnd.sketchup.skp'
};
}
if (checkString('-----BEGIN PGP MESSAGE-----')) {
return {
ext: 'pgp',
mime: 'application/pgp-encrypted'
};
}
// Check MPEG 1 or 2 Layer 3 header, or 'layer 0' for ADTS (MPEG sync-word 0xFFE)
if (buffer.length >= 2 && check([0xFF, 0xE0], {offset: 0, mask: [0xFF, 0xE0]})) {
if (check([0x10], {offset: 1, mask: [0x16]})) {
// Check for (ADTS) MPEG-2
if (check([0x08], {offset: 1, mask: [0x08]})) {
return {
ext: 'aac',
mime: 'audio/aac'
};
}
// Must be (ADTS) MPEG-4
return {
ext: 'aac',
mime: 'audio/aac'
};
}
// MPEG 1 or 2 Layer 3 header
// Check for MPEG layer 3
if (check([0x02], {offset: 1, mask: [0x06]})) {
return {
ext: 'mp3',
mime: 'audio/mpeg'
};
}
// Check for MPEG layer 2
if (check([0x04], {offset: 1, mask: [0x06]})) {
return {
ext: 'mp2',
mime: 'audio/mpeg'
};
}
// Check for MPEG layer 1
if (check([0x06], {offset: 1, mask: [0x06]})) {
return {
ext: 'mp1',
mime: 'audio/mpeg'
};
}
}
}
const stream = readableStream => new Promise((resolve, reject) => {
// Using `eval` to work around issues when bundling with Webpack
const stream = eval('require')('stream'); // eslint-disable-line no-eval
readableStream.on('error', reject);
readableStream.once('readable', async () => {
// Set up output stream
const pass = new stream.PassThrough();
let outputStream;
if (stream.pipeline) {
outputStream = stream.pipeline(readableStream, pass, () => {
});
} else {
outputStream = readableStream.pipe(pass);
}
// Read the input stream and detect the filetype
const chunk = readableStream.read(minimumBytes) || readableStream.read() || Buffer.alloc(0);
try {
const fileType = await fromBuffer(chunk);
pass.fileType = fileType;
} catch (error) {
reject(error);
}
resolve(outputStream);
});
});
const fileType$1 = {
fromStream,
fromTokenizer,
fromBuffer,
stream
};
Object.defineProperty(fileType$1, 'extensions', {
get() {
return new Set(supported.extensions);
}
});
Object.defineProperty(fileType$1, 'mimeTypes', {
get() {
return new Set(supported.mimeTypes);
}
});
var core = fileType$1;
async function fromFile(path) {
const tokenizer = await lib$1.fromFile(path);
try {
return await core.fromTokenizer(tokenizer);
} finally {
await tokenizer.close();
}
}
const fileType = {
fromFile
};
Object.assign(fileType, core);
Object.defineProperty(fileType, 'extensions', {
get() {
return core.extensions;
}
});
Object.defineProperty(fileType, 'mimeTypes', {
get() {
return core.mimeTypes;
}
});
var fileType_1 = fileType;
var util = createCommonjsModule(function (module, exports) {
const nameStartChar = ':A-Za-z_\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD';
const nameChar = nameStartChar + '\\-.\\d\\u00B7\\u0300-\\u036F\\u203F-\\u2040';
const nameRegexp = '[' + nameStartChar + '][' + nameChar + ']*';
const regexName = new RegExp('^' + nameRegexp + '$');
const getAllMatches = function(string, regex) {
const matches = [];
let match = regex.exec(string);
while (match) {
const allmatches = [];
const len = match.length;
for (let index = 0; index < len; index++) {
allmatches.push(match[index]);
}
matches.push(allmatches);
match = regex.exec(string);
}
return matches;
};
const isName = function(string) {
const match = regexName.exec(string);
return !(match === null || typeof match === 'undefined');
};
exports.isExist = function(v) {
return typeof v !== 'undefined';
};
exports.isEmptyObject = function(obj) {
return Object.keys(obj).length === 0;
};
/**
* Copy all the properties of a into b.
* @param {*} target
* @param {*} a
*/
exports.merge = function(target, a, arrayMode) {
if (a) {
const keys = Object.keys(a); // will return an array of own properties
const len = keys.length; //don't make it inline
for (let i = 0; i < len; i++) {
if (arrayMode === 'strict') {
target[keys[i]] = [ a[keys[i]] ];
} else {
target[keys[i]] = a[keys[i]];
}
}
}
};
/* exports.merge =function (b,a){
return Object.assign(b,a);
} */
exports.getValue = function(v) {
if (exports.isExist(v)) {
return v;
} else {
return '';
}
};
// const fakeCall = function(a) {return a;};
// const fakeCallNoReturn = function() {};
exports.buildOptions = function(options, defaultOptions, props) {
var newOptions = {};
if (!options) {
return defaultOptions; //if there are not options
}
for (let i = 0; i < props.length; i++) {
if (options[props[i]] !== undefined) {
newOptions[props[i]] = options[props[i]];
} else {
newOptions[props[i]] = defaultOptions[props[i]];
}
}
return newOptions;
};
/**
* Check if a tag name should be treated as array
*
* @param tagName the node tagname
* @param arrayMode the array mode option
* @param parentTagName the parent tag name
* @returns {boolean} true if node should be parsed as array
*/
exports.isTagNameInArrayMode = function (tagName, arrayMode, parentTagName) {
if (arrayMode === false) {
return false;
} else if (arrayMode instanceof RegExp) {
return arrayMode.test(tagName);
} else if (typeof arrayMode === 'function') {
return !!arrayMode(tagName, parentTagName);
}
return arrayMode === "strict";
};
exports.isName = isName;
exports.getAllMatches = getAllMatches;
exports.nameRegexp = nameRegexp;
});
const convertToJson = function(node, options, parentTagName) {
const jObj = {};
// when no child node or attr is present
if ((!node.child || util.isEmptyObject(node.child)) && (!node.attrsMap || util.isEmptyObject(node.attrsMap))) {
return util.isExist(node.val) ? node.val : '';
}
// otherwise create a textnode if node has some text
if (util.isExist(node.val) && !(typeof node.val === 'string' && (node.val === '' || node.val === options.cdataPositionChar))) {
const asArray = util.isTagNameInArrayMode(node.tagname, options.arrayMode, parentTagName);
jObj[options.textNodeName] = asArray ? [node.val] : node.val;
}
util.merge(jObj, node.attrsMap, options.arrayMode);
const keys = Object.keys(node.child);
for (let index = 0; index < keys.length; index++) {
const tagName = keys[index];
if (node.child[tagName] && node.child[tagName].length > 1) {
jObj[tagName] = [];
for (let tag in node.child[tagName]) {
if (node.child[tagName].hasOwnProperty(tag)) {
jObj[tagName].push(convertToJson(node.child[tagName][tag], options, tagName));
}
}
} else {
const result = convertToJson(node.child[tagName][0], options, tagName);
const asArray = (options.arrayMode === true && typeof result === 'object') || util.isTagNameInArrayMode(tagName, options.arrayMode, parentTagName);
jObj[tagName] = asArray ? [result] : result;
}
}
//add value
return jObj;
};
var convertToJson_1 = convertToJson;
var node2json = {
convertToJson: convertToJson_1
};
var xmlNode = function(tagname, parent, val) {
this.tagname = tagname;
this.parent = parent;
this.child = {}; //child tags
this.attrsMap = {}; //attributes map
this.val = val; //text only
this.addChild = function(child) {
if (Array.isArray(this.child[child.tagname])) {
//already presents
this.child[child.tagname].push(child);
} else {
this.child[child.tagname] = [child];
}
};
};
const hexRegex = /0x[a-zA-Z0-9]+/;
const numRegex = /^([\-\+])?(0*)(\.[0-9]+(e\-?[0-9]+)?|[0-9]+(\.[0-9]+(e\-?[0-9]+)?)?)$/;
// const octRegex = /0x[a-z0-9]+/;
// const binRegex = /0x[a-z0-9]+/;
const consider = {
hex : true,
leadingZeros: true,
decimalPoint: "\.",
//skipLike: /regex/
};
function toNumber(str, options = {}){
// const options = Object.assign({}, consider);
// if(opt.leadingZeros === false){
// options.leadingZeros = false;
// }else if(opt.hex === false){
// options.hex = false;
// }
options = Object.assign({}, consider, options );
if(!str || typeof str !== "string" ) return str;
let trimmedStr = str.trim();
if(options.skipLike !== undefined && options.skipLike.test(trimmedStr)) return str;
else if (options.hex && hexRegex.test(trimmedStr)) {
return Number.parseInt(trimmedStr, 16);
// } else if (options.parseOct && octRegex.test(str)) {
// return Number.parseInt(val, 8);
// }else if (options.parseBin && binRegex.test(str)) {
// return Number.parseInt(val, 2);
}else {
//separate negative sign, leading zeros, and rest number
const match = numRegex.exec(trimmedStr);
if(match){
match[1];
const leadingZeros = match[2];
const num = match[3]; //complete num
match[4] || match[6];
if(leadingZeros.length === 1 && num[0] === ".") return Number(str);
else if(!options.leadingZeros && leadingZeros.length > 0) return str;
else return Number(trimmedStr);
}else { //non-numeric string
return str;
}
}
}
var strnum = toNumber;
const buildOptions$3 = util.buildOptions;
'<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|((NAME:)?(NAME))([^>]*)>|((\\/)(NAME)\\s*>))([^<]*)'
.replace(/NAME/g, util.nameRegexp);
//const tagsRegx = new RegExp("<(\\/?[\\w:\\-\._]+)([^>]*)>(\\s*"+cdataRegx+")*([^<]+)?","g");
//const tagsRegx = new RegExp("<(\\/?)((\\w*:)?([\\w:\\-\._]+))([^>]*)>([^<]*)("+cdataRegx+"([^<]*))*([^<]+)?","g");
//polyfill
if (!Number.parseInt && window.parseInt) {
Number.parseInt = window.parseInt;
}
if (!Number.parseFloat && window.parseFloat) {
Number.parseFloat = window.parseFloat;
}
const defaultOptions$2 = {
attributeNamePrefix: '@_',
attrNodeName: false,
textNodeName: '#text',
ignoreAttributes: true,
ignoreNameSpace: false,
allowBooleanAttributes: false, //a tag can have attributes without any value
//ignoreRootElement : false,
parseNodeValue: true,
parseAttributeValue: false,
arrayMode: false,
trimValues: true, //Trim string values of tag and attributes
cdataTagName: false,
cdataPositionChar: '\\c',
numParseOptions: {
hex: true,
leadingZeros: true
},
tagValueProcessor: function(a, tagName) {
return a;
},
attrValueProcessor: function(a, attrName) {
return a;
},
stopNodes: []
//decodeStrict: false,
};
var defaultOptions_1 = defaultOptions$2;
const props$2 = [
'attributeNamePrefix',
'attrNodeName',
'textNodeName',
'ignoreAttributes',
'ignoreNameSpace',
'allowBooleanAttributes',
'parseNodeValue',
'parseAttributeValue',
'arrayMode',
'trimValues',
'cdataTagName',
'cdataPositionChar',
'tagValueProcessor',
'attrValueProcessor',
'parseTrueNumberOnly',
'numParseOptions',
'stopNodes'
];
var props_1 = props$2;
/**
* Trim -> valueProcessor -> parse value
* @param {string} tagName
* @param {string} val
* @param {object} options
*/
function processTagValue(tagName, val, options) {
if (val) {
if (options.trimValues) {
val = val.trim();
}
val = options.tagValueProcessor(val, tagName);
val = parseValue(val, options.parseNodeValue, options.numParseOptions);
}
return val;
}
function resolveNameSpace(tagname, options) {
if (options.ignoreNameSpace) {
const tags = tagname.split(':');
const prefix = tagname.charAt(0) === '/' ? '/' : '';
if (tags[0] === 'xmlns') {
return '';
}
if (tags.length === 2) {
tagname = prefix + tags[1];
}
}
return tagname;
}
function parseValue(val, shouldParse, options) {
if (shouldParse && typeof val === 'string') {
//console.log(options)
const newval = val.trim();
if(newval === 'true' ) return true;
else if(newval === 'false' ) return false;
else return strnum(val, options);
} else {
if (util.isExist(val)) {
return val;
} else {
return '';
}
}
}
//TODO: change regex to capture NS
//const attrsRegx = new RegExp("([\\w\\-\\.\\:]+)\\s*=\\s*(['\"])((.|\n)*?)\\2","gm");
const attrsRegx = new RegExp('([^\\s=]+)\\s*(=\\s*([\'"])(.*?)\\3)?', 'g');
function buildAttributesMap(attrStr, options) {
if (!options.ignoreAttributes && typeof attrStr === 'string') {
attrStr = attrStr.replace(/\r?\n/g, ' ');
//attrStr = attrStr || attrStr.trim();
const matches = util.getAllMatches(attrStr, attrsRegx);
const len = matches.length; //don't make it inline
const attrs = {};
for (let i = 0; i < len; i++) {
const attrName = resolveNameSpace(matches[i][1], options);
if (attrName.length) {
if (matches[i][4] !== undefined) {
if (options.trimValues) {
matches[i][4] = matches[i][4].trim();
}
matches[i][4] = options.attrValueProcessor(matches[i][4], attrName);
attrs[options.attributeNamePrefix + attrName] = parseValue(
matches[i][4],
options.parseAttributeValue,
options.numParseOptions
);
} else if (options.allowBooleanAttributes) {
attrs[options.attributeNamePrefix + attrName] = true;
}
}
}
if (!Object.keys(attrs).length) {
return;
}
if (options.attrNodeName) {
const attrCollection = {};
attrCollection[options.attrNodeName] = attrs;
return attrCollection;
}
return attrs;
}
}
const getTraversalObj = function(xmlData, options) {
xmlData = xmlData.replace(/\r\n?/g, "\n");
options = buildOptions$3(options, defaultOptions$2, props$2);
const xmlObj = new xmlNode('!xml');
let currentNode = xmlObj;
let textData = "";
//function match(xmlData){
for(let i=0; i< xmlData.length; i++){
const ch = xmlData[i];
if(ch === '<'){
if( xmlData[i+1] === '/') {//Closing Tag
const closeIndex = findClosingIndex(xmlData, ">", i, "Closing Tag is not closed.");
let tagName = xmlData.substring(i+2,closeIndex).trim();
if(options.ignoreNameSpace){
const colonIndex = tagName.indexOf(":");
if(colonIndex !== -1){
tagName = tagName.substr(colonIndex+1);
}
}
/* if (currentNode.parent) {
currentNode.parent.val = util.getValue(currentNode.parent.val) + '' + processTagValue2(tagName, textData , options);
} */
if(currentNode){
if(currentNode.val){
currentNode.val = util.getValue(currentNode.val) + '' + processTagValue(tagName, textData , options);
}else {
currentNode.val = processTagValue(tagName, textData , options);
}
}
if (options.stopNodes.length && options.stopNodes.includes(currentNode.tagname)) {
currentNode.child = [];
if (currentNode.attrsMap == undefined) { currentNode.attrsMap = {};}
currentNode.val = xmlData.substr(currentNode.startIndex + 1, i - currentNode.startIndex - 1);
}
currentNode = currentNode.parent;
textData = "";
i = closeIndex;
} else if( xmlData[i+1] === '?') {
i = findClosingIndex(xmlData, "?>", i, "Pi Tag is not closed.");
} else if(xmlData.substr(i + 1, 3) === '!--') {
i = findClosingIndex(xmlData, "-->", i, "Comment is not closed.");
} else if( xmlData.substr(i + 1, 2) === '!D') {
const closeIndex = findClosingIndex(xmlData, ">", i, "DOCTYPE is not closed.");
const tagExp = xmlData.substring(i, closeIndex);
if(tagExp.indexOf("[") >= 0){
i = xmlData.indexOf("]>", i) + 1;
}else {
i = closeIndex;
}
}else if(xmlData.substr(i + 1, 2) === '![') {
const closeIndex = findClosingIndex(xmlData, "]]>", i, "CDATA is not closed.") - 2;
const tagExp = xmlData.substring(i + 9,closeIndex);
//considerations
//1. CDATA will always have parent node
//2. A tag with CDATA is not a leaf node so it's value would be string type.
if(textData){
currentNode.val = util.getValue(currentNode.val) + '' + processTagValue(currentNode.tagname, textData , options);
textData = "";
}
if (options.cdataTagName) {
//add cdata node
const childNode = new xmlNode(options.cdataTagName, currentNode, tagExp);
currentNode.addChild(childNode);
//for backtracking
currentNode.val = util.getValue(currentNode.val) + options.cdataPositionChar;
//add rest value to parent node
if (tagExp) {
childNode.val = tagExp;
}
} else {
currentNode.val = (currentNode.val || '') + (tagExp || '');
}
i = closeIndex + 2;
}else {//Opening tag
const result = closingIndexForOpeningTag(xmlData, i+1);
let tagExp = result.data;
const closeIndex = result.index;
const separatorIndex = tagExp.indexOf(" ");
let tagName = tagExp;
let shouldBuildAttributesMap = true;
if(separatorIndex !== -1){
tagName = tagExp.substr(0, separatorIndex).replace(/\s\s*$/, '');
tagExp = tagExp.substr(separatorIndex + 1);
}
if(options.ignoreNameSpace){
const colonIndex = tagName.indexOf(":");
if(colonIndex !== -1){
tagName = tagName.substr(colonIndex+1);
shouldBuildAttributesMap = tagName !== result.data.substr(colonIndex + 1);
}
}
//save text to parent node
if (currentNode && textData) {
if(currentNode.tagname !== '!xml'){
currentNode.val = util.getValue(currentNode.val) + '' + processTagValue( currentNode.tagname, textData, options);
}
}
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){//selfClosing tag
if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
tagName = tagName.substr(0, tagName.length - 1);
tagExp = tagName;
}else {
tagExp = tagExp.substr(0, tagExp.length - 1);
}
const childNode = new xmlNode(tagName, currentNode, '');
if(tagName !== tagExp){
childNode.attrsMap = buildAttributesMap(tagExp, options);
}
currentNode.addChild(childNode);
}else {//opening tag
const childNode = new xmlNode( tagName, currentNode );
if (options.stopNodes.length && options.stopNodes.includes(childNode.tagname)) {
childNode.startIndex=closeIndex;
}
if(tagName !== tagExp && shouldBuildAttributesMap){
childNode.attrsMap = buildAttributesMap(tagExp, options);
}
currentNode.addChild(childNode);
currentNode = childNode;
}
textData = "";
i = closeIndex;
}
}else {
textData += xmlData[i];
}
}
return xmlObj;
};
function closingIndexForOpeningTag(data, i){
let attrBoundary;
let tagExp = "";
for (let index = i; index < data.length; index++) {
let ch = data[index];
if (attrBoundary) {
if (ch === attrBoundary) attrBoundary = "";//reset
} else if (ch === '"' || ch === "'") {
attrBoundary = ch;
} else if (ch === '>') {
return {
data: tagExp,
index: index
}
} else if (ch === '\t') {
ch = " ";
}
tagExp += ch;
}
}
function findClosingIndex(xmlData, str, i, errMsg){
const closingIndex = xmlData.indexOf(str, i);
if(closingIndex === -1){
throw new Error(errMsg)
}else {
return closingIndex + str.length - 1;
}
}
var getTraversalObj_1 = getTraversalObj;
var xmlstr2xmlnode = {
defaultOptions: defaultOptions_1,
props: props_1,
getTraversalObj: getTraversalObj_1
};
const defaultOptions$1 = {
allowBooleanAttributes: false, //A tag can have attributes without any value
};
const props$1 = ['allowBooleanAttributes'];
//const tagsPattern = new RegExp("<\\/?([\\w:\\-_\.]+)\\s*\/?>","g");
var validate = function (xmlData, options) {
options = util.buildOptions(options, defaultOptions$1, props$1);
//xmlData = xmlData.replace(/(\r\n|\n|\r)/gm,"");//make it single line
//xmlData = xmlData.replace(/(^\s*<\?xml.*?\?>)/g,"");//Remove XML starting tag
//xmlData = xmlData.replace(/(<!DOCTYPE[\s\w\"\.\/\-\:]+(\[.*\])*\s*>)/g,"");//Remove DOCTYPE
const tags = [];
let tagFound = false;
//indicates that the root tag has been closed (aka. depth 0 has been reached)
let reachedRoot = false;
if (xmlData[0] === '\ufeff') {
// check for byte order mark (BOM)
xmlData = xmlData.substr(1);
}
for (let i = 0; i < xmlData.length; i++) {
if (xmlData[i] === '<' && xmlData[i+1] === '?') {
i+=2;
i = readPI(xmlData,i);
if (i.err) return i;
}else if (xmlData[i] === '<') {
//starting of tag
//read until you reach to '>' avoiding any '>' in attribute value
i++;
if (xmlData[i] === '!') {
i = readCommentAndCDATA(xmlData, i);
continue;
} else {
let closingTag = false;
if (xmlData[i] === '/') {
//closing tag
closingTag = true;
i++;
}
//read tagname
let tagName = '';
for (; i < xmlData.length &&
xmlData[i] !== '>' &&
xmlData[i] !== ' ' &&
xmlData[i] !== '\t' &&
xmlData[i] !== '\n' &&
xmlData[i] !== '\r'; i++
) {
tagName += xmlData[i];
}
tagName = tagName.trim();
//console.log(tagName);
if (tagName[tagName.length - 1] === '/') {
//self closing tag without attributes
tagName = tagName.substring(0, tagName.length - 1);
//continue;
i--;
}
if (!validateTagName(tagName)) {
let msg;
if (tagName.trim().length === 0) {
msg = "There is an unnecessary space between tag name and backward slash '</ ..'.";
} else {
msg = "Tag '"+tagName+"' is an invalid name.";
}
return getErrorObject('InvalidTag', msg, getLineNumberForPosition(xmlData, i));
}
const result = readAttributeStr(xmlData, i);
if (result === false) {
return getErrorObject('InvalidAttr', "Attributes for '"+tagName+"' have open quote.", getLineNumberForPosition(xmlData, i));
}
let attrStr = result.value;
i = result.index;
if (attrStr[attrStr.length - 1] === '/') {
//self closing tag
attrStr = attrStr.substring(0, attrStr.length - 1);
const isValid = validateAttributeString(attrStr, options);
if (isValid === true) {
tagFound = true;
//continue; //text may presents after self closing tag
} else {
//the result from the nested function returns the position of the error within the attribute
//in order to get the 'true' error line, we need to calculate the position where the attribute begins (i - attrStr.length) and then add the position within the attribute
//this gives us the absolute index in the entire xml, which we can use to find the line at last
return getErrorObject(isValid.err.code, isValid.err.msg, getLineNumberForPosition(xmlData, i - attrStr.length + isValid.err.line));
}
} else if (closingTag) {
if (!result.tagClosed) {
return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' doesn't have proper closing.", getLineNumberForPosition(xmlData, i));
} else if (attrStr.trim().length > 0) {
return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' can't have attributes or invalid starting.", getLineNumberForPosition(xmlData, i));
} else {
const otg = tags.pop();
if (tagName !== otg) {
return getErrorObject('InvalidTag', "Closing tag '"+otg+"' is expected inplace of '"+tagName+"'.", getLineNumberForPosition(xmlData, i));
}
//when there are no more tags, we reached the root level.
if (tags.length == 0) {
reachedRoot = true;
}
}
} else {
const isValid = validateAttributeString(attrStr, options);
if (isValid !== true) {
//the result from the nested function returns the position of the error within the attribute
//in order to get the 'true' error line, we need to calculate the position where the attribute begins (i - attrStr.length) and then add the position within the attribute
//this gives us the absolute index in the entire xml, which we can use to find the line at last
return getErrorObject(isValid.err.code, isValid.err.msg, getLineNumberForPosition(xmlData, i - attrStr.length + isValid.err.line));
}
//if the root level has been reached before ...
if (reachedRoot === true) {
return getErrorObject('InvalidXml', 'Multiple possible root nodes found.', getLineNumberForPosition(xmlData, i));
} else {
tags.push(tagName);
}
tagFound = true;
}
//skip tag text value
//It may include comments and CDATA value
for (i++; i < xmlData.length; i++) {
if (xmlData[i] === '<') {
if (xmlData[i + 1] === '!') {
//comment or CADATA
i++;
i = readCommentAndCDATA(xmlData, i);
continue;
} else if (xmlData[i+1] === '?') {
i = readPI(xmlData, ++i);
if (i.err) return i;
} else {
break;
}
} else if (xmlData[i] === '&') {
const afterAmp = validateAmpersand(xmlData, i);
if (afterAmp == -1)
return getErrorObject('InvalidChar', "char '&' is not expected.", getLineNumberForPosition(xmlData, i));
i = afterAmp;
}
} //end of reading tag text value
if (xmlData[i] === '<') {
i--;
}
}
} else {
if (xmlData[i] === ' ' || xmlData[i] === '\t' || xmlData[i] === '\n' || xmlData[i] === '\r') {
continue;
}
return getErrorObject('InvalidChar', "char '"+xmlData[i]+"' is not expected.", getLineNumberForPosition(xmlData, i));
}
}
if (!tagFound) {
return getErrorObject('InvalidXml', 'Start tag expected.', 1);
} else if (tags.length > 0) {
return getErrorObject('InvalidXml', "Invalid '"+JSON.stringify(tags, null, 4).replace(/\r?\n/g, '')+"' found.", 1);
}
return true;
};
/**
* Read Processing insstructions and skip
* @param {*} xmlData
* @param {*} i
*/
function readPI(xmlData, i) {
var start = i;
for (; i < xmlData.length; i++) {
if (xmlData[i] == '?' || xmlData[i] == ' ') {
//tagname
var tagname = xmlData.substr(start, i - start);
if (i > 5 && tagname === 'xml') {
return getErrorObject('InvalidXml', 'XML declaration allowed only at the start of the document.', getLineNumberForPosition(xmlData, i));
} else if (xmlData[i] == '?' && xmlData[i + 1] == '>') {
//check if valid attribut string
i++;
break;
} else {
continue;
}
}
}
return i;
}
function readCommentAndCDATA(xmlData, i) {
if (xmlData.length > i + 5 && xmlData[i + 1] === '-' && xmlData[i + 2] === '-') {
//comment
for (i += 3; i < xmlData.length; i++) {
if (xmlData[i] === '-' && xmlData[i + 1] === '-' && xmlData[i + 2] === '>') {
i += 2;
break;
}
}
} else if (
xmlData.length > i + 8 &&
xmlData[i + 1] === 'D' &&
xmlData[i + 2] === 'O' &&
xmlData[i + 3] === 'C' &&
xmlData[i + 4] === 'T' &&
xmlData[i + 5] === 'Y' &&
xmlData[i + 6] === 'P' &&
xmlData[i + 7] === 'E'
) {
let angleBracketsCount = 1;
for (i += 8; i < xmlData.length; i++) {
if (xmlData[i] === '<') {
angleBracketsCount++;
} else if (xmlData[i] === '>') {
angleBracketsCount--;
if (angleBracketsCount === 0) {
break;
}
}
}
} else if (
xmlData.length > i + 9 &&
xmlData[i + 1] === '[' &&
xmlData[i + 2] === 'C' &&
xmlData[i + 3] === 'D' &&
xmlData[i + 4] === 'A' &&
xmlData[i + 5] === 'T' &&
xmlData[i + 6] === 'A' &&
xmlData[i + 7] === '['
) {
for (i += 8; i < xmlData.length; i++) {
if (xmlData[i] === ']' && xmlData[i + 1] === ']' && xmlData[i + 2] === '>') {
i += 2;
break;
}
}
}
return i;
}
var doubleQuote = '"';
var singleQuote = "'";
/**
* Keep reading xmlData until '<' is found outside the attribute value.
* @param {string} xmlData
* @param {number} i
*/
function readAttributeStr(xmlData, i) {
let attrStr = '';
let startChar = '';
let tagClosed = false;
for (; i < xmlData.length; i++) {
if (xmlData[i] === doubleQuote || xmlData[i] === singleQuote) {
if (startChar === '') {
startChar = xmlData[i];
} else if (startChar !== xmlData[i]) {
//if vaue is enclosed with double quote then single quotes are allowed inside the value and vice versa
continue;
} else {
startChar = '';
}
} else if (xmlData[i] === '>') {
if (startChar === '') {
tagClosed = true;
break;
}
}
attrStr += xmlData[i];
}
if (startChar !== '') {
return false;
}
return {
value: attrStr,
index: i,
tagClosed: tagClosed
};
}
/**
* Select all the attributes whether valid or invalid.
*/
const validAttrStrRegxp = new RegExp('(\\s*)([^\\s=]+)(\\s*=)?(\\s*([\'"])(([\\s\\S])*?)\\5)?', 'g');
//attr, ="sd", a="amit's", a="sd"b="saf", ab cd=""
function validateAttributeString(attrStr, options) {
//console.log("start:"+attrStr+":end");
//if(attrStr.trim().length === 0) return true; //empty string
const matches = util.getAllMatches(attrStr, validAttrStrRegxp);
const attrNames = {};
for (let i = 0; i < matches.length; i++) {
if (matches[i][1].length === 0) {
//nospace before attribute name: a="sd"b="saf"
return getErrorObject('InvalidAttr', "Attribute '"+matches[i][2]+"' has no space in starting.", getPositionFromMatch(attrStr, matches[i][0]))
} else if (matches[i][3] === undefined && !options.allowBooleanAttributes) {
//independent attribute: ab
return getErrorObject('InvalidAttr', "boolean attribute '"+matches[i][2]+"' is not allowed.", getPositionFromMatch(attrStr, matches[i][0]));
}
/* else if(matches[i][6] === undefined){//attribute without value: ab=
return { err: { code:"InvalidAttr",msg:"attribute " + matches[i][2] + " has no value assigned."}};
} */
const attrName = matches[i][2];
if (!validateAttrName(attrName)) {
return getErrorObject('InvalidAttr', "Attribute '"+attrName+"' is an invalid name.", getPositionFromMatch(attrStr, matches[i][0]));
}
if (!attrNames.hasOwnProperty(attrName)) {
//check for duplicate attribute.
attrNames[attrName] = 1;
} else {
return getErrorObject('InvalidAttr', "Attribute '"+attrName+"' is repeated.", getPositionFromMatch(attrStr, matches[i][0]));
}
}
return true;
}
function validateNumberAmpersand(xmlData, i) {
let re = /\d/;
if (xmlData[i] === 'x') {
i++;
re = /[\da-fA-F]/;
}
for (; i < xmlData.length; i++) {
if (xmlData[i] === ';')
return i;
if (!xmlData[i].match(re))
break;
}
return -1;
}
function validateAmpersand(xmlData, i) {
// https://www.w3.org/TR/xml/#dt-charref
i++;
if (xmlData[i] === ';')
return -1;
if (xmlData[i] === '#') {
i++;
return validateNumberAmpersand(xmlData, i);
}
let count = 0;
for (; i < xmlData.length; i++, count++) {
if (xmlData[i].match(/\w/) && count < 20)
continue;
if (xmlData[i] === ';')
break;
return -1;
}
return i;
}
function getErrorObject(code, message, lineNumber) {
return {
err: {
code: code,
msg: message,
line: lineNumber,
},
};
}
function validateAttrName(attrName) {
return util.isName(attrName);
}
// const startsWithXML = /^xml/i;
function validateTagName(tagname) {
return util.isName(tagname) /* && !tagname.match(startsWithXML) */;
}
//this function returns the line number for the character at the given index
function getLineNumberForPosition(xmlData, index) {
var lines = xmlData.substring(0, index).split(/\r?\n/);
return lines.length;
}
//this function returns the position of the last character of match within attrStr
function getPositionFromMatch(attrStr, match) {
return attrStr.indexOf(match) + match.length;
}
var validator = {
validate: validate
};
const char = function(a) {
return String.fromCharCode(a);
};
const chars = {
nilChar: char(176),
missingChar: char(201),
nilPremitive: char(175),
missingPremitive: char(200),
emptyChar: char(178),
emptyValue: char(177), //empty Premitive
boundryChar: char(179),
objStart: char(198),
arrStart: char(204),
arrayEnd: char(185),
};
const charsArr = [
chars.nilChar,
chars.nilPremitive,
chars.missingChar,
chars.missingPremitive,
chars.boundryChar,
chars.emptyChar,
chars.emptyValue,
chars.arrayEnd,
chars.objStart,
chars.arrStart,
];
const _e = function(node, e_schema, options) {
if (typeof e_schema === 'string') {
//premitive
if (node && node[0] && node[0].val !== undefined) {
return getValue(node[0].val);
} else {
return getValue(node);
}
} else {
const hasValidData = hasData(node);
if (hasValidData === true) {
let str = '';
if (Array.isArray(e_schema)) {
//attributes can't be repeated. hence check in children tags only
str += chars.arrStart;
const itemSchema = e_schema[0];
//var itemSchemaType = itemSchema;
const arr_len = node.length;
if (typeof itemSchema === 'string') {
for (let arr_i = 0; arr_i < arr_len; arr_i++) {
const r = getValue(node[arr_i].val);
str = processValue(str, r);
}
} else {
for (let arr_i = 0; arr_i < arr_len; arr_i++) {
const r = _e(node[arr_i], itemSchema, options);
str = processValue(str, r);
}
}
str += chars.arrayEnd; //indicates that next item is not array item
} else {
//object
str += chars.objStart;
const keys = Object.keys(e_schema);
if (Array.isArray(node)) {
node = node[0];
}
for (let i in keys) {
const key = keys[i];
//a property defined in schema can be present either in attrsMap or children tags
//options.textNodeName will not present in both maps, take it's value from val
//options.attrNodeName will be present in attrsMap
let r;
if (!options.ignoreAttributes && node.attrsMap && node.attrsMap[key]) {
r = _e(node.attrsMap[key], e_schema[key], options);
} else if (key === options.textNodeName) {
r = _e(node.val, e_schema[key], options);
} else {
r = _e(node.child[key], e_schema[key], options);
}
str = processValue(str, r);
}
}
return str;
} else {
return hasValidData;
}
}
};
const getValue = function(a /*, type*/) {
switch (a) {
case undefined:
return chars.missingPremitive;
case null:
return chars.nilPremitive;
case '':
return chars.emptyValue;
default:
return a;
}
};
const processValue = function(str, r) {
if (!isAppChar(r[0]) && !isAppChar(str[str.length - 1])) {
str += chars.boundryChar;
}
return str + r;
};
const isAppChar = function(ch) {
return charsArr.indexOf(ch) !== -1;
};
function hasData(jObj) {
if (jObj === undefined) {
return chars.missingChar;
} else if (jObj === null) {
return chars.nilChar;
} else if (
jObj.child &&
Object.keys(jObj.child).length === 0 &&
(!jObj.attrsMap || Object.keys(jObj.attrsMap).length === 0)
) {
return chars.emptyChar;
} else {
return true;
}
}
const buildOptions$2 = util.buildOptions;
const convert2nimn = function(node, e_schema, options) {
options = buildOptions$2(options, xmlstr2xmlnode.defaultOptions, xmlstr2xmlnode.props);
return _e(node, e_schema, options);
};
var convert2nimn_1 = convert2nimn;
var nimndata = {
convert2nimn: convert2nimn_1
};
const buildOptions$1 = util.buildOptions;
//TODO: do it later
const convertToJsonString = function(node, options) {
options = buildOptions$1(options, xmlstr2xmlnode.defaultOptions, xmlstr2xmlnode.props);
options.indentBy = options.indentBy || '';
return _cToJsonStr(node, options);
};
const _cToJsonStr = function(node, options, level) {
let jObj = '{';
//traver through all the children
const keys = Object.keys(node.child);
for (let index = 0; index < keys.length; index++) {
var tagname = keys[index];
if (node.child[tagname] && node.child[tagname].length > 1) {
jObj += '"' + tagname + '" : [ ';
for (var tag in node.child[tagname]) {
jObj += _cToJsonStr(node.child[tagname][tag], options) + ' , ';
}
jObj = jObj.substr(0, jObj.length - 1) + ' ] '; //remove extra comma in last
} else {
jObj += '"' + tagname + '" : ' + _cToJsonStr(node.child[tagname][0], options) + ' ,';
}
}
util.merge(jObj, node.attrsMap);
//add attrsMap as new children
if (util.isEmptyObject(jObj)) {
return util.isExist(node.val) ? node.val : '';
} else {
if (util.isExist(node.val)) {
if (!(typeof node.val === 'string' && (node.val === '' || node.val === options.cdataPositionChar))) {
jObj += '"' + options.textNodeName + '" : ' + stringval(node.val);
}
}
}
//add value
if (jObj[jObj.length - 1] === ',') {
jObj = jObj.substr(0, jObj.length - 2);
}
return jObj + '}';
};
function stringval(v) {
if (v === true || v === false || !isNaN(v)) {
return v;
} else {
return '"' + v + '"';
}
}
var convertToJsonString_1 = convertToJsonString;
var node2json_str = {
convertToJsonString: convertToJsonString_1
};
//parse Empty Node as self closing node
const buildOptions = util.buildOptions;
const defaultOptions = {
attributeNamePrefix: '@_',
attrNodeName: false,
textNodeName: '#text',
ignoreAttributes: true,
cdataTagName: false,
cdataPositionChar: '\\c',
format: false,
indentBy: ' ',
supressEmptyNode: false,
tagValueProcessor: function(a) {
return a;
},
attrValueProcessor: function(a) {
return a;
},
};
const props = [
'attributeNamePrefix',
'attrNodeName',
'textNodeName',
'ignoreAttributes',
'cdataTagName',
'cdataPositionChar',
'format',
'indentBy',
'supressEmptyNode',
'tagValueProcessor',
'attrValueProcessor',
];
function Parser(options) {
this.options = buildOptions(options, defaultOptions, props);
if (this.options.ignoreAttributes || this.options.attrNodeName) {
this.isAttribute = function(/*a*/) {
return false;
};
} else {
this.attrPrefixLen = this.options.attributeNamePrefix.length;
this.isAttribute = isAttribute;
}
if (this.options.cdataTagName) {
this.isCDATA = isCDATA;
} else {
this.isCDATA = function(/*a*/) {
return false;
};
}
this.replaceCDATAstr = replaceCDATAstr;
this.replaceCDATAarr = replaceCDATAarr;
if (this.options.format) {
this.indentate = indentate;
this.tagEndChar = '>\n';
this.newLine = '\n';
} else {
this.indentate = function() {
return '';
};
this.tagEndChar = '>';
this.newLine = '';
}
if (this.options.supressEmptyNode) {
this.buildTextNode = buildEmptyTextNode;
this.buildObjNode = buildEmptyObjNode;
} else {
this.buildTextNode = buildTextValNode;
this.buildObjNode = buildObjectNode;
}
this.buildTextValNode = buildTextValNode;
this.buildObjectNode = buildObjectNode;
}
Parser.prototype.parse = function(jObj) {
return this.j2x(jObj, 0).val;
};
Parser.prototype.j2x = function(jObj, level) {
let attrStr = '';
let val = '';
const keys = Object.keys(jObj);
const len = keys.length;
for (let i = 0; i < len; i++) {
const key = keys[i];
if (typeof jObj[key] === 'undefined') ; else if (jObj[key] === null) {
val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
} else if (jObj[key] instanceof Date) {
val += this.buildTextNode(jObj[key], key, '', level);
} else if (typeof jObj[key] !== 'object') {
//premitive type
const attr = this.isAttribute(key);
if (attr) {
attrStr += ' ' + attr + '="' + this.options.attrValueProcessor('' + jObj[key]) + '"';
} else if (this.isCDATA(key)) {
if (jObj[this.options.textNodeName]) {
val += this.replaceCDATAstr(jObj[this.options.textNodeName], jObj[key]);
} else {
val += this.replaceCDATAstr('', jObj[key]);
}
} else {
//tag value
if (key === this.options.textNodeName) {
if (jObj[this.options.cdataTagName]) ; else {
val += this.options.tagValueProcessor('' + jObj[key]);
}
} else {
val += this.buildTextNode(jObj[key], key, '', level);
}
}
} else if (Array.isArray(jObj[key])) {
//repeated nodes
if (this.isCDATA(key)) {
val += this.indentate(level);
if (jObj[this.options.textNodeName]) {
val += this.replaceCDATAarr(jObj[this.options.textNodeName], jObj[key]);
} else {
val += this.replaceCDATAarr('', jObj[key]);
}
} else {
//nested nodes
const arrLen = jObj[key].length;
for (let j = 0; j < arrLen; j++) {
const item = jObj[key][j];
if (typeof item === 'undefined') ; else if (item === null) {
val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
} else if (typeof item === 'object') {
const result = this.j2x(item, level + 1);
val += this.buildObjNode(result.val, key, result.attrStr, level);
} else {
val += this.buildTextNode(item, key, '', level);
}
}
}
} else {
//nested node
if (this.options.attrNodeName && key === this.options.attrNodeName) {
const Ks = Object.keys(jObj[key]);
const L = Ks.length;
for (let j = 0; j < L; j++) {
attrStr += ' ' + Ks[j] + '="' + this.options.attrValueProcessor('' + jObj[key][Ks[j]]) + '"';
}
} else {
const result = this.j2x(jObj[key], level + 1);
val += this.buildObjNode(result.val, key, result.attrStr, level);
}
}
}
return {attrStr: attrStr, val: val};
};
function replaceCDATAstr(str, cdata) {
str = this.options.tagValueProcessor('' + str);
if (this.options.cdataPositionChar === '' || str === '') {
return str + '<![CDATA[' + cdata + ']]' + this.tagEndChar;
} else {
return str.replace(this.options.cdataPositionChar, '<![CDATA[' + cdata + ']]' + this.tagEndChar);
}
}
function replaceCDATAarr(str, cdata) {
str = this.options.tagValueProcessor('' + str);
if (this.options.cdataPositionChar === '' || str === '') {
return str + '<![CDATA[' + cdata.join(']]><![CDATA[') + ']]' + this.tagEndChar;
} else {
for (let v in cdata) {
str = str.replace(this.options.cdataPositionChar, '<![CDATA[' + cdata[v] + ']]>');
}
return str + this.newLine;
}
}
function buildObjectNode(val, key, attrStr, level) {
if (attrStr && !val.includes('<')) {
return (
this.indentate(level) +
'<' +
key +
attrStr +
'>' +
val +
//+ this.newLine
// + this.indentate(level)
'</' +
key +
this.tagEndChar
);
} else {
return (
this.indentate(level) +
'<' +
key +
attrStr +
this.tagEndChar +
val +
//+ this.newLine
this.indentate(level) +
'</' +
key +
this.tagEndChar
);
}
}
function buildEmptyObjNode(val, key, attrStr, level) {
if (val !== '') {
return this.buildObjectNode(val, key, attrStr, level);
} else {
return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
//+ this.newLine
}
}
function buildTextValNode(val, key, attrStr, level) {
return (
this.indentate(level) +
'<' +
key +
attrStr +
'>' +
this.options.tagValueProcessor(val) +
'</' +
key +
this.tagEndChar
);
}
function buildEmptyTextNode(val, key, attrStr, level) {
if (val !== '') {
return this.buildTextValNode(val, key, attrStr, level);
} else {
return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
}
}
function indentate(level) {
return this.options.indentBy.repeat(level);
}
function isAttribute(name /*, options*/) {
if (name.startsWith(this.options.attributeNamePrefix)) {
return name.substr(this.attrPrefixLen);
} else {
return false;
}
}
function isCDATA(name) {
return name === this.options.cdataTagName;
}
//formatting
//indentation
//\n after each closing or self closing tag
var json2xml = Parser;
var parser = createCommonjsModule(function (module, exports) {
const x2xmlnode = xmlstr2xmlnode;
const buildOptions = util.buildOptions;
exports.parse = function(xmlData, givenOptions = {}, validationOption) {
if( validationOption){
if(validationOption === true) validationOption = {};
const result = validator.validate(xmlData, validationOption);
if (result !== true) {
throw Error( result.err.msg)
}
}
if(givenOptions.parseTrueNumberOnly
&& givenOptions.parseNodeValue !== false
&& !givenOptions.numParseOptions){
givenOptions.numParseOptions = {
leadingZeros: false,
};
}
let options = buildOptions(givenOptions, x2xmlnode.defaultOptions, x2xmlnode.props);
const traversableObj = xmlstr2xmlnode.getTraversalObj(xmlData, options);
//print(traversableObj, " ");
return node2json.convertToJson(traversableObj, options);
};
exports.convertTonimn = nimndata.convert2nimn;
exports.getTraversalObj = xmlstr2xmlnode.getTraversalObj;
exports.convertToJson = node2json.convertToJson;
exports.convertToJsonString = node2json_str.convertToJsonString;
exports.validate = validator.validate;
exports.j2xParser = json2xml;
exports.parseToNimn = function(xmlData, schema, options) {
return exports.convertTonimn(exports.getTraversalObj(xmlData, options), schema, options);
};
});
const isSvg = input => {
if (input === undefined || input === null) {
return false;
}
input = input.toString().trim();
if (input.length === 0) {
return false;
}
// Has to be `!==` as it can also return an object with error info.
if (parser.validate(input) !== true) {
return false;
}
let jsonObject;
try {
jsonObject = parser.parse(input);
} catch (_) {
return false;
}
if (!jsonObject) {
return false;
}
if (!('svg' in jsonObject)) {
return false;
}
return true;
};
var isSvg_1 = isSvg;
// TODO: Remove this for the next major release
var _default$5 = isSvg;
isSvg_1.default = _default$5;
var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
var escapeStringRegexp = function (str) {
if (typeof str !== 'string') {
throw new TypeError('Expected a string');
}
return str.replace(matchOperatorsRe, '\\$&');
};
var trimRepeated = function (str, target) {
if (typeof str !== 'string' || typeof target !== 'string') {
throw new TypeError('Expected a string');
}
return str.replace(new RegExp('(?:' + escapeStringRegexp(target) + '){2,}', 'g'), target);
};
/* eslint-disable no-control-regex */
// TODO: remove parens when Node.js 6 is targeted. Node.js 4 barfs at it.
var filenameReservedRegex = () => (/[<>:"\/\\|?*\x00-\x1F]/g);
var windowsNames = () => (/^(con|prn|aux|nul|com[0-9]|lpt[0-9])$/i);
filenameReservedRegex.windowsNames = windowsNames;
var stripOuter = function (str, sub) {
if (typeof str !== 'string' || typeof sub !== 'string') {
throw new TypeError();
}
sub = escapeStringRegexp(sub);
return str.replace(new RegExp('^' + sub + '|' + sub + '$', 'g'), '');
};
// Doesn't make sense to have longer filenames
const MAX_FILENAME_LENGTH = 100;
const reControlChars = /[\u0000-\u001F\u0080-\u009F]/g; // eslint-disable-line no-control-regex
const reRelativePath = /^\.+/;
const reTrailingPeriods = /\.+$/;
function filenamify(string, options = {}) {
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}
const replacement = options.replacement === undefined ? '!' : options.replacement;
if (filenameReservedRegex().test(replacement) && reControlChars.test(replacement)) {
throw new Error('Replacement string cannot contain reserved filename characters');
}
string = string.replace(filenameReservedRegex(), replacement);
string = string.replace(reControlChars, replacement);
string = string.replace(reRelativePath, replacement);
string = string.replace(reTrailingPeriods, '');
if (replacement.length > 0) {
string = trimRepeated(string, replacement);
string = string.length > 1 ? stripOuter(string, replacement) : string;
}
string = filenameReservedRegex.windowsNames().test(string) ? string + replacement : string;
string = string.slice(0, typeof options.maxLength === 'number' ? options.maxLength : MAX_FILENAME_LENGTH);
return string;
}
const FILENAME_TEMPLATE = "media";
const MAX_FILENAME_INDEX = 1000;
const FILENAME_ATTEMPTS = 5;
const EXTERNAL_MEDIA_LINK_PATTERN = /\!\[(?<anchor>.*?)\]\((?<link>.+?)\)/g;
const DIRTY_IMAGE_TAG = /\[\!\[\[(?<anchor>.*?)\]\]\((?<link>.+?)\)\]/g;
const ANY_URL_PATTERN = /[a-zA-Z\d]+:\/\/(\w+:\w+@)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(\/.*)?/i;
// Looks like timeouts in Obsidian API are set in milliseconds
const NOTICE_TIMEOUT = 10 * 1000;
const TIMEOUT_LIKE_INFINITY = 24 * 60 * 60 * 1000;
const FORBIDDEN_SYMBOLS_FILENAME_PATTERN = /\s+/g;
const DEFAULT_SETTINGS = {
realTimeUpdate: false,
realTimeUpdateInterval: 1000,
realTimeAttemptsToProcess: 3,
cleanContent: true,
showNotifications: false,
include: ".*\\.md",
mediaRootDirectory: "media",
};
/*
https://stackoverflow.com/a/48032528/1020973
It will be better to do it type-correct.
*/
function replaceAsync(str, regex, asyncFn) {
return __awaiter(this, void 0, void 0, function* () {
const promises = [];
str.replace(regex, (match, ...args) => {
const promise = asyncFn(match, ...args);
promises.push(promise);
});
const data = yield Promise.all(promises);
return str.replace(regex, () => data.shift());
});
}
function isUrl(link) {
try {
return Boolean(new URL(link));
}
catch (_) {
return false;
}
}
function downloadImage(url) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield got(url, { responseType: "buffer" });
return res.body;
});
}
function fileExtByContent(content) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const fileExt = (_a = (yield fileType_1.fromBuffer(content))) === null || _a === void 0 ? void 0 : _a.ext;
// if XML, probably it is SVG
if (fileExt == "xml") {
const buffer = Buffer.from(content);
if (isSvg_1(buffer))
return "svg";
}
return fileExt;
});
}
function recreateImageTag(match, anchor, link) {
return `![${anchor}](${link})`;
}
function cleanContent(content) {
const cleanedContent = content.replace(DIRTY_IMAGE_TAG, recreateImageTag);
return cleanedContent;
}
function cleanFileName(name) {
const cleanedName = filenamify(name).replace(FORBIDDEN_SYMBOLS_FILENAME_PATTERN, "_");
return cleanedName;
}
function pathJoin(dir, subpath) {
const result = path__default['default'].join(dir, subpath);
// it seems that obsidian do not understand paths with backslashes in Windows, so turn them into forward slashes
return result.replace(/\\/g, "/");
}
/**
C-like unsigned 32 bits integers in Javascript
Copyright (C) 2013, Pierre Curto
MIT license
*/
var uint32 = createCommonjsModule(function (module) {
(function (root) {
// Local cache for typical radices
({
36: UINT32( Math.pow(36, 5) )
, 16: UINT32( Math.pow(16, 7) )
, 10: UINT32( Math.pow(10, 9) )
, 2: UINT32( Math.pow(2, 30) )
});
({
36: UINT32(36)
, 16: UINT32(16)
, 10: UINT32(10)
, 2: UINT32(2)
});
/**
* Represents an unsigned 32 bits integer
* @constructor
* @param {Number|String|Number} low bits | integer as a string | integer as a number
* @param {Number|Number|Undefined} high bits | radix (optional, default=10)
* @return
*/
function UINT32 (l, h) {
if ( !(this instanceof UINT32) )
return new UINT32(l, h)
this._low = 0;
this._high = 0;
this.remainder = null;
if (typeof h == 'undefined')
return fromNumber.call(this, l)
if (typeof l == 'string')
return fromString.call(this, l, h)
fromBits.call(this, l, h);
}
/**
* Set the current _UINT32_ object with its low and high bits
* @method fromBits
* @param {Number} low bits
* @param {Number} high bits
* @return ThisExpression
*/
function fromBits (l, h) {
this._low = l | 0;
this._high = h | 0;
return this
}
UINT32.prototype.fromBits = fromBits;
/**
* Set the current _UINT32_ object from a number
* @method fromNumber
* @param {Number} number
* @return ThisExpression
*/
function fromNumber (value) {
this._low = value & 0xFFFF;
this._high = value >>> 16;
return this
}
UINT32.prototype.fromNumber = fromNumber;
/**
* Set the current _UINT32_ object from a string
* @method fromString
* @param {String} integer as a string
* @param {Number} radix (optional, default=10)
* @return ThisExpression
*/
function fromString (s, radix) {
var value = parseInt(s, radix || 10);
this._low = value & 0xFFFF;
this._high = value >>> 16;
return this
}
UINT32.prototype.fromString = fromString;
/**
* Convert this _UINT32_ to a number
* @method toNumber
* @return {Number} the converted UINT32
*/
UINT32.prototype.toNumber = function () {
return (this._high * 65536) + this._low
};
/**
* Convert this _UINT32_ to a string
* @method toString
* @param {Number} radix (optional, default=10)
* @return {String} the converted UINT32
*/
UINT32.prototype.toString = function (radix) {
return this.toNumber().toString(radix || 10)
};
/**
* Add two _UINT32_. The current _UINT32_ stores the result
* @method add
* @param {Object} other UINT32
* @return ThisExpression
*/
UINT32.prototype.add = function (other) {
var a00 = this._low + other._low;
var a16 = a00 >>> 16;
a16 += this._high + other._high;
this._low = a00 & 0xFFFF;
this._high = a16 & 0xFFFF;
return this
};
/**
* Subtract two _UINT32_. The current _UINT32_ stores the result
* @method subtract
* @param {Object} other UINT32
* @return ThisExpression
*/
UINT32.prototype.subtract = function (other) {
//TODO inline
return this.add( other.clone().negate() )
};
/**
* Multiply two _UINT32_. The current _UINT32_ stores the result
* @method multiply
* @param {Object} other UINT32
* @return ThisExpression
*/
UINT32.prototype.multiply = function (other) {
/*
a = a00 + a16
b = b00 + b16
a*b = (a00 + a16)(b00 + b16)
= a00b00 + a00b16 + a16b00 + a16b16
a16b16 overflows the 32bits
*/
var a16 = this._high;
var a00 = this._low;
var b16 = other._high;
var b00 = other._low;
/* Removed to increase speed under normal circumstances (i.e. not multiplying by 0 or 1)
// this == 0 or other == 1: nothing to do
if ((a00 == 0 && a16 == 0) || (b00 == 1 && b16 == 0)) return this
// other == 0 or this == 1: this = other
if ((b00 == 0 && b16 == 0) || (a00 == 1 && a16 == 0)) {
this._low = other._low
this._high = other._high
return this
}
*/
var c16, c00;
c00 = a00 * b00;
c16 = c00 >>> 16;
c16 += a16 * b00;
c16 &= 0xFFFF; // Not required but improves performance
c16 += a00 * b16;
this._low = c00 & 0xFFFF;
this._high = c16 & 0xFFFF;
return this
};
/**
* Divide two _UINT32_. The current _UINT32_ stores the result.
* The remainder is made available as the _remainder_ property on
* the _UINT32_ object. It can be null, meaning there are no remainder.
* @method div
* @param {Object} other UINT32
* @return ThisExpression
*/
UINT32.prototype.div = function (other) {
if ( (other._low == 0) && (other._high == 0) ) throw Error('division by zero')
// other == 1
if (other._high == 0 && other._low == 1) {
this.remainder = new UINT32(0);
return this
}
// other > this: 0
if ( other.gt(this) ) {
this.remainder = this.clone();
this._low = 0;
this._high = 0;
return this
}
// other == this: 1
if ( this.eq(other) ) {
this.remainder = new UINT32(0);
this._low = 1;
this._high = 0;
return this
}
// Shift the divisor left until it is higher than the dividend
var _other = other.clone();
var i = -1;
while ( !this.lt(_other) ) {
// High bit can overflow the default 16bits
// Its ok since we right shift after this loop
// The overflown bit must be kept though
_other.shiftLeft(1, true);
i++;
}
// Set the remainder
this.remainder = this.clone();
// Initialize the current result to 0
this._low = 0;
this._high = 0;
for (; i >= 0; i--) {
_other.shiftRight(1);
// If shifted divisor is smaller than the dividend
// then subtract it from the dividend
if ( !this.remainder.lt(_other) ) {
this.remainder.subtract(_other);
// Update the current result
if (i >= 16) {
this._high |= 1 << (i - 16);
} else {
this._low |= 1 << i;
}
}
}
return this
};
/**
* Negate the current _UINT32_
* @method negate
* @return ThisExpression
*/
UINT32.prototype.negate = function () {
var v = ( ~this._low & 0xFFFF ) + 1;
this._low = v & 0xFFFF;
this._high = (~this._high + (v >>> 16)) & 0xFFFF;
return this
};
/**
* Equals
* @method eq
* @param {Object} other UINT32
* @return {Boolean}
*/
UINT32.prototype.equals = UINT32.prototype.eq = function (other) {
return (this._low == other._low) && (this._high == other._high)
};
/**
* Greater than (strict)
* @method gt
* @param {Object} other UINT32
* @return {Boolean}
*/
UINT32.prototype.greaterThan = UINT32.prototype.gt = function (other) {
if (this._high > other._high) return true
if (this._high < other._high) return false
return this._low > other._low
};
/**
* Less than (strict)
* @method lt
* @param {Object} other UINT32
* @return {Boolean}
*/
UINT32.prototype.lessThan = UINT32.prototype.lt = function (other) {
if (this._high < other._high) return true
if (this._high > other._high) return false
return this._low < other._low
};
/**
* Bitwise OR
* @method or
* @param {Object} other UINT32
* @return ThisExpression
*/
UINT32.prototype.or = function (other) {
this._low |= other._low;
this._high |= other._high;
return this
};
/**
* Bitwise AND
* @method and
* @param {Object} other UINT32
* @return ThisExpression
*/
UINT32.prototype.and = function (other) {
this._low &= other._low;
this._high &= other._high;
return this
};
/**
* Bitwise NOT
* @method not
* @return ThisExpression
*/
UINT32.prototype.not = function() {
this._low = ~this._low & 0xFFFF;
this._high = ~this._high & 0xFFFF;
return this
};
/**
* Bitwise XOR
* @method xor
* @param {Object} other UINT32
* @return ThisExpression
*/
UINT32.prototype.xor = function (other) {
this._low ^= other._low;
this._high ^= other._high;
return this
};
/**
* Bitwise shift right
* @method shiftRight
* @param {Number} number of bits to shift
* @return ThisExpression
*/
UINT32.prototype.shiftRight = UINT32.prototype.shiftr = function (n) {
if (n > 16) {
this._low = this._high >> (n - 16);
this._high = 0;
} else if (n == 16) {
this._low = this._high;
this._high = 0;
} else {
this._low = (this._low >> n) | ( (this._high << (16-n)) & 0xFFFF );
this._high >>= n;
}
return this
};
/**
* Bitwise shift left
* @method shiftLeft
* @param {Number} number of bits to shift
* @param {Boolean} allow overflow
* @return ThisExpression
*/
UINT32.prototype.shiftLeft = UINT32.prototype.shiftl = function (n, allowOverflow) {
if (n > 16) {
this._high = this._low << (n - 16);
this._low = 0;
if (!allowOverflow) {
this._high &= 0xFFFF;
}
} else if (n == 16) {
this._high = this._low;
this._low = 0;
} else {
this._high = (this._high << n) | (this._low >> (16-n));
this._low = (this._low << n) & 0xFFFF;
if (!allowOverflow) {
// Overflow only allowed on the high bits...
this._high &= 0xFFFF;
}
}
return this
};
/**
* Bitwise rotate left
* @method rotl
* @param {Number} number of bits to rotate
* @return ThisExpression
*/
UINT32.prototype.rotateLeft = UINT32.prototype.rotl = function (n) {
var v = (this._high << 16) | this._low;
v = (v << n) | (v >>> (32 - n));
this._low = v & 0xFFFF;
this._high = v >>> 16;
return this
};
/**
* Bitwise rotate right
* @method rotr
* @param {Number} number of bits to rotate
* @return ThisExpression
*/
UINT32.prototype.rotateRight = UINT32.prototype.rotr = function (n) {
var v = (this._high << 16) | this._low;
v = (v >>> n) | (v << (32 - n));
this._low = v & 0xFFFF;
this._high = v >>> 16;
return this
};
/**
* Clone the current _UINT32_
* @method clone
* @return {Object} cloned UINT32
*/
UINT32.prototype.clone = function () {
return new UINT32(this._low, this._high)
};
if (module.exports) {
// Node.js
module.exports = UINT32;
} else {
// Browser
root['UINT32'] = UINT32;
}
})(commonjsGlobal);
});
/**
C-like unsigned 64 bits integers in Javascript
Copyright (C) 2013, Pierre Curto
MIT license
*/
var uint64 = createCommonjsModule(function (module) {
(function (root) {
// Local cache for typical radices
var radixPowerCache = {
16: UINT64( Math.pow(16, 5) )
, 10: UINT64( Math.pow(10, 5) )
, 2: UINT64( Math.pow(2, 5) )
};
var radixCache = {
16: UINT64(16)
, 10: UINT64(10)
, 2: UINT64(2)
};
/**
* Represents an unsigned 64 bits integer
* @constructor
* @param {Number} first low bits (8)
* @param {Number} second low bits (8)
* @param {Number} first high bits (8)
* @param {Number} second high bits (8)
* or
* @param {Number} low bits (32)
* @param {Number} high bits (32)
* or
* @param {String|Number} integer as a string | integer as a number
* @param {Number|Undefined} radix (optional, default=10)
* @return
*/
function UINT64 (a00, a16, a32, a48) {
if ( !(this instanceof UINT64) )
return new UINT64(a00, a16, a32, a48)
this.remainder = null;
if (typeof a00 == 'string')
return fromString.call(this, a00, a16)
if (typeof a16 == 'undefined')
return fromNumber.call(this, a00)
fromBits.apply(this, arguments);
}
/**
* Set the current _UINT64_ object with its low and high bits
* @method fromBits
* @param {Number} first low bits (8)
* @param {Number} second low bits (8)
* @param {Number} first high bits (8)
* @param {Number} second high bits (8)
* or
* @param {Number} low bits (32)
* @param {Number} high bits (32)
* @return ThisExpression
*/
function fromBits (a00, a16, a32, a48) {
if (typeof a32 == 'undefined') {
this._a00 = a00 & 0xFFFF;
this._a16 = a00 >>> 16;
this._a32 = a16 & 0xFFFF;
this._a48 = a16 >>> 16;
return this
}
this._a00 = a00 | 0;
this._a16 = a16 | 0;
this._a32 = a32 | 0;
this._a48 = a48 | 0;
return this
}
UINT64.prototype.fromBits = fromBits;
/**
* Set the current _UINT64_ object from a number
* @method fromNumber
* @param {Number} number
* @return ThisExpression
*/
function fromNumber (value) {
this._a00 = value & 0xFFFF;
this._a16 = value >>> 16;
this._a32 = 0;
this._a48 = 0;
return this
}
UINT64.prototype.fromNumber = fromNumber;
/**
* Set the current _UINT64_ object from a string
* @method fromString
* @param {String} integer as a string
* @param {Number} radix (optional, default=10)
* @return ThisExpression
*/
function fromString (s, radix) {
radix = radix || 10;
this._a00 = 0;
this._a16 = 0;
this._a32 = 0;
this._a48 = 0;
/*
In Javascript, bitwise operators only operate on the first 32 bits
of a number, even though parseInt() encodes numbers with a 53 bits
mantissa.
Therefore UINT64(<Number>) can only work on 32 bits.
The radix maximum value is 36 (as per ECMA specs) (26 letters + 10 digits)
maximum input value is m = 32bits as 1 = 2^32 - 1
So the maximum substring length n is:
36^(n+1) - 1 = 2^32 - 1
36^(n+1) = 2^32
(n+1)ln(36) = 32ln(2)
n = 32ln(2)/ln(36) - 1
n = 5.189644915687692
n = 5
*/
var radixUint = radixPowerCache[radix] || new UINT64( Math.pow(radix, 5) );
for (var i = 0, len = s.length; i < len; i += 5) {
var size = Math.min(5, len - i);
var value = parseInt( s.slice(i, i + size), radix );
this.multiply(
size < 5
? new UINT64( Math.pow(radix, size) )
: radixUint
)
.add( new UINT64(value) );
}
return this
}
UINT64.prototype.fromString = fromString;
/**
* Convert this _UINT64_ to a number (last 32 bits are dropped)
* @method toNumber
* @return {Number} the converted UINT64
*/
UINT64.prototype.toNumber = function () {
return (this._a16 * 65536) + this._a00
};
/**
* Convert this _UINT64_ to a string
* @method toString
* @param {Number} radix (optional, default=10)
* @return {String} the converted UINT64
*/
UINT64.prototype.toString = function (radix) {
radix = radix || 10;
var radixUint = radixCache[radix] || new UINT64(radix);
if ( !this.gt(radixUint) ) return this.toNumber().toString(radix)
var self = this.clone();
var res = new Array(64);
for (var i = 63; i >= 0; i--) {
self.div(radixUint);
res[i] = self.remainder.toNumber().toString(radix);
if ( !self.gt(radixUint) ) break
}
res[i-1] = self.toNumber().toString(radix);
return res.join('')
};
/**
* Add two _UINT64_. The current _UINT64_ stores the result
* @method add
* @param {Object} other UINT64
* @return ThisExpression
*/
UINT64.prototype.add = function (other) {
var a00 = this._a00 + other._a00;
var a16 = a00 >>> 16;
a16 += this._a16 + other._a16;
var a32 = a16 >>> 16;
a32 += this._a32 + other._a32;
var a48 = a32 >>> 16;
a48 += this._a48 + other._a48;
this._a00 = a00 & 0xFFFF;
this._a16 = a16 & 0xFFFF;
this._a32 = a32 & 0xFFFF;
this._a48 = a48 & 0xFFFF;
return this
};
/**
* Subtract two _UINT64_. The current _UINT64_ stores the result
* @method subtract
* @param {Object} other UINT64
* @return ThisExpression
*/
UINT64.prototype.subtract = function (other) {
return this.add( other.clone().negate() )
};
/**
* Multiply two _UINT64_. The current _UINT64_ stores the result
* @method multiply
* @param {Object} other UINT64
* @return ThisExpression
*/
UINT64.prototype.multiply = function (other) {
/*
a = a00 + a16 + a32 + a48
b = b00 + b16 + b32 + b48
a*b = (a00 + a16 + a32 + a48)(b00 + b16 + b32 + b48)
= a00b00 + a00b16 + a00b32 + a00b48
+ a16b00 + a16b16 + a16b32 + a16b48
+ a32b00 + a32b16 + a32b32 + a32b48
+ a48b00 + a48b16 + a48b32 + a48b48
a16b48, a32b32, a48b16, a48b32 and a48b48 overflow the 64 bits
so it comes down to:
a*b = a00b00 + a00b16 + a00b32 + a00b48
+ a16b00 + a16b16 + a16b32
+ a32b00 + a32b16
+ a48b00
= a00b00
+ a00b16 + a16b00
+ a00b32 + a16b16 + a32b00
+ a00b48 + a16b32 + a32b16 + a48b00
*/
var a00 = this._a00;
var a16 = this._a16;
var a32 = this._a32;
var a48 = this._a48;
var b00 = other._a00;
var b16 = other._a16;
var b32 = other._a32;
var b48 = other._a48;
var c00 = a00 * b00;
var c16 = c00 >>> 16;
c16 += a00 * b16;
var c32 = c16 >>> 16;
c16 &= 0xFFFF;
c16 += a16 * b00;
c32 += c16 >>> 16;
c32 += a00 * b32;
var c48 = c32 >>> 16;
c32 &= 0xFFFF;
c32 += a16 * b16;
c48 += c32 >>> 16;
c32 &= 0xFFFF;
c32 += a32 * b00;
c48 += c32 >>> 16;
c48 += a00 * b48;
c48 &= 0xFFFF;
c48 += a16 * b32;
c48 &= 0xFFFF;
c48 += a32 * b16;
c48 &= 0xFFFF;
c48 += a48 * b00;
this._a00 = c00 & 0xFFFF;
this._a16 = c16 & 0xFFFF;
this._a32 = c32 & 0xFFFF;
this._a48 = c48 & 0xFFFF;
return this
};
/**
* Divide two _UINT64_. The current _UINT64_ stores the result.
* The remainder is made available as the _remainder_ property on
* the _UINT64_ object. It can be null, meaning there are no remainder.
* @method div
* @param {Object} other UINT64
* @return ThisExpression
*/
UINT64.prototype.div = function (other) {
if ( (other._a16 == 0) && (other._a32 == 0) && (other._a48 == 0) ) {
if (other._a00 == 0) throw Error('division by zero')
// other == 1: this
if (other._a00 == 1) {
this.remainder = new UINT64(0);
return this
}
}
// other > this: 0
if ( other.gt(this) ) {
this.remainder = this.clone();
this._a00 = 0;
this._a16 = 0;
this._a32 = 0;
this._a48 = 0;
return this
}
// other == this: 1
if ( this.eq(other) ) {
this.remainder = new UINT64(0);
this._a00 = 1;
this._a16 = 0;
this._a32 = 0;
this._a48 = 0;
return this
}
// Shift the divisor left until it is higher than the dividend
var _other = other.clone();
var i = -1;
while ( !this.lt(_other) ) {
// High bit can overflow the default 16bits
// Its ok since we right shift after this loop
// The overflown bit must be kept though
_other.shiftLeft(1, true);
i++;
}
// Set the remainder
this.remainder = this.clone();
// Initialize the current result to 0
this._a00 = 0;
this._a16 = 0;
this._a32 = 0;
this._a48 = 0;
for (; i >= 0; i--) {
_other.shiftRight(1);
// If shifted divisor is smaller than the dividend
// then subtract it from the dividend
if ( !this.remainder.lt(_other) ) {
this.remainder.subtract(_other);
// Update the current result
if (i >= 48) {
this._a48 |= 1 << (i - 48);
} else if (i >= 32) {
this._a32 |= 1 << (i - 32);
} else if (i >= 16) {
this._a16 |= 1 << (i - 16);
} else {
this._a00 |= 1 << i;
}
}
}
return this
};
/**
* Negate the current _UINT64_
* @method negate
* @return ThisExpression
*/
UINT64.prototype.negate = function () {
var v = ( ~this._a00 & 0xFFFF ) + 1;
this._a00 = v & 0xFFFF;
v = (~this._a16 & 0xFFFF) + (v >>> 16);
this._a16 = v & 0xFFFF;
v = (~this._a32 & 0xFFFF) + (v >>> 16);
this._a32 = v & 0xFFFF;
this._a48 = (~this._a48 + (v >>> 16)) & 0xFFFF;
return this
};
/**
* @method eq
* @param {Object} other UINT64
* @return {Boolean}
*/
UINT64.prototype.equals = UINT64.prototype.eq = function (other) {
return (this._a48 == other._a48) && (this._a00 == other._a00)
&& (this._a32 == other._a32) && (this._a16 == other._a16)
};
/**
* Greater than (strict)
* @method gt
* @param {Object} other UINT64
* @return {Boolean}
*/
UINT64.prototype.greaterThan = UINT64.prototype.gt = function (other) {
if (this._a48 > other._a48) return true
if (this._a48 < other._a48) return false
if (this._a32 > other._a32) return true
if (this._a32 < other._a32) return false
if (this._a16 > other._a16) return true
if (this._a16 < other._a16) return false
return this._a00 > other._a00
};
/**
* Less than (strict)
* @method lt
* @param {Object} other UINT64
* @return {Boolean}
*/
UINT64.prototype.lessThan = UINT64.prototype.lt = function (other) {
if (this._a48 < other._a48) return true
if (this._a48 > other._a48) return false
if (this._a32 < other._a32) return true
if (this._a32 > other._a32) return false
if (this._a16 < other._a16) return true
if (this._a16 > other._a16) return false
return this._a00 < other._a00
};
/**
* Bitwise OR
* @method or
* @param {Object} other UINT64
* @return ThisExpression
*/
UINT64.prototype.or = function (other) {
this._a00 |= other._a00;
this._a16 |= other._a16;
this._a32 |= other._a32;
this._a48 |= other._a48;
return this
};
/**
* Bitwise AND
* @method and
* @param {Object} other UINT64
* @return ThisExpression
*/
UINT64.prototype.and = function (other) {
this._a00 &= other._a00;
this._a16 &= other._a16;
this._a32 &= other._a32;
this._a48 &= other._a48;
return this
};
/**
* Bitwise XOR
* @method xor
* @param {Object} other UINT64
* @return ThisExpression
*/
UINT64.prototype.xor = function (other) {
this._a00 ^= other._a00;
this._a16 ^= other._a16;
this._a32 ^= other._a32;
this._a48 ^= other._a48;
return this
};
/**
* Bitwise NOT
* @method not
* @return ThisExpression
*/
UINT64.prototype.not = function() {
this._a00 = ~this._a00 & 0xFFFF;
this._a16 = ~this._a16 & 0xFFFF;
this._a32 = ~this._a32 & 0xFFFF;
this._a48 = ~this._a48 & 0xFFFF;
return this
};
/**
* Bitwise shift right
* @method shiftRight
* @param {Number} number of bits to shift
* @return ThisExpression
*/
UINT64.prototype.shiftRight = UINT64.prototype.shiftr = function (n) {
n %= 64;
if (n >= 48) {
this._a00 = this._a48 >> (n - 48);
this._a16 = 0;
this._a32 = 0;
this._a48 = 0;
} else if (n >= 32) {
n -= 32;
this._a00 = ( (this._a32 >> n) | (this._a48 << (16-n)) ) & 0xFFFF;
this._a16 = (this._a48 >> n) & 0xFFFF;
this._a32 = 0;
this._a48 = 0;
} else if (n >= 16) {
n -= 16;
this._a00 = ( (this._a16 >> n) | (this._a32 << (16-n)) ) & 0xFFFF;
this._a16 = ( (this._a32 >> n) | (this._a48 << (16-n)) ) & 0xFFFF;
this._a32 = (this._a48 >> n) & 0xFFFF;
this._a48 = 0;
} else {
this._a00 = ( (this._a00 >> n) | (this._a16 << (16-n)) ) & 0xFFFF;
this._a16 = ( (this._a16 >> n) | (this._a32 << (16-n)) ) & 0xFFFF;
this._a32 = ( (this._a32 >> n) | (this._a48 << (16-n)) ) & 0xFFFF;
this._a48 = (this._a48 >> n) & 0xFFFF;
}
return this
};
/**
* Bitwise shift left
* @method shiftLeft
* @param {Number} number of bits to shift
* @param {Boolean} allow overflow
* @return ThisExpression
*/
UINT64.prototype.shiftLeft = UINT64.prototype.shiftl = function (n, allowOverflow) {
n %= 64;
if (n >= 48) {
this._a48 = this._a00 << (n - 48);
this._a32 = 0;
this._a16 = 0;
this._a00 = 0;
} else if (n >= 32) {
n -= 32;
this._a48 = (this._a16 << n) | (this._a00 >> (16-n));
this._a32 = (this._a00 << n) & 0xFFFF;
this._a16 = 0;
this._a00 = 0;
} else if (n >= 16) {
n -= 16;
this._a48 = (this._a32 << n) | (this._a16 >> (16-n));
this._a32 = ( (this._a16 << n) | (this._a00 >> (16-n)) ) & 0xFFFF;
this._a16 = (this._a00 << n) & 0xFFFF;
this._a00 = 0;
} else {
this._a48 = (this._a48 << n) | (this._a32 >> (16-n));
this._a32 = ( (this._a32 << n) | (this._a16 >> (16-n)) ) & 0xFFFF;
this._a16 = ( (this._a16 << n) | (this._a00 >> (16-n)) ) & 0xFFFF;
this._a00 = (this._a00 << n) & 0xFFFF;
}
if (!allowOverflow) {
this._a48 &= 0xFFFF;
}
return this
};
/**
* Bitwise rotate left
* @method rotl
* @param {Number} number of bits to rotate
* @return ThisExpression
*/
UINT64.prototype.rotateLeft = UINT64.prototype.rotl = function (n) {
n %= 64;
if (n == 0) return this
if (n >= 32) {
// A.B.C.D
// B.C.D.A rotl(16)
// C.D.A.B rotl(32)
var v = this._a00;
this._a00 = this._a32;
this._a32 = v;
v = this._a48;
this._a48 = this._a16;
this._a16 = v;
if (n == 32) return this
n -= 32;
}
var high = (this._a48 << 16) | this._a32;
var low = (this._a16 << 16) | this._a00;
var _high = (high << n) | (low >>> (32 - n));
var _low = (low << n) | (high >>> (32 - n));
this._a00 = _low & 0xFFFF;
this._a16 = _low >>> 16;
this._a32 = _high & 0xFFFF;
this._a48 = _high >>> 16;
return this
};
/**
* Bitwise rotate right
* @method rotr
* @param {Number} number of bits to rotate
* @return ThisExpression
*/
UINT64.prototype.rotateRight = UINT64.prototype.rotr = function (n) {
n %= 64;
if (n == 0) return this
if (n >= 32) {
// A.B.C.D
// D.A.B.C rotr(16)
// C.D.A.B rotr(32)
var v = this._a00;
this._a00 = this._a32;
this._a32 = v;
v = this._a48;
this._a48 = this._a16;
this._a16 = v;
if (n == 32) return this
n -= 32;
}
var high = (this._a48 << 16) | this._a32;
var low = (this._a16 << 16) | this._a00;
var _high = (high >>> n) | (low << (32 - n));
var _low = (low >>> n) | (high << (32 - n));
this._a00 = _low & 0xFFFF;
this._a16 = _low >>> 16;
this._a32 = _high & 0xFFFF;
this._a48 = _high >>> 16;
return this
};
/**
* Clone the current _UINT64_
* @method clone
* @return {Object} cloned UINT64
*/
UINT64.prototype.clone = function () {
return new UINT64(this._a00, this._a16, this._a32, this._a48)
};
if (module.exports) {
// Node.js
module.exports = UINT64;
} else {
// Browser
root['UINT64'] = UINT64;
}
})(commonjsGlobal);
});
var UINT32 = uint32;
var UINT64 = uint64;
var cuint = {
UINT32: UINT32,
UINT64: UINT64
};
/**
* Convert string to proper UTF-8 array
*/
function toUTF8Array(str) {
const len = str.length;
const utf8 = [];
for (let i = 0; i < len; i++) {
let c = str.charCodeAt(i);
if (c < 0x80) {
utf8.push(c);
}
else if (c < 0x800) {
utf8.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f));
}
else if (c < 0xd800 || c >= 0xe000) {
utf8.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));
}
else {
// surrogate pair
i++;
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
utf8.push(0xf0 | (c >> 18), 0x80 | ((c >> 12) & 0x3f), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));
}
}
return new Uint8Array(utf8);
}
var _default$4 = toUTF8Array;
var toUtf8Array = /*#__PURE__*/Object.defineProperty({
default: _default$4
}, '__esModule', {value: true});
function default_1(input) {
if (input instanceof ArrayBuffer) {
return new Uint8Array(input);
}
else if (typeof input === "string") {
return toUtf8Array.default(input);
}
return input;
}
var _default$3 = default_1;
var toBuffer = /*#__PURE__*/Object.defineProperty({
default: _default$3
}, '__esModule', {value: true});
function isClonable(obj) {
return obj.hasOwnProperty("clone");
}
class XXHash extends cuint.UINT64 {
/**
* @param seed unsigned 32-bit integer
*/
constructor(uintConstructor) {
super(NaN);
this.uintConstructor = uintConstructor;
}
get vn() {
return [this.v1, this.v2, this.v3, this.v4];
}
getIncrement() {
return this.size / 4;
}
reseed(seed) {
this.seed = isClonable(seed)
? seed.clone()
: this.uintConstructor(seed);
this.v1 = this.seed
.clone()
.add(this.primes.P1)
.add(this.primes.P2);
this.v2 = this.seed.clone().add(this.primes.P2);
this.v3 = this.seed.clone();
this.v4 = this.seed.clone().subtract(this.primes.P1);
this.totalLen = 0;
this.memsize = 0;
this.memory = undefined;
}
/**
* Finalize the hash computation. The hash instance is ready for reuse for the given seed
*/
digest() {
const m = this.memory;
if (m === undefined)
throw new ReferenceError("Hash Memory not set, .update() has to be called before digest()");
const { P5 } = this.primes;
const h = this.totalLen >= this.size
? this.v1
.rotl(1)
.add(this.v2.clone().rotl(7))
.add(this.v3.clone().rotl(12))
.add(this.v4.clone().rotl(18))
: this.seed.clone().add(P5);
const hash = this.digestCore(m, h);
// Reset the state
this.reseed(this.seed);
return hash;
}
/**
* Add data to be computed for the hash
*/
update(v) {
const input = toBuffer.default(v);
const len = input.length;
if (len === 0)
return this;
this.totalLen += len;
const memory = this.memsize === 0
? input instanceof Buffer
? new Buffer(this.size)
: new Uint8Array(this.size)
: this.memory;
if (this.memsize + len < this.size) {
// fill in tmp buffer
// XXH64_memcpy(memory + this.memsize, input, len)
if (input instanceof Buffer) {
input.copy(memory, this.memsize, 0, len);
}
else {
memory.set(input.subarray(0, len), this.memsize);
}
this.memsize += len;
this.memory = memory;
return this;
}
let p = 0;
const bEnd = p + len;
const inc = this.getIncrement();
if (this.memsize > 0) {
// some data left from previous update
// XXH64_memcpy(memory + this.memsize, input, 16-this.memsize);
if (input instanceof Buffer) {
input.copy(memory, this.memsize, 0, this.size - this.memsize);
}
else {
memory.set(input.subarray(0, this.size - this.memsize), this.memsize);
}
let i = 0;
for (const v of this.vn) {
this.shiftUpdate(v, memory, i);
i += inc;
}
p += this.size - this.memsize;
this.memsize = 0;
}
if (p <= bEnd - this.size) {
const limit = bEnd - this.size;
do {
for (const v of this.vn) {
this.shiftUpdate(v, input, p);
p += inc;
}
} while (p <= limit);
}
if (p < bEnd) {
// XXH64_memcpy(memory, p, bEnd-p);
if (input instanceof Buffer) {
input.copy(memory, this.memsize, p, bEnd);
}
else {
memory.set(input.subarray(p, bEnd), this.memsize);
}
this.memsize = bEnd - p;
}
this.memory = memory;
return this;
}
}
var _default$2 = XXHash;
var xxhash = /*#__PURE__*/Object.defineProperty({
default: _default$2
}, '__esModule', {value: true});
class XXHash64 extends xxhash.default {
constructor(seed) {
super(cuint.UINT64);
this.size = 32;
this.primes = {
P1: this.uintConstructor("11400714785074694791"),
P2: this.uintConstructor("14029467366897019727"),
P3: this.uintConstructor("1609587929392839161"),
P4: this.uintConstructor("9650029242287828579"),
P5: this.uintConstructor("2870177450012600261")
};
this.reseed(seed);
}
static hash(seed, input) {
const instance = new this(seed);
if (input === undefined)
return instance;
return instance.update(input).digest();
}
shiftDigest(h, v) {
h.xor(v
.multiply(this.primes.P2)
.rotl(31)
.multiply(this.primes.P1));
h.multiply(this.primes.P1).add(this.primes.P4);
}
shiftUpdate(v, m, p) {
v.add(this.uintConstructor((m[p + 1] << 8) | m[p], (m[p + 3] << 8) | m[p + 2], (m[p + 5] << 8) | m[p + 4], (m[p + 7] << 8) | m[p + 6]).multiply(this.primes.P2))
.rotl(31)
.multiply(this.primes.P1);
}
digestCore(m, h) {
const { P1, P2, P3, P4, P5 } = this.primes;
if (this.totalLen >= this.size) {
for (const v of this.vn) {
this.shiftDigest(h, v);
}
}
const u = this.uintConstructor(NaN);
h.add(u.fromNumber(this.totalLen));
let i = 0;
const inc = this.getIncrement();
while (i <= this.memsize - inc) {
u.fromBits((m[i + 1] << 8) | m[i], (m[i + 3] << 8) | m[i + 2], (m[i + 5] << 8) | m[i + 4], (m[i + 7] << 8) | m[i + 6]);
u.multiply(P2)
.rotl(31)
.multiply(P1);
h.xor(u)
.rotl(27)
.multiply(P1)
.add(P4);
i += inc;
}
if (i + 4 <= this.memsize) {
u.fromBits((m[i + 1] << 8) | m[i], (m[i + 3] << 8) | m[i + 2], 0, 0);
h.xor(u.multiply(P1))
.rotl(23)
.multiply(P2)
.add(P3);
i += 4;
}
while (i < this.memsize) {
u.fromBits(m[i++], 0, 0, 0);
h.xor(u.multiply(P5))
.rotl(11)
.multiply(P1);
}
h.xor(h.clone().shiftRight(33)).multiply(P2);
h.xor(h.clone().shiftRight(29)).multiply(P3);
h.xor(h.clone().shiftRight(32));
return h;
}
}
var _default$1 = XXHash64;
var xxhash64 = /*#__PURE__*/Object.defineProperty({
default: _default$1
}, '__esModule', {value: true});
class XXHash32$1 extends xxhash.default {
constructor(seed) {
super(cuint.UINT32);
this.size = 16;
this.primes = {
P1: this.uintConstructor("2654435761"),
P2: this.uintConstructor("2246822519"),
P3: this.uintConstructor("3266489917"),
P4: this.uintConstructor("668265263"),
P5: this.uintConstructor("374761393")
};
this.reseed(seed);
}
static hash(seed, input) {
const instance = new this(seed);
if (input === undefined)
return instance;
return instance.update(input).digest();
}
/**
* Merged this sequence of method calls as it speeds up
the calculations by a factor of 2
*/
updateUint(uint, low, high) {
const { P1, P2 } = this.primes;
let b00 = P2._low;
let b16 = P2._high;
let c00 = low * b00;
let c16 = c00 >>> 16;
c16 += high * b00;
c16 &= 0xffff; // Not required but improves performance
c16 += low * b16;
let a00 = uint._low + (c00 & 0xffff);
let a16 = a00 >>> 16;
a16 += uint._high + (c16 & 0xffff);
let v = (a16 << 16) | (a00 & 0xffff);
v = (v << 13) | (v >>> 19);
a00 = v & 0xffff;
a16 = v >>> 16;
b00 = P1._low;
b16 = P1._high;
c00 = a00 * b00;
c16 = c00 >>> 16;
c16 += a16 * b00;
c16 &= 0xffff; // Not required but improves performance
c16 += a00 * b16;
uint._low = c00 & 0xffff;
uint._high = c16 & 0xffff;
}
shiftUpdate(v, m, p) {
this.updateUint(v, (m[p + 1] << 8) | m[p], (m[p + 3] << 8) | m[p + 2]);
}
digestCore(m, h) {
const { P1, P2, P3, P4, P5 } = this.primes;
const u = this.uintConstructor(NaN);
h.add(u.fromNumber(this.totalLen));
let i = 0;
const inc = this.getIncrement();
while (i <= this.memsize - inc) {
u.fromBits((m[i + 1] << 8) | m[i], (m[i + 3] << 8) | m[i + 2]);
h.add(u.multiply(P3))
.rotl(17)
.multiply(P4);
i += inc;
}
while (i < this.memsize) {
u.fromBits(m[i++], 0);
h.add(u.multiply(P5))
.rotl(11)
.multiply(P1);
}
h.xor(h.clone().shiftRight(15)).multiply(P2);
h.xor(h.clone().shiftRight(13)).multiply(P3);
h.xor(h.clone().shiftRight(16));
return h;
}
}
var _default = XXHash32$1;
var xxhash32 = /*#__PURE__*/Object.defineProperty({
default: _default
}, '__esModule', {value: true});
xxhash64.default;
var XXHash32 = xxhash32.default;
class LinkHashes {
constructor() {
this.linksInfo = {};
}
ensureHashGenerated(link, data) {
if (!this.linksInfo[link]) {
this.linksInfo[link] = XXHash32.hash(0, data).toNumber();
}
}
isSame(link, data) {
const fileHash = XXHash32.hash(0, data).toNumber();
return this.linksInfo[link] == fileHash;
}
}
const linkHashes = new LinkHashes();
function imageTagProcessor(app, mediaDir) {
function processImageTag(match, anchor, link) {
return __awaiter(this, void 0, void 0, function* () {
if (!isUrl(link)) {
return match;
}
try {
const fileData = yield downloadImage(link);
// when several images refer to the same file they can be partly
// failed to download because file already exists, so try to resuggest filename several times
let attempt = 0;
while (attempt < FILENAME_ATTEMPTS) {
try {
const { fileName, needWrite } = yield chooseFileName(app.vault.adapter, mediaDir, anchor, link, fileData);
if (needWrite && fileName) {
yield app.vault.createBinary(fileName, fileData);
}
if (fileName) {
return `![${anchor}](${fileName})`;
}
else {
return match;
}
}
catch (error) {
if (error.message === "File already exists.") {
attempt++;
}
else {
throw error;
}
}
}
return match;
}
catch (error) {
console.warn("Image processing failed: ", error);
return match;
}
});
}
return processImageTag;
}
function chooseFileName(adapter, dir, baseName, link, contentData) {
return __awaiter(this, void 0, void 0, function* () {
const fileExt = yield fileExtByContent(contentData);
if (!fileExt) {
return { fileName: "", needWrite: false };
}
// if there is no anchor try get file name from url
if (!baseName) {
const parsedUrl = new url_1.URL(link);
baseName = path__default['default'].basename(parsedUrl.pathname);
}
// if there is no part for file name from url use name template
if (!baseName) {
baseName = FILENAME_TEMPLATE;
}
// if filename already ends with correct extension, remove it to work with base name
if (baseName.endsWith(`.${fileExt}`)) {
baseName = baseName.slice(0, -1 * (fileExt.length + 1));
}
baseName = cleanFileName(baseName);
let fileName = "";
let needWrite = true;
let index = 0;
while (!fileName && index < MAX_FILENAME_INDEX) {
const suggestedName = index
? pathJoin(dir, `${baseName}-${index}.${fileExt}`)
: pathJoin(dir, `${baseName}.${fileExt}`);
if (yield adapter.exists(suggestedName, false)) {
linkHashes.ensureHashGenerated(link, contentData);
const fileData = yield adapter.readBinary(suggestedName);
if (linkHashes.isSame(link, fileData)) {
fileName = suggestedName;
needWrite = false;
}
}
else {
fileName = suggestedName;
}
index++;
}
if (!fileName) {
throw new Error("Failed to generate file name for media file.");
}
linkHashes.ensureHashGenerated(link, contentData);
return { fileName, needWrite };
});
}
// Queue that keep only unique values and counts attempts
class UniqueQueue {
constructor() {
this.queue = new Array();
}
push(value, attempts) {
if (attempts < 1) {
console.error("Triyng to enqueue item with attempts < 1");
return;
}
this.remove(value);
this.queue.push({ value, attempts });
}
remove(value) {
this.queue = this.queue.filter((item) => item.value !== value);
}
iterationQueue() {
const extractIteration = (prev, curr) => {
prev.iteration.push(curr.value);
if (curr.attempts > 1) {
prev.queue.push(curr.value, curr.attempts - 1);
}
return prev;
};
const { queue, iteration } = this.queue.reduce(extractIteration, {
queue: new UniqueQueue(),
iteration: [],
});
this.queue = queue.queue;
return iteration;
}
}
class LocalImagesPlugin extends obsidian.Plugin {
constructor() {
super(...arguments);
this.modifiedQueue = new UniqueQueue();
this.intervalId = null;
// using arrow syntax for callbacks to correctly pass this context
this.processActivePage = () => __awaiter(this, void 0, void 0, function* () {
const activeFile = this.app.workspace.getActiveFile();
yield this.proccessPage(activeFile);
});
this.processAllPages = () => __awaiter(this, void 0, void 0, function* () {
const files = this.app.vault.getMarkdownFiles();
const includeRegex = new RegExp(this.settings.include, "i");
const pagesCount = files.length;
const notice = this.settings.showNotifications
? new obsidian.Notice(`Local Images \nStart processing. Total ${pagesCount} pages. `, TIMEOUT_LIKE_INFINITY)
: null;
for (const [index, file] of files.entries()) {
if (file.path.match(includeRegex)) {
if (notice) {
// setMessage() is undeclared but factically existing, so ignore the TS error
// @ts-expect-error
notice.setMessage(`Local Images: Processing \n"${file.path}" \nPage ${index} of ${pagesCount}`);
}
yield this.proccessPage(file, true);
}
}
if (notice) {
// @ts-expect-error
notice.setMessage(`Local Images: ${pagesCount} pages were processed.`);
setTimeout(() => {
notice.hide();
}, NOTICE_TIMEOUT);
}
});
this.processModifiedQueue = () => __awaiter(this, void 0, void 0, function* () {
const iteration = this.modifiedQueue.iterationQueue();
for (const page of iteration) {
this.proccessPage(page);
}
});
}
proccessPage(file, silent = false) {
return __awaiter(this, void 0, void 0, function* () {
// const content = await this.app.vault.read(file);
const content = yield this.app.vault.cachedRead(file);
yield this.ensureFolderExists(this.settings.mediaRootDirectory);
const cleanedContent = this.settings.cleanContent
? cleanContent(content)
: content;
const fixedContent = yield replaceAsync(cleanedContent, EXTERNAL_MEDIA_LINK_PATTERN, imageTagProcessor(this.app, this.settings.mediaRootDirectory));
if (content != fixedContent) {
this.modifiedQueue.remove(file);
yield this.app.vault.modify(file, fixedContent);
if (!silent && this.settings.showNotifications) {
new obsidian.Notice(`Images for "${file.path}" were processed.`);
}
}
else {
if (!silent && this.settings.showNotifications) {
new obsidian.Notice(`Page "${file.path}" has been processed, but nothing was changed.`);
}
}
});
}
onload() {
return __awaiter(this, void 0, void 0, function* () {
yield this.loadSettings();
this.addCommand({
id: "download-images",
name: "Download images locally",
callback: this.processActivePage,
});
this.addCommand({
id: "download-images-all",
name: "Download images locally for all your notes",
callback: this.processAllPages,
});
this.registerCodeMirror((cm) => {
// on("beforeChange") can not execute async function in event handler, so we use queue to pass modified pages to timeouted handler
cm.on("change", (instance, changeObj) => __awaiter(this, void 0, void 0, function* () {
if (changeObj.origin == "paste" &&
ANY_URL_PATTERN.test(changeObj.text)) {
this.enqueueActivePage();
}
}));
});
this.setupQueueInterval();
this.addSettingTab(new SettingTab(this.app, this));
});
}
setupQueueInterval() {
if (this.intervalId) {
const intervalId = this.intervalId;
this.intervalId = null;
window.clearInterval(intervalId);
}
if (this.settings.realTimeUpdate &&
this.settings.realTimeUpdateInterval > 0) {
this.intervalId = window.setInterval(this.processModifiedQueue, this.settings.realTimeUpdateInterval);
this.registerInterval(this.intervalId);
}
}
enqueueActivePage() {
const activeFile = this.app.workspace.getActiveFile();
this.modifiedQueue.push(activeFile, this.settings.realTimeAttemptsToProcess);
}
// It is good idea to create the plugin more verbose
displayError(error, file) {
if (file) {
new obsidian.Notice(`LocalImages: Error while handling file ${file.name}, ${error.toString()}`);
}
else {
new obsidian.Notice(error.toString());
}
console.error(`LocalImages: error: ${error}`);
}
onunload() { }
loadSettings() {
return __awaiter(this, void 0, void 0, function* () {
this.settings = Object.assign({}, DEFAULT_SETTINGS, yield this.loadData());
this.setupQueueInterval();
});
}
saveSettings() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.saveData(this.settings);
}
catch (error) {
this.displayError(error);
}
});
}
ensureFolderExists(folderPath) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.app.vault.createFolder(folderPath);
}
catch (error) {
if (!error.message.contains("Folder already exists")) {
throw error;
}
}
});
}
}
class SettingTab extends obsidian.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
let { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Local images" });
new obsidian.Setting(containerEl)
.setName("On paste processing")
.setDesc("Process active page if external link was pasted.")
.addToggle((toggle) => toggle
.setValue(this.plugin.settings.realTimeUpdate)
.onChange((value) => __awaiter(this, void 0, void 0, function* () {
this.plugin.settings.realTimeUpdate = value;
yield this.plugin.saveSettings();
this.plugin.setupQueueInterval();
})));
new obsidian.Setting(containerEl)
.setName("On paste processing interval")
.setDesc("Interval in milliseconds for processing update.")
.setTooltip("I could not process content on the fly when it is pasted. So real processing implements periodically with the given here timeout.")
.addText((text) => text
.setValue(String(this.plugin.settings.realTimeUpdateInterval))
.onChange((value) => __awaiter(this, void 0, void 0, function* () {
const numberValue = Number(value);
if (isNaN(numberValue) ||
!Number.isInteger(numberValue) ||
numberValue < 0) {
this.plugin.displayError("Realtime processing interval should be a positive integer number!");
return;
}
this.plugin.settings.realTimeUpdateInterval = numberValue;
yield this.plugin.saveSettings();
this.plugin.setupQueueInterval();
})));
new obsidian.Setting(containerEl)
.setName("Attempts to process")
.setDesc("Number of attempts to process content on paste. For me 3 attempts is enouth with 1 second update interval.")
.setTooltip("I could not find the way to access newly pasted content immediatily, after pasting, Plugin's API returns old text for a while. The workaround is to process page several times until content is changed.")
.addText((text) => text
.setValue(String(this.plugin.settings.realTimeAttemptsToProcess))
.onChange((value) => __awaiter(this, void 0, void 0, function* () {
const numberValue = Number(value);
if (isNaN(numberValue) ||
!Number.isInteger(numberValue) ||
numberValue < 1 ||
numberValue > 100) {
this.plugin.displayError("Realtime processing interval should be a positive integer number greater than 1 and lower than 100!");
return;
}
this.plugin.settings.realTimeAttemptsToProcess = numberValue;
yield this.plugin.saveSettings();
})));
new obsidian.Setting(containerEl)
.setName("Clean content")
.setDesc("Clean malformed image tags before processing.")
.addToggle((toggle) => toggle
.setValue(this.plugin.settings.cleanContent)
.onChange((value) => __awaiter(this, void 0, void 0, function* () {
this.plugin.settings.cleanContent = value;
yield this.plugin.saveSettings();
})));
new obsidian.Setting(containerEl)
.setName("Show notifications")
.setDesc("Show notifications when pages were processed.")
.addToggle((toggle) => toggle
.setValue(this.plugin.settings.showNotifications)
.onChange((value) => __awaiter(this, void 0, void 0, function* () {
this.plugin.settings.showNotifications = value;
yield this.plugin.saveSettings();
})));
new obsidian.Setting(containerEl)
.setName("Include")
.setDesc("Include only files matching this regex pattern when running on all notes.")
.addText((text) => text.setValue(this.plugin.settings.include).onChange((value) => __awaiter(this, void 0, void 0, function* () {
if (!safeRegex_1(value)) {
this.plugin.displayError("Unsafe regex! https://www.npmjs.com/package/safe-regex");
return;
}
this.plugin.settings.include = value;
yield this.plugin.saveSettings();
})));
new obsidian.Setting(containerEl)
.setName("Media folder")
.setDesc("Folder to keep all downloaded media files.")
.addText((text) => text
.setValue(this.plugin.settings.mediaRootDirectory)
.onChange((value) => __awaiter(this, void 0, void 0, function* () {
this.plugin.settings.mediaRootDirectory = value;
yield this.plugin.saveSettings();
})));
}
}
module.exports = LocalImagesPlugin;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZXMiOlsiLi4vbm9kZV9tb2R1bGVzL3RzbGliL3RzbGliLmVzNi5qcyIsIi4uL25vZGVfbW9kdWxlcy9zYWZlLXJlZ2V4L2xpYi9hbmFseXplci5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L2NvbXBhdC10cmFuc3BpbGVyL3RyYW5zZm9ybXMvY29tcGF0LWRvdGFsbC1zLXRyYW5zZm9ybS5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L2NvbXBhdC10cmFuc3BpbGVyL3RyYW5zZm9ybXMvY29tcGF0LW5hbWVkLWNhcHR1cmluZy1ncm91cHMtdHJhbnNmb3JtLmpzIiwiLi4vbm9kZV9tb2R1bGVzL3JlZ2V4cC10cmVlL2Rpc3QvY29tcGF0LXRyYW5zcGlsZXIvdHJhbnNmb3Jtcy9jb21wYXQteC1mbGFnLXRyYW5zZm9ybS5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L2NvbXBhdC10cmFuc3BpbGVyL3RyYW5zZm9ybXMvaW5kZXguanMiLCIuLi9ub2RlX21vZHVsZXMvcmVnZXhwLXRyZWUvZGlzdC9nZW5lcmF0b3IvaW5kZXguanMiLCIuLi9ub2RlX21vZHVsZXMvcmVnZXhwLXRyZWUvZGlzdC9wYXJzZXIvdW5pY29kZS9wYXJzZXItdW5pY29kZS1wcm9wZXJ0aWVzLmpzIiwiLi4vbm9kZV9tb2R1bGVzL3JlZ2V4cC10cmVlL2Rpc3QvcGFyc2VyL2dlbmVyYXRlZC9yZWdleHAtdHJlZS5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L3BhcnNlci9pbmRleC5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L3RyYXZlcnNlL25vZGUtcGF0aC5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L3RyYXZlcnNlL2luZGV4LmpzIiwiLi4vbm9kZV9tb2R1bGVzL3JlZ2V4cC10cmVlL2Rpc3QvdHJhbnNmb3JtL2luZGV4LmpzIiwiLi4vbm9kZV9tb2R1bGVzL3JlZ2V4cC10cmVlL2Rpc3QvY29tcGF0LXRyYW5zcGlsZXIvaW5kZXguanMiLCIuLi9ub2RlX21vZHVsZXMvcmVnZXhwLXRyZWUvZGlzdC91dGlscy9jbG9uZS5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L29wdGltaXplci90cmFuc2Zvcm1zL2NoYXItc3Vycm9nYXRlLXBhaXItdG8tc2luZ2xlLXVuaWNvZGUtdHJhbnNmb3JtLmpzIiwiLi4vbm9kZV9tb2R1bGVzL3JlZ2V4cC10cmVlL2Rpc3Qvb3B0aW1pemVyL3RyYW5zZm9ybXMvY2hhci1jb2RlLXRvLXNpbXBsZS1jaGFyLXRyYW5zZm9ybS5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L29wdGltaXplci90cmFuc2Zvcm1zL2NoYXItY2FzZS1pbnNlbnNpdGl2ZS1sb3dlcmNhc2UtdHJhbnNmb3JtLmpzIiwiLi4vbm9kZV9tb2R1bGVzL3JlZ2V4cC10cmVlL2Rpc3Qvb3B0aW1pemVyL3RyYW5zZm9ybXMvY2hhci1jbGFzcy1yZW1vdmUtZHVwbGljYXRlcy10cmFuc2Zvcm0uanMiLCIuLi9ub2RlX21vZHVsZXMvcmVnZXhwLXRyZWUvZGlzdC90cmFuc2Zvcm0vdXRpbHMuanMiLCIuLi9ub2RlX21vZHVsZXMvcmVnZXhwLXRyZWUvZGlzdC9vcHRpbWl6ZXIvdHJhbnNmb3Jtcy9xdWFudGlmaWVycy1tZXJnZS10cmFuc2Zvcm0uanMiLCIuLi9ub2RlX21vZHVsZXMvcmVnZXhwLXRyZWUvZGlzdC9vcHRpbWl6ZXIvdHJhbnNmb3Jtcy9xdWFudGlmaWVyLXJhbmdlLXRvLXN5bWJvbC10cmFuc2Zvcm0uanMiLCIuLi9ub2RlX21vZHVsZXMvcmVnZXhwLXRyZWUvZGlzdC9vcHRpbWl6ZXIvdHJhbnNmb3Jtcy9jaGFyLWNsYXNzLWNsYXNzcmFuZ2VzLXRvLWNoYXJzLXRyYW5zZm9ybS5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L29wdGltaXplci90cmFuc2Zvcm1zL2NoYXItY2xhc3MtdG8tbWV0YS10cmFuc2Zvcm0uanMiLCIuLi9ub2RlX21vZHVsZXMvcmVnZXhwLXRyZWUvZGlzdC9vcHRpbWl6ZXIvdHJhbnNmb3Jtcy9jaGFyLWNsYXNzLXRvLXNpbmdsZS1jaGFyLXRyYW5zZm9ybS5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L29wdGltaXplci90cmFuc2Zvcm1zL2NoYXItZXNjYXBlLXVuZXNjYXBlLXRyYW5zZm9ybS5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L29wdGltaXplci90cmFuc2Zvcm1zL2NoYXItY2xhc3MtY2xhc3NyYW5nZXMtbWVyZ2UtdHJhbnNmb3JtLmpzIiwiLi4vbm9kZV9tb2R1bGVzL3JlZ2V4cC10cmVlL2Rpc3Qvb3B0aW1pemVyL3RyYW5zZm9ybXMvZGlzanVuY3Rpb24tcmVtb3ZlLWR1cGxpY2F0ZXMtdHJhbnNmb3JtLmpzIiwiLi4vbm9kZV9tb2R1bGVzL3JlZ2V4cC10cmVlL2Rpc3Qvb3B0aW1pemVyL3RyYW5zZm9ybXMvZ3JvdXAtc2luZ2xlLWNoYXJzLXRvLWNoYXItY2xhc3MuanMiLCIuLi9ub2RlX21vZHVsZXMvcmVnZXhwLXRyZWUvZGlzdC9vcHRpbWl6ZXIvdHJhbnNmb3Jtcy9yZW1vdmUtZW1wdHktZ3JvdXAtdHJhbnNmb3JtLmpzIiwiLi4vbm9kZV9tb2R1bGVzL3JlZ2V4cC10cmVlL2Rpc3Qvb3B0aW1pemVyL3RyYW5zZm9ybXMvdW5ncm91cC10cmFuc2Zvcm0uanMiLCIuLi9ub2RlX21vZHVsZXMvcmVnZXhwLXRyZWUvZGlzdC9vcHRpbWl6ZXIvdHJhbnNmb3Jtcy9jb21iaW5lLXJlcGVhdGluZy1wYXR0ZXJucy10cmFuc2Zvcm0uanMiLCIuLi9ub2RlX21vZHVsZXMvcmVnZXhwLXRyZWUvZGlzdC9vcHRpbWl6ZXIvdHJhbnNmb3Jtcy9pbmRleC5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L29wdGltaXplci9pbmRleC5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L2ludGVycHJldGVyL2Zpbml0ZS1hdXRvbWF0b24vc3BlY2lhbC1zeW1ib2xzLmpzIiwiLi4vbm9kZV9tb2R1bGVzL3JlZ2V4cC10cmVlL2Rpc3QvaW50ZXJwcmV0ZXIvZmluaXRlLWF1dG9tYXRvbi9uZmEvbmZhLmpzIiwiLi4vbm9kZV9tb2R1bGVzL3JlZ2V4cC10cmVlL2Rpc3QvaW50ZXJwcmV0ZXIvZmluaXRlLWF1dG9tYXRvbi9kZmEvZGZhLW1pbmltaXplci5qcyIsIi4uL25vZGVfbW9kdWxlcy9yZWdleHAtdHJlZS9kaXN0L2ludGVycHJldGVyL2Zpbml0ZS1hdXRvbWF0b24vZGZhL2RmYS5qcyIsIi4uL25vZGVfbW9